Skip to content

Commit

Permalink
Improve highlighting of parameters and dependnecies in recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Jan 4, 2024
1 parent ba36c85 commit a5e0069
Show file tree
Hide file tree
Showing 7 changed files with 3,727 additions and 3,511 deletions.
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("attribute", $.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
attribute: ((identifier) @keyword
(#any-of? @keyword
"private"
"allow-duplicate-recipes"
"dotenv-filename"
"dotenv-load"
"dotenv-path"
"export"
"fallback"
"ignore-comments"
"positional-arguments"
"shell"
"tempdi"
"windows-powershell"
"windows-shell"
)))

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

(recipe_header (identifier) @function)
(attribute
attribute: (identifier) @variable)

; FIXME: what's up with $identifier params
(parameter) @variable.parameter
(recipe_header
recipe_name: (identifier) @function)

; 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 a5e0069

Please sign in to comment.