Skip to content

Commit

Permalink
Update scanners
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Jan 24, 2024
1 parent ccd9ba6 commit 396b535
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 78 deletions.
42 changes: 15 additions & 27 deletions queries-src/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
(alias left: (identifier) @variable)
(assignment
left: (identifier) @variable
[":="] @operator)
":=" @operator)

(module mod_name: (identifier) @namespace)

; highlight known settings (filtering does not always work)
(setting
left: ((identifier) @keyword
":="? @operator
(#any-of? @keyword
"allow-duplicate-recipes"
"dotenv-filename"
Expand All @@ -38,24 +39,8 @@
(function_call
name: (identifier) @function)

; highlight known attributes (filtering does not always work)
(attribute
attr_item: ((identifier) @attribute
(#any-of? @attribute
"private"
"allow-duplicate-recipes"
"dotenv-filename"
"dotenv-load"
"dotenv-path"
"export"
"fallback"
"ignore-comments"
"positional-arguments"
"shell"
"tempdi"
"windows-powershell"
"windows-shell"
)))
attr_item: ((identifier) @attribute))

(recipe_header
recipe_name: (identifier) @function)
Expand All @@ -71,16 +56,14 @@
(dependency_expression recipe: (identifier) @function)

; handle escape sequences
(string_literal (string_escape) @constant.character.escape)
(string_literal) @string
(string_literal (escape_sequence) @constant.character.escape)
[
(string_literal)
(raw_string_literal)
] @string

(comment) @comment.line

; (interpolation) @string

; FIXME: interpreter
; (shebang interpreter:(TEXT) @keyword ) @comment

[
"alias"
"export"
Expand All @@ -89,14 +72,19 @@
"set"
] @keyword

; exclude `=` and `&&` since they are valid in more normal scopes
; exclude `=` and `&&` since they are valid only in specific scopes
; (matching is covered in their parent nodes)
["@" "==" "!=" "+" "*" ":" "/" "?"] @operator

["(" ")" "[" "]" "{{" "}}" "{" "}"] @punctuation.bracket

["," ":"] @punctuation.delimiter

"`" @punctuation.special

; open and close are "`" or "```"
(external_command
open: _ @punctuation.special
close: _ @punctuation.special
)

(ERROR) @error
1 change: 1 addition & 0 deletions queries-src/indents.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

(recipe) @indent @extend
(string_literal) @indent @extend
(raw_string_literal) @indent @extend
(external_command) @indent @extend
170 changes: 158 additions & 12 deletions queries-src/injections.scm
Original file line number Diff line number Diff line change
@@ -1,15 +1,161 @@
(comment) @comment
; This query specifies how nested languages are handled

; FIXME: shebang
; (shebang_recipe
; (shebang
; interpreter:(TEXT) @language)
; (shebang_body) @content
; )
; ================ Standards ================
((comment) @injection.content
(#set! injection.language "comment"))

(source_file
; (item (setting lang:(identifier) @language))
(item (recipe (recipe_body) @content))
)
; Highlight the RHS of `=~` as regex
((regex_literal
[
(string_literal)
(raw_string_literal)
] @injection.content)
(#set! injection.language "regex"))

; (interpolation (expression) @just)

; ================ Global language ================
; Global language is set with something like one of the following:
;
; set shell := ["bash", "-c", ...]
; set shell := ["pwsh.exe"]
;
; We can extract the first item of the array, but we can't extract the language
; name from that with something like regex. So instead we special case two
; things: powershell, which is likely to come with a `.exe` attachment, and
; everything else which hopefully has no extension. We separate this with a
; `#match`.
;
; On NeoVim we have `#gsub!`, so this split is not required.
;
; We also don't have a way to apply this captured language to more than one
; possible item, so instead we do each of the above patterns twice to cover
; all options (recipe bodies and external commands)
;
; Unfortunately, there also isn't a way to allow arbitrary nesting or
; alternatively set "global" capture variables. So we can set this for item-
; level external commands, but not for e.g. external commands within an
; expression without getting _really_ annoying. Should at least look fine since
; they default to bash. Limitations...
; See https://github.com/tree-sitter/tree-sitter/issues/880 for more on that.

(source_file ; NVIM-DISABLE
(item ; NVIM-DISABLE
(setting ; NVIM-DISABLE
left: (identifier) @setting ; NVIM-DISABLE
(#match? @setting "^shell$") ; NVIM-DISABLE
right: (array ; NVIM-DISABLE
(string_literal) @executable ; NVIM-DISABLE
(#match? @executable ".*(powershell|pwsh|cmd).*") ; NVIM-DISABLE
(#set! injection.language "powershell") ; NVIM-DISABLE
))) ; NVIM-DISABLE
(item ; NVIM-DISABLE
(recipe ; NVIM-DISABLE
(recipe_body ; NVIM-DISABLE
(recipe_line ; NVIM-DISABLE
(text) @injection.content))))) ; NVIM-DISABLE
; NVIM-DISABLE
(source_file ; NVIM-DISABLE
(item ; NVIM-DISABLE
(setting ; NVIM-DISABLE
left: (identifier) @setting ; NVIM-DISABLE
(#match? @setting "^shell$") ; NVIM-DISABLE
right: (array ; NVIM-DISABLE
(string_literal) @injection.language ; NVIM-DISABLE
(#not-match? @executable ".*(powershell|pwsh|cmd).*") ; NVIM-DISABLE
))) ; NVIM-DISABLE
(item ; NVIM-DISABLE
(recipe ; NVIM-DISABLE
(recipe_body ; NVIM-DISABLE
(recipe_line ; NVIM-DISABLE
(text) @injection.content))))) ; NVIM-DISABLE
; NVIM-DISABLE
(source_file ; NVIM-DISABLE
(item ; NVIM-DISABLE
(setting ; NVIM-DISABLE
left: (identifier) @setting ; NVIM-DISABLE
(#match? @setting "^shell$") ; NVIM-DISABLE
right: (array ; NVIM-DISABLE
(string_literal) @executable ; NVIM-DISABLE
(#match? @executable ".*(powershell|pwsh|cmd).*") ; NVIM-DISABLE
(#set! injection.language "powershell") ; NVIM-DISABLE
))) ; NVIM-DISABLE
(item ; NVIM-DISABLE
(assignment ; NVIM-DISABLE
right: (expression ; NVIM-DISABLE
(value ; NVIM-DISABLE
(external_command ; NVIM-DISABLE
body: (command_body) @injection.content)))))) ; NVIM-DISABLE
; NVIM-DISABLE
(source_file ; NVIM-DISABLE
(item ; NVIM-DISABLE
(setting ; NVIM-DISABLE
left: (identifier) @setting ; NVIM-DISABLE
(#match? @setting "^shell$") ; NVIM-DISABLE
right: (array ; NVIM-DISABLE
(string_literal) @injection.language ; NVIM-DISABLE
(#not-match? @executable ".*(powershell|pwsh|cmd).*") ; NVIM-DISABLE
))) ; NVIM-DISABLE
(item ; NVIM-DISABLE
(assignment ; NVIM-DISABLE
right: (expression ; NVIM-DISABLE
(value ; NVIM-DISABLE
(external_command ; NVIM-DISABLE
body: (command_body) @injection.content)))))) ; NVIM-DISABLE

; (source_file ; NVIM-ENABLE
; (item ; NVIM-ENABLE
; (setting ; NVIM-ENABLE
; left: (identifier) @setting ; NVIM-ENABLE
; (#match? @setting "^shell$") ; NVIM-ENABLE
; right: (array ; NVIM-ENABLE
; (string_literal) @injection.language ; NVIM-ENABLE
; (#gsub! injection.language "(%S+)%*" "%1") ; NVIM-ENABLE
; ))) ; NVIM-ENABLE
; (item ; NVIM-ENABLE
; (assignment ; NVIM-ENABLE
; right: (expression ; NVIM-ENABLE
; (value ; NVIM-ENABLE
; (external_command ; NVIM-ENABLE
; body: (command_body) @injection.content)))))) ; NVIM-ENABLE
; ; NVIM-ENABLE
; (source_file ; NVIM-ENABLE
; (item ; NVIM-ENABLE
; (setting ; NVIM-ENABLE
; left: (identifier) @setting ; NVIM-ENABLE
; (#match? @setting "^shell$") ; NVIM-ENABLE
; right: (array ; NVIM-ENABLE
; (string_literal) @injection.language ; NVIM-ENABLE
; (#gsub! injection.language "(%S+)%*" "%1") ; NVIM-ENABLE
; ))) ; NVIM-ENABLE
; (item ; NVIM-ENABLE
; (assignment ; NVIM-ENABLE
; right: (expression ; NVIM-ENABLE
; (value ; NVIM-ENABLE
; (external_command ; NVIM-ENABLE
; body: (command_body) @injection.content)))))) ; NVIM-ENABLE

; ================ Handle shebangs ================

(recipe_body ; NVIM-DISABLE
(shebang) @injection.shebang ; NVIM-DISABLE
(recipe_line ; NVIM-DISABLE
(text) @injection.content)) ; NVIM-DISABLE

; On NeoVIM, use gsub to extract the language from the shebang
; (recipe_body ; NVIM-ENABLE
; (shebang) @injection.language ; NVIM-ENABLE
; (recipe_line ; NVIM-ENABLE
; (text) @injection.content) ; NVIM-ENABLE
; (#gsub! injection.language "/#!%*[\/ ](%S+)/" "%1")) ; NVIM-ENABLE


; ================ Default recipe body ================

; If there are no other matches, set defaults to bash
(recipe_line
(text) @injection.content
(#set! injection.language "bash"))

(external_command
(command_body) @injection.content
(#set! injection.language "bash"))
42 changes: 15 additions & 27 deletions queries/just/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
(alias left: (identifier) @variable)
(assignment
left: (identifier) @variable
[":="] @operator)
":=" @operator)

(module mod_name: (identifier) @namespace)

; highlight known settings (filtering does not always work)
(setting
left: ((identifier) @keyword
":="? @operator
(#any-of? @keyword
"allow-duplicate-recipes"
"dotenv-filename"
Expand All @@ -40,24 +41,8 @@
(function_call
name: (identifier) @function)

; highlight known attributes (filtering does not always work)
(attribute
attr_item: ((identifier) @attribute
(#any-of? @attribute
"private"
"allow-duplicate-recipes"
"dotenv-filename"
"dotenv-load"
"dotenv-path"
"export"
"fallback"
"ignore-comments"
"positional-arguments"
"shell"
"tempdi"
"windows-powershell"
"windows-shell"
)))
attr_item: ((identifier) @attribute))

(recipe_header
recipe_name: (identifier) @function)
Expand All @@ -73,16 +58,14 @@
(dependency_expression recipe: (identifier) @function)

; handle escape sequences
(string_literal (string_escape) @string.escape)
(string_literal) @string
(string_literal (escape_sequence) @string.escape)
[
(string_literal)
(raw_string_literal)
] @string

(comment) @comment

; (interpolation) @string

; FIXME: interpreter
; (shebang interpreter:(TEXT) @keyword ) @comment

[
"alias"
"export"
Expand All @@ -91,14 +74,19 @@
"set"
] @keyword

; exclude `=` and `&&` since they are valid in more normal scopes
; exclude `=` and `&&` since they are valid only in specific scopes
; (matching is covered in their parent nodes)
["@" "==" "!=" "+" "*" ":" "/" "?"] @operator

["(" ")" "[" "]" "{{" "}}" "{" "}"] @punctuation.bracket

["," ":"] @punctuation.delimiter

"`" @punctuation.special

; open and close are "`" or "```"
(external_command
open: _ @punctuation.special
close: _ @punctuation.special
)

(ERROR) @error
1 change: 1 addition & 0 deletions queries/just/indents.scm
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

(recipe) @indent @extend
(string_literal) @indent @extend
(raw_string_literal) @indent @extend
(external_command) @indent @extend
Loading

0 comments on commit 396b535

Please sign in to comment.