From d9c9f6dc2cd3080009c4ce2c3f0f340949f4f53c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 May 2021 17:58:03 +0300 Subject: cleanups --- .../src/handlers/introduce_named_lifetime.rs | 4 ++-- .../handlers/replace_impl_trait_with_generic.rs | 24 +++++++++++----------- crates/ide_assists/src/utils/suggest_name.rs | 10 ++++++++- 3 files changed, 23 insertions(+), 15 deletions(-) (limited to 'crates/ide_assists/src') diff --git a/crates/ide_assists/src/handlers/introduce_named_lifetime.rs b/crates/ide_assists/src/handlers/introduce_named_lifetime.rs index 9f4f71d6c..cb71ca8bd 100644 --- a/crates/ide_assists/src/handlers/introduce_named_lifetime.rs +++ b/crates/ide_assists/src/handlers/introduce_named_lifetime.rs @@ -139,12 +139,12 @@ fn generate_unique_lifetime_param_name( fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) { let generic_param = - make::generic_param(format!("'{}", new_lifetime_param), None).clone_for_update(); + make::generic_param(&format!("'{}", new_lifetime_param), None).clone_for_update(); type_params.add_generic_param(generic_param); } fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime { - make::generic_param(format!("'{}", new_lifetime_param), None) + make::generic_param(&format!("'{}", new_lifetime_param), None) .syntax() .descendants() .find_map(ast::Lifetime::cast) diff --git a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs index 8509a5e11..26a0e81f0 100644 --- a/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs @@ -1,6 +1,6 @@ use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner}; -use crate::{AssistContext, AssistId, AssistKind, Assists}; +use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; // Assist: replace_impl_trait_with_generic // @@ -17,30 +17,30 @@ pub(crate) fn replace_impl_trait_with_generic( acc: &mut Assists, ctx: &AssistContext, ) -> Option<()> { - let type_impl_trait = ctx.find_node_at_offset::()?; - let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?; - let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?; + let impl_trait_type = ctx.find_node_at_offset::()?; + let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?; + let fn_ = param.syntax().ancestors().find_map(ast::Fn::cast)?; - let impl_trait_ty = type_impl_trait.type_bound_list()?; + let type_bound_list = impl_trait_type.type_bound_list()?; - let target = type_fn.syntax().text_range(); + let target = fn_.syntax().text_range(); acc.add( AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite), "Replace impl trait with generic", target, |edit| { - let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string(); + let type_param_name = suggest_name::generic_parameter(&impl_trait_type); - let generic_param_list = type_fn + let generic_param_list = fn_ .generic_param_list() .unwrap_or_else(|| make::generic_param_list(None)) - .append_param(make::generic_param(generic_letter.clone(), Some(impl_trait_ty))); + .append_param(make::generic_param(&type_param_name, Some(type_bound_list))); - let new_type_fn = type_fn - .replace_descendant::(type_impl_trait.into(), make::ty(&generic_letter)) + let new_type_fn = fn_ + .replace_descendant::(impl_trait_type.into(), make::ty(&type_param_name)) .with_generic_param_list(generic_param_list); - edit.replace_ast(type_fn.clone(), new_type_fn); + edit.replace_ast(fn_.clone(), new_type_fn); }, ) } diff --git a/crates/ide_assists/src/utils/suggest_name.rs b/crates/ide_assists/src/utils/suggest_name.rs index deafcd630..c8487846d 100644 --- a/crates/ide_assists/src/utils/suggest_name.rs +++ b/crates/ide_assists/src/utils/suggest_name.rs @@ -6,7 +6,7 @@ use itertools::Itertools; use stdx::to_lower_snake_case; use syntax::{ ast::{self, NameOwner}, - match_ast, AstNode, + match_ast, AstNode, SmolStr, }; /// Trait names, that will be ignored when in `impl Trait` and `dyn Trait` @@ -57,6 +57,14 @@ const USELESS_METHODS: &[&str] = &[ "iter_mut", ]; +pub(crate) fn generic_parameter(ty: &ast::ImplTraitType) -> SmolStr { + let c = ty + .type_bound_list() + .and_then(|bounds| bounds.syntax().text().char_at(0.into())) + .unwrap_or('T'); + c.encode_utf8(&mut [0; 4]).into() +} + /// Suggest name of variable for given expression /// /// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name. -- cgit v1.2.3