Skip to content

Commit 552206d

Browse files
committed
feat: migrate sort_items assist to use SyntaxFactory
Signed-off-by: Tarek <[email protected]>
1 parent 8d5e91c commit 552206d

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

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

+22-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use itertools::Itertools;
44

55
use syntax::{
66
ast::{self, HasName},
7-
ted, AstNode, TextRange,
7+
AstNode, SyntaxNode,
88
};
99

1010
use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
@@ -114,7 +114,7 @@ trait AddRewrite {
114114
label: &str,
115115
old: Vec<T>,
116116
new: Vec<T>,
117-
target: TextRange,
117+
target: SyntaxNode,
118118
) -> Option<()>;
119119
}
120120

@@ -124,15 +124,24 @@ impl AddRewrite for Assists {
124124
label: &str,
125125
old: Vec<T>,
126126
new: Vec<T>,
127-
target: TextRange,
127+
target: SyntaxNode,
128128
) -> Option<()> {
129-
self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| {
130-
let mutable: Vec<T> = old.into_iter().map(|it| builder.make_mut(it)).collect();
131-
mutable
132-
.into_iter()
133-
.zip(new)
134-
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
135-
})
129+
let node = old.first().unwrap().syntax().parent().unwrap();
130+
self.add(
131+
AssistId("sort_items", AssistKind::RefactorRewrite),
132+
label,
133+
target.text_range(),
134+
|builder| {
135+
let mut editor = builder.make_editor(&node);
136+
137+
old.into_iter().zip(new).for_each(|(old, new)| {
138+
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
139+
editor.replace(old.syntax(), new.clone_for_update().syntax())
140+
});
141+
142+
builder.add_file_edits(builder.file_id, editor)
143+
},
144+
)
136145
}
137146
}
138147

@@ -167,7 +176,7 @@ fn add_sort_methods_assist(
167176
return None;
168177
}
169178

170-
acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().text_range())
179+
acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().clone())
171180
}
172181

173182
fn add_sort_fields_assist(
@@ -186,7 +195,7 @@ fn add_sort_fields_assist(
186195
"Sort fields alphabetically",
187196
fields,
188197
sorted,
189-
record_field_list.syntax().text_range(),
198+
record_field_list.syntax().clone(),
190199
)
191200
}
192201

@@ -199,12 +208,7 @@ fn add_sort_variants_assist(acc: &mut Assists, variant_list: ast::VariantList) -
199208
return None;
200209
}
201210

202-
acc.add_rewrite(
203-
"Sort variants alphabetically",
204-
variants,
205-
sorted,
206-
variant_list.syntax().text_range(),
207-
)
211+
acc.add_rewrite("Sort variants alphabetically", variants, sorted, variant_list.syntax().clone())
208212
}
209213

210214
fn sort_by_name<T: HasName + Clone>(initial: &[T]) -> Vec<T> {

0 commit comments

Comments
 (0)