Add syntax highlighting for nu scripts

This commit is contained in:
Mikayla 2023-09-09 14:44:52 -07:00
parent ef03e206d6
commit 2be34ef254
No known key found for this signature in database
8 changed files with 311 additions and 63 deletions

2
Cargo.lock generated
View File

@ -8409,7 +8409,7 @@ dependencies = [
[[package]]
name = "tree-sitter-nu"
version = "0.0.1"
source = "git+https://github.com/mikayla-maki/tree-sitter-nu/?rev=6cf9ee39ceb3da79501de7646b10e5e1da800ab8#6cf9ee39ceb3da79501de7646b10e5e1da800ab8"
source = "git+https://github.com/nushell/tree-sitter-nu?rev=786689b0562b9799ce53e824cb45a1a2a04dc673#786689b0562b9799ce53e824cb45a1a2a04dc673"
dependencies = [
"cc",
"tree-sitter",

View File

@ -141,7 +141,7 @@ tree-sitter-racket = { git = "https://github.com/zed-industries/tree-sitter-rack
tree-sitter-yaml = { git = "https://github.com/zed-industries/tree-sitter-yaml", rev = "f545a41f57502e1b5ddf2a6668896c1b0620f930"}
tree-sitter-lua = "0.0.14"
tree-sitter-nix = { git = "https://github.com/nix-community/tree-sitter-nix", rev = "66e3e9ce9180ae08fc57372061006ef83f0abde7" }
tree-sitter-nu = { git = "https://github.com/mikayla-maki/tree-sitter-nu/", rev = "6cf9ee39ceb3da79501de7646b10e5e1da800ab8"}
tree-sitter-nu = { git = "https://github.com/nushell/tree-sitter-nu", rev = "786689b0562b9799ce53e824cb45a1a2a04dc673"}
[patch.crates-io]
tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "35a6052fbcafc5e5fc0f9415b8652be7dcaf7222" }

View File

@ -0,0 +1,4 @@
("(" @open ")" @close)
("[" @open "]" @close)
("{" @open "}" @close)
(parameter_pipes "|" @open "|" @close)

View File

@ -0,0 +1,302 @@
;;; ---
;;; keywords
[
"def"
"def-env"
"alias"
"export-env"
"export"
"extern"
"module"
"let"
"let-env"
"mut"
"const"
"hide-env"
"source"
"source-env"
"overlay"
"register"
"loop"
"while"
"error"
"do"
"if"
"else"
"try"
"catch"
"match"
"break"
"continue"
"return"
] @keyword
(hide_mod "hide" @keyword)
(decl_use "use" @keyword)
(ctrl_for
"for" @keyword
"in" @keyword
)
(overlay_list "list" @keyword)
(overlay_hide "hide" @keyword)
(overlay_new "new" @keyword)
(overlay_use
"use" @keyword
"as" @keyword
)
(ctrl_error "make" @keyword)
;;; ---
;;; literals
(val_number) @constant
(val_duration
unit: [
"ns" "µs" "us" "ms" "sec" "min" "hr" "day" "wk"
] @variable
)
(val_filesize
unit: [
"b" "B"
"kb" "kB" "Kb" "KB"
"mb" "mB" "Mb" "MB"
"gb" "gB" "Gb" "GB"
"tb" "tB" "Tb" "TB"
"pb" "pB" "Pb" "PB"
"eb" "eB" "Eb" "EB"
"zb" "zB" "Zb" "ZB"
"kib" "kiB" "kIB" "kIb" "Kib" "KIb" "KIB"
"mib" "miB" "mIB" "mIb" "Mib" "MIb" "MIB"
"gib" "giB" "gIB" "gIb" "Gib" "GIb" "GIB"
"tib" "tiB" "tIB" "tIb" "Tib" "TIb" "TIB"
"pib" "piB" "pIB" "pIb" "Pib" "PIb" "PIB"
"eib" "eiB" "eIB" "eIb" "Eib" "EIb" "EIB"
"zib" "ziB" "zIB" "zIb" "Zib" "ZIb" "ZIB"
] @variable
)
(val_binary
[
"0b"
"0o"
"0x"
] @constant
"[" @punctuation.bracket
digit: [
"," @punctuation.delimiter
(hex_digit) @constant
]
"]" @punctuation.bracket
) @constant
(val_bool) @constant.builtin
(val_nothing) @constant.builtin
(val_string) @string
(val_date) @constant
(inter_escape_sequence) @constant
(escape_sequence) @constant
(val_interpolated [
"$\""
"$\'"
"\""
"\'"
] @string)
(unescaped_interpolated_content) @string
(escaped_interpolated_content) @string
(expr_interpolated ["(" ")"] @variable)
;;; ---
;;; operators
(expr_binary [
"+"
"-"
"*"
"/"
"mod"
"//"
"++"
"**"
"=="
"!="
"<"
"<="
">"
">="
"=~"
"!~"
"and"
"or"
"xor"
"bit-or"
"bit-xor"
"bit-and"
"bit-shl"
"bit-shr"
"in"
"not-in"
"starts-with"
"ends-with"
] @operator)
(expr_binary opr: ([
"and"
"or"
"xor"
"bit-or"
"bit-xor"
"bit-and"
"bit-shl"
"bit-shr"
"in"
"not-in"
"starts-with"
"ends-with"
]) @keyword)
(where_command [
"+"
"-"
"*"
"/"
"mod"
"//"
"++"
"**"
"=="
"!="
"<"
"<="
">"
">="
"=~"
"!~"
"and"
"or"
"xor"
"bit-or"
"bit-xor"
"bit-and"
"bit-shl"
"bit-shr"
"in"
"not-in"
"starts-with"
"ends-with"
] @operator)
(assignment [
"="
"+="
"-="
"*="
"/="
"++="
] @operator)
(expr_unary ["not" "-"] @operator)
(val_range [
".."
"..="
"..<"
] @operator)
["=>" "=" "|"] @operator
[
"o>" "out>"
"e>" "err>"
"e+o>" "err+out>"
"o+e>" "out+err>"
] @special
;;; ---
;;; punctuation
[
","
";"
] @punctuation.delimiter
(param_short_flag "-" @punctuation.delimiter)
(param_long_flag ["--"] @punctuation.delimiter)
(long_flag ["--"] @punctuation.delimiter)
(param_rest "..." @punctuation.delimiter)
(param_type [":"] @punctuation.special)
(param_value ["="] @punctuation.special)
(param_cmd ["@"] @punctuation.special)
(param_opt ["?"] @punctuation.special)
[
"(" ")"
"{" "}"
"[" "]"
] @punctuation.bracket
(val_record
(record_entry ":" @punctuation.delimiter))
;;; ---
;;; identifiers
(param_rest
name: (_) @variable)
(param_opt
name: (_) @variable)
(parameter
param_name: (_) @variable)
(param_cmd
(cmd_identifier) @string)
(param_long_flag) @variable
(param_short_flag) @variable
(short_flag) @variable
(long_flag) @variable
(scope_pattern [(wild_card) @function])
(cmd_identifier) @function
(command
"^" @punctuation.delimiter
head: (_) @function
)
"where" @function
(path
["." "?"] @punctuation.delimiter
) @variable
(val_variable
"$" @operator
[
(identifier) @variable
"in" @type.builtin
"nu" @type.builtin
"env" @type.builtin
"nothing" @type.builtin
] ; If we have a special styling, use it here
)
;;; ---
;;; types
(flat_type) @type.builtin
(list_type
"list" @type
["<" ">"] @punctuation.bracket
)
(collection_type
["record" "table"] @type
"<" @punctuation.bracket
key: (_) @variable
["," ":"] @punctuation.delimiter
">" @punctuation.bracket
)
(shebang) @comment
(comment) @comment

View File

@ -0,0 +1,3 @@
(_ "[" "]" @end) @indent
(_ "{" "}" @end) @indent
(_ "(" ")" @end) @indent

View File

@ -1,61 +0,0 @@
(string) @string
(type) @type
(value_path) @variable
(comment) @comment
(number_literal) @number
(range from: (number_literal) @number)
(range to: (number_literal) @number)
(command cmd_name: (identifier) @function)
(function_definition func_name: (identifier) @function)
[
(variable_declaration name: (identifier))
(parameter (identifier))
(flag (flag_name))
(flag (flag_shorthand_name))
(record_entry entry_name: (identifier))
(block_args block_param: (identifier))
] @property
; (parameter (identifier) @variable.parameter) ; -- alternative highlighting group?
(cmd_invocation) @embedded
((identifier) @constant
(.match? @constant "^[A-Z][A-Z\\d_]*$"))
[
"if"
"else"
"not"
"let"
"def"
"def-env"
"export"
"true"
"false"
"and"
"or"
] @keyword
[
; "/" Not making / an operator may lead to better highlighting?
"$"
"|"
"+"
"-"
"*"
"="
"!="
"and"
"or"
"=="
">"
] @operator
["."
","
";"
] @delimiter