Skip to content

Commit a07e54c

Browse files
committed
bundle old root into SyntaxEdit result
useful for `SourceChangeBuilder` so it can still perform a tree diff without having to store the old root separately
1 parent 4e81ca3 commit a07e54c

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,23 @@ impl SyntaxEditor {
102102
}
103103
}
104104

105+
/// Represents a completed [`SyntaxEditor`] operation.
105106
pub struct SyntaxEdit {
106-
root: SyntaxNode,
107+
old_root: SyntaxNode,
108+
new_root: SyntaxNode,
107109
changed_elements: Vec<SyntaxElement>,
108110
annotations: FxHashMap<SyntaxAnnotation, Vec<SyntaxElement>>,
109111
}
110112

111113
impl SyntaxEdit {
112-
/// Root of the modified syntax tree
113-
pub fn root(&self) -> &SyntaxNode {
114-
&self.root
114+
/// Root of the initial unmodified syntax tree.
115+
pub fn old_root(&self) -> &SyntaxNode {
116+
&self.old_root
117+
}
118+
119+
/// Root of the modified syntax tree.
120+
pub fn new_root(&self) -> &SyntaxNode {
121+
&self.new_root
115122
}
116123

117124
/// Which syntax elements in the modified syntax tree were inserted or
@@ -441,14 +448,14 @@ mod tests {
441448
let var_name = 2 + 2;
442449
(var_name, true)
443450
}"#]];
444-
expect.assert_eq(&edit.root.to_string());
451+
expect.assert_eq(&edit.new_root.to_string());
445452

446453
assert_eq!(edit.find_annotation(placeholder_snippet).len(), 2);
447454
assert!(edit
448455
.annotations
449456
.iter()
450457
.flat_map(|(_, elements)| elements)
451-
.all(|element| element.ancestors().any(|it| &it == edit.root())))
458+
.all(|element| element.ancestors().any(|it| &it == edit.new_root())))
452459
}
453460

454461
#[test]
@@ -495,7 +502,7 @@ mod tests {
495502
let first = 1;{
496503
let second = 2;let third = 3;
497504
}"#]];
498-
expect.assert_eq(&edit.root.to_string());
505+
expect.assert_eq(&edit.new_root.to_string());
499506
}
500507

501508
#[test]
@@ -556,7 +563,7 @@ mod tests {
556563
}
557564
}
558565
}"#]];
559-
expect.assert_eq(&edit.root.to_string());
566+
expect.assert_eq(&edit.new_root.to_string());
560567
}
561568

562569
#[test]
@@ -599,6 +606,6 @@ mod tests {
599606
let second = 2;
600607
}
601608
}"#]];
602-
expect.assert_eq(&edit.root.to_string());
609+
expect.assert_eq(&edit.new_root.to_string());
603610
}
604611
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
8181
"some replace change ranges intersect: {:?}",
8282
changes
8383
) {
84-
return SyntaxEdit { root, annotations: Default::default(), changed_elements: vec![] };
84+
return SyntaxEdit {
85+
old_root: root.clone(),
86+
new_root: root,
87+
annotations: Default::default(),
88+
changed_elements: vec![],
89+
};
8590
}
8691

8792
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@@ -273,7 +278,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
273278
annotation_groups.entry(annotation).or_insert(vec![]).push(element);
274279
}
275280

276-
SyntaxEdit { root, changed_elements, annotations: annotation_groups }
281+
SyntaxEdit {
282+
old_root: tree_mutator.immutable,
283+
new_root: root,
284+
changed_elements,
285+
annotations: annotation_groups,
286+
}
277287
}
278288

279289
fn to_owning_node(element: &SyntaxElement) -> SyntaxNode {
@@ -329,14 +339,15 @@ impl ChangedAncestor {
329339
}
330340

331341
struct TreeMutator {
342+
immutable: SyntaxNode,
332343
mutable_clone: SyntaxNode,
333344
}
334345

335346
impl TreeMutator {
336347
fn new(immutable: &SyntaxNode) -> TreeMutator {
337348
let immutable = immutable.clone();
338349
let mutable_clone = immutable.clone_for_update();
339-
TreeMutator { mutable_clone }
350+
TreeMutator { immutable, mutable_clone }
340351
}
341352

342353
fn make_element_mut(&self, element: &SyntaxElement) -> SyntaxElement {

0 commit comments

Comments
 (0)