Skip to content

Commit

Permalink
Working with @injection.shebang
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Jan 8, 2024
1 parent 7ee36dc commit 44be759
Show file tree
Hide file tree
Showing 11 changed files with 3,848 additions and 4,076 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
17 changes: 1 addition & 16 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,7 @@ module.exports = grammar({

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

shebang: ($) =>
prec.left(
seq(
choice($.shebang_executable, field("unrecognized_shebang", /#!.*/)),
optional($._newline),
),
),

// Rule is never used directly
shebang_executable: ($) =>
token.immediate(seq(
"#!",
/\S*[/ ]/,
field("cmd", /\S+/),
/.*/,
)),
shebang: ($) => prec.left(seq(/#!.*/, optional($._newline))),

// prec(
// 4,
Expand Down
20 changes: 10 additions & 10 deletions queries-src/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
((comment) @injection.content
(#set! injection.language "comment"))

; FIXME: read default from shebang
; NVIM-DISABLE-START
(recipe_body
(shebang
(shebang_executable
_
)
; "#!"
; _
)
(shebang) @injection.shebang
(recipe_line
(text) @injection.content
(#set! injection.language "bash")))
(text) @injection.content))
; NVIM-DISABLE-END

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

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

(external_command
(command_body) @injection.content
(#set! injection.language "bash"))
Expand Down
19 changes: 7 additions & 12 deletions queries/just/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@
((comment) @injection.content
(#set! injection.language "comment"))

; FIXME: read default from shebang
(recipe_body
(shebang
(shebang_executable
_
)
; "#!"
; _
)
(recipe_line
(text) @injection.content
(#set! injection.language "bash")))


(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"))
Expand Down
45 changes: 2 additions & 43 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1258,21 +1258,8 @@
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "shebang_executable"
},
{
"type": "FIELD",
"name": "unrecognized_shebang",
"content": {
"type": "PATTERN",
"value": "#!.*"
}
}
]
"type": "PATTERN",
"value": "#!.*"
},
{
"type": "CHOICE",
Expand All @@ -1289,34 +1276,6 @@
]
}
},
"shebang_executable": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "#!"
},
{
"type": "PATTERN",
"value": "\\S*[/ ]"
},
{
"type": "FIELD",
"name": "cmd",
"content": {
"type": "PATTERN",
"value": "\\S+"
}
},
{
"type": "PATTERN",
"value": ".*"
}
]
}
},
"shebang_language": {
"type": "PATTERN",
"value": ".+"
Expand Down
16 changes: 1 addition & 15 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,7 @@
{
"type": "shebang",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "shebang_executable",
"named": true
}
]
}
"fields": {}
},
{
"type": "source_file",
Expand Down Expand Up @@ -1038,10 +1028,6 @@
"type": "set",
"named": false
},
{
"type": "shebang_executable",
"named": true
},
{
"type": "shell",
"named": false
Expand Down
Loading

0 comments on commit 44be759

Please sign in to comment.