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
This commit is contained in:
parent
4e81ca344b
commit
a07e54c6ea
@ -102,16 +102,23 @@ pub fn finish(self) -> SyntaxEdit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a completed [`SyntaxEditor`] operation.
|
||||||
pub struct SyntaxEdit {
|
pub struct SyntaxEdit {
|
||||||
root: SyntaxNode,
|
old_root: SyntaxNode,
|
||||||
|
new_root: SyntaxNode,
|
||||||
changed_elements: Vec<SyntaxElement>,
|
changed_elements: Vec<SyntaxElement>,
|
||||||
annotations: FxHashMap<SyntaxAnnotation, Vec<SyntaxElement>>,
|
annotations: FxHashMap<SyntaxAnnotation, Vec<SyntaxElement>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SyntaxEdit {
|
impl SyntaxEdit {
|
||||||
/// Root of the modified syntax tree
|
/// Root of the initial unmodified syntax tree.
|
||||||
pub fn root(&self) -> &SyntaxNode {
|
pub fn old_root(&self) -> &SyntaxNode {
|
||||||
&self.root
|
&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
|
/// Which syntax elements in the modified syntax tree were inserted or
|
||||||
@ -441,14 +448,14 @@ fn basic_usage() {
|
|||||||
let var_name = 2 + 2;
|
let var_name = 2 + 2;
|
||||||
(var_name, true)
|
(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_eq!(edit.find_annotation(placeholder_snippet).len(), 2);
|
||||||
assert!(edit
|
assert!(edit
|
||||||
.annotations
|
.annotations
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(_, elements)| elements)
|
.flat_map(|(_, elements)| elements)
|
||||||
.all(|element| element.ancestors().any(|it| &it == edit.root())))
|
.all(|element| element.ancestors().any(|it| &it == edit.new_root())))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -495,7 +502,7 @@ fn test_insert_independent() {
|
|||||||
let first = 1;{
|
let first = 1;{
|
||||||
let second = 2;let third = 3;
|
let second = 2;let third = 3;
|
||||||
}"#]];
|
}"#]];
|
||||||
expect.assert_eq(&edit.root.to_string());
|
expect.assert_eq(&edit.new_root.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -556,7 +563,7 @@ fn test_insert_dependent() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}"#]];
|
}"#]];
|
||||||
expect.assert_eq(&edit.root.to_string());
|
expect.assert_eq(&edit.new_root.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -599,6 +606,6 @@ fn test_replace_root_with_dependent() {
|
|||||||
let second = 2;
|
let second = 2;
|
||||||
}
|
}
|
||||||
}"#]];
|
}"#]];
|
||||||
expect.assert_eq(&edit.root.to_string());
|
expect.assert_eq(&edit.new_root.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
|
|||||||
"some replace change ranges intersect: {:?}",
|
"some replace change ranges intersect: {:?}",
|
||||||
changes
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
@ -273,7 +278,12 @@ struct DependentChange {
|
|||||||
annotation_groups.entry(annotation).or_insert(vec![]).push(element);
|
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 {
|
fn to_owning_node(element: &SyntaxElement) -> SyntaxNode {
|
||||||
@ -329,6 +339,7 @@ fn affected_range(&self) -> TextRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct TreeMutator {
|
struct TreeMutator {
|
||||||
|
immutable: SyntaxNode,
|
||||||
mutable_clone: SyntaxNode,
|
mutable_clone: SyntaxNode,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,7 +347,7 @@ impl TreeMutator {
|
|||||||
fn new(immutable: &SyntaxNode) -> TreeMutator {
|
fn new(immutable: &SyntaxNode) -> TreeMutator {
|
||||||
let immutable = immutable.clone();
|
let immutable = immutable.clone();
|
||||||
let mutable_clone = immutable.clone_for_update();
|
let mutable_clone = immutable.clone_for_update();
|
||||||
TreeMutator { mutable_clone }
|
TreeMutator { immutable, mutable_clone }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_element_mut(&self, element: &SyntaxElement) -> SyntaxElement {
|
fn make_element_mut(&self, element: &SyntaxElement) -> SyntaxElement {
|
||||||
|
Loading…
Reference in New Issue
Block a user