Skip to content

Commit 93c9f06

Browse files
committed
fix: Properly handle removals in SyntaxEditor
1 parent 6dda2e8 commit 93c9f06

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

crates/syntax/src/syntax_editor/edit_algo.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
102102
let mut changed_ancestors: VecDeque<ChangedAncestor> = VecDeque::new();
103103
let mut dependent_changes = vec![];
104104
let mut independent_changes = vec![];
105+
let mut outdated_changes = vec![];
105106

106107
for (change_index, change) in changes.iter().enumerate() {
107108
// Check if this change is dependent on another change (i.e. it's contained within another range)
@@ -116,10 +117,14 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
116117
// FIXME: Resolve changes that depend on a range of elements
117118
let ancestor = &changed_ancestors[index];
118119

119-
dependent_changes.push(DependentChange {
120-
parent: ancestor.change_index as u32,
121-
child: change_index as u32,
122-
});
120+
if let Change::Replace(_, None) = changes[ancestor.change_index] {
121+
outdated_changes.push(change_index as u32);
122+
} else {
123+
dependent_changes.push(DependentChange {
124+
parent: ancestor.change_index as u32,
125+
child: change_index as u32,
126+
});
127+
}
123128
} else {
124129
// This change is independent of any other change
125130

@@ -195,8 +200,9 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
195200
Change::Replace(target, Some(new_target)) => {
196201
(to_owning_node(target), to_owning_node(new_target))
197202
}
198-
// Silently drop outdated change
199-
Change::Replace(_, None) => continue,
203+
Change::Replace(_, None) => {
204+
unreachable!("deletions should not generate dependent changes")
205+
}
200206
Change::ReplaceAll(_, _) | Change::ReplaceWithMany(_, _) => {
201207
unimplemented!("cannot resolve changes that depend on replacing many elements")
202208
}
@@ -234,6 +240,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
234240
}
235241
}
236242

243+
// We reverse here since we pushed to this in ascending order,
244+
// and we want to remove elements in descending order
245+
for idx in outdated_changes.into_iter().rev() {
246+
changes.remove(idx as usize);
247+
}
248+
237249
// Apply changes
238250
let mut root = tree_mutator.mutable_clone;
239251

0 commit comments

Comments
 (0)