aboutsummaryrefslogtreecommitdiff
path: root/crates
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
parent984d20aad8445ecbdc05d1dc3ea2de104c685af0 (diff)
internal: rewrite **Repalce impl Trait** assist to mutable syntax trees
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs24
-rw-r--r--crates/syntax/src/ast/edit_in_place.rs19
2 files changed, 20 insertions, 23 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 );
diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs
index 04f97f368..168355555 100644
--- a/crates/syntax/src/ast/edit_in_place.rs
+++ b/crates/syntax/src/ast/edit_in_place.rs
@@ -195,18 +195,13 @@ impl ast::GenericParamList {
195 pub fn add_generic_param(&self, generic_param: ast::GenericParam) { 195 pub fn add_generic_param(&self, generic_param: ast::GenericParam) {
196 match self.generic_params().last() { 196 match self.generic_params().last() {
197 Some(last_param) => { 197 Some(last_param) => {
198 let mut elems = Vec::new(); 198 let position = Position::after(last_param.syntax());
199 if !last_param 199 let elements = vec![
200 .syntax() 200 make::token(T![,]).into(),
201 .siblings_with_tokens(Direction::Next) 201 make::tokens::single_space().into(),
202 .any(|it| it.kind() == T![,]) 202 generic_param.syntax().clone().into(),
203 { 203 ];
204 elems.push(make::token(T![,]).into()); 204 ted::insert_all(position, elements);
205 elems.push(make::tokens::single_space().into());
206 };
207 elems.push(generic_param.syntax().clone().into());
208 let after_last_param = Position::after(last_param.syntax());
209 ted::insert_all(after_last_param, elems);
210 } 205 }
211 None => { 206 None => {
212 let after_l_angle = Position::after(self.l_angle_token().unwrap()); 207 let after_l_angle = Position::after(self.l_angle_token().unwrap());