Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve distinction of flags and parameters #110

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ target/

/build/

# C lsp cache
.ccls-cache/

# Workspace Config
.nvim/
# .vscode/
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module.exports = grammar({

extras: ($) => [/\s/, $.comment],

// externals: $ => [
// $.long_flag_equals_value
// ],

conflicts: ($) => [
[$._declaration, $._declaration_last],
[$._declaration_parenthesized, $._declaration_parenthesized_last],
Expand Down Expand Up @@ -1264,6 +1268,8 @@ module.exports = grammar({
field("arg_str", alias($.unquoted, $.val_string)),
),

flag_value: ($) => choice($._value, $.val_string),

redirection: ($) =>
choice(
prec.right(
Expand All @@ -1288,9 +1294,23 @@ module.exports = grammar({
long_flag: ($) =>
prec.right(
10,
choice(OPR().long_flag, seq(OPR().long_flag, $.long_flag_identifier)),
choice(
OPR().long_flag,
seq(OPR().long_flag, $.long_flag_identifier),
$.long_flag_equals_value,
),
),

long_flag_equals_value: ($) =>
seq(
OPR().long_flag,
$.long_flag_identifier,
PUNC().eq,
$.long_flag_value,
),

long_flag_value: ($) => $._cmd_arg,

unquoted: _unquoted_rule(false),
_unquoted_in_list: _unquoted_rule(true),

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"main": "bindings/node",
"types": "bindings/node",
"scripts": {
"test": " ",
"install": "node-gyp-build",
"prebuildify": "prebuildify --napi --strip"
"prestart": "tree-sitter build --wasm",
"start": "tree-sitter playground",
"test": "node --test bindings/node/*_test.js"
},
"author": "The Nushell Contributors",
"license": "MIT",
Expand Down
8 changes: 5 additions & 3 deletions queries/nu/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
) @number
(val_bool) @constant.builtin
(val_nothing) @constant.builtin
(val_string) @string
(val_string) @variable.parameter
arg_str: (val_string) @variable.parameter
file_path: (val_string) @variable.parameter
(val_date) @number
Expand Down Expand Up @@ -189,7 +189,9 @@ file_path: (val_string) @variable.parameter

(param_long_flag ["--"] @punctuation.delimiter)
(long_flag ["--"] @punctuation.delimiter)
(long_flag_equals_value ["--"] @punctuation.delimiter)
(short_flag ["-"] @punctuation.delimiter)
(long_flag_equals_value ["="] @punctuation.special)
(param_short_flag ["-"] @punctuation.delimiter)
(param_rest "..." @punctuation.delimiter)
(param_type [":"] @punctuation.special)
Expand Down Expand Up @@ -221,8 +223,8 @@ key: (identifier) @property
(param_long_flag (long_flag_identifier) @variable.parameter)
(param_short_flag (param_short_flag_identifier) @variable.parameter)

(short_flag (short_flag_identifier) @variable.parameter)
(long_flag (long_flag_identifier) @variable.parameter)
(short_flag_identifier) @type
(long_flag_identifier) @type

(scope_pattern [(wild_card) @function])

Expand Down
42 changes: 42 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -10563,6 +10563,19 @@
}
]
},
"flag_value": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_value"
},
{
"type": "SYMBOL",
"name": "val_string"
}
]
},
"redirection": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -10727,10 +10740,39 @@
"name": "long_flag_identifier"
}
]
},
{
"type": "SYMBOL",
"name": "long_flag_equals_value"
}
]
}
},
"long_flag_equals_value": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "--"
},
{
"type": "SYMBOL",
"name": "long_flag_identifier"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "long_flag_value"
}
]
},
"long_flag_value": {
"type": "SYMBOL",
"name": "_cmd_arg"
},
"unquoted": {
"type": "PREC_LEFT",
"value": -69,
Expand Down
Loading