Skip to content

Commit

Permalink
Provide a reasonable amount of handling for global set shell commands
Browse files Browse the repository at this point in the history
See notes in `injections.scm` for reasons about why this can't be great.
  • Loading branch information
tgross35 committed Jan 9, 2024
1 parent 7765fb3 commit 2bb4349
Show file tree
Hide file tree
Showing 19 changed files with 4,002 additions and 4,283 deletions.
23 changes: 23 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[submodule "submodules/tree-sitter-bash"]
path = submodules/tree-sitter-bash
url = https://github.com/tgross35/tree-sitter-bash.git
branch = patch-1
shallow = true
[submodule "submodules/tree-sitter-PowerShell"]
path = submodules/tree-sitter-PowerShell
url = https://github.com/tgross35/tree-sitter-PowerShell.git
branch = add-ts-package-config
shallow = true
[submodule "submodules/tree-sitter-perl"]
path = submodules/tree-sitter-perl
url = https://github.com/tgross35/tree-sitter-perl.git
branch = patch-1
shallow = true
[submodule "submodules/tree-sitter-python"]
path = submodules/tree-sitter-python
url = https://github.com/tree-sitter/tree-sitter-python.git
shallow = true
[submodule "submodules/tree-sitter-regex"]
path = submodules/tree-sitter-regex
url = https://github.com/tree-sitter/tree-sitter-regex.git
shallow = true
4 changes: 2 additions & 2 deletions build-flavored-queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
]

REPLACEMENTS_RE = [
(r";\s*NVIM-DISABLE-START.*;\s*NVIM-DISABLE-END", "", re.MULTILINE | re.DOTALL),
("^.*NVIM-ENABLE(?P<content>.*)$", r"\g<content>", re.MULTILINE),
(r"^[^;] ?(.*;\s*NVIM-DISABLE)$", "", re.MULTILINE),
(r"^; ?(.*;\s*NVIM-ENABLE)$", r"\1", re.MULTILINE),
]


Expand Down
23 changes: 7 additions & 16 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function comma_sep1(item) {
}

// Create an array with the given item as contents
function array(item) {
function make_array(item) {
const array_item = field("array_item", item);
return field(
"array",
Expand Down Expand Up @@ -119,26 +119,17 @@ module.exports = grammar({
seq(
"set",
field("left", $.identifier),
field(
"right",
optional(
seq(":=", choice($.boolean, $._string, array($._string))),
),
),
$.eol,
),
seq(
"set",
"shell",
":=",
field(
"right",
array($._string),
optional(
seq(":=", field("right", choice($.boolean, $._string, $.array))),
),
$.eol,
),
),

// Our only use of arrays (setting) only accepts strings. We may want to figure
// out how to better reuse `array` while specifying a type.
array: ($) => make_array($._string),

// boolean : ':=' ('true' | 'false')
boolean: (_) => choice("true", "false"),

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"injection-regex": "^(?i)just(file)?$",
"first-line-regex": "#!\\S*bin\\S*[/ ]just",
"highlights": ["queries-src/highlights.scm"],
"locals": ["queries-src/locals.scm"]
"locals": ["queries-src/locals.scm"],
"injections": ["queries-src/injections.scm"]
}
]
}
21 changes: 3 additions & 18 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 Down
167 changes: 148 additions & 19 deletions queries-src/injections.scm
Original file line number Diff line number Diff line change
@@ -1,32 +1,161 @@
; This query specifies how nested languages are handled

; ================ Standards ================
((comment) @injection.content
(#set! injection.language "comment"))

; NVIM-DISABLE-START
(recipe_body
(shebang) @injection.shebang
(recipe_line
(text) @injection.content))
; NVIM-DISABLE-END
; Highlight the RHS of `=~` as regex
((regex_literal
[
(string_literal)
(raw_string_literal)
] @injection.content)
(#set! injection.language "regex"))


; ================ 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"))

; NVIM-ENABLE (recipe_body
; NVIM-ENABLE (shebang) @injection.language
; NVIM-ENABLE (recipe_line
; NVIM-ENABLE (text) @injection.content)
; NVIM-ENABLE (#gsub! injection.language "/#!%*[\/ ](%S+)/" "%1"))

(external_command
(command_body) @injection.content
(#set! injection.language "bash"))

((regex_literal
[
(string_literal)
(raw_string_literal)
] @injection.content)
(#set! injection.language "regex"))
21 changes: 3 additions & 18 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 Down
Loading

0 comments on commit 2bb4349

Please sign in to comment.