Skip to content

Commit a43a309

Browse files
committed
feat: migrate reorder_impl_items assist to use SyntaxFactory
Signed-off-by: Tarek <[email protected]>
1 parent ba56d9b commit a43a309

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

crates/ide-assists/src/handlers/reorder_impl_items.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ide_db::{FxHashMap, RootDatabase};
33
use itertools::Itertools;
44
use syntax::{
55
ast::{self, HasName},
6-
ted, AstNode,
6+
AstNode, SyntaxElement,
77
};
88

99
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -46,6 +46,11 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4646
let impl_ast = ctx.find_node_at_offset::<ast::Impl>()?;
4747
let items = impl_ast.assoc_item_list()?;
4848

49+
let parent_node = match ctx.covering_element() {
50+
SyntaxElement::Node(n) => n,
51+
SyntaxElement::Token(t) => t.parent()?,
52+
};
53+
4954
// restrict the range
5055
// if cursor is in assoc_items, abort
5156
let assoc_range = items.syntax().text_range();
@@ -94,12 +99,16 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
9499
"Sort items by trait definition",
95100
target,
96101
|builder| {
102+
let mut editor = builder.make_editor(&parent_node);
103+
97104
let assoc_items =
98105
assoc_items.into_iter().map(|item| builder.make_mut(item)).collect::<Vec<_>>();
99-
assoc_items
100-
.into_iter()
101-
.zip(sorted)
102-
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
106+
assoc_items.into_iter().zip(sorted).for_each(|(old, new)| {
107+
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
108+
editor.replace(old.syntax(), new.clone_for_update().syntax())
109+
});
110+
111+
builder.add_file_edits(ctx.file_id(), editor);
103112
},
104113
)
105114
}

0 commit comments

Comments
 (0)