mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Add parent links
This commit is contained in:
parent
423298dddd
commit
a2a810f118
2 changed files with 15 additions and 3 deletions
|
@ -16,7 +16,7 @@ pub(crate) struct RedNode {
|
|||
|
||||
#[derive(Debug)]
|
||||
struct ParentData {
|
||||
parent: *const RedNode,
|
||||
parent: ptr::NonNull<RedNode>,
|
||||
start_offset: TextUnit,
|
||||
index_in_parent: usize,
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ impl RedNode {
|
|||
|
||||
fn new_child(
|
||||
green: GreenNode,
|
||||
parent: *const RedNode,
|
||||
parent: ptr::NonNull<RedNode>,
|
||||
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::<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))
|
||||
}
|
||||
let child = children[idx].as_ref().unwrap();
|
||||
ptr::NonNull::from(&**child)
|
||||
}
|
||||
|
||||
pub(crate) fn parent(&self) -> Option<ptr::NonNull<RedNode>> {
|
||||
Some(self.parent.as_ref()?.parent)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,6 +90,14 @@ impl<ROOT: TreeRoot> SyntaxNode<ROOT> {
|
|||
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 {
|
||||
unsafe { self.red.as_ref() }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue