feat: migrate reorder_fields assist to use SyntaxFactory

Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
Tarek 2024-11-13 15:02:08 +02:00
parent 61dba0292d
commit acb9da7c93
No known key found for this signature in database
GPG Key ID: 58C48198E7CC6EBD

View File

@ -1,7 +1,7 @@
use either::Either;
use ide_db::FxHashMap;
use itertools::Itertools;
use syntax::{ast, ted, AstNode, SmolStr, ToSmolStr};
use syntax::{ast, syntax_editor::SyntaxEditor, AstNode, SmolStr, SyntaxElement, ToSmolStr};
use crate::{AssistContext, AssistId, AssistKind, Assists};
@ -24,6 +24,11 @@ pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
let record =
path.syntax().parent().and_then(<Either<ast::RecordExpr, ast::RecordPat>>::cast)?;
let parent_node = match ctx.covering_element() {
SyntaxElement::Node(n) => n,
SyntaxElement::Token(t) => t.parent()?,
};
let ranks = compute_fields_ranks(&path, ctx)?;
let get_rank_of_field = |of: Option<SmolStr>| {
*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
AssistId("reorder_fields", AssistKind::RefactorRewrite),
"Reorder record fields",
target,
|builder| match fields {
Either::Left((sorted, field_list)) => {
replace(builder.make_mut(field_list).fields(), sorted)
}
Either::Right((sorted, field_list)) => {
replace(builder.make_mut(field_list).fields(), sorted)
|builder| {
let mut editor = builder.make_editor(&parent_node);
match fields {
Either::Left((sorted, field_list)) => {
replace(&mut editor, field_list.fields(), sorted)
}
Either::Right((sorted, field_list)) => {
replace(&mut editor, field_list.fields(), sorted)
}
}
builder.add_file_edits(ctx.file_id(), editor);
},
)
}
fn replace<T: AstNode + PartialEq>(
editor: &mut SyntaxEditor,
fields: impl Iterator<Item = T>,
sorted_fields: impl IntoIterator<Item = T>,
) {
fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
editor.replace(field.syntax(), sorted_field.syntax().clone_for_update())
});
}