From a2a810f118bb2ec541f852bf02eebdf02c7326f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 12:44:14 +0300 Subject: [PATCH] Add parent links --- src/yellow/red.rs | 10 +++++++--- src/yellow/syntax.rs | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/yellow/red.rs b/src/yellow/red.rs index 3002153ca3..e0fa27e5f7 100644 --- a/src/yellow/red.rs +++ b/src/yellow/red.rs @@ -16,7 +16,7 @@ pub(crate) struct RedNode { #[derive(Debug)] struct ParentData { - parent: *const RedNode, + parent: ptr::NonNull, start_offset: TextUnit, index_in_parent: usize, } @@ -30,7 +30,7 @@ impl RedNode { fn new_child( green: GreenNode, - parent: *const RedNode, + parent: ptr::NonNull, start_offset: TextUnit, index_in_parent: usize, ) -> RedNode { @@ -76,10 +76,14 @@ impl RedNode { let green_children = self.green.children(); let start_offset = self.start_offset() + green_children[..idx].iter().map(|x| x.text_len()).sum::(); - let child = RedNode::new_child(green_children[idx].clone(), self, start_offset, idx); + let child = RedNode::new_child(green_children[idx].clone(), self.into(), start_offset, idx); children[idx] = Some(Box::new(child)) } let child = children[idx].as_ref().unwrap(); ptr::NonNull::from(&**child) } + + pub(crate) fn parent(&self) -> Option> { + Some(self.parent.as_ref()?.parent) + } } diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index c31b832d07..ae3302ed90 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs @@ -90,6 +90,14 @@ impl SyntaxNode { res } + pub fn parent(&self) -> Option> { + let parent = self.red().parent()?; + Some(SyntaxNode { + root: self.root.clone(), + red: parent, + }) + } + fn red(&self) -> &RedNode { unsafe { self.red.as_ref() } }