aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-05-14 16:47:08 +0100
committerAleksey Kladov <[email protected]>2021-05-14 16:47:08 +0100
commitcea589b3b52ff5c4e358db52dc6de150eb48a9a0 (patch)
tree99c4c1e83bfd743ac3b30eb43c4a37b00484ea14 /crates/ide_assists
parent73123a7550a667ebc42be96651a8e36be482a828 (diff)
internal: rewrite assoc item manipulaion to use mutable trees
Diffstat (limited to 'crates/ide_assists')
-rw-r--r--crates/ide_assists/src/handlers/add_missing_impl_members.rs3
-rw-r--r--crates/ide_assists/src/tests/generated.rs1
-rw-r--r--crates/ide_assists/src/utils.rs26
3 files changed, 16 insertions, 14 deletions
diff --git a/crates/ide_assists/src/handlers/add_missing_impl_members.rs b/crates/ide_assists/src/handlers/add_missing_impl_members.rs
index 0148635f9..8225ae22c 100644
--- a/crates/ide_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ide_assists/src/handlers/add_missing_impl_members.rs
@@ -64,7 +64,6 @@ pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -
64// impl Trait for () { 64// impl Trait for () {
65// type X = (); 65// type X = ();
66// fn foo(&self) {}$0 66// fn foo(&self) {}$0
67//
68// } 67// }
69// ``` 68// ```
70// -> 69// ->
@@ -195,6 +194,7 @@ impl Foo for S {
195 fn baz(&self) { 194 fn baz(&self) {
196 todo!() 195 todo!()
197 } 196 }
197
198}"#, 198}"#,
199 ); 199 );
200 } 200 }
@@ -231,6 +231,7 @@ impl Foo for S {
231 fn foo(&self) { 231 fn foo(&self) {
232 ${0:todo!()} 232 ${0:todo!()}
233 } 233 }
234
234}"#, 235}"#,
235 ); 236 );
236 } 237 }
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index 49c1a9776..4406406a2 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -50,7 +50,6 @@ trait Trait {
50impl Trait for () { 50impl Trait for () {
51 type X = (); 51 type X = ();
52 fn foo(&self) {}$0 52 fn foo(&self) {}$0
53
54} 53}
55"#####, 54"#####,
56 r#####" 55 r#####"
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs
index 0dcf20c61..7d562d1d4 100644
--- a/crates/ide_assists/src/utils.rs
+++ b/crates/ide_assists/src/utils.rs
@@ -128,15 +128,12 @@ pub fn add_trait_assoc_items_to_impl(
128 sema: &hir::Semantics<ide_db::RootDatabase>, 128 sema: &hir::Semantics<ide_db::RootDatabase>,
129 items: Vec<ast::AssocItem>, 129 items: Vec<ast::AssocItem>,
130 trait_: hir::Trait, 130 trait_: hir::Trait,
131 impl_def: ast::Impl, 131 impl_: ast::Impl,
132 target_scope: hir::SemanticsScope, 132 target_scope: hir::SemanticsScope,
133) -> (ast::Impl, ast::AssocItem) { 133) -> (ast::Impl, ast::AssocItem) {
134 let impl_item_list = impl_def.assoc_item_list().unwrap_or_else(make::assoc_item_list);
135
136 let n_existing_items = impl_item_list.assoc_items().count();
137 let source_scope = sema.scope_for_def(trait_); 134 let source_scope = sema.scope_for_def(trait_);
138 let ast_transform = QualifyPaths::new(&target_scope, &source_scope) 135 let ast_transform = QualifyPaths::new(&target_scope, &source_scope)
139 .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def.clone())); 136 .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_.clone()));
140 137
141 let items = items 138 let items = items
142 .into_iter() 139 .into_iter()
@@ -147,13 +144,18 @@ pub fn add_trait_assoc_items_to_impl(
147 ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()), 144 ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()),
148 _ => it, 145 _ => it,
149 }) 146 })
150 .map(|it| edit::remove_attrs_and_docs(&it)); 147 .map(|it| edit::remove_attrs_and_docs(&it).clone_subtree().clone_for_update());
151 148
152 let new_impl_item_list = impl_item_list.append_items(items); 149 let res = impl_.clone_for_update();
153 let new_impl_def = impl_def.with_assoc_item_list(new_impl_item_list); 150 let assoc_item_list = res.get_or_create_assoc_item_list();
154 let first_new_item = 151 let mut first_item = None;
155 new_impl_def.assoc_item_list().unwrap().assoc_items().nth(n_existing_items).unwrap(); 152 for item in items {
156 return (new_impl_def, first_new_item); 153 if first_item.is_none() {
154 first_item = Some(item.clone())
155 }
156 assoc_item_list.add_item(item)
157 }
158 return (res, first_item.unwrap());
157 159
158 fn add_body(fn_def: ast::Fn) -> ast::Fn { 160 fn add_body(fn_def: ast::Fn) -> ast::Fn {
159 match fn_def.body() { 161 match fn_def.body() {