Skip to content

Commit e6e72bf

Browse files
committed
Migrate add_missing_impl_members to mutable ast
`replace_derive_with_manual_impl` was slightly since it used `add_trait_assoc_items_to_impl` (which was also used by `add_missing_impl_members`)
1 parent 2f1b7ce commit e6e72bf

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

crates/ide-assists/src/handlers/add_missing_impl_members.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use syntax::ast::{self, make, AstNode};
44

55
use crate::{
66
assist_context::{AssistContext, Assists},
7-
utils::{
8-
add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_fn_body, render_snippet,
9-
Cursor, DefaultMethods,
10-
},
7+
utils::{add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_fn_body, DefaultMethods},
118
AssistId, AssistKind,
129
};
1310

@@ -130,7 +127,8 @@ fn add_missing_impl_members_inner(
130127
}
131128

132129
let target = impl_def.syntax().text_range();
133-
acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
130+
acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |edit| {
131+
let new_impl_def = edit.make_mut(impl_def.clone());
134132
let missing_items = missing_items
135133
.into_iter()
136134
.map(|it| {
@@ -142,38 +140,34 @@ fn add_missing_impl_members_inner(
142140
it.clone_for_update()
143141
})
144142
.collect();
145-
let (new_impl_def, first_new_item) = add_trait_assoc_items_to_impl(
143+
let first_new_item = add_trait_assoc_items_to_impl(
146144
&ctx.sema,
147145
missing_items,
148146
trait_,
149-
impl_def.clone(),
147+
&new_impl_def,
150148
target_scope,
151149
);
152-
match ctx.config.snippet_cap {
153-
None => builder.replace(target, new_impl_def.to_string()),
154-
Some(cap) => {
155-
let mut cursor = Cursor::Before(first_new_item.syntax());
156-
let placeholder;
157-
if let DefaultMethods::No = mode {
158-
if let ast::AssocItem::Fn(func) = &first_new_item {
159-
if try_gen_trait_body(ctx, func, trait_ref, &impl_def).is_none() {
160-
if let Some(m) =
161-
func.syntax().descendants().find_map(ast::MacroCall::cast)
162-
{
163-
if m.syntax().text() == "todo!()" {
164-
placeholder = m;
165-
cursor = Cursor::Replace(placeholder.syntax());
166-
}
150+
151+
if let Some(cap) = ctx.config.snippet_cap {
152+
let mut placeholder = None;
153+
if let DefaultMethods::No = mode {
154+
if let ast::AssocItem::Fn(func) = &first_new_item {
155+
if try_gen_trait_body(ctx, func, trait_ref, &impl_def).is_none() {
156+
if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
157+
{
158+
if m.syntax().text() == "todo!()" {
159+
placeholder = Some(m);
167160
}
168161
}
169162
}
170163
}
171-
builder.replace_snippet(
172-
cap,
173-
target,
174-
render_snippet(cap, new_impl_def.syntax(), cursor),
175-
)
176164
}
165+
166+
if let Some(macro_call) = placeholder {
167+
edit.add_placeholder_snippet(cap, macro_call);
168+
} else {
169+
edit.add_tabstop_before(cap, first_new_item);
170+
};
177171
};
178172
})
179173
}

crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ fn impl_def_from_trait(
182182
let impl_def = {
183183
use syntax::ast::Impl;
184184
let text = generate_trait_impl_text(adt, trait_path.to_string().as_str(), "");
185-
let parse = syntax::SourceFile::parse(&text);
185+
// FIXME: `generate_trait_impl_text` currently generates two newlines
186+
// at the front, but these leading newlines should really instead be
187+
// inserted at the same time the impl is inserted
188+
assert_eq!(&text[..2], "\n\n", "`generate_trait_impl_text` output changed");
189+
let parse = syntax::SourceFile::parse(&text[2..]);
186190
let node = match parse.tree().syntax().descendants().find_map(Impl::cast) {
187191
Some(it) => it,
188192
None => {
@@ -193,7 +197,7 @@ fn impl_def_from_trait(
193197
)
194198
}
195199
};
196-
let node = node.clone_subtree();
200+
let node = node.clone_for_update();
197201
assert_eq!(node.syntax().text_range().start(), 0.into());
198202
node
199203
};
@@ -209,8 +213,8 @@ fn impl_def_from_trait(
209213
it.clone_for_update()
210214
})
211215
.collect();
212-
let (impl_def, first_assoc_item) =
213-
add_trait_assoc_items_to_impl(sema, trait_items, trait_, impl_def, target_scope);
216+
let first_assoc_item =
217+
add_trait_assoc_items_to_impl(sema, trait_items, trait_, &impl_def, target_scope);
214218

215219
// Generate a default `impl` function body for the derived trait.
216220
if let ast::AssocItem::Fn(ref func) = first_assoc_item {

crates/ide-assists/src/utils.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ pub fn add_trait_assoc_items_to_impl(
132132
sema: &Semantics<'_, RootDatabase>,
133133
items: Vec<ast::AssocItem>,
134134
trait_: hir::Trait,
135-
impl_: ast::Impl,
135+
impl_: &ast::Impl,
136136
target_scope: hir::SemanticsScope<'_>,
137-
) -> (ast::Impl, ast::AssocItem) {
137+
) -> ast::AssocItem {
138138
let source_scope = sema.scope_for_def(trait_);
139139

140140
let transform = PathTransform::trait_impl(&target_scope, &source_scope, trait_, impl_.clone());
@@ -147,9 +147,7 @@ pub fn add_trait_assoc_items_to_impl(
147147
assoc_item
148148
});
149149

150-
let res = impl_.clone_for_update();
151-
152-
let assoc_item_list = res.get_or_create_assoc_item_list();
150+
let assoc_item_list = impl_.get_or_create_assoc_item_list();
153151
let mut first_item = None;
154152
for item in items {
155153
first_item.get_or_insert_with(|| item.clone());
@@ -172,7 +170,7 @@ pub fn add_trait_assoc_items_to_impl(
172170
assoc_item_list.add_item(item)
173171
}
174172

175-
(res, first_item.unwrap())
173+
first_item.unwrap()
176174
}
177175

178176
#[derive(Clone, Copy, Debug)]

0 commit comments

Comments
 (0)