pub struct Cursor<'a>(/* private fields */);Expand description
An AST cursor.
Implementations§
Source§impl<'a> Cursor<'a>
impl<'a> Cursor<'a>
Sourcepub fn goto_next_sibling(&mut self) -> bool
pub fn goto_next_sibling(&mut self) -> bool
Moves to the next sibling; false when there is none.
Sourcepub fn goto_first_child(&mut self) -> bool
pub fn goto_first_child(&mut self) -> bool
Moves to the first child; false when there is none.
Sourcepub fn goto_first_child_for_byte(&mut self, byte: usize) -> Option<usize>
pub fn goto_first_child_for_byte(&mut self, byte: usize) -> Option<usize>
Moves to the first child that ends after byte and returns its
index in Node::children order, or None when no child does.
“Ends after” is strict, so a zero-width child at byte — a
MISSING token the parser inserted during error recovery — is
skipped and the cursor lands on its successor. That is the one way
this differs from children().position(..), which finds such a
node; a caller resolving a node’s own index must therefore check
Cursor::node against it, as both call sites here do.
The index is what makes this worth having over a sibling scan:
tree-sitter stores a repeat() child list under balanced hidden
nodes and skips each by its cached visible-child count, so this
is O(log n) in the sibling count where children().position(..)
is O(n) — the difference between a linear and a quadratic walk
when every child of a wide list asks for its own index.
Only for such a list. The seek steps through the children in
order and can skip only a hidden subtree, so over a flat child
array — an ERROR that recovery built by skipping tokens has no
hidden levels — it is the same O(index) scan (#1584).