Skip to content

Commit acb9da7

Browse files
committed
feat: migrate reorder_fields assist to use SyntaxFactory
Signed-off-by: Tarek <[email protected]>
1 parent 61dba02 commit acb9da7

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

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

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use either::Either;
22
use ide_db::FxHashMap;
33
use itertools::Itertools;
4-
use syntax::{ast, ted, AstNode, SmolStr, ToSmolStr};
4+
use syntax::{ast, syntax_editor::SyntaxEditor, AstNode, SmolStr, SyntaxElement, ToSmolStr};
55

66
use crate::{AssistContext, AssistId, AssistKind, Assists};
77

@@ -24,6 +24,11 @@ pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
2424
let record =
2525
path.syntax().parent().and_then(<Either<ast::RecordExpr, ast::RecordPat>>::cast)?;
2626

27+
let parent_node = match ctx.covering_element() {
28+
SyntaxElement::Node(n) => n,
29+
SyntaxElement::Token(t) => t.parent()?,
30+
};
31+
2732
let ranks = compute_fields_ranks(&path, ctx)?;
2833
let get_rank_of_field = |of: Option<SmolStr>| {
2934
*ranks.get(of.unwrap_or_default().trim_start_matches("r#")).unwrap_or(&usize::MAX)
@@ -65,23 +70,31 @@ pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
6570
AssistId("reorder_fields", AssistKind::RefactorRewrite),
6671
"Reorder record fields",
6772
target,
68-
|builder| match fields {
69-
Either::Left((sorted, field_list)) => {
70-
replace(builder.make_mut(field_list).fields(), sorted)
71-
}
72-
Either::Right((sorted, field_list)) => {
73-
replace(builder.make_mut(field_list).fields(), sorted)
73+
|builder| {
74+
let mut editor = builder.make_editor(&parent_node);
75+
76+
match fields {
77+
Either::Left((sorted, field_list)) => {
78+
replace(&mut editor, field_list.fields(), sorted)
79+
}
80+
Either::Right((sorted, field_list)) => {
81+
replace(&mut editor, field_list.fields(), sorted)
82+
}
7483
}
84+
85+
builder.add_file_edits(ctx.file_id(), editor);
7586
},
7687
)
7788
}
7889

7990
fn replace<T: AstNode + PartialEq>(
91+
editor: &mut SyntaxEditor,
8092
fields: impl Iterator<Item = T>,
8193
sorted_fields: impl IntoIterator<Item = T>,
8294
) {
8395
fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
84-
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
96+
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
97+
editor.replace(field.syntax(), sorted_field.syntax().clone_for_update())
8598
});
8699
}
87100

0 commit comments

Comments
 (0)