Add parent links

This commit is contained in:
Aleksey Kladov 2018-07-30 12:44:14 +03:00
parent 423298dddd
commit a2a810f118
2 changed files with 15 additions and 3 deletions

View file

@ -16,7 +16,7 @@ pub(crate) struct RedNode {
#[derive(Debug)] #[derive(Debug)]
struct ParentData { struct ParentData {
parent: *const RedNode, parent: ptr::NonNull<RedNode>,
start_offset: TextUnit, start_offset: TextUnit,
index_in_parent: usize, index_in_parent: usize,
} }
@ -30,7 +30,7 @@ impl RedNode {
fn new_child( fn new_child(
green: GreenNode, green: GreenNode,
parent: *const RedNode, parent: ptr::NonNull<RedNode>,
start_offset: TextUnit, start_offset: TextUnit,
index_in_parent: usize, index_in_parent: usize,
) -> RedNode { ) -> RedNode {
@ -76,10 +76,14 @@ impl RedNode {
let green_children = self.green.children(); let green_children = self.green.children();
let start_offset = self.start_offset() let start_offset = self.start_offset()
+ green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>(); + green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>();
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)) children[idx] = Some(Box::new(child))
} }
let child = children[idx].as_ref().unwrap(); let child = children[idx].as_ref().unwrap();
ptr::NonNull::from(&**child) ptr::NonNull::from(&**child)
} }
pub(crate) fn parent(&self) -> Option<ptr::NonNull<RedNode>> {
Some(self.parent.as_ref()?.parent)
}
} }

View file

@ -90,6 +90,14 @@ impl<ROOT: TreeRoot> SyntaxNode<ROOT> {
res res
} }
pub fn parent(&self) -> Option<SyntaxNode<ROOT>> {
let parent = self.red().parent()?;
Some(SyntaxNode {
root: self.root.clone(),
red: parent,
})
}
fn red(&self) -> &RedNode { fn red(&self) -> &RedNode {
unsafe { self.red.as_ref() } unsafe { self.red.as_ref() }
} }