Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate reorder_impl_items Assist to Use SyntaxFactory #18521

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions crates/ide-assists/src/handlers/reorder_impl_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
},
)
}
Expand Down