Skip to content

Commit

Permalink
bundle old root into SyntaxEdit result
Browse files Browse the repository at this point in the history
useful for `SourceChangeBuilder` so it can still perform a tree diff without having to store the old root separately
  • Loading branch information
DropDemBits committed Sep 3, 2024
1 parent 4e81ca3 commit a07e54c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
25 changes: 16 additions & 9 deletions src/tools/rust-analyzer/crates/syntax/src/syntax_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,23 @@ impl SyntaxEditor {
}
}

/// Represents a completed [`SyntaxEditor`] operation.
pub struct SyntaxEdit {
root: SyntaxNode,
old_root: SyntaxNode,
new_root: SyntaxNode,
changed_elements: Vec<SyntaxElement>,
annotations: FxHashMap<SyntaxAnnotation, Vec<SyntaxElement>>,
}

impl SyntaxEdit {
/// Root of the modified syntax tree
pub fn root(&self) -> &SyntaxNode {
&self.root
/// Root of the initial unmodified syntax tree.
pub fn old_root(&self) -> &SyntaxNode {
&self.old_root
}

/// Root of the modified syntax tree.
pub fn new_root(&self) -> &SyntaxNode {
&self.new_root
}

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

assert_eq!(edit.find_annotation(placeholder_snippet).len(), 2);
assert!(edit
.annotations
.iter()
.flat_map(|(_, elements)| elements)
.all(|element| element.ancestors().any(|it| &it == edit.root())))
.all(|element| element.ancestors().any(|it| &it == edit.new_root())))
}

#[test]
Expand Down Expand Up @@ -495,7 +502,7 @@ mod tests {
let first = 1;{
let second = 2;let third = 3;
}"#]];
expect.assert_eq(&edit.root.to_string());
expect.assert_eq(&edit.new_root.to_string());
}

#[test]
Expand Down Expand Up @@ -556,7 +563,7 @@ mod tests {
}
}
}"#]];
expect.assert_eq(&edit.root.to_string());
expect.assert_eq(&edit.new_root.to_string());
}

#[test]
Expand Down Expand Up @@ -599,6 +606,6 @@ mod tests {
let second = 2;
}
}"#]];
expect.assert_eq(&edit.root.to_string());
expect.assert_eq(&edit.new_root.to_string());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
"some replace change ranges intersect: {:?}",
changes
) {
return SyntaxEdit { root, annotations: Default::default(), changed_elements: vec![] };
return SyntaxEdit {
old_root: root.clone(),
new_root: root,
annotations: Default::default(),
changed_elements: vec![],
};
}

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

SyntaxEdit { root, changed_elements, annotations: annotation_groups }
SyntaxEdit {
old_root: tree_mutator.immutable,
new_root: root,
changed_elements,
annotations: annotation_groups,
}
}

fn to_owning_node(element: &SyntaxElement) -> SyntaxNode {
Expand Down Expand Up @@ -329,14 +339,15 @@ impl ChangedAncestor {
}

struct TreeMutator {
immutable: SyntaxNode,
mutable_clone: SyntaxNode,
}

impl TreeMutator {
fn new(immutable: &SyntaxNode) -> TreeMutator {
let immutable = immutable.clone();
let mutable_clone = immutable.clone_for_update();
TreeMutator { mutable_clone }
TreeMutator { immutable, mutable_clone }
}

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

0 comments on commit a07e54c

Please sign in to comment.