mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 22:24:14 +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::{
|
use syntax::{
|
||||||
ast::{self, HasName},
|
ast::{self, HasName},
|
||||||
ted, AstNode, TextRange,
|
AstNode, SyntaxNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
|
use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
@ -114,7 +114,7 @@ trait AddRewrite {
|
||||||
label: &str,
|
label: &str,
|
||||||
old: Vec<T>,
|
old: Vec<T>,
|
||||||
new: Vec<T>,
|
new: Vec<T>,
|
||||||
target: TextRange,
|
target: SyntaxNode,
|
||||||
) -> Option<()>;
|
) -> Option<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,15 +124,24 @@ impl AddRewrite for Assists {
|
||||||
label: &str,
|
label: &str,
|
||||||
old: Vec<T>,
|
old: Vec<T>,
|
||||||
new: Vec<T>,
|
new: Vec<T>,
|
||||||
target: TextRange,
|
target: SyntaxNode,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| {
|
let node = old.first().unwrap().syntax().parent().unwrap();
|
||||||
let mutable: Vec<T> = old.into_iter().map(|it| builder.make_mut(it)).collect();
|
self.add(
|
||||||
mutable
|
AssistId("sort_items", AssistKind::RefactorRewrite),
|
||||||
.into_iter()
|
label,
|
||||||
.zip(new)
|
target.text_range(),
|
||||||
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
|
|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;
|
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(
|
fn add_sort_fields_assist(
|
||||||
|
@ -186,7 +195,7 @@ fn add_sort_fields_assist(
|
||||||
"Sort fields alphabetically",
|
"Sort fields alphabetically",
|
||||||
fields,
|
fields,
|
||||||
sorted,
|
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;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
acc.add_rewrite(
|
acc.add_rewrite("Sort variants alphabetically", variants, sorted, variant_list.syntax().clone())
|
||||||
"Sort variants alphabetically",
|
|
||||||
variants,
|
|
||||||
sorted,
|
|
||||||
variant_list.syntax().text_range(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sort_by_name<T: HasName + Clone>(initial: &[T]) -> Vec<T> {
|
fn sort_by_name<T: HasName + Clone>(initial: &[T]) -> Vec<T> {
|
||||||
|
|
Loading…
Reference in a new issue