node-child-by-field: Get a node's child by field id or name

node-child-by-fieldR Documentation

Get a node's child by field id or name

Description

These functions return children of x by field id or name.

  • node_child_by_field_id() retrieves a child by field id.

  • node_child_by_field_name() retrieves a child by field name.

Use language_field_id_for_name() to get the field id for a field name.

Usage

node_child_by_field_id(x, id)

node_child_by_field_name(x, name)

Arguments

x

⁠[tree_sitter_node]⁠

A node.

id

⁠[integer(1)]⁠

The field id of the child to return.

name

⁠[character(1)]⁠

The field name of the child to return.

Value

A child of x, or NULL if no matching child can be found.

Examples


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

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

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

# Get the field name of the first child
name <- node_field_name_for_child(node, 1)
name

# Now get the child again by that field name
node_child_by_field_name(node, name)

# If you need to look up by field name many times, you can look up the
# more direct field id first and use that instead
id <- language_field_id_for_name(language, name)
id

node_child_by_field_id(node, id)

# Returns `NULL` if no matching child
node_child_by_field_id(node, 10000)


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