Skip to content

Commit b4c83da

Browse files
authored
Merge pull request #18531 from Giga-Bowser/map-new-nodes
fix: Map new replacement nodes to their mutable equivalents in `SyntaxEditor`
2 parents 7625d76 + 30e3d23 commit b4c83da

File tree

7 files changed

+22
-20
lines changed

7 files changed

+22
-20
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/flip_binexpr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
5252
if let FlipAction::FlipAndReplaceOp(binary_op) = action {
5353
editor.replace(op_token, make.token(binary_op))
5454
};
55-
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
56-
editor.replace(lhs.syntax(), rhs.syntax().clone_for_update());
57-
editor.replace(rhs.syntax(), lhs.syntax().clone_for_update());
55+
editor.replace(lhs.syntax(), rhs.syntax());
56+
editor.replace(rhs.syntax(), lhs.syntax());
5857
editor.add_mappings(make.finish_with_mappings());
5958
builder.add_file_edits(ctx.file_id(), editor);
6059
},

src/tools/rust-analyzer/crates/ide-assists/src/handlers/flip_comma.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
3939
return None;
4040
}
4141

42-
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
4342
let prev = match prev {
44-
SyntaxElement::Node(node) => node.clone_for_update().syntax_element(),
43+
SyntaxElement::Node(node) => node.syntax_element(),
4544
_ => prev,
4645
};
4746
let next = match next {
48-
SyntaxElement::Node(node) => node.clone_for_update().syntax_element(),
47+
SyntaxElement::Node(node) => node.syntax_element(),
4948
_ => next,
5049
};
5150

src/tools/rust-analyzer/crates/ide-assists/src/handlers/flip_trait_bound.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub(crate) fn flip_trait_bound(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
3737
target,
3838
|builder| {
3939
let mut editor = builder.make_editor(parent.syntax());
40-
editor.replace(before.clone(), after.clone_for_update());
41-
editor.replace(after.clone(), before.clone_for_update());
40+
editor.replace(before.clone(), after.clone());
41+
editor.replace(after, before);
4242
builder.add_file_edits(ctx.file_id(), editor);
4343
},
4444
)

src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_fields.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ fn replace<T: AstNode + PartialEq>(
9292
fields: impl Iterator<Item = T>,
9393
sorted_fields: impl IntoIterator<Item = T>,
9494
) {
95-
fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
96-
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
97-
editor.replace(field.syntax(), sorted_field.syntax().clone_for_update())
98-
});
95+
fields
96+
.zip(sorted_fields)
97+
.for_each(|(field, sorted_field)| editor.replace(field.syntax(), sorted_field.syntax()));
9998
}
10099

101100
fn compute_fields_ranks(

src/tools/rust-analyzer/crates/ide-assists/src/handlers/reorder_impl_items.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
101101
|builder| {
102102
let mut editor = builder.make_editor(&parent_node);
103103

104-
assoc_items.into_iter().zip(sorted).for_each(|(old, new)| {
105-
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
106-
editor.replace(old.syntax(), new.clone_for_update().syntax())
107-
});
104+
assoc_items
105+
.into_iter()
106+
.zip(sorted)
107+
.for_each(|(old, new)| editor.replace(old.syntax(), new.syntax()));
108108

109109
builder.add_file_edits(ctx.file_id(), editor);
110110
},

src/tools/rust-analyzer/crates/ide-assists/src/handlers/sort_items.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ impl AddRewrite for Assists {
133133
|builder| {
134134
let mut editor = builder.make_editor(target);
135135

136-
old.into_iter().zip(new).for_each(|(old, new)| {
137-
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
138-
editor.replace(old.syntax(), new.clone_for_update().syntax())
139-
});
136+
old.into_iter()
137+
.zip(new)
138+
.for_each(|(old, new)| editor.replace(old.syntax(), new.syntax()));
140139

141140
builder.add_file_edits(builder.file_id, editor)
142141
},

src/tools/rust-analyzer/crates/syntax/src/syntax_editor/edit_algo.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
155155
}
156156
};
157157
}
158+
Change::Replace(SyntaxElement::Node(target), Some(SyntaxElement::Node(new_target))) => {
159+
*target = tree_mutator.make_syntax_mut(target);
160+
if new_target.ancestors().any(|node| node == tree_mutator.immutable) {
161+
*new_target = new_target.clone_for_update();
162+
}
163+
}
158164
Change::Replace(target, _) | Change::ReplaceWithMany(target, _) => {
159165
*target = tree_mutator.make_element_mut(target);
160166
}

0 commit comments

Comments
 (0)