mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
feat: migrate sort_items
assist to use SyntaxFactory
Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
parent
edb432639b
commit
7149c4dab9
1 changed files with 22 additions and 18 deletions
|
@ -4,7 +4,7 @@ use itertools::Itertools;
|
|||
|
||||
use syntax::{
|
||||
ast::{self, HasName},
|
||||
ted, AstNode, TextRange,
|
||||
AstNode, SyntaxNode,
|
||||
};
|
||||
|
||||
use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
|
||||
|
@ -114,7 +114,7 @@ trait AddRewrite {
|
|||
label: &str,
|
||||
old: Vec<T>,
|
||||
new: Vec<T>,
|
||||
target: TextRange,
|
||||
target: SyntaxNode,
|
||||
) -> Option<()>;
|
||||
}
|
||||
|
||||
|
@ -124,15 +124,24 @@ impl AddRewrite for Assists {
|
|||
label: &str,
|
||||
old: Vec<T>,
|
||||
new: Vec<T>,
|
||||
target: TextRange,
|
||||
target: SyntaxNode,
|
||||
) -> Option<()> {
|
||||
self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| {
|
||||
let mutable: Vec<T> = old.into_iter().map(|it| builder.make_mut(it)).collect();
|
||||
mutable
|
||||
.into_iter()
|
||||
.zip(new)
|
||||
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
|
||||
})
|
||||
let node = old.first().unwrap().syntax().parent().unwrap();
|
||||
self.add(
|
||||
AssistId("sort_items", AssistKind::RefactorRewrite),
|
||||
label,
|
||||
target.text_range(),
|
||||
|builder| {
|
||||
let mut editor = builder.make_editor(&node);
|
||||
|
||||
old.into_iter().zip(new).for_each(|(old, new)| {
|
||||
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
|
||||
editor.replace(old.syntax(), new.clone_for_update().syntax())
|
||||
});
|
||||
|
||||
builder.add_file_edits(builder.file_id, editor)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -167,7 +176,7 @@ fn add_sort_methods_assist(
|
|||
return None;
|
||||
}
|
||||
|
||||
acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().text_range())
|
||||
acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().clone())
|
||||
}
|
||||
|
||||
fn add_sort_fields_assist(
|
||||
|
@ -186,7 +195,7 @@ fn add_sort_fields_assist(
|
|||
"Sort fields alphabetically",
|
||||
fields,
|
||||
sorted,
|
||||
record_field_list.syntax().text_range(),
|
||||
record_field_list.syntax().clone(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -199,12 +208,7 @@ fn add_sort_variants_assist(acc: &mut Assists, variant_list: ast::VariantList) -
|
|||
return None;
|
||||
}
|
||||
|
||||
acc.add_rewrite(
|
||||
"Sort variants alphabetically",
|
||||
variants,
|
||||
sorted,
|
||||
variant_list.syntax().text_range(),
|
||||
)
|
||||
acc.add_rewrite("Sort variants alphabetically", variants, sorted, variant_list.syntax().clone())
|
||||
}
|
||||
|
||||
fn sort_by_name<T: HasName + Clone>(initial: &[T]) -> Vec<T> {
|
||||
|
|
Loading…
Reference in a new issue