Skip to content

Commit 5dba7fb

Browse files
committed
feat: add Node::first_child, Node::last_child
1 parent 7a37385 commit 5dba7fb

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

crates/postgresql-cst-parser/src/tree_sitter.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,34 @@ impl<'a> Node<'a> {
180180
}
181181
}
182182

183+
/// Returns the first child element of this node.
184+
/// this is not tree-sitter's API
185+
pub fn first_child(&self) -> Option<Node<'a>> {
186+
if let Some(node) = self.node_or_token.as_node() {
187+
node.first_child_or_token().map(|child| Node {
188+
input: self.input,
189+
range_map: Rc::clone(&self.range_map),
190+
node_or_token: child,
191+
})
192+
} else {
193+
None
194+
}
195+
}
196+
197+
/// Returns the last child element of this node.
198+
/// this is not tree-sitter's API
199+
pub fn last_child(&self) -> Option<Node<'a>> {
200+
if let Some(node) = self.node_or_token.as_node() {
201+
node.last_child_or_token().map(|child| Node {
202+
input: self.input,
203+
range_map: Rc::clone(&self.range_map),
204+
node_or_token: child,
205+
})
206+
} else {
207+
None
208+
}
209+
}
210+
183211
pub fn next_sibling(&self) -> Option<Node<'a>> {
184212
self.node_or_token
185213
.next_sibling_or_token()
@@ -212,7 +240,7 @@ impl<'a> Node<'a> {
212240
matches!(self.kind(), SyntaxKind::C_COMMENT | SyntaxKind::SQL_COMMENT)
213241
}
214242

215-
/// Return the rightmost token in the subtree of this node
243+
/// Returns the rightmost token in the subtree of this node.
216244
/// this is not tree-sitter's API
217245
pub fn last_node(&self) -> Option<Node<'a>> {
218246
match &self.node_or_token {

0 commit comments

Comments
 (0)