Skip to content

Commit

Permalink
Apply changes from Amaan's query updates
Browse files Browse the repository at this point in the history
Co-authored-by: Amaan Qureshi <[email protected]>
  • Loading branch information
tgross35 and amaanq committed Feb 20, 2024
1 parent 159e866 commit be0fdf7
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 148 deletions.
16 changes: 13 additions & 3 deletions build-flavored-queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<https://docs.helix-editor.com/master/themes.html#syntax-highlighting> has a bit better
guide for these parameters than tree-sitter does.
"""

import re
from glob import glob
from pathlib import Path

Expand All @@ -24,6 +24,13 @@
("@variable.parameter", "@parameter"),
("@comment.line", "@comment"),
("@constant.character.escape", "@string.escape"),
("@keyword.module", "@module"),
]

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


Expand All @@ -35,8 +42,11 @@ def main():
with open(fname) as f:
contents += f.read()

for x, to in REPLACEMENTS:
contents = contents.replace(x, to)
for query, sub in REPLACEMENTS:
contents = contents.replace(query, sub)

for query, sub, flags in REPLACEMENTS_RE:
contents = re.sub(query, sub, contents, flags=flags)

dest = Path("queries") / "just" / Path(fname).name
with open(dest, "w") as f:
Expand Down
152 changes: 94 additions & 58 deletions queries-src/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,16 +1,104 @@
; This file specifies how matched syntax patterns should be highlighted
;
; This file is ordered roughly the same as grammar.js

(shebang) @comment
[
"export"
"import"
] @keyword.import

"mod" @keyword.module

[
"alias"
"set"
"shell"
] @keyword

[
"if"
"else"
] @keyword.control.conditional

(boolean ["true" "false"]) @constant.builtin.boolean

; Variables

(value (identifier) @variable)

(alias left: (identifier) @variable)
(assignment
left: (identifier) @variable
[":="] @operator)

(assignment left: (identifier) @variable)

; Functions

(recipe_header name: (identifier) @function)

(dependency name: (identifier) @function.call)

(dependency_expression name: (identifier) @function.call)

(function_call name: (identifier) @function.call)

; Parameters

(parameter name: (identifier) @variable.parameter)

; Namespaces

(module name: (identifier) @namespace)

; Operators

[
":="
"?"
"=="
"!="
"=~"
"@"
"="
"$"
"*"
"+"
"&&"
"@-"
"-@"
"-"
"/"
":"
] @operator

; Punctuation

[ "," ] @punctuation.delimiter

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

[ "{{" "}}" "`" ] @punctuation.special

; Literals

(boolean) @boolean

[
(string)
(external_command)
] @string

(escape_sequence) @constant.character.escape

; Comments

(comment) @comment.line @spell

(shebang) @comment

; highlight known settings (filtering does not always work)
(setting
left: ((identifier) @keyword
Expand All @@ -29,15 +117,6 @@
"windows-shell"
)))

(boolean ["true" "false"]) @constant.builtin.boolean

["if" "else"] @keyword.control.conditional

(value (identifier) @variable)

(function_call
name: (identifier) @function)

; highlight known attributes (filtering does not always work)
(attribute
((identifier) @attribute
Expand All @@ -56,46 +135,3 @@
"windows-powershell"
"windows-shell"
)))

(recipe_header name: (identifier) @function)

; recipe argument specification
; pattern includes variadic_parameter
(parameter
name: (identifier) @variable.parameter
"="? @operator)

(dependencies "&&" @operator)
(dependency name: (identifier) @function)
(dependency_expression name: (identifier) @function)

; handle escape sequences
(string (escape_sequence) @constant.character.escape)
(string) @string

(comment) @comment.line

; (interpolation) @string

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

[
"alias"
"export"
"import"
"mod"
"set"
] @keyword

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

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

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

"`" @punctuation.special

(ERROR) @error
10 changes: 7 additions & 3 deletions queries-src/indents.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
;
; Better documentation is in https://docs.helix-editor.com/guides/indent.html

(recipe) @indent @extend
(string) @indent @extend
(external_command) @indent @extend
[
(recipe)
(string)
(external_command)
] @indent @extend

(comment) @indent.auto
16 changes: 14 additions & 2 deletions queries-src/locals.scm
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
; This file tells us about the scope of variables so e.g. local
; variables override global functions with the same name

; Scope

(recipe) @local.scope

; definition sources
; Definitions

(alias left: (identifier) @local.definition.variable)

(assignment left: (identifier) @local.definition.variable)

(module name: (identifier) @local.definition.namespace)

(parameter name: (identifier) @local.definition.variable)

(recipe_header name: (identifier) @local.definition.function)

; reference locations
; References

(alias right: (identifier) @local.reference.variable)

(function_call name: (identifier) @local.reference.function)

(dependency name: (identifier) @local.reference.function)

(dependency_expression name: (identifier) @local.reference.function)

(value (identifier) @local.reference.variable)
Loading

0 comments on commit be0fdf7

Please sign in to comment.