aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-30 10:44:14 +0100
committerAleksey Kladov <[email protected]>2018-07-30 10:44:14 +0100
commita2a810f118bb2ec541f852bf02eebdf02c7326f7 (patch)
tree1b48a5b2f10e0f4a706e496eabd27e8490c332be
parent423298dddd50d28ce891b10695417401226235a3 (diff)
Add parent links
-rw-r--r--src/yellow/red.rs10
-rw-r--r--src/yellow/syntax.rs8
2 files changed, 15 insertions, 3 deletions
diff --git a/src/yellow/red.rs b/src/yellow/red.rs
index 3002153ca..e0fa27e5f 100644
--- a/src/yellow/red.rs
+++ b/src/yellow/red.rs
@@ -16,7 +16,7 @@ pub(crate) struct RedNode {
16 16
17#[derive(Debug)] 17#[derive(Debug)]
18struct ParentData { 18struct ParentData {
19 parent: *const RedNode, 19 parent: ptr::NonNull<RedNode>,
20 start_offset: TextUnit, 20 start_offset: TextUnit,
21 index_in_parent: usize, 21 index_in_parent: usize,
22} 22}
@@ -30,7 +30,7 @@ impl RedNode {
30 30
31 fn new_child( 31 fn new_child(
32 green: GreenNode, 32 green: GreenNode,
33 parent: *const RedNode, 33 parent: ptr::NonNull<RedNode>,
34 start_offset: TextUnit, 34 start_offset: TextUnit,
35 index_in_parent: usize, 35 index_in_parent: usize,
36 ) -> RedNode { 36 ) -> RedNode {
@@ -76,10 +76,14 @@ impl RedNode {
76 let green_children = self.green.children(); 76 let green_children = self.green.children();
77 let start_offset = self.start_offset() 77 let start_offset = self.start_offset()
78 + green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>(); 78 + green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>();
79 let child = RedNode::new_child(green_children[idx].clone(), self, start_offset, idx); 79 let child = RedNode::new_child(green_children[idx].clone(), self.into(), start_offset, idx);
80 children[idx] = Some(Box::new(child)) 80 children[idx] = Some(Box::new(child))
81 } 81 }
82 let child = children[idx].as_ref().unwrap(); 82 let child = children[idx].as_ref().unwrap();
83 ptr::NonNull::from(&**child) 83 ptr::NonNull::from(&**child)
84 } 84 }
85
86 pub(crate) fn parent(&self) -> Option<ptr::NonNull<RedNode>> {
87 Some(self.parent.as_ref()?.parent)
88 }
85} 89}
diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs
index c31b832d0..ae3302ed9 100644
--- a/src/yellow/syntax.rs
+++ b/src/yellow/syntax.rs
@@ -90,6 +90,14 @@ impl<ROOT: TreeRoot> SyntaxNode<ROOT> {
90 res 90 res
91 } 91 }
92 92
93 pub fn parent(&self) -> Option<SyntaxNode<ROOT>> {
94 let parent = self.red().parent()?;
95 Some(SyntaxNode {
96 root: self.root.clone(),
97 red: parent,
98 })
99 }
100
93 fn red(&self) -> &RedNode { 101 fn red(&self) -> &RedNode {
94 unsafe { self.red.as_ref() } 102 unsafe { self.red.as_ref() }
95 } 103 }