Skip to content

Commit afa488c

Browse files
Merge pull request #21867 from Shourya742/2026-03-25-migrate-from-generate-impl
Replace direct use of make in generate_impl with SyntaxFactory
2 parents 71d06e7 + 596f1ec commit afa488c

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
use syntax::{
2-
ast::{self, AstNode, HasGenericParams, HasName, edit::AstNodeEdit, make},
2+
ast::{
3+
self, AstNode, HasGenericParams, HasName, edit::AstNodeEdit, syntax_factory::SyntaxFactory,
4+
},
35
syntax_editor::{Position, SyntaxEditor},
46
};
57

68
use crate::{
79
AssistContext, AssistId, Assists,
8-
utils::{self, DefaultMethods, IgnoreAssocItems},
10+
utils::{
11+
self, DefaultMethods, IgnoreAssocItems, generate_impl_with_factory,
12+
generate_trait_impl_intransitive,
13+
},
914
};
1015

1116
fn insert_impl(
1217
editor: &mut SyntaxEditor,
18+
make: &SyntaxFactory,
1319
impl_: &ast::Impl,
1420
nominal: &impl AstNodeEdit,
1521
) -> ast::Impl {
@@ -20,7 +26,7 @@ fn insert_impl(
2026
Position::after(nominal.syntax()),
2127
vec![
2228
// Add a blank line after the ADT, and indentation for the impl to match the ADT
23-
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
29+
make.whitespace(&format!("\n\n{indent}")).into(),
2430
impl_.syntax().clone().into(),
2531
],
2632
);
@@ -59,12 +65,13 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
5965
format!("Generate impl for `{name}`"),
6066
target,
6167
|edit| {
68+
let make = SyntaxFactory::with_mappings();
6269
// Generate the impl
63-
let impl_ = utils::generate_impl(&nominal);
70+
let impl_ = generate_impl_with_factory(&make, &nominal);
6471

6572
let mut editor = edit.make_editor(nominal.syntax());
6673

67-
let impl_ = insert_impl(&mut editor, &impl_, &nominal);
74+
let impl_ = insert_impl(&mut editor, &make, &impl_, &nominal);
6875
// Add a tabstop after the left curly brace
6976
if let Some(cap) = ctx.config.snippet_cap
7077
&& let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token())
@@ -73,6 +80,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
7380
editor.add_annotation(l_curly, tabstop);
7481
}
7582

83+
editor.add_mappings(make.finish_with_mappings());
7684
edit.add_file_edits(ctx.vfs_file_id(), editor);
7785
},
7886
)
@@ -109,12 +117,13 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
109117
format!("Generate trait impl for `{name}`"),
110118
target,
111119
|edit| {
120+
let make = SyntaxFactory::with_mappings();
112121
// Generate the impl
113-
let impl_ = utils::generate_trait_impl_intransitive(&nominal, make::ty_placeholder());
122+
let impl_ = generate_trait_impl_intransitive(&make, &nominal, make.ty_placeholder());
114123

115124
let mut editor = edit.make_editor(nominal.syntax());
116125

117-
let impl_ = insert_impl(&mut editor, &impl_, &nominal);
126+
let impl_ = insert_impl(&mut editor, &make, &impl_, &nominal);
118127
// Make the trait type a placeholder snippet
119128
if let Some(cap) = ctx.config.snippet_cap {
120129
if let Some(trait_) = impl_.trait_() {
@@ -128,6 +137,7 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
128137
}
129138
}
130139

140+
editor.add_mappings(make.finish_with_mappings());
131141
edit.add_file_edits(ctx.vfs_file_id(), editor);
132142
},
133143
)
@@ -166,9 +176,10 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
166176
format!("Generate `{name}` impl for type"),
167177
target,
168178
|edit| {
179+
let make = SyntaxFactory::with_mappings();
169180
let mut editor = edit.make_editor(trait_.syntax());
170181

171-
let holder_arg = ast::GenericArg::TypeArg(make::type_arg(make::ty_placeholder()));
182+
let holder_arg = ast::GenericArg::TypeArg(make.type_arg(make.ty_placeholder()));
172183
let missing_items = utils::filter_assoc_items(
173184
&ctx.sema,
174185
&hir_trait.items(ctx.db()),
@@ -177,25 +188,24 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
177188
);
178189

179190
let trait_gen_args = trait_.generic_param_list().map(|list| {
180-
make::generic_arg_list(list.generic_params().map(|_| holder_arg.clone()))
191+
make.generic_arg_list(list.generic_params().map(|_| holder_arg.clone()), false)
181192
});
182193

183194
let make_impl_ = |body| {
184-
make::impl_trait(
195+
make.impl_trait(
185196
None,
186197
trait_.unsafe_token().is_some(),
187198
None,
188199
trait_gen_args.clone(),
189200
None,
190201
None,
191202
false,
192-
make::ty(&name.text()),
193-
make::ty_placeholder(),
203+
make.ty(&name.text()),
204+
make.ty_placeholder(),
194205
None,
195206
None,
196207
body,
197208
)
198-
.clone_for_update()
199209
};
200210

201211
let impl_ = if missing_items.is_empty() {
@@ -210,11 +220,12 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
210220
&impl_,
211221
&target_scope,
212222
);
213-
let assoc_item_list = make::assoc_item_list(Some(assoc_items));
223+
let assoc_item_list = make.assoc_item_list(assoc_items);
214224
make_impl_(Some(assoc_item_list))
215225
};
216226

217-
let impl_ = insert_impl(&mut editor, &impl_, &trait_);
227+
let impl_ = insert_impl(&mut editor, &make, &impl_, &trait_);
228+
editor.add_mappings(make.finish_with_mappings());
218229

219230
if let Some(cap) = ctx.config.snippet_cap {
220231
if let Some(generics) = impl_.trait_().and_then(|it| it.generic_arg_list()) {

crates/ide-assists/src/utils.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ pub(crate) fn wrap_paren(expr: ast::Expr, make: &SyntaxFactory, prec: ExprPreced
103103
}
104104

105105
pub(crate) fn wrap_paren_in_call(expr: ast::Expr, make: &SyntaxFactory) -> ast::Expr {
106-
if needs_parens_in_call(&expr) { make.expr_paren(expr).into() } else { expr }
106+
if needs_parens_in_call(make, &expr) { make.expr_paren(expr).into() } else { expr }
107107
}
108108

109-
fn needs_parens_in_call(param: &ast::Expr) -> bool {
110-
let call = make::expr_call(make::ext::expr_unit(), make::arg_list(Vec::new()));
109+
fn needs_parens_in_call(make: &SyntaxFactory, param: &ast::Expr) -> bool {
110+
let call = make.expr_call(make.expr_unit(), make.arg_list(Vec::new()));
111111
let callable = call.expr().expect("invalid make call");
112112
param.needs_parens_in_place_of(call.syntax(), callable.syntax())
113113
}
@@ -739,6 +739,10 @@ pub(crate) fn generate_impl_with_item(
739739
generate_impl_inner(false, adt, None, true, body)
740740
}
741741

742+
pub(crate) fn generate_impl_with_factory(make: &SyntaxFactory, adt: &ast::Adt) -> ast::Impl {
743+
generate_impl_inner_with_factory(make, false, adt, None, true, None)
744+
}
745+
742746
pub(crate) fn generate_impl(adt: &ast::Adt) -> ast::Impl {
743747
generate_impl_inner(false, adt, None, true, None)
744748
}
@@ -760,8 +764,12 @@ pub(crate) fn generate_trait_impl(
760764
/// and lifetime parameters, with `impl`'s generic parameters' bounds kept as-is.
761765
///
762766
/// This is useful for traits like `From<T>`, since `impl<T> From<T> for U<T>` doesn't require `T: From<T>`.
763-
pub(crate) fn generate_trait_impl_intransitive(adt: &ast::Adt, trait_: ast::Type) -> ast::Impl {
764-
generate_impl_inner(false, adt, Some(trait_), false, None)
767+
pub(crate) fn generate_trait_impl_intransitive(
768+
make: &SyntaxFactory,
769+
adt: &ast::Adt,
770+
trait_: ast::Type,
771+
) -> ast::Impl {
772+
generate_impl_inner_with_factory(make, false, adt, Some(trait_), false, None)
765773
}
766774

767775
fn generate_impl_inner(

0 commit comments

Comments
 (0)