Merge pull request #18521 from tareknaser/syntax_factory_reorder_impl_items

Migrate `reorder_impl_items` Assist to Use `SyntaxFactory`
This commit is contained in:
David Barsky 2024-11-19 20:38:57 +00:00 committed by GitHub
commit 84078238c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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);
},
)
}