Learn R Programming

treesitter (version 0.3.0)

node-child: Get a node's child by index

Description

These functions return the ith child of x.

  • node_child() considers both named and anonymous children.

  • node_named_child() considers only named children.

Usage

node_child(x, i)

node_named_child(x, i)

Value

The ith child node of x or NULL if there is no child at that index.

Arguments

x

[tree_sitter_node]

A node.

i

[integer(1)]

The index of the child to return.

Examples

Run this code
if (FALSE) { # rlang::is_installed("treesitter.r")
language <- treesitter.r::language()
parser <- parser(language)

text <- "fn <- function() { 1 + 1 }"
tree <- parser_parse(parser, text)
node <- tree_root_node(tree)

# Starts with `program` node for the whole document
node

# Navigate to first child
node <- node_child(node, 1)
node

# Note how the named variant skips the anonymous operator node
node_child(node, 2)
node_named_child(node, 2)

# OOB indices return `NULL`
node_child(node, 5)
}

Run the code above in your browser using DataLab