feat(languages): update just grammar and queries (#11306)

* feat(languages): update `just` grammar and queries

Bump the

* refactor(syntax): inject shebang by id not name

---------

Co-authored-by: Trevor Gross <tmgross@umich.edu>
This commit is contained in:
RoloEdits 2024-07-28 06:54:10 -07:00 committed by GitHub
parent ae72a1dc42
commit 6eae846197
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 296 additions and 95 deletions

View file

@ -1025,9 +1025,10 @@ impl Loader {
match capture { match capture {
InjectionLanguageMarker::Name(string) => self.language_config_for_name(string), InjectionLanguageMarker::Name(string) => self.language_config_for_name(string),
InjectionLanguageMarker::Filename(file) => self.language_config_for_file_name(file), InjectionLanguageMarker::Filename(file) => self.language_config_for_file_name(file),
InjectionLanguageMarker::Shebang(shebang) => { InjectionLanguageMarker::Shebang(shebang) => self
self.language_config_for_language_id(shebang) .language_config_ids_by_shebang
} .get(shebang)
.and_then(|&id| self.language_configs.get(id).cloned()),
} }
} }

View file

@ -3082,7 +3082,7 @@ indent = { tab-width = 4, unit = " " }
[[grammar]] [[grammar]]
name = "just" name = "just"
source = { git = "https://github.com/IndianBoy42/tree-sitter-just", rev = "8af0aab79854aaf25b620a52c39485849922f766" } source = { git = "https://github.com/IndianBoy42/tree-sitter-just", rev = "379fbe36d1e441bc9414ea050ad0c85c9d6935ea" }
[[language]] [[language]]
name = "gn" name = "gn"

View file

@ -1,4 +1,10 @@
(body) @fold ; From <https://github.com/IndianBoy42/tree-sitter-just/blob/6c2f018ab1d90946c0ce029bb2f7d57f56895dff/queries-flavored/helix/folds.scm>
(recipe) @fold
(interpolation) @fold ; Define collapse points
(item (_) @fold)
([
(recipe)
(string)
(external_command)
] @fold
(#trim! @fold))

View file

@ -1,33 +1,149 @@
(assignment (NAME) @variable) ; From <https://github.com/IndianBoy42/tree-sitter-just/blob/6c2f018ab1d90946c0ce029bb2f7d57f56895dff/queries-flavored/helix/highlights.scm>
(alias (NAME) @variable)
(value (NAME) @variable)
(parameter (NAME) @variable)
(setting (NAME) @keyword)
(setting "shell" @keyword)
(call (NAME) @function) ; This file specifies how matched syntax patterns should be highlighted
(dependency (NAME) @function)
(depcall (NAME) @function)
(recipeheader (NAME) @function)
(depcall (expression) @variable.parameter) [
(parameter) @variable.parameter "export"
(variadic_parameters) @variable.parameter "import"
] @keyword.control.import
["if" "else"] @keyword.control.conditional "mod" @keyword.directive
(string) @string [
"alias"
"set"
"shell"
] @keyword
(boolean ["true" "false"]) @constant.builtin.boolean [
"if"
"else"
] @keyword.control.conditional
(comment) @comment ; Variables
; (interpolation) @string (value
(identifier) @variable)
(shebang interpreter:(TEXT) @keyword ) @comment (alias
left: (identifier) @variable)
["export" "alias" "set"] @keyword (assignment
left: (identifier) @variable)
["@" "==" "!=" "+" ":="] @operator ; Functions
[ "(" ")" "[" "]" "{{" "}}" "{" "}"] @punctuation.bracket (recipe_header
name: (identifier) @function)
(dependency
name: (identifier) @function)
(dependency_expression
name: (identifier) @function)
(function_call
name: (identifier) @function)
; Parameters
(parameter
name: (identifier) @variable.parameter)
; Namespaces
(module
name: (identifier) @namespace)
; Operators
[
":="
"?"
"=="
"!="
"=~"
"@"
"="
"$"
"*"
"+"
"&&"
"@-"
"-@"
"-"
"/"
":"
] @operator
; Punctuation
"," @punctuation.delimiter
[
"{"
"}"
"["
"]"
"("
")"
"{{"
"}}"
] @punctuation.bracket
[ "`" "```" ] @punctuation.special
; Literals
(boolean) @constant.builtin.boolean
[
(string)
(external_command)
] @string
(escape_sequence) @constant.character.escape
; Comments
(comment) @comment.line
(shebang) @keyword.directive
; highlight known settings (filtering does not always work)
(setting
left: (identifier) @keyword
(#any-of? @keyword
"allow-duplicate-recipes"
"dotenv-filename"
"dotenv-load"
"dotenv-path"
"export"
"fallback"
"ignore-comments"
"positional-arguments"
"shell"
"tempdi"
"windows-powershell"
"windows-shell"))
; highlight known attributes (filtering does not always work)
(attribute
(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"))
; Numbers are part of the syntax tree, even if disallowed
(numeric_error) @error

View file

@ -1,3 +1,11 @@
; From <https://github.com/IndianBoy42/tree-sitter-just/blob/6c2f018ab1d90946c0ce029bb2f7d57f56895dff/queries-flavored/helix/indents.scm>
;
; This query specifies how to auto-indent logical blocks.
;
; Better documentation with diagrams is in https://docs.helix-editor.com/guides/indent.html
[ [
(recipe_body) (recipe)
] @indent (string)
(external_command)
] @indent @extend

View file

@ -1,16 +1,84 @@
; From <https://github.com/IndianBoy42/tree-sitter-just/blob/6c2f018ab1d90946c0ce029bb2f7d57f56895dff/queries-flavored/helix/injections.scm>
;
; Specify nested languages that live within a `justfile`
; ================ Always applicable ================
((comment) @injection.content ((comment) @injection.content
(#set! injection.language "comment")) (#set! injection.language "comment"))
(shebang_recipe ; Highlight the RHS of `=~` as regex
(shebang ((regex_literal
interpreter:(TEXT) @injection.language) (_) @injection.content)
(shebang_body) @injection.content (#set! injection.language "regex"))
)
; ================ Global defaults ================
; Default everything to be bash
(recipe_body
!shebang
(#set! injection.language "bash")
(#set! injection.include-children)) @injection.content
(external_command
(command_body) @injection.content
(#set! injection.language "bash"))
; ================ Global language specified ================
; 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 the string with something like regex. So instead we special case
; two things: powershell, which is likely to come with a `.exe` attachment that
; we need to strip, and everything else which hopefully has no extension. We
; separate this with a `#match?`.
;
; 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 (source_file
(item (setting lang:(NAME) @injection.language)) (setting "shell" ":=" "[" (string) @_langstr
(item (recipe (body (recipe_body) @injection.content))) (#match? @_langstr ".*(powershell|pwsh|cmd).*")
) (#set! injection.language "powershell"))
[
(recipe
(recipe_body
!shebang
(#set! injection.include-children)) @injection.content)
; ((interpolation (expression) @injection.content) (assignment
; (#set! injection.language "just")) (expression
(value
(external_command
(command_body) @injection.content))))
])
(source_file
(setting "shell" ":=" "[" (string) @injection.language
(#not-match? @injection.language ".*(powershell|pwsh|cmd).*"))
[
(recipe
(recipe_body
!shebang
(#set! injection.include-children)) @injection.content)
(assignment
(expression
(value
(external_command
(command_body) @injection.content))))
])
; ================ Recipe language specified - Helix only ================
; Set highlighting for recipes that specify a language using builtin shebang matching
(recipe_body
(shebang) @injection.shebang
(#set! injection.include-children)) @injection.content

View file

@ -1,10 +1,42 @@
(assignment (NAME) @local.definition) ; From <https://github.com/IndianBoy42/tree-sitter-just/blob/6c2f018ab1d90946c0ce029bb2f7d57f56895dff/queries-flavored/helix/locals.scm>
(alias left:(NAME) @local.definition) ;
(alias right:(NAME) @local.reference) ; This file tells us about the scope of variables so e.g. local
(value (NAME) @local.reference) ; variables override global functions with the same name
(parameter (NAME) @local.definition)
(call (NAME) @local.reference) ; Scope
(dependency (NAME) @local.reference)
(depcall (NAME) @local.reference) (recipe) @local.scope
(recipeheader (NAME) @local.definition)
; Definitions
(alias
left: (identifier) @local.definition)
(assignment
left: (identifier) @local.definition)
(module
name: (identifier) @local.definition)
(parameter
name: (identifier) @local.definition)
(recipe_header
name: (identifier) @local.definition)
; References
(alias
right: (identifier) @local.reference)
(function_call
name: (identifier) @local.reference)
(dependency
name: (identifier) @local.reference)
(dependency_expression
name: (identifier) @local.reference)
(value
(identifier) @local.reference)

View file

@ -1,48 +1,18 @@
(body) @function.inside ; From <https://github.com/IndianBoy42/tree-sitter-just/blob/6c2f018ab1d90946c0ce029bb2f7d57f56895dff/queries-flavored/helix/textobjects.scm>
(recipe) @function.around ;
(expression ; Specify how to navigate around logical blocks in code
if:(expression) @function.inside
)
(expression
else:(expression) @function.inside
)
(interpolation (expression) @function.inside) @function.around
(settinglist (stringlist) @function.inside) @function.around
(call (NAME) @class.inside) @class.around (recipe
(dependency (NAME) @class.inside) @class.around (recipe_body) @function.inside) @function.around
(depcall (NAME) @class.inside)
(dependency) @parameter.around
(depcall) @parameter.inside
(depcall (expression) @parameter.inside)
(stringlist
(string) @parameter.inside
. ","? @_end
; Commented out since we don't support `#make-range!` at the moment
; (#make-range! "parameter.around" @parameter.inside @_end)
)
(parameters (parameters
[(parameter) ((_) @parameter.inside . ","? @parameter.around)) @parameter.around
(variadic_parameters)] @parameter.inside
. " "? @_end
; Commented out since we don't support `#make-range!` at the moment
; (#make-range! "parameter.around" @parameter.inside @_end)
)
(expression (dependency_expression
(condition) @function.inside (_) @parameter.inside) @parameter.around
) @function.around
(expression
if:(expression) @function.inside
)
(expression
else:(expression) @function.inside
)
(item [(alias) (assignment) (export) (setting)]) @class.around (function_call
(recipeheader) @class.around arguments: (sequence
(line) @class.around (expression) @parameter.inside) @parameter.around) @function.around
(comment) @comment.around (comment) @comment.around