1
Fork 0
mirror of https://github.com/RGBCube/cstree synced 2025-07-27 17:17:45 +00:00

add methods for arity of syntax nodes

This commit is contained in:
Domenic Quirl 2021-02-21 20:04:17 +01:00
parent be8477e5a4
commit c47b430135
3 changed files with 21 additions and 2 deletions

View file

@ -13,7 +13,7 @@ pub(super) type GreenElement = NodeOrToken<GreenNode, GreenToken>;
pub(crate) type GreenElementRef<'a> = NodeOrToken<&'a GreenNode, &'a GreenToken>; pub(crate) type GreenElementRef<'a> = NodeOrToken<&'a GreenNode, &'a GreenToken>;
#[repr(transparent)] #[repr(transparent)]
pub(super) struct PackedGreenElement { pub(crate) struct PackedGreenElement {
ptr: ErasedPtr, ptr: ErasedPtr,
} }
@ -113,7 +113,7 @@ impl From<PackedGreenElement> for GreenElement {
} }
impl PackedGreenElement { impl PackedGreenElement {
fn is_node(&self) -> bool { pub(crate) fn is_node(&self) -> bool {
self.ptr as usize & 1 == 0 self.ptr as usize & 1 == 0
} }

View file

@ -115,6 +115,11 @@ impl GreenNode {
self.data.header.header.text_len self.data.header.header.text_len
} }
#[inline]
pub(crate) fn iter(&self) -> slice::Iter<'_, PackedGreenElement> {
self.data.slice.iter()
}
/// Iterator over all children of this node. /// Iterator over all children of this node.
#[inline] #[inline]
pub fn children(&self) -> Children<'_> { pub fn children(&self) -> Children<'_> {

View file

@ -695,6 +695,20 @@ impl<L: Language, D, R> SyntaxNode<L, D, R> {
} }
} }
/// The number of child nodes (!) of this node.
///
/// If you want to also consider leafs, see [`arity_with_tokens`](SyntaxNode::arity_with_tokens).
#[inline]
pub fn arity(&self) -> usize {
self.green().iter().filter(|&child| child.is_node()).count()
}
/// The number of children of this node.
#[inline]
pub fn arity_with_tokens(&self) -> usize {
self.data().children.len()
}
/// Returns an iterator along the chain of parents of this node. /// Returns an iterator along the chain of parents of this node.
#[inline] #[inline]
pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode<L, D, R>> { pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode<L, D, R>> {