Skip to content

Commit 4fac9df

Browse files
Merge pull request #21993 from Shourya742/2026-04-08-remove-generate-trait-from-impl
Remove make with SyntaxFactory in generate_trait_from_impl
2 parents ea8c16c + 20c47a9 commit 4fac9df

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use syntax::{
44
AstNode, AstToken, SyntaxKind, T,
55
ast::{
66
self, HasDocComments, HasGenericParams, HasName, HasVisibility, edit::AstNodeEdit, make,
7+
syntax_factory::SyntaxFactory,
78
},
89
syntax_editor::{Position, SyntaxEditor},
910
};
@@ -107,17 +108,18 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
107108
});
108109
ast::AssocItemList::cast(trait_items_editor.finish().new_root().clone()).unwrap()
109110
};
110-
let trait_ast = make::trait_(
111+
112+
let factory = SyntaxFactory::with_mappings();
113+
let trait_ast = factory.trait_(
111114
false,
112115
&trait_name(&impl_assoc_items).text(),
113116
impl_ast.generic_param_list(),
114117
impl_ast.where_clause(),
115118
trait_items,
116-
)
117-
.clone_for_update();
119+
);
118120

119121
let trait_name = trait_ast.name().expect("new trait should have a name");
120-
let trait_name_ref = make::name_ref(&trait_name.to_string()).clone_for_update();
122+
let trait_name_ref = factory.name_ref(&trait_name.to_string());
121123

122124
// Change `impl Foo` to `impl NewTrait for Foo`
123125
let mut elements = vec![
@@ -128,7 +130,7 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
128130
];
129131

130132
if let Some(params) = impl_ast.generic_param_list() {
131-
let gen_args = &params.to_generic_args().clone_for_update();
133+
let gen_args = &params.to_generic_args();
132134
elements.insert(1, gen_args.syntax().clone().into());
133135
}
134136

@@ -156,6 +158,7 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
156158
editor.add_annotation(trait_name_ref.syntax(), placeholder);
157159
}
158160

161+
editor.add_mappings(factory.finish_with_mappings());
159162
builder.add_file_edits(ctx.vfs_file_id(), editor);
160163
},
161164
);

crates/syntax/src/ast/syntax_factory/constructors.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,47 @@ impl SyntaxFactory {
19601960
ast
19611961
}
19621962

1963+
pub fn trait_(
1964+
&self,
1965+
is_unsafe: bool,
1966+
ident: &str,
1967+
generic_param_list: Option<ast::GenericParamList>,
1968+
where_clause: Option<ast::WhereClause>,
1969+
assoc_items: ast::AssocItemList,
1970+
) -> ast::Trait {
1971+
let ast = make::trait_(
1972+
is_unsafe,
1973+
ident,
1974+
generic_param_list.clone(),
1975+
where_clause.clone(),
1976+
assoc_items.clone(),
1977+
)
1978+
.clone_for_update();
1979+
1980+
if let Some(mut mapping) = self.mappings() {
1981+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
1982+
if let Some(generic_param_list) = generic_param_list {
1983+
builder.map_node(
1984+
generic_param_list.syntax().clone(),
1985+
ast.generic_param_list().unwrap().syntax().clone(),
1986+
);
1987+
}
1988+
if let Some(where_clause) = where_clause {
1989+
builder.map_node(
1990+
where_clause.syntax().clone(),
1991+
ast.where_clause().unwrap().syntax().clone(),
1992+
);
1993+
}
1994+
builder.map_node(
1995+
assoc_items.syntax().clone(),
1996+
ast.assoc_item_list().unwrap().syntax().clone(),
1997+
);
1998+
builder.finish(&mut mapping);
1999+
}
2000+
2001+
ast
2002+
}
2003+
19632004
pub fn ret_type(&self, ty: ast::Type) -> ast::RetType {
19642005
let ast = make::ret_type(ty.clone()).clone_for_update();
19652006

0 commit comments

Comments
 (0)