fix: Map new replacement nodes to their mutable equivalents in SyntaxEditor

This commit is contained in:
Giga Bowser 2024-11-18 18:11:13 -05:00
parent 4c755e62a6
commit e66b4f336d
7 changed files with 22 additions and 20 deletions

View file

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

View file

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

View file

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

View file

@ -92,10 +92,9 @@ fn replace<T: AstNode + PartialEq>(
fields: impl Iterator<Item = T>,
sorted_fields: impl IntoIterator<Item = T>,
) {
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(

View file

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

View file

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

View file

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