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 ide_db::syntax_helpers::suggest_name;
use syntax::{
ast::{self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasName},
ted, NodeOrToken,
@ -6,7 +7,7 @@ use syntax::{
SyntaxNode, T,
};
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
use crate::{AssistContext, AssistId, AssistKind, Assists};
// Assist: extract_variable
//

View file

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

View file

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

View file

@ -1,9 +1,10 @@
use ide_db::syntax_helpers::suggest_name;
use syntax::{
ast::{self, make, AstNode},
ted,
};
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
use crate::{AssistContext, AssistId, AssistKind, Assists};
// 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;
pub(crate) mod ref_field_expr;
pub(crate) mod suggest_name;
pub(crate) fn unwrap_trivial_block(block_expr: ast::BlockExpr) -> ast::Expr {
extract_trivial_expression(&block_expr)

View file

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

View file

@ -1,14 +1,16 @@
//! This module contains functions to suggest names for expressions, functions and other items
use hir::Semantics;
use ide_db::{FxHashSet, RootDatabase};
use itertools::Itertools;
use rustc_hash::FxHashSet;
use stdx::to_lower_snake_case;
use syntax::{
ast::{self, HasName},
match_ast, AstNode, Edition, SmolStr,
};
use crate::RootDatabase;
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
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.
/// If so, it will try to resolve the conflict by adding a number suffix, e.g.
/// `T`, `T0`, `T1`, ...
pub(crate) fn for_unique_generic_name(
name: &str,
existing_params: &ast::GenericParamList,
) -> SmolStr {
pub fn for_unique_generic_name(name: &str, existing_params: &ast::GenericParamList) -> SmolStr {
let param_names = existing_params
.generic_params()
.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
/// 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,
existing_params: &ast::GenericParamList,
) -> SmolStr {
@ -132,7 +131,7 @@ pub(crate) fn for_impl_trait_as_generic(
///
/// Currently it sticks to the first name found.
// 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
// it need the largest context possible
// so we check firstmost
@ -184,7 +183,7 @@ fn normalize(name: &str) -> Option<String> {
fn is_valid_name(name: &str) -> bool {
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))
)
}