Skip to content

Commit

Permalink
Improve highlighting of parameters and dependnecies in recipes (India…
Browse files Browse the repository at this point in the history
…nBoy42#46)

* Improve highlighting of parameters and dependnecies in recipes

* Improve highlighting for attributes
  • Loading branch information
tgross35 authored Jan 4, 2024
1 parent ba36c85 commit 87f48b8
Show file tree
Hide file tree
Showing 8 changed files with 3,746 additions and 3,511 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ tree-sitter parse test/test.just

You can use the `nvim-treesitter/playground` plugin [from here](https://github.com/nvim-treesitter/playground), to explore the resulting parse tree. Use `TSPlaygroundToggle` to view the parse tree, and use `TSHighlightCapturesUnderCursor` to view highlight groups

## Quirks of Just

Just currently doesn't seem to support comments between attributes or within if
statements, so we do not either.

```just
[private]
# hello!
[no-cd]
foo:
```

```just
foo := if true {
# nope!
"abcd"
}
```

## TODO

- [x] Implement a basic parser that is able to understand all features of Justfiles
Expand Down
55 changes: 35 additions & 20 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = grammar({
inline: (
$,
) => [
$._dependency_with_args,
$._expression_braced,
$._expression_recurse,
],
Expand Down Expand Up @@ -86,7 +85,13 @@ module.exports = grammar({
import: ($) => seq("import", optional("?"), $.string),

// module : 'mod' '?'? string?
module: ($) => seq("mod", optional("?"), $.identifier, optional($.string)),
module: ($) =>
seq(
"mod",
optional("?"),
field("modname", $.identifier),
optional($.string),
),

// setting : 'set' 'dotenv-load' boolean?
// | 'set' 'export' boolean?
Expand Down Expand Up @@ -189,20 +194,13 @@ module.exports = grammar({
// | expression ','?
sequence: ($) => comma_sep1($.expression),

// string : STRING
// | INDENTED_STRING
// | RAW_STRING
// | INDENTED_RAW_STRING
string: ($) =>
choice(
$.basic_string,
$.basic_string_indented,
$.raw_string,
$.raw_string_indented,
),

attribute: ($) =>
seq("[", field("contents", comma_sep1($.identifier)), "]", $.eol),
seq(
"[",
field("contents", comma_sep1(field("attr_item", $.identifier))),
"]",
$.eol,
),

// A complete recipe
// recipe : attribute? '@'? NAME parameter* variadic_parameters? ':' dependency* body?
Expand All @@ -217,7 +215,7 @@ module.exports = grammar({
recipe_header: ($) =>
seq(
optional("@"),
$.identifier,
field("recipe_name", $.identifier),
optional($.parameters),
":",
repeat($.dependency),
Expand All @@ -226,6 +224,7 @@ module.exports = grammar({
parameters: ($) =>
seq(repeat($.parameter), choice($.parameter, $.variadic_parameter)),

// FIXME: do we really have leading `$`s here?`
// parameter : '$'? NAME
// | '$'? NAME '=' value
parameter: ($) =>
Expand All @@ -247,14 +246,16 @@ module.exports = grammar({
dependency: ($) =>
choice(
field("recipe", $.identifier),
field("call", seq("(", $._dependency_with_args, ")")),
$.dependency_expression,
),

// contents of `(recipe expression)`
_dependency_with_args: ($) =>
dependency_expression: ($) =>
seq(
"(",
field("recipe", $.identifier),
repeat(field("expression", $.expression)),
")",
),

// body : INDENT line+ DEDENT
Expand Down Expand Up @@ -287,15 +288,29 @@ module.exports = grammar({

identifier: (_) => /[a-zA-Z_][a-zA-Z0-9_-]*/,

backticked: (_) => seq("`", repeat(/./), "`"),
indented_backticked: (_) => seq("```", repeat(/./), "```"),
// string : STRING
// | INDENTED_STRING
// | RAW_STRING
// | INDENTED_RAW_STRING
string: ($) =>
choice(
$.basic_string,
$.basic_string_indented,
$.raw_string,
$.raw_string_indented,
),

raw_string: (_) => /'[^']*'/,
raw_string_indented: (_) => seq("'''", repeat(/./), "'''"),
basic_string: ($) =>
seq('"', repeat(choice($.string_escape, /[^\\"]+/)), '"'),
basic_string_indented: ($) =>
seq('"""', repeat(choice($.string_escape, /[^\\"]+/)), '"""'),
string_escape: (_) => /\\[nrt"\\]/,

backticked: (_) => seq("`", repeat(/./), "`"),
indented_backticked: (_) => seq("```", repeat(/./), "```"),

text: (_) => /.+/, //recipe TEXT, only matches in a recipe body
// text: (_) => /\S+/, //recipe TEXT, only matches in a recipe body
},
Expand Down
51 changes: 33 additions & 18 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
(shebang) @comment

(alias
left: (identifier) @variable
)
left: (identifier) @variable)

(assignment
left: (identifier) @variable)

(module (identifier) @namespace)
(module
modname: (identifier) @namespace)

(setting "shell" @keyword)
(setting
Expand All @@ -36,33 +36,48 @@
["if" "else"] @keyword.control.conditional

(value (identifier) @variable)
; (call (identifier) @function)


; ([parameter varidic_parameter]
; (identifier) @variable)
(parameter (identifier) @variable)
(variadic_parameter (parameter (identifier)) @variable)

(function_call
name: (identifier) @function)


(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"
)))

(attribute
attr_item: (identifier) @variable)

(dependency
recipe: (identifier) @function
expression: (expression)* @parameter)

(recipe_header (identifier) @function)
(recipe_header
recipe_name: (identifier) @function)

; FIXME: what's up with $identifier params
(parameter) @variable.parameter
; pattern includes variadic_parameter
(parameter
param: (identifier) @variable.parameter
"="? @operator)

(dependency
recipe: (identifier) @function)
(dependency_expression
recipe: (identifier) @function)

(string (_ (string_escape) @constant.character.escape))
(string) @string


(comment) @comment.line

; (interpolation) @string
Expand Down
Loading

0 comments on commit 87f48b8

Please sign in to comment.