Skip to content

Commit 23339cb

Browse files
authored
Merge pull request rust-lang#18538 from tareknaser/syntax_factory_sort_items
Migrate `sort_items` Assist to Use `SyntaxFactory`
2 parents 6ef7f8e + a1fa497 commit 23339cb

File tree

1 file changed

+21
-23
lines changed
  • src/tools/rust-analyzer/crates/ide-assists/src/handlers

1 file changed

+21
-23
lines changed

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

Lines changed: 21 additions & 23 deletions
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,23 @@ 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+
self.add(
130+
AssistId("sort_items", AssistKind::RefactorRewrite),
131+
label,
132+
target.text_range(),
133+
|builder| {
134+
let mut editor = builder.make_editor(target);
135+
136+
old.into_iter().zip(new).for_each(|(old, new)| {
137+
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
138+
editor.replace(old.syntax(), new.clone_for_update().syntax())
139+
});
140+
141+
builder.add_file_edits(builder.file_id, editor)
142+
},
143+
)
136144
}
137145
}
138146

@@ -167,7 +175,7 @@ fn add_sort_methods_assist(
167175
return None;
168176
}
169177

170-
acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().text_range())
178+
acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax())
171179
}
172180

173181
fn add_sort_fields_assist(
@@ -182,12 +190,7 @@ fn add_sort_fields_assist(
182190
return None;
183191
}
184192

185-
acc.add_rewrite(
186-
"Sort fields alphabetically",
187-
fields,
188-
sorted,
189-
record_field_list.syntax().text_range(),
190-
)
193+
acc.add_rewrite("Sort fields alphabetically", fields, sorted, record_field_list.syntax())
191194
}
192195

193196
fn add_sort_variants_assist(acc: &mut Assists, variant_list: ast::VariantList) -> Option<()> {
@@ -199,12 +202,7 @@ fn add_sort_variants_assist(acc: &mut Assists, variant_list: ast::VariantList) -
199202
return None;
200203
}
201204

202-
acc.add_rewrite(
203-
"Sort variants alphabetically",
204-
variants,
205-
sorted,
206-
variant_list.syntax().text_range(),
207-
)
205+
acc.add_rewrite("Sort variants alphabetically", variants, sorted, variant_list.syntax())
208206
}
209207

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

0 commit comments

Comments
 (0)