package tree-sitter

  1. Overview
  2. Docs

Module Tree_sitter.Tree_cursorSource

Efficient tree traversal cursors.

A Tree_cursor.t walks a syntax tree without allocating a new Node.t at each step. Prefer this over Node navigation functions (e.g. Node.child, Node.next_sibling) when visiting many nodes.

Sourcetype t

The type for tree cursors. Automatically garbage collected, but delete can release resources sooner.

Constructors

Sourceval create : Node.t -> t

create node is a cursor starting at node. The cursor cannot walk above node.

Sourceval delete : t -> unit

delete cursor frees resources immediately. The cursor must not be used after this call.

Sourceval reset : t -> Node.t -> unit

reset cursor node repositions cursor to start at node.

Current position

Sourceval current_node : t -> Node.t

current_node cursor is the node the cursor is on.

Sourceval current_field_name : t -> string option

current_field_name cursor is the field name of the current node in its parent, if any.

Sourceval current_field_id : t -> int

current_field_id cursor is the field ID of the current node, or 0 if it has no field.

Sourceval current_depth : t -> int

current_depth cursor is the depth of the current node relative to the cursor's starting node.

Movement

Sourceval goto_parent : t -> bool

goto_parent cursor moves to the parent. Returns false if already at the starting node.

Sourceval goto_first_child : t -> bool

goto_first_child cursor moves to the first child. Returns false if the current node has no children.

Sourceval goto_last_child : t -> bool

goto_last_child cursor moves to the last child. Returns false if the current node has no children.

Sourceval goto_next_sibling : t -> bool

goto_next_sibling cursor moves to the next sibling. Returns false if there is no next sibling.

Sourceval goto_previous_sibling : t -> bool

goto_previous_sibling cursor moves to the previous sibling. Returns false if there is no previous sibling.

Sourceval goto_first_child_for_byte : t -> int -> int

goto_first_child_for_byte cursor byte moves to the first child that contains or starts after byte. Returns the child index, or -1 if no such child exists.