Skip to content

Commit

Permalink
Merge pull request rust-lang#18531 from Giga-Bowser/map-new-nodes
Browse files Browse the repository at this point in the history
fix: Map new replacement nodes to their mutable equivalents in `SyntaxEditor`
  • Loading branch information
Veykril authored Dec 9, 2024
2 parents 7625d76 + 30e3d23 commit b4c83da
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit b4c83da

Please sign in to comment.