From cea589b3b52ff5c4e358db52dc6de150eb48a9a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 14 May 2021 18:47:08 +0300 Subject: internal: rewrite assoc item manipulaion to use mutable trees --- .../src/handlers/add_missing_impl_members.rs | 3 ++- crates/ide_assists/src/tests/generated.rs | 1 - crates/ide_assists/src/utils.rs | 26 ++++++++++++---------- 3 files changed, 16 insertions(+), 14 deletions(-) (limited to 'crates/ide_assists') 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) - // impl Trait for () { // type X = (); // fn foo(&self) {}$0 -// // } // ``` // -> @@ -195,6 +194,7 @@ impl Foo for S { fn baz(&self) { todo!() } + }"#, ); } @@ -231,6 +231,7 @@ impl Foo for S { fn foo(&self) { ${0:todo!()} } + }"#, ); } 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 { impl Trait for () { type X = (); fn foo(&self) {}$0 - } "#####, 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( sema: &hir::Semantics, items: Vec, trait_: hir::Trait, - impl_def: ast::Impl, + impl_: ast::Impl, target_scope: hir::SemanticsScope, ) -> (ast::Impl, ast::AssocItem) { - let impl_item_list = impl_def.assoc_item_list().unwrap_or_else(make::assoc_item_list); - - let n_existing_items = impl_item_list.assoc_items().count(); let source_scope = sema.scope_for_def(trait_); let ast_transform = QualifyPaths::new(&target_scope, &source_scope) - .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def.clone())); + .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_.clone())); let items = items .into_iter() @@ -147,13 +144,18 @@ pub fn add_trait_assoc_items_to_impl( ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()), _ => it, }) - .map(|it| edit::remove_attrs_and_docs(&it)); - - let new_impl_item_list = impl_item_list.append_items(items); - let new_impl_def = impl_def.with_assoc_item_list(new_impl_item_list); - let first_new_item = - new_impl_def.assoc_item_list().unwrap().assoc_items().nth(n_existing_items).unwrap(); - return (new_impl_def, first_new_item); + .map(|it| edit::remove_attrs_and_docs(&it).clone_subtree().clone_for_update()); + + let res = impl_.clone_for_update(); + let assoc_item_list = res.get_or_create_assoc_item_list(); + let mut first_item = None; + for item in items { + if first_item.is_none() { + first_item = Some(item.clone()) + } + assoc_item_list.add_item(item) + } + return (res, first_item.unwrap()); fn add_body(fn_def: ast::Fn) -> ast::Fn { match fn_def.body() { -- cgit v1.2.3