From e66b4f336dfb402ac28445d695d21e8709edc4d4 Mon Sep 17 00:00:00 2001 From: Giga Bowser <45986823+Giga-Bowser@users.noreply.github.com> Date: Mon, 18 Nov 2024 18:11:13 -0500 Subject: [PATCH] fix: Map new replacement nodes to their mutable equivalents in `SyntaxEditor` --- crates/ide-assists/src/handlers/flip_binexpr.rs | 5 ++--- crates/ide-assists/src/handlers/flip_comma.rs | 5 ++--- crates/ide-assists/src/handlers/flip_trait_bound.rs | 4 ++-- crates/ide-assists/src/handlers/reorder_fields.rs | 7 +++---- crates/ide-assists/src/handlers/reorder_impl_items.rs | 8 ++++---- crates/ide-assists/src/handlers/sort_items.rs | 7 +++---- crates/syntax/src/syntax_editor/edit_algo.rs | 6 ++++++ 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/crates/ide-assists/src/handlers/flip_binexpr.rs b/crates/ide-assists/src/handlers/flip_binexpr.rs index 601fd29f8e..818a868fe3 100644 --- a/crates/ide-assists/src/handlers/flip_binexpr.rs +++ b/crates/ide-assists/src/handlers/flip_binexpr.rs @@ -52,9 +52,8 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option if let FlipAction::FlipAndReplaceOp(binary_op) = action { editor.replace(op_token, make.token(binary_op)) }; - // FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us - editor.replace(lhs.syntax(), rhs.syntax().clone_for_update()); - editor.replace(rhs.syntax(), lhs.syntax().clone_for_update()); + editor.replace(lhs.syntax(), rhs.syntax()); + editor.replace(rhs.syntax(), lhs.syntax()); editor.add_mappings(make.finish_with_mappings()); builder.add_file_edits(ctx.file_id(), editor); }, diff --git a/crates/ide-assists/src/handlers/flip_comma.rs b/crates/ide-assists/src/handlers/flip_comma.rs index 490a9ee3c0..95e035c053 100644 --- a/crates/ide-assists/src/handlers/flip_comma.rs +++ b/crates/ide-assists/src/handlers/flip_comma.rs @@ -39,13 +39,12 @@ pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<( return None; } - // FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us let prev = match prev { - SyntaxElement::Node(node) => node.clone_for_update().syntax_element(), + SyntaxElement::Node(node) => node.syntax_element(), _ => prev, }; let next = match next { - SyntaxElement::Node(node) => node.clone_for_update().syntax_element(), + SyntaxElement::Node(node) => node.syntax_element(), _ => next, }; diff --git a/crates/ide-assists/src/handlers/flip_trait_bound.rs b/crates/ide-assists/src/handlers/flip_trait_bound.rs index 03366bd861..298e5bd82c 100644 --- a/crates/ide-assists/src/handlers/flip_trait_bound.rs +++ b/crates/ide-assists/src/handlers/flip_trait_bound.rs @@ -37,8 +37,8 @@ pub(crate) fn flip_trait_bound(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op target, |builder| { let mut editor = builder.make_editor(parent.syntax()); - editor.replace(before.clone(), after.clone_for_update()); - editor.replace(after.clone(), before.clone_for_update()); + editor.replace(before.clone(), after.clone()); + editor.replace(after, before); builder.add_file_edits(ctx.file_id(), editor); }, ) diff --git a/crates/ide-assists/src/handlers/reorder_fields.rs b/crates/ide-assists/src/handlers/reorder_fields.rs index 4d3e85ab1b..972303c2a0 100644 --- a/crates/ide-assists/src/handlers/reorder_fields.rs +++ b/crates/ide-assists/src/handlers/reorder_fields.rs @@ -92,10 +92,9 @@ fn replace( fields: impl Iterator, sorted_fields: impl IntoIterator, ) { - fields.zip(sorted_fields).for_each(|(field, sorted_field)| { - // FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us - editor.replace(field.syntax(), sorted_field.syntax().clone_for_update()) - }); + fields + .zip(sorted_fields) + .for_each(|(field, sorted_field)| editor.replace(field.syntax(), sorted_field.syntax())); } fn compute_fields_ranks( diff --git a/crates/ide-assists/src/handlers/reorder_impl_items.rs b/crates/ide-assists/src/handlers/reorder_impl_items.rs index d7fa882612..eb1d538f87 100644 --- a/crates/ide-assists/src/handlers/reorder_impl_items.rs +++ b/crates/ide-assists/src/handlers/reorder_impl_items.rs @@ -101,10 +101,10 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) -> |builder| { 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()) - }); + assoc_items + .into_iter() + .zip(sorted) + .for_each(|(old, new)| editor.replace(old.syntax(), new.syntax())); builder.add_file_edits(ctx.file_id(), editor); }, diff --git a/crates/ide-assists/src/handlers/sort_items.rs b/crates/ide-assists/src/handlers/sort_items.rs index 7307325e49..54e16d4d80 100644 --- a/crates/ide-assists/src/handlers/sort_items.rs +++ b/crates/ide-assists/src/handlers/sort_items.rs @@ -133,10 +133,9 @@ impl AddRewrite for Assists { |builder| { let mut editor = builder.make_editor(target); - old.into_iter().zip(new).for_each(|(old, new)| { - // FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us - editor.replace(old.syntax(), new.clone_for_update().syntax()) - }); + old.into_iter() + .zip(new) + .for_each(|(old, new)| editor.replace(old.syntax(), new.syntax())); builder.add_file_edits(builder.file_id, editor) }, diff --git a/crates/syntax/src/syntax_editor/edit_algo.rs b/crates/syntax/src/syntax_editor/edit_algo.rs index 71b69dbec1..57ecbe5701 100644 --- a/crates/syntax/src/syntax_editor/edit_algo.rs +++ b/crates/syntax/src/syntax_editor/edit_algo.rs @@ -155,6 +155,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit { } }; } + Change::Replace(SyntaxElement::Node(target), Some(SyntaxElement::Node(new_target))) => { + *target = tree_mutator.make_syntax_mut(target); + if new_target.ancestors().any(|node| node == tree_mutator.immutable) { + *new_target = new_target.clone_for_update(); + } + } Change::Replace(target, _) | Change::ReplaceWithMany(target, _) => { *target = tree_mutator.make_element_mut(target); }