refactor: move ide_assist::utils::suggest_name to ide-db

This commit is contained in:
roife 2024-09-03 05:01:56 +08:00
parent 779d9eee2e
commit b207e5781e
7 changed files with 16 additions and 13 deletions

View file

@ -1,4 +1,5 @@
use hir::TypeInfo; use hir::TypeInfo;
use ide_db::syntax_helpers::suggest_name;
use syntax::{ use syntax::{
ast::{self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasName}, ast::{self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasName},
ted, NodeOrToken, ted, NodeOrToken,
@ -6,7 +7,7 @@ use syntax::{
SyntaxNode, T, SyntaxNode, T,
}; };
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; use crate::{AssistContext, AssistId, AssistKind, Assists};
// Assist: extract_variable // Assist: extract_variable
// //

View file

@ -2,13 +2,14 @@ use std::ops::Not;
use crate::{ use crate::{
assist_context::{AssistContext, Assists}, assist_context::{AssistContext, Assists},
utils::{convert_param_list_to_arg_list, suggest_name}, utils::convert_param_list_to_arg_list,
}; };
use either::Either; use either::Either;
use hir::{db::HirDatabase, HasVisibility}; use hir::{db::HirDatabase, HasVisibility};
use ide_db::{ use ide_db::{
assists::{AssistId, GroupLabel}, assists::{AssistId, GroupLabel},
path_transform::PathTransform, path_transform::PathTransform,
syntax_helpers::suggest_name,
FxHashMap, FxHashSet, FxHashMap, FxHashSet,
}; };
use itertools::Itertools; use itertools::Itertools;

View file

@ -1,9 +1,10 @@
use ide_db::syntax_helpers::suggest_name;
use syntax::{ use syntax::{
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasGenericParams}, ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasGenericParams},
ted, ted,
}; };
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; use crate::{AssistContext, AssistId, AssistKind, Assists};
// Assist: introduce_named_generic // Assist: introduce_named_generic
// //

View file

@ -1,9 +1,10 @@
use ide_db::syntax_helpers::suggest_name;
use syntax::{ use syntax::{
ast::{self, make, AstNode}, ast::{self, make, AstNode},
ted, ted,
}; };
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; use crate::{AssistContext, AssistId, AssistKind, Assists};
// Assist: replace_is_some_with_if_let_some // Assist: replace_is_some_with_if_let_some
// //

View file

@ -23,7 +23,6 @@ use crate::assist_context::{AssistContext, SourceChangeBuilder};
mod gen_trait_fn_body; mod gen_trait_fn_body;
pub(crate) mod ref_field_expr; pub(crate) mod ref_field_expr;
pub(crate) mod suggest_name;
pub(crate) fn unwrap_trivial_block(block_expr: ast::BlockExpr) -> ast::Expr { pub(crate) fn unwrap_trivial_block(block_expr: ast::BlockExpr) -> ast::Expr {
extract_trivial_expression(&block_expr) extract_trivial_expression(&block_expr)

View file

@ -38,6 +38,7 @@ pub mod syntax_helpers {
pub mod format_string_exprs; pub mod format_string_exprs;
pub use hir::insert_whitespace_into_node; pub use hir::insert_whitespace_into_node;
pub mod node_ext; pub mod node_ext;
pub mod suggest_name;
pub use parser::LexedStr; pub use parser::LexedStr;
} }

View file

@ -1,14 +1,16 @@
//! This module contains functions to suggest names for expressions, functions and other items //! This module contains functions to suggest names for expressions, functions and other items
use hir::Semantics; use hir::Semantics;
use ide_db::{FxHashSet, RootDatabase};
use itertools::Itertools; use itertools::Itertools;
use rustc_hash::FxHashSet;
use stdx::to_lower_snake_case; use stdx::to_lower_snake_case;
use syntax::{ use syntax::{
ast::{self, HasName}, ast::{self, HasName},
match_ast, AstNode, Edition, SmolStr, match_ast, AstNode, Edition, SmolStr,
}; };
use crate::RootDatabase;
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait` /// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
const USELESS_TRAITS: &[&str] = &["Send", "Sync", "Copy", "Clone", "Eq", "PartialEq"]; const USELESS_TRAITS: &[&str] = &["Send", "Sync", "Copy", "Clone", "Eq", "PartialEq"];
@ -66,10 +68,7 @@ const USELESS_METHODS: &[&str] = &[
/// The function checks if the name conflicts with existing generic parameters. /// The function checks if the name conflicts with existing generic parameters.
/// If so, it will try to resolve the conflict by adding a number suffix, e.g. /// If so, it will try to resolve the conflict by adding a number suffix, e.g.
/// `T`, `T0`, `T1`, ... /// `T`, `T0`, `T1`, ...
pub(crate) fn for_unique_generic_name( pub fn for_unique_generic_name(name: &str, existing_params: &ast::GenericParamList) -> SmolStr {
name: &str,
existing_params: &ast::GenericParamList,
) -> SmolStr {
let param_names = existing_params let param_names = existing_params
.generic_params() .generic_params()
.map(|param| match param { .map(|param| match param {
@ -101,7 +100,7 @@ pub(crate) fn for_unique_generic_name(
/// ///
/// If the name conflicts with existing generic parameters, it will try to /// If the name conflicts with existing generic parameters, it will try to
/// resolve the conflict with `for_unique_generic_name`. /// resolve the conflict with `for_unique_generic_name`.
pub(crate) fn for_impl_trait_as_generic( pub fn for_impl_trait_as_generic(
ty: &ast::ImplTraitType, ty: &ast::ImplTraitType,
existing_params: &ast::GenericParamList, existing_params: &ast::GenericParamList,
) -> SmolStr { ) -> SmolStr {
@ -132,7 +131,7 @@ pub(crate) fn for_impl_trait_as_generic(
/// ///
/// Currently it sticks to the first name found. /// Currently it sticks to the first name found.
// FIXME: Microoptimize and return a `SmolStr` here. // FIXME: Microoptimize and return a `SmolStr` here.
pub(crate) fn for_variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> String { pub fn for_variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> String {
// `from_param` does not benefit from stripping // `from_param` does not benefit from stripping
// it need the largest context possible // it need the largest context possible
// so we check firstmost // so we check firstmost
@ -184,7 +183,7 @@ fn normalize(name: &str) -> Option<String> {
fn is_valid_name(name: &str) -> bool { fn is_valid_name(name: &str) -> bool {
matches!( matches!(
ide_db::syntax_helpers::LexedStr::single_token(syntax::Edition::CURRENT_FIXME, name), super::LexedStr::single_token(syntax::Edition::CURRENT_FIXME, name),
Some((syntax::SyntaxKind::IDENT, _error)) Some((syntax::SyntaxKind::IDENT, _error))
) )
} }