if (FALSE) { # rlang::is_installed("treesitter.r")
# This query looks for binary operators where the left hand side is an
# identifier named `fn`, and the right hand side is a function definition.
# The operator can be `<-` or `=` (technically it can also be things like
# `+` as well in this example).
source <- '(binary_operator
lhs: (identifier) @lhs
operator: _ @operator
rhs: (function_definition) @rhs
(#eq? @lhs "fn")
)'
language <- treesitter.r::language()
query <- query(language, source)
text <- "
fn <- function() {}
fn2 <- function() {}
fn <- 5
fn = function(a, b, c) { a + b + c }
"
parser <- parser(language)
tree <- parser_parse(parser, text)
node <- tree_root_node(tree)
query_matches(query, node)
}
Run the code above in your browser using DataLab