node-child-by-field | R Documentation |
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.
node_child_by_field_id(x, id)
node_child_by_field_name(x, name)
x |
A node. |
id |
The field id of the child to return. |
name |
The field name of the child to return. |
A child of x
, or NULL
if no matching child can be found.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.