TreeCursor | R Documentation |
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()
.
R6 object representing the tree cursor.
new()
Create a new tree cursor.
TreeCursor$new(node)
node
[tree_sitter_node]
The node to start walking from.
reset()
Reset the tree cursor to a new root node.
TreeCursor$reset(node)
node
[tree_sitter_node]
The node to start walking from.
node()
Get the current node that the cursor points to.
TreeCursor$node()
field_name()
Get the field name of the current node.
TreeCursor$field_name()
field_id()
Get the field id of the current node.
TreeCursor$field_id()
descendant_index()
Get the descendent index of the current node.
TreeCursor$descendant_index()
goto_parent()
Go to the current node's parent.
Returns TRUE
if a parent was found, and FALSE
if not.
TreeCursor$goto_parent()
goto_next_sibling()
Go to the current node's next sibling.
Returns TRUE
if a sibling was found, and FALSE
if not.
TreeCursor$goto_next_sibling()
goto_previous_sibling()
Go to the current node's previous sibling.
Returns TRUE
if a sibling was found, and FALSE
if not.
TreeCursor$goto_previous_sibling()
goto_first_child()
Go to the current node's first child.
Returns TRUE
if a child was found, and FALSE
if not.
TreeCursor$goto_first_child()
goto_last_child()
Go to the current node's last child.
Returns TRUE
if a child was found, and FALSE
if not.
TreeCursor$goto_last_child()
depth()
Get the depth of the current node.
TreeCursor$depth()
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.
TreeCursor$goto_first_child_for_byte(byte)
byte
[double(1)]
The byte to move the cursor past.
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.
TreeCursor$goto_first_child_for_point(point)
point
[tree_sitter_point]
The point to move the cursor past.
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.