Skip to content

Commit dab40c3

Browse files
authored
Merge pull request #21886 from Shourya742/2026-03-26-migrate-gen-trait-fn-body
Replace make constructor with syntaxFactory in utils/gen trait fn body
2 parents 9be14d6 + b70d09d commit dab40c3

5 files changed

Lines changed: 300 additions & 266 deletions

File tree

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use hir::HasSource;
22
use syntax::{
33
Edition,
4-
ast::{self, AstNode, make},
4+
ast::{self, AstNode, syntax_factory::SyntaxFactory},
55
syntax_editor::{Position, SyntaxEditor},
66
};
77

88
use crate::{
99
AssistId,
1010
assist_context::{AssistContext, Assists},
1111
utils::{
12-
DefaultMethods, IgnoreAssocItems, add_trait_assoc_items_to_impl, filter_assoc_items,
13-
gen_trait_fn_body,
12+
DefaultMethods, IgnoreAssocItems, add_trait_assoc_items_to_impl_with_factory,
13+
filter_assoc_items, gen_trait_fn_body,
1414
},
1515
};
1616

@@ -148,7 +148,9 @@ fn add_missing_impl_members_inner(
148148

149149
let target = impl_def.syntax().text_range();
150150
acc.add(AssistId::quick_fix(assist_id), label, target, |edit| {
151-
let new_item = add_trait_assoc_items_to_impl(
151+
let make = SyntaxFactory::with_mappings();
152+
let new_item = add_trait_assoc_items_to_impl_with_factory(
153+
&make,
152154
&ctx.sema,
153155
ctx.config,
154156
&missing_items,
@@ -164,6 +166,7 @@ fn add_missing_impl_members_inner(
164166
let mut first_new_item = if let DefaultMethods::No = mode
165167
&& let ast::AssocItem::Fn(func) = &first_new_item
166168
&& let Some(body) = try_gen_trait_body(
169+
&make,
167170
ctx,
168171
func,
169172
trait_ref,
@@ -189,10 +192,10 @@ fn add_missing_impl_members_inner(
189192
if let Some(assoc_item_list) = impl_def.assoc_item_list() {
190193
assoc_item_list.add_items(&mut editor, new_assoc_items);
191194
} else {
192-
let assoc_item_list = make::assoc_item_list(Some(new_assoc_items)).clone_for_update();
195+
let assoc_item_list = make.assoc_item_list(new_assoc_items);
193196
editor.insert_all(
194197
Position::after(impl_def.syntax()),
195-
vec![make::tokens::whitespace(" ").into(), assoc_item_list.syntax().clone().into()],
198+
vec![make.whitespace(" ").into(), assoc_item_list.syntax().clone().into()],
196199
);
197200
first_new_item = assoc_item_list.assoc_items().next();
198201
}
@@ -215,23 +218,24 @@ fn add_missing_impl_members_inner(
215218
editor.add_annotation(first_new_item.syntax(), tabstop);
216219
};
217220
};
221+
editor.add_mappings(make.finish_with_mappings());
218222
edit.add_file_edits(ctx.vfs_file_id(), editor);
219223
})
220224
}
221225

222226
fn try_gen_trait_body(
227+
make: &SyntaxFactory,
223228
ctx: &AssistContext<'_>,
224229
func: &ast::Fn,
225230
trait_ref: hir::TraitRef<'_>,
226231
impl_def: &ast::Impl,
227232
edition: Edition,
228233
) -> Option<ast::BlockExpr> {
229-
let trait_path = make::ext::ident_path(
230-
&trait_ref.trait_().name(ctx.db()).display(ctx.db(), edition).to_string(),
231-
);
234+
let trait_path =
235+
make.ident_path(&trait_ref.trait_().name(ctx.db()).display(ctx.db(), edition).to_string());
232236
let hir_ty = ctx.sema.resolve_type(&impl_def.self_ty()?)?;
233237
let adt = hir_ty.as_adt()?.source(ctx.db())?;
234-
gen_trait_fn_body(func, &trait_path, &adt.value, Some(trait_ref))
238+
gen_trait_fn_body(make, func, &trait_path, &adt.value, Some(trait_ref))
235239
}
236240

237241
#[cfg(test)]

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
assist_context::{AssistContext, Assists},
1414
utils::{
1515
DefaultMethods, IgnoreAssocItems, add_trait_assoc_items_to_impl_with_factory,
16-
filter_assoc_items, gen_trait_fn_body, generate_trait_impl,
16+
filter_assoc_items, gen_trait_fn_body, generate_trait_impl, generate_trait_impl_with_item,
1717
},
1818
};
1919

@@ -127,7 +127,7 @@ fn add_assist(
127127
let label = format!("Convert to manual `impl {replace_trait_path} for {annotated_name}`");
128128

129129
acc.add(AssistId::refactor("replace_derive_with_manual_impl"), label, target, |builder| {
130-
let make = SyntaxFactory::without_mappings();
130+
let make = SyntaxFactory::with_mappings();
131131
let insert_after = Position::after(adt.syntax());
132132
let impl_is_unsafe = trait_.map(|s| s.is_unsafe(ctx.db())).unwrap_or(false);
133133
let impl_def = impl_def_from_trait(
@@ -141,7 +141,7 @@ fn add_assist(
141141
);
142142

143143
let mut editor = builder.make_editor(attr.syntax());
144-
update_attribute(&mut editor, old_derives, old_tree, old_trait_path, attr);
144+
update_attribute(&make, &mut editor, old_derives, old_tree, old_trait_path, attr);
145145

146146
let trait_path = make.ty_path(replace_trait_path.clone()).into();
147147

@@ -177,6 +177,7 @@ fn add_assist(
177177
insert_after,
178178
vec![make.whitespace("\n\n").into(), impl_def.syntax().clone().into()],
179179
);
180+
editor.add_mappings(make.finish_with_mappings());
180181
builder.add_file_edits(ctx.vfs_file_id(), editor);
181182
})
182183
}
@@ -207,8 +208,8 @@ fn impl_def_from_trait(
207208
return None;
208209
}
209210
let make = SyntaxFactory::without_mappings();
210-
let trait_ty = make.ty_path(trait_path.clone()).into();
211-
let impl_def = generate_trait_impl(&make, impl_is_unsafe, adt, trait_ty);
211+
let trait_ty: ast::Type = make.ty_path(trait_path.clone()).into();
212+
let impl_def = generate_trait_impl(&make, impl_is_unsafe, adt, trait_ty.clone());
212213

213214
let assoc_items = add_trait_assoc_items_to_impl_with_factory(
214215
&make,
@@ -223,7 +224,7 @@ fn impl_def_from_trait(
223224
assoc_items.split_first().map(|(first, other)| (first.clone_subtree(), other))
224225
{
225226
let first_item = if let ast::AssocItem::Fn(ref func) = first
226-
&& let Some(body) = gen_trait_fn_body(func, trait_path, adt, None)
227+
&& let Some(body) = gen_trait_fn_body(&make, func, trait_path, adt, None)
227228
&& let Some(func_body) = func.body()
228229
{
229230
let mut editor = SyntaxEditor::new(first.syntax().clone());
@@ -239,21 +240,17 @@ fn impl_def_from_trait(
239240
make.assoc_item_list_empty()
240241
};
241242

242-
let impl_def = impl_def.clone_subtree();
243-
let mut editor = SyntaxEditor::new(impl_def.syntax().clone());
244-
editor.replace(impl_def.assoc_item_list()?.syntax(), assoc_item_list.syntax());
245-
let impl_def = ast::Impl::cast(editor.finish().new_root().clone())?;
246-
Some(impl_def)
243+
Some(generate_trait_impl_with_item(&make, impl_is_unsafe, adt, trait_ty, assoc_item_list))
247244
}
248245

249246
fn update_attribute(
247+
make: &SyntaxFactory,
250248
editor: &mut SyntaxEditor,
251249
old_derives: &[ast::Path],
252250
old_tree: &ast::TokenTree,
253251
old_trait_path: &ast::Path,
254252
attr: &ast::Attr,
255253
) {
256-
let make = SyntaxFactory::without_mappings();
257254
let new_derives = old_derives
258255
.iter()
259256
.filter(|t| t.to_string() != old_trait_path.to_string())

crates/ide-assists/src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,16 @@ pub(crate) fn generate_trait_impl_intransitive_with_item(
758758
generate_impl_inner_with_factory(make, false, adt, Some(trait_), false, Some(body))
759759
}
760760

761+
pub(crate) fn generate_trait_impl_with_item(
762+
make: &SyntaxFactory,
763+
is_unsafe: bool,
764+
adt: &ast::Adt,
765+
trait_: ast::Type,
766+
body: ast::AssocItemList,
767+
) -> ast::Impl {
768+
generate_impl_inner_with_factory(make, is_unsafe, adt, Some(trait_), true, Some(body))
769+
}
770+
761771
fn generate_impl_inner(
762772
is_unsafe: bool,
763773
adt: &ast::Adt,

0 commit comments

Comments
 (0)