aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-05-09 16:20:37 +0100
committerAleksey Kladov <[email protected]>2021-05-09 16:20:37 +0100
commit5342800147679a0ded5546322c94aa6339d58fbc (patch)
tree7a70c7145b5e3a738bf289b7f54941fc645b4fe1 /crates/ide_assists
parent984d20aad8445ecbdc05d1dc3ea2de104c685af0 (diff)
internal: rewrite **Repalce impl Trait** assist to mutable syntax trees
Diffstat (limited to 'crates/ide_assists')
-rw-r--r--crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs24
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 @@
1use syntax::ast::{self, edit::AstNodeEdit, make, AstNode, GenericParamsOwner}; 1use syntax::{
2 ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode},
3 ted,
4};
2 5
3use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; 6use 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<
127fn foo< 129fn 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 );