about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorDavid Barsky <me@davidbarsky.com>2024-11-19 20:38:57 +0000
committerGitHub <noreply@github.com>2024-11-19 20:38:57 +0000
commit70a098e6fc24ae46e6eca70412d6b599136ebc31 (patch)
tree7fa6d6407e9c501ee18edd46f8844db6ecf1ccc9 /src/tools
parentaf318f732024fc47def3cfc9cd254b61ea1337d7 (diff)
parenta96a0934de14b4bd921b43695a1b50ea72f3077d (diff)
Merge pull request #18521 from tareknaser/syntax_factory_reorder_impl_items
Migrate `reorder_impl_items` Assist to Use `SyntaxFactory`
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs
index ada89ce7c40..d7fa8826125 100644
--- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs
+++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs
@@ -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);
         },
     )
 }