TreeCursor: Tree cursors

TreeCursorR Documentation

Tree cursors

Description

TreeCursor is an R6 class that allows you to walk a tree in a more efficient way than calling ⁠node_*()⁠ functions like node_child() repeatedly.

You can also more elegantly create a cursor with node_walk() and tree_walk().

Value

R6 object representing the tree cursor.

Methods

Public methods


Method new()

Create a new tree cursor.

Usage
TreeCursor$new(node)
Arguments
node

⁠[tree_sitter_node]⁠

The node to start walking from.


Method reset()

Reset the tree cursor to a new root node.

Usage
TreeCursor$reset(node)
Arguments
node

⁠[tree_sitter_node]⁠

The node to start walking from.


Method node()

Get the current node that the cursor points to.

Usage
TreeCursor$node()

Method field_name()

Get the field name of the current node.

Usage
TreeCursor$field_name()

Method field_id()

Get the field id of the current node.

Usage
TreeCursor$field_id()

Method descendant_index()

Get the descendent index of the current node.

Usage
TreeCursor$descendant_index()

Method goto_parent()

Go to the current node's parent.

Returns TRUE if a parent was found, and FALSE if not.

Usage
TreeCursor$goto_parent()

Method goto_next_sibling()

Go to the current node's next sibling.

Returns TRUE if a sibling was found, and FALSE if not.

Usage
TreeCursor$goto_next_sibling()

Method goto_previous_sibling()

Go to the current node's previous sibling.

Returns TRUE if a sibling was found, and FALSE if not.

Usage
TreeCursor$goto_previous_sibling()

Method goto_first_child()

Go to the current node's first child.

Returns TRUE if a child was found, and FALSE if not.

Usage
TreeCursor$goto_first_child()

Method goto_last_child()

Go to the current node's last child.

Returns TRUE if a child was found, and FALSE if not.

Usage
TreeCursor$goto_last_child()

Method depth()

Get the depth of the current node.

Usage
TreeCursor$depth()

Method goto_first_child_for_byte()

Move the cursor to the first child of its current node that extends beyond the given byte offset.

Returns TRUE if a child was found, and FALSE if not.

Usage
TreeCursor$goto_first_child_for_byte(byte)
Arguments
byte

⁠[double(1)]⁠

The byte to move the cursor past.


Method goto_first_child_for_point()

Move the cursor to the first child of its current node that extends beyond the given point.

Returns TRUE if a child was found, and FALSE if not.

Usage
TreeCursor$goto_first_child_for_point(point)
Arguments
point

⁠[tree_sitter_point]⁠

The point to move the cursor past.

Examples


language <- treesitter.r::language()
parser <- parser(language)

text <- "fn <- function(a, b) { a + b }"

tree <- parser_parse(parser, text)
node <- tree_root_node(tree)

cursor <- TreeCursor$new(node)

cursor$node()
cursor$goto_first_child()
cursor$goto_first_child()
cursor$node()
cursor$goto_next_sibling()
cursor$node()


treesitter documentation built on June 24, 2024, 5:07 p.m.