Skip to content

Commit cca89bc

Browse files
committed
add SyntaxFactory::record_expr to hide clone_for_update
1 parent 8f89872 commit cca89bc

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use ide_db::defs::{Definition, NameRefClass};
44
use std::ops::RangeInclusive;
55
use syntax::{
66
SyntaxElement, SyntaxKind, SyntaxNode, T, TextSize,
7-
ast::{self, AstNode, HasAttrs, HasGenericParams, HasVisibility},
7+
ast::{
8+
self, AstNode, HasAttrs, HasGenericParams, HasVisibility, syntax_factory::SyntaxFactory,
9+
},
810
match_ast,
911
syntax_editor::{Element, Position, SyntaxEditor},
1012
};
@@ -105,7 +107,8 @@ fn edit_struct_def(
105107
);
106108
ast::RecordField::cast(field_editor.finish().new_root().clone())
107109
});
108-
let record_fields = ast::make::record_field_list(record_fields).clone_for_update();
110+
let make = SyntaxFactory::without_mappings();
111+
let record_fields = make.record_field_list(record_fields);
109112
let tuple_fields_before = Position::before(tuple_fields.syntax());
110113

111114
if let Either::Left(strukt) = strukt {
@@ -145,10 +148,11 @@ fn edit_struct_references(
145148
let usages = strukt_def.usages(&ctx.sema).include_self_refs().all();
146149

147150
let edit_node = |node: SyntaxNode| -> Option<SyntaxNode> {
151+
let make = SyntaxFactory::without_mappings();
148152
match_ast! {
149153
match node {
150154
ast::TupleStructPat(tuple_struct_pat) => {
151-
Some(ast::make::record_pat_with_fields(
155+
Some(make.record_pat_with_fields(
152156
tuple_struct_pat.path()?,
153157
ast::make::record_pat_field_list(tuple_struct_pat.fields().zip(names).map(
154158
|(pat, name)| {
@@ -158,7 +162,7 @@ fn edit_struct_references(
158162
)
159163
},
160164
), None),
161-
).syntax().clone_for_update())
165+
).syntax().clone())
162166
},
163167
// for tuple struct creations like Foo(42)
164168
ast::CallExpr(call_expr) => {
@@ -174,9 +178,8 @@ fn edit_struct_references(
174178
}
175179

176180
let arg_list = call_expr.syntax().descendants().find_map(ast::ArgList::cast)?;
177-
178181
Some(
179-
ast::make::record_expr(
182+
make.record_expr(
180183
path,
181184
ast::make::record_expr_field_list(arg_list.args().zip(names).map(
182185
|(expr, name)| {
@@ -186,7 +189,7 @@ fn edit_struct_references(
186189
)
187190
},
188191
)),
189-
).syntax().clone_for_update()
192+
).syntax().clone()
190193
)
191194
},
192195
_ => return None,
@@ -271,11 +274,12 @@ fn edit_field_references(
271274
}
272275

273276
fn generate_names(fields: impl Iterator<Item = ast::TupleField>) -> Vec<ast::Name> {
277+
let make = SyntaxFactory::without_mappings();
274278
fields
275279
.enumerate()
276280
.map(|(i, _)| {
277281
let idx = i + 1;
278-
ast::make::name(&format!("field{idx}")).clone_for_update()
282+
make.name(&format!("field{idx}"))
279283
})
280284
.collect()
281285
}

src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,24 @@ impl SyntaxFactory {
939939
ast
940940
}
941941

942+
pub fn record_expr(
943+
&self,
944+
path: ast::Path,
945+
fields: ast::RecordExprFieldList,
946+
) -> ast::RecordExpr {
947+
let ast = make::record_expr(path.clone(), fields.clone()).clone_for_update();
948+
if let Some(mut mapping) = self.mappings() {
949+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
950+
builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
951+
builder.map_node(
952+
fields.syntax().clone(),
953+
ast.record_expr_field_list().unwrap().syntax().clone(),
954+
);
955+
builder.finish(&mut mapping);
956+
}
957+
ast
958+
}
959+
942960
pub fn record_expr_field(
943961
&self,
944962
name: ast::NameRef,

0 commit comments

Comments
 (0)