Skip to content

Commit f78b75d

Browse files
committed
edit_algo only handled the node mutability scenario earlier, this commits supports it for token as well, making its parent mutable
1 parent 3e5f921 commit f78b75d

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

crates/syntax/src/syntax_editor/edit_algo.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,41 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
215215
*node = node.clone_for_update();
216216
}
217217
}
218+
Change::Insert(_, SyntaxElement::Token(token))
219+
| Change::Replace(_, Some(SyntaxElement::Token(token))) => {
220+
if token.parent().is_some() {
221+
let idx = token.index();
222+
let new_parent = token.parent().unwrap().clone_subtree().clone_for_update();
223+
*token = new_parent
224+
.children_with_tokens()
225+
.nth(idx)
226+
.and_then(SyntaxElement::into_token)
227+
.unwrap();
228+
}
229+
}
218230
Change::InsertAll(_, elements)
219231
| Change::ReplaceWithMany(_, elements)
220232
| Change::ReplaceAll(_, elements) => {
221233
for element in elements {
222-
if let SyntaxElement::Node(node) = element {
223-
if node.parent().is_some() {
224-
*node = node.clone_subtree().clone_for_update();
225-
} else if !node.is_mutable() {
226-
*node = node.clone_for_update();
234+
match element {
235+
SyntaxElement::Node(node) => {
236+
if node.parent().is_some() {
237+
*node = node.clone_subtree().clone_for_update();
238+
} else if !node.is_mutable() {
239+
*node = node.clone_for_update();
240+
}
241+
}
242+
SyntaxElement::Token(token) => {
243+
if token.parent().is_some() {
244+
let idx = token.index();
245+
let new_parent =
246+
token.parent().unwrap().clone_subtree().clone_for_update();
247+
*token = new_parent
248+
.children_with_tokens()
249+
.nth(idx)
250+
.and_then(SyntaxElement::into_token)
251+
.unwrap();
252+
}
227253
}
228254
}
229255
}

0 commit comments

Comments
 (0)