Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs')
-rw-r--r--crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs44
1 files changed, 19 insertions, 25 deletions
diff --git a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
index fc63c59799..751cd42f6e 100644
--- a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
+++ b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs
@@ -128,7 +128,8 @@ fn add_assist(
let label = format!("Convert to manual `impl {replace_trait_path} for {annotated_name}`");
acc.add(AssistId::refactor("replace_derive_with_manual_impl"), label, target, |builder| {
- let mut editor = builder.make_editor(attr.syntax());
+ let editor = builder.make_editor(attr.syntax());
+ let make = editor.make();
let insert_after = Position::after(adt.syntax());
let impl_is_unsafe = trait_.map(|s| s.is_unsafe(ctx.db())).unwrap_or(false);
let impl_def = impl_def_from_trait(
@@ -141,9 +142,9 @@ fn add_assist(
replace_trait_path,
impl_is_unsafe,
);
- update_attribute(&mut editor, old_derives, old_tree, old_trait_path, attr);
+ update_attribute(&editor, old_derives, old_tree, old_trait_path, attr);
- let trait_path = editor.make().ty_path(replace_trait_path.clone()).into();
+ let trait_path = make.ty_path(replace_trait_path.clone()).into();
let (impl_def, first_assoc_item) = if let Some(impl_def) = impl_def {
(
@@ -151,7 +152,7 @@ fn add_assist(
impl_def.assoc_item_list().and_then(|list| list.assoc_items().next()),
)
} else {
- (generate_trait_impl(editor.make(), impl_is_unsafe, adt, trait_path), None)
+ (generate_trait_impl(make, impl_is_unsafe, adt, trait_path), None)
};
if let Some(cap) = ctx.config.snippet_cap {
@@ -175,7 +176,7 @@ fn add_assist(
editor.insert_all(
insert_after,
- vec![editor.make().whitespace("\n\n").into(), impl_def.syntax().clone().into()],
+ vec![make.whitespace("\n\n").into(), impl_def.syntax().clone().into()],
);
builder.add_file_edits(ctx.vfs_file_id(), editor);
})
@@ -191,6 +192,7 @@ fn impl_def_from_trait(
trait_path: &ast::Path,
impl_is_unsafe: bool,
) -> Option<ast::Impl> {
+ let make = editor.make();
let trait_ = trait_?;
let target_scope = sema.scope(annotated_name.syntax())?;
@@ -207,11 +209,11 @@ fn impl_def_from_trait(
if trait_items.is_empty() {
return None;
}
- let trait_ty: ast::Type = editor.make().ty_path(trait_path.clone()).into();
- let impl_def = generate_trait_impl(editor.make(), impl_is_unsafe, adt, trait_ty.clone());
+ let trait_ty: ast::Type = make.ty_path(trait_path.clone()).into();
+ let impl_def = generate_trait_impl(make, impl_is_unsafe, adt, trait_ty.clone());
let assoc_items = add_trait_assoc_items_to_impl(
- editor.make(),
+ make,
sema,
config,
&trait_items,
@@ -221,10 +223,10 @@ fn impl_def_from_trait(
);
let assoc_item_list = if let Some((first, other)) = assoc_items.split_first() {
let first_item = if let ast::AssocItem::Fn(func) = first
- && let Some(body) = gen_trait_fn_body(editor.make(), func, trait_path, adt, None)
+ && let Some(body) = gen_trait_fn_body(make, func, trait_path, adt, None)
&& let Some(func_body) = func.body()
{
- let (mut editor, _) = SyntaxEditor::new(first.syntax().clone());
+ let (editor, _) = SyntaxEditor::new(first.syntax().clone());
editor.replace(func_body.syntax(), body.syntax());
ast::AssocItem::cast(editor.finish().new_root().clone())
} else {
@@ -232,27 +234,22 @@ fn impl_def_from_trait(
};
let items: Vec<ast::AssocItem> =
first_item.into_iter().chain(other.iter().cloned()).collect();
- editor.make().assoc_item_list(items)
+ make.assoc_item_list(items)
} else {
- editor.make().assoc_item_list_empty()
+ make.assoc_item_list_empty()
};
- Some(generate_trait_impl_with_item(
- editor.make(),
- impl_is_unsafe,
- adt,
- trait_ty,
- assoc_item_list,
- ))
+ Some(generate_trait_impl_with_item(make, impl_is_unsafe, adt, trait_ty, assoc_item_list))
}
fn update_attribute(
- editor: &mut SyntaxEditor,
+ editor: &SyntaxEditor,
old_derives: &[ast::Path],
old_tree: &ast::TokenTree,
old_trait_path: &ast::Path,
attr: &ast::Attr,
) {
+ let make = editor.make();
let new_derives = old_derives
.iter()
.filter(|t| t.to_string() != old_trait_path.to_string())
@@ -267,16 +264,13 @@ fn update_attribute(
.collect::<Vec<_>>()
});
// ...which are interspersed with ", "
- let tt = Itertools::intersperse(
- tt,
- vec![editor.make().token(T![,]), editor.make().whitespace(" ")],
- );
+ let tt = Itertools::intersperse(tt, vec![make.token(T![,]), make.whitespace(" ")]);
// ...wrap them into the appropriate `NodeOrToken` variant
let tt = tt.flatten().map(syntax::NodeOrToken::Token);
// ...and make them into a flat list of tokens
let tt = tt.collect::<Vec<_>>();
- let new_tree = editor.make().token_tree(T!['('], tt);
+ let new_tree = make.token_tree(T!['('], tt);
editor.replace(old_tree.syntax(), new_tree.syntax());
} else {
// Remove the attr and any trailing whitespace