DavisVaughan / r-tree-sitter

https://davisvaughan.github.io/r-tree-sitter/
Other
31 stars 2 forks source link

Add `any-` prefix to queries #19

Open kylebutts opened 4 months ago

kylebutts commented 4 months ago

This adds support for the any- prefix for predicates. I've added tests to try and ensure coverage.

Here's an example where I think it's very useful. Find comments that precede an assignment operator and check if any #' @ comment is included:

library(treesitter)
library(treesitter.r)
language <- treesitter.r::language()
text <- "
  fn <- function() {}
  fn2 <- function() {}

  #| label: 'test'
  2 + 2
  3^2

  # Adding comment
  fn <- 5

  #' Sum three numbers
  #' @param a Number
  #' @param b Number
  #' @param c Number
  #'
  #' @export
  fn <- function(a, b, c) { a + b + c }
  fn(10, 9, 7)

  #' Sum three numbers
  #' @param a Number
  #' @param b Number
  #' @param c Number
  #'
  #' @export
  fn2 <- function(a, b, c) { a + b + c }

  #' This package blah blah
  #'
  #' @export
  \"packageName\"
"
parser <- parser(language)
tree <- parser_parse(parser, text)
node <- tree_root_node(tree)
source = '
  (
    (comment)* @roxygen
    (binary_operator
      operator: ["<-" "="]
    )
    (#any-match? @roxygen "^#\'\\\\s*\\\\@")
  )
'

query_matches(query(language, source), node)[[1]]
#> [[1]]
#> [[1]]$name
#> [1] "roxygen" "roxygen" "roxygen" "roxygen" "roxygen" "roxygen"
#> 
#> [[1]]$node
#> [[1]]$node[[1]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' Sum three numbers
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(11, 2), (11, 22)])
#> 
#> [[1]]$node[[2]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @param a Number
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(12, 2), (12, 20)])
#> 
#> [[1]]$node[[3]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @param b Number
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(13, 2), (13, 20)])
#> 
#> [[1]]$node[[4]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @param c Number
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(14, 2), (14, 20)])
#> 
#> [[1]]$node[[5]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #'
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(15, 2), (15, 4)])
#> 
#> [[1]]$node[[6]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @export
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(16, 2), (16, 12)])
#> 
#> 
#> 
#> [[2]]
#> [[2]]$name
#> [1] "roxygen" "roxygen" "roxygen" "roxygen" "roxygen" "roxygen"
#> 
#> [[2]]$node
#> [[2]]$node[[1]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' Sum three numbers
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(20, 2), (20, 22)])
#> 
#> [[2]]$node[[2]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @param a Number
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(21, 2), (21, 20)])
#> 
#> [[2]]$node[[3]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @param b Number
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(22, 2), (22, 20)])
#> 
#> [[2]]$node[[4]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @param c Number
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(23, 2), (23, 20)])
#> 
#> [[2]]$node[[5]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #'
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(24, 2), (24, 4)])
#> 
#> [[2]]$node[[6]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #' @export
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(25, 2), (25, 12)])
DavisVaughan commented 4 months ago

Cool! Also add a news bullet? Will probably take me a few weeks before I can look at this, at useR this week.