TreeCursor#
- class tree_sitter.TreeCursor#
A class for walking a syntax
Tree
efficiently.Important
The cursor can only walk into children of the node that it started from.
Methods#
- copy()#
Create an independent copy of the cursor.
- goto_descendant(index, /)#
Move the cursor to the node that is the n-th descendant of the original node that the cursor was constructed with, where
0
represents the original node itself.
- goto_first_child()#
Move this cursor to the first child of its current node.
- Returns:
True
if the cursor successfully moved, orFalse
if there were no children.
- goto_first_child_for_byte(byte, /)#
Move this cursor to the first child of its current node that extends beyond the given byte offset.
- Returns:
The index of the child node if it was found,
None
otherwise.
- goto_first_child_for_point(point, /)#
Move this cursor to the first child of its current node that extends beyond the given row/column point.
- Returns:
The index of the child node if it was found,
None
otherwise.
- goto_last_child()#
Move this cursor to the last child of its current node.
- Returns:
True
if the cursor successfully moved, orFalse
if there were no children.
Caution
This method may be slower than
goto_first_child()
because it needs to iterate through all the children to compute the child’s position.
- goto_next_sibling()#
Move this cursor to the next sibling of its current node.
- Returns:
True
if the cursor successfully moved, orFalse
if there was no next sibling.
- goto_parent()#
Move this cursor to the parent of its current node.
- Returns:
True
if the cursor successfully moved, orFalse
if there was no parent node (i.e. the cursor was already on the root node).
- goto_previous_sibling()#
Move this cursor to the previous sibling of its current node.
- Returns:
True
if the cursor successfully moved, orFalse
if there was no previous sibling.
Caution
This method may be slower than
goto_next_sibling()
due to how node positions are stored. In the worst case, this will need to iterate through all the children up to the previous sibling node to recalculate its position.
- reset(node, /)#
Re-initialize the cursor to start at the original node that it was constructed with.
Special Methods#
- __copy__()#
Use
copy.copy()
to create a copy of the cursor.
Attributes#
- depth#
The depth of the cursor’s current node relative to the original node that it was constructed with.
- descendant_index#
The index of the cursor’s current node out of all of the descendants of the original node that the cursor was constructed with.
- field_id#
The numerical field id of this tree cursor’s current node, if available.
- field_name#
The field name of this tree cursor’s current node, if available.
- node#
The current node.