feat: reverse iteration through node parents (#3214)

This commit is contained in:
vanaigr 2024-04-23 09:19:57 -05:00 committed by GitHub
parent 0f125e2d09
commit 90e0e28b95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 84 additions and 24 deletions

View file

@ -355,9 +355,13 @@ extern "C" {
pub fn ts_node_next_parse_state(self_: TSNode) -> TSStateId;
}
extern "C" {
#[doc = " Get the node's immediate parent."]
#[doc = " Get the node's immediate parent.\n Prefer [`ts_node_child_containing_descendant`] for\n iterating over the node's ancestors."]
pub fn ts_node_parent(self_: TSNode) -> TSNode;
}
extern "C" {
#[doc = " Get the node's child that contains `descendant`."]
pub fn ts_node_child_containing_descendant(self_: TSNode, descendant: TSNode) -> TSNode;
}
extern "C" {
#[doc = " Get the node's child at the given index, where zero represents the first\n child."]
pub fn ts_node_child(self_: TSNode, child_index: u32) -> TSNode;

View file

@ -1331,12 +1331,21 @@ impl<'tree> Node<'tree> {
}
/// Get this node's immediate parent.
/// Prefer [`child_containing_descendant`](Node::child_containing_descendant)
/// for iterating over this node's ancestors.
#[doc(alias = "ts_node_parent")]
#[must_use]
pub fn parent(&self) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_parent(self.0) })
}
/// Get this node's child that contains `descendant`.
#[doc(alias = "ts_node_child_containing_descendant")]
#[must_use]
pub fn child_containing_descendant(&self, descendant: Self) -> Option<Self> {
Self::new(unsafe { ffi::ts_node_child_containing_descendant(self.0, descendant.0) })
}
/// Get this node's next sibling.
#[doc(alias = "ts_node_next_sibling")]
#[must_use]