From 31c468ab95fe775dc65bef0674ca3c3ec219259f Mon Sep 17 00:00:00 2001 From: Matthew Toohey Date: Thu, 7 Apr 2022 20:30:44 -0400 Subject: [PATCH] add languages `r` and `rmarkdown` (#1998) * add languages `r` and `rmarkdown` * r: fix highlights * rmarkdown: add eof in queries * rmarkdown: update lang-support.md * r: fix highlight query precedence --- book/src/generated/lang-support.md | 2 + languages.toml | 25 +++++ runtime/queries/r/highlights.scm | 128 +++++++++++++++++++++++ runtime/queries/r/locals.scm | 11 ++ runtime/queries/rmarkdown/highlights.scm | 1 + runtime/queries/rmarkdown/indents.scm | 1 + runtime/queries/rmarkdown/injections.scm | 1 + 7 files changed, 169 insertions(+) create mode 100644 runtime/queries/r/highlights.scm create mode 100644 runtime/queries/r/locals.scm create mode 100644 runtime/queries/rmarkdown/highlights.scm create mode 100644 runtime/queries/rmarkdown/indents.scm create mode 100644 runtime/queries/rmarkdown/injections.scm diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index b71349f3..e8bb65e4 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -50,9 +50,11 @@ | prolog | | | | `swipl` | | protobuf | ✓ | | ✓ | | | python | ✓ | ✓ | ✓ | `pylsp` | +| r | ✓ | | | `R` | | racket | | | | `racket` | | regex | ✓ | | | | | rescript | ✓ | ✓ | | `rescript-language-server` | +| rmarkdown | ✓ | | ✓ | `R` | | ron | ✓ | | ✓ | | | ruby | ✓ | | ✓ | `solargraph` | | rust | ✓ | ✓ | ✓ | `rust-analyzer` | diff --git a/languages.toml b/languages.toml index d81d0373..2c6f0c9a 100644 --- a/languages.toml +++ b/languages.toml @@ -1038,3 +1038,28 @@ roots = [] comment-token = "//" indent = { tab-width = 4, unit = " " } grammar = "rust" + +[[language]] +name = "r" +scope = "source.r" +injection-regex = "(r|R)" +file-types = ["r", "R"] +shebangs = ["r", "R"] +roots = [] +comment-token = "#" +indent = { tab-width = 2, unit = " " } +language-server = { command = "R", args = ["--slave", "-e", "languageserver::run()"] } + +[[grammar]] +name = "r" +source = { git = "https://github.com/r-lib/tree-sitter-r", rev = "cc04302e1bff76fa02e129f332f44636813b0c3c" } + +[[language]] +name = "rmarkdown" +scope = "source.rmd" +injection-regex = "(r|R)md" +file-types = ["rmd", "Rmd"] +roots = [] +indent = { tab-width = 2, unit = " " } +grammar = "markdown" +language-server = { command = "R", args = ["--slave", "-e", "languageserver::run()"] } diff --git a/runtime/queries/r/highlights.scm b/runtime/queries/r/highlights.scm new file mode 100644 index 00000000..4b931a05 --- /dev/null +++ b/runtime/queries/r/highlights.scm @@ -0,0 +1,128 @@ +; highlights.scm + + +; Literals + +(integer) @constant.numeric.integer + +(float) @constant.numeric.float + +(complex) @constant.numeric.integer + +(string) @string +(string (escape_sequence) @constant.character.escape) + +(comment) @comment + +(formal_parameters (identifier) @variable.parameter) +(formal_parameters (default_parameter (identifier) @variable.parameter)) + +; Operators +[ + "=" + "<-" + "<<-" + "->>" + "->" +] @operator + +(unary operator: [ + "-" + "+" + "!" + "~" +] @operator) + +(binary operator: [ + "-" + "+" + "*" + "/" + "^" + "<" + ">" + "<=" + ">=" + "==" + "!=" + "||" + "|" + "&&" + "&" + ":" + "~" +] @operator) + +[ + "|>" + (special) +] @operator + +(lambda_function "\\" @operator) + +[ + "(" + ")" + "[" + "]" + "{" + "}" +] @punctuation.bracket + +(dollar "$" @operator) + +(subset2 + [ + "[[" + "]]" + ] @punctuation.bracket) + +[ + "in" + (dots) + (break) + (next) + (inf) +] @keyword + +[ + (nan) + (na) + (null) +] @type.builtin + +[ + "if" + "else" + "switch" +] @keyword.control.conditional + +[ + "while" + "repeat" + "for" +] @keyword.control.repeat + +[ + (true) + (false) +] @constant.builtin.boolean + +"function" @keyword.function + +(call function: (identifier) @function) +(default_argument name: (identifier) @variable.parameter) + + +(namespace_get namespace: (identifier) @namespace + "::" @operator) +(namespace_get_internal namespace: (identifier) @namespace + ":::" @operator) + +(namespace_get function: (identifier) @function.method) +(namespace_get_internal function: (identifier) @function.method) + +(identifier) @variable + +; Error +(ERROR) @error diff --git a/runtime/queries/r/locals.scm b/runtime/queries/r/locals.scm new file mode 100644 index 00000000..be6cc637 --- /dev/null +++ b/runtime/queries/r/locals.scm @@ -0,0 +1,11 @@ +; locals.scm + +(function_definition) @local.scope + +(formal_parameters (identifier) @local.definition) + +(left_assignment name: (identifier) @local.definition) +(equals_assignment name: (identifier) @local.definition) +(right_assignment name: (identifier) @local.definition) + +(identifier) @local.reference diff --git a/runtime/queries/rmarkdown/highlights.scm b/runtime/queries/rmarkdown/highlights.scm new file mode 100644 index 00000000..a3a30e34 --- /dev/null +++ b/runtime/queries/rmarkdown/highlights.scm @@ -0,0 +1 @@ +; inherits: markdown diff --git a/runtime/queries/rmarkdown/indents.scm b/runtime/queries/rmarkdown/indents.scm new file mode 100644 index 00000000..a3a30e34 --- /dev/null +++ b/runtime/queries/rmarkdown/indents.scm @@ -0,0 +1 @@ +; inherits: markdown diff --git a/runtime/queries/rmarkdown/injections.scm b/runtime/queries/rmarkdown/injections.scm new file mode 100644 index 00000000..a3a30e34 --- /dev/null +++ b/runtime/queries/rmarkdown/injections.scm @@ -0,0 +1 @@ +; inherits: markdown