diff options
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs | 24 |
1 files changed, 13 insertions, 11 deletions
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 899c773df..16cae0281 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,4 +1,7 @@ | |||
1 | use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner}; | 1 | use syntax::{ |
2 | ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode}, | ||
3 | ted, | ||
4 | }; | ||
2 | 5 | ||
3 | use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; |
4 | 7 | ||
@@ -29,18 +32,17 @@ pub(crate) fn replace_impl_trait_with_generic( | |||
29 | "Replace impl trait with generic", | 32 | "Replace impl trait with generic", |
30 | target, | 33 | target, |
31 | |edit| { | 34 | |edit| { |
32 | let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type); | 35 | let impl_trait_type = edit.make_ast_mut(impl_trait_type); |
36 | let fn_ = edit.make_ast_mut(fn_); | ||
33 | 37 | ||
34 | let generic_param_list = fn_ | 38 | let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type); |
35 | .generic_param_list() | ||
36 | .unwrap_or_else(|| make::generic_param_list(None)) | ||
37 | .append_param(make::generic_param(&type_param_name, Some(type_bound_list))); | ||
38 | 39 | ||
39 | let new_type_fn = fn_ | 40 | let type_param = |
40 | .replace_descendant::<ast::Type>(impl_trait_type.into(), make::ty(&type_param_name)) | 41 | make::generic_param(&type_param_name, Some(type_bound_list)).clone_for_update(); |
41 | .with_generic_param_list(generic_param_list); | 42 | let new_ty = make::ty(&type_param_name).clone_for_update(); |
42 | 43 | ||
43 | edit.replace_ast(fn_.clone(), new_type_fn); | 44 | ted::replace(impl_trait_type.syntax(), new_ty.syntax()); |
45 | fn_.get_or_create_generic_param_list().add_generic_param(type_param) | ||
44 | }, | 46 | }, |
45 | ) | 47 | ) |
46 | } | 48 | } |
@@ -127,7 +129,7 @@ fn foo< | |||
127 | fn foo< | 129 | fn foo< |
128 | G: Foo, | 130 | G: Foo, |
129 | F, | 131 | F, |
130 | H, B: Bar | 132 | H, B: Bar, |
131 | >(bar: B) {} | 133 | >(bar: B) {} |
132 | "#, | 134 | "#, |
133 | ); | 135 | ); |