feat: migrate reorder_impl_items assist to use SyntaxFactory

Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
Tarek 2024-11-17 12:17:44 +02:00
parent 647749aa32
commit a96a0934de
No known key found for this signature in database
GPG key ID: 58C48198E7CC6EBD

View file

@ -3,7 +3,7 @@ use ide_db::{FxHashMap, RootDatabase};
use itertools::Itertools;
use syntax::{
ast::{self, HasName},
ted, AstNode,
AstNode, SyntaxElement,
};
use crate::{AssistContext, AssistId, AssistKind, Assists};
@ -46,6 +46,11 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let impl_ast = ctx.find_node_at_offset::<ast::Impl>()?;
let items = impl_ast.assoc_item_list()?;
let parent_node = match ctx.covering_element() {
SyntaxElement::Node(n) => n,
SyntaxElement::Token(t) => t.parent()?,
};
// restrict the range
// if cursor is in assoc_items, abort
let assoc_range = items.syntax().text_range();
@ -94,12 +99,14 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
"Sort items by trait definition",
target,
|builder| {
let assoc_items =
assoc_items.into_iter().map(|item| builder.make_mut(item)).collect::<Vec<_>>();
assoc_items
.into_iter()
.zip(sorted)
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
let mut editor = builder.make_editor(&parent_node);
assoc_items.into_iter().zip(sorted).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(ctx.file_id(), editor);
},
)
}