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

node-childR Documentation

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)

Arguments

x

⁠[tree_sitter_node]⁠

A node.

i

⁠[integer(1)]⁠

The index of the child to return.

Value

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

Examples


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)


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