Skip to main content

Cursor

Struct Cursor 

Source
pub struct Cursor<'a>(/* private fields */);
Expand description

An AST cursor.

Implementations§

Source§

impl<'a> Cursor<'a>

Source

pub fn reset(&mut self, node: &Node<'a>)

Re-seats the cursor on node.

Source

pub fn goto_next_sibling(&mut self) -> bool

Moves to the next sibling; false when there is none.

Source

pub fn goto_first_child(&mut self) -> bool

Moves to the first child; false when there is none.

Source

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). So it is over whatever precedes a repeat() in a node that parsed: the comments between a list’s opening token and its first element are the list’s own children, not the repeat’s (#1868).

Source

pub fn goto_parent(&mut self) -> bool

Moves to the parent; false when there is none.

The node the cursor was built on (Node::cursor) or last reset to is the ceiling: from there this returns false and leaves the cursor where it is, even when the node has a parent in the tree. A traversal that descends and climbs back through one cursor therefore cannot wander above its subtree.

Source

pub fn field_name(&self) -> Option<&'static str>

The grammar field through which the parent reaches the node the cursor currently sits on, or None when it occupies no field — most punctuation, an extra such as a comment, or the node the cursor started on, whose parent it cannot see. Anonymous tokens can sit in a field: a binary_expression’s + is its operator.

Reflects the cursor’s position: it is answered from the path the cursor descended, so a field inherited through a hidden grammar node is resolved the same way Node::field_name_for_child resolves it — both stop at the nearest visible ancestor and both answer None for an extra. The two are different algorithms, though: this one climbs the cursor’s stack and stops at the first extra entry, while the index lookup descends and reads the field map at each hidden level. They agree wherever every extra is a leaf or a visible rule, which holds for every pinned grammar; a hidden extra wrapper with visible children is the one shape that could split them.

Source

pub fn node(&self) -> Node<'a>

The node the cursor currently sits on.

Trait Implementations§

Source§

impl<'a> Clone for Cursor<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Cursor<'a>

§

impl<'a> Freeze for Cursor<'a>

§

impl<'a> Move for Cursor<'a>

§

impl<'a> RefUnwindSafe for Cursor<'a>

§

impl<'a> Send for Cursor<'a>

§

impl<'a> Sync for Cursor<'a>

§

impl<'a> Unpin for Cursor<'a>

§

impl<'a> UnsafeUnpin for Cursor<'a>

§

impl<'a> UnwindSafe for Cursor<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.