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)
# 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)
}
Run the code above in your browser using DataLab