diff options
Diffstat (limited to 'crates')
4 files changed, 25 insertions, 17 deletions
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( | |||
139 | 139 | ||
140 | fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) { | 140 | fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) { |
141 | let generic_param = | 141 | let generic_param = |
142 | make::generic_param(format!("'{}", new_lifetime_param), None).clone_for_update(); | 142 | make::generic_param(&format!("'{}", new_lifetime_param), None).clone_for_update(); |
143 | type_params.add_generic_param(generic_param); | 143 | type_params.add_generic_param(generic_param); |
144 | } | 144 | } |
145 | 145 | ||
146 | fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime { | 146 | fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime { |
147 | make::generic_param(format!("'{}", new_lifetime_param), None) | 147 | make::generic_param(&format!("'{}", new_lifetime_param), None) |
148 | .syntax() | 148 | .syntax() |
149 | .descendants() | 149 | .descendants() |
150 | .find_map(ast::Lifetime::cast) | 150 | .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 @@ | |||
1 | use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner}; | 1 | use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner}; |
2 | 2 | ||
3 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 3 | use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; |
4 | 4 | ||
5 | // Assist: replace_impl_trait_with_generic | 5 | // Assist: replace_impl_trait_with_generic |
6 | // | 6 | // |
@@ -17,30 +17,30 @@ pub(crate) fn replace_impl_trait_with_generic( | |||
17 | acc: &mut Assists, | 17 | acc: &mut Assists, |
18 | ctx: &AssistContext, | 18 | ctx: &AssistContext, |
19 | ) -> Option<()> { | 19 | ) -> Option<()> { |
20 | let type_impl_trait = ctx.find_node_at_offset::<ast::ImplTraitType>()?; | 20 | let impl_trait_type = ctx.find_node_at_offset::<ast::ImplTraitType>()?; |
21 | let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?; | 21 | let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?; |
22 | let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?; | 22 | let fn_ = param.syntax().ancestors().find_map(ast::Fn::cast)?; |
23 | 23 | ||
24 | let impl_trait_ty = type_impl_trait.type_bound_list()?; | 24 | let type_bound_list = impl_trait_type.type_bound_list()?; |
25 | 25 | ||
26 | let target = type_fn.syntax().text_range(); | 26 | let target = fn_.syntax().text_range(); |
27 | acc.add( | 27 | acc.add( |
28 | AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite), | 28 | AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite), |
29 | "Replace impl trait with generic", | 29 | "Replace impl trait with generic", |
30 | target, | 30 | target, |
31 | |edit| { | 31 | |edit| { |
32 | let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string(); | 32 | let type_param_name = suggest_name::generic_parameter(&impl_trait_type); |
33 | 33 | ||
34 | let generic_param_list = type_fn | 34 | let generic_param_list = fn_ |
35 | .generic_param_list() | 35 | .generic_param_list() |
36 | .unwrap_or_else(|| make::generic_param_list(None)) | 36 | .unwrap_or_else(|| make::generic_param_list(None)) |
37 | .append_param(make::generic_param(generic_letter.clone(), Some(impl_trait_ty))); | 37 | .append_param(make::generic_param(&type_param_name, Some(type_bound_list))); |
38 | 38 | ||
39 | let new_type_fn = type_fn | 39 | let new_type_fn = fn_ |
40 | .replace_descendant::<ast::Type>(type_impl_trait.into(), make::ty(&generic_letter)) | 40 | .replace_descendant::<ast::Type>(impl_trait_type.into(), make::ty(&type_param_name)) |
41 | .with_generic_param_list(generic_param_list); | 41 | .with_generic_param_list(generic_param_list); |
42 | 42 | ||
43 | edit.replace_ast(type_fn.clone(), new_type_fn); | 43 | edit.replace_ast(fn_.clone(), new_type_fn); |
44 | }, | 44 | }, |
45 | ) | 45 | ) |
46 | } | 46 | } |
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; | |||
6 | use stdx::to_lower_snake_case; | 6 | use stdx::to_lower_snake_case; |
7 | use syntax::{ | 7 | use syntax::{ |
8 | ast::{self, NameOwner}, | 8 | ast::{self, NameOwner}, |
9 | match_ast, AstNode, | 9 | match_ast, AstNode, SmolStr, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | /// Trait names, that will be ignored when in `impl Trait` and `dyn Trait` | 12 | /// Trait names, that will be ignored when in `impl Trait` and `dyn Trait` |
@@ -57,6 +57,14 @@ const USELESS_METHODS: &[&str] = &[ | |||
57 | "iter_mut", | 57 | "iter_mut", |
58 | ]; | 58 | ]; |
59 | 59 | ||
60 | pub(crate) fn generic_parameter(ty: &ast::ImplTraitType) -> SmolStr { | ||
61 | let c = ty | ||
62 | .type_bound_list() | ||
63 | .and_then(|bounds| bounds.syntax().text().char_at(0.into())) | ||
64 | .unwrap_or('T'); | ||
65 | c.encode_utf8(&mut [0; 4]).into() | ||
66 | } | ||
67 | |||
60 | /// Suggest name of variable for given expression | 68 | /// Suggest name of variable for given expression |
61 | /// | 69 | /// |
62 | /// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name. | 70 | /// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name. |
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 5a6687397..2289d8f3e 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -475,8 +475,8 @@ pub fn param_list( | |||
475 | }; | 475 | }; |
476 | ast_from_text(&list) | 476 | ast_from_text(&list) |
477 | } | 477 | } |
478 | 478 | // FIXME: s/&str/ast:Name | |
479 | pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam { | 479 | pub fn generic_param(name: &str, ty: Option<ast::TypeBoundList>) -> ast::GenericParam { |
480 | let bound = match ty { | 480 | let bound = match ty { |
481 | Some(it) => format!(": {}", it), | 481 | Some(it) => format!(": {}", it), |
482 | None => String::new(), | 482 | None => String::new(), |