Skip to content

Commit

Permalink
Add logic for setting recipe body language with shebangs
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Jan 8, 2024
1 parent e4e2405 commit 7765fb3
Show file tree
Hide file tree
Showing 12 changed files with 3,539 additions and 3,522 deletions.
13 changes: 11 additions & 2 deletions build-flavored-queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import re
from glob import glob
from pathlib import Path

Expand All @@ -26,6 +27,11 @@
("@constant.character.escape", "@string.escape"),
]

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


def main():
sources = glob("queries-src/*.scm")
Expand All @@ -35,8 +41,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
4 changes: 3 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ module.exports = grammar({
// justfile : item* EOF
source_file: ($) => seq(optional($.shebang), repeat($.item)),

// _sheb: ($) => ,

// item : recipe
// | alias
// | assignment
Expand Down Expand Up @@ -295,7 +297,7 @@ module.exports = grammar({

recipe_line_prefix: (_) => choice("@-", "-@", "@", "-"),

shebang: ($) => seq(/\s*#!.*/, $._newline),
shebang: ($) => prec.left(seq(/#!.*/, optional($._newline))),

// `# ...` comment
comment: ($) => seq(/#.*/, $._newline),
Expand Down
13 changes: 12 additions & 1 deletion queries-src/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
((comment) @injection.content
(#set! injection.language "comment"))

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

; FIXME: read default from shebang
(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"))
Expand Down
23 changes: 11 additions & 12 deletions queries/just/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@
(#set! injection.language "comment"))



(recipe_line
(text) @injection.content
(#set! injection.language "bash"))

(recipe_body
(shebang) @injection.shebang
(recipe_line
(text) @injection.content
(#set! injection.language "python")))

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

((regex_literal
[(string_literal)
(raw_string_literal)] @injection.content)
[
(string_literal)
(raw_string_literal)
] @injection.content)
(#set! injection.language "regex"))

; ; FIXME: read default from shebang
; ([
; (recipe_body
; (recipe_line (text)))
; (external_command
; body: (_)*)
; ] @injection.content
; (#set! injection.language "bash")
; )
38 changes: 27 additions & 11 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1252,17 +1252,33 @@
]
},
"shebang": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "\\s*#!.*"
},
{
"type": "SYMBOL",
"name": "_newline"
}
]
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "#!.*"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_newline"
},
{
"type": "BLANK"
}
]
}
]
}
},
"shebang_language": {
"type": "PATTERN",
"value": ".+"
},
"comment": {
"type": "SEQ",
Expand Down
9 changes: 5 additions & 4 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,11 @@
}
}
},
{
"type": "text",
"named": true,
"fields": {}
},
{
"type": "value",
"named": true,
Expand Down Expand Up @@ -1027,10 +1032,6 @@
"type": "shell",
"named": false
},
{
"type": "text",
"named": true
},
{
"type": "true",
"named": false
Expand Down
Loading

0 comments on commit 7765fb3

Please sign in to comment.