Skip to content

Commit

Permalink
Merge pull request #5 from blindFS/dev
Browse files Browse the repository at this point in the history
feat: adapted to the latest parser
  • Loading branch information
blindFS authored Dec 15, 2024
2 parents b6d8484 + ac17f64 commit 6f0657d
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 82 deletions.
11 changes: 4 additions & 7 deletions languages.ncl
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
languages = {
nushell = {
nu = {
extensions = ["nu"],
grammar = {
source.git = {
git = "https://github.com/nushell/tree-sitter-nu.git",
rev = "7e0f16f608a9e804fae61430ade734f9f849fb80",
},
symbol = "tree_sitter_nu",
grammar.source.git = {
git = "https://github.com/nushell/tree-sitter-nu.git",
rev = "f85d2481616537d1d54894bc278d64b24581ea5c",
},
},
},
Expand Down
132 changes: 73 additions & 59 deletions languages/nushell.scm → languages/nu.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
[
(cell_path)
(comment)
(long_flag_equals_value)
(shebang)
(unquoted)
(val_binary)
Expand All @@ -19,23 +18,54 @@
;; keep empty lines
(_) @allow_blank_line_before

;; TODO: temp workaround for the whitespace issue
[
":"
";"
"do"
"if"
"match"
"try"
"while"
] @append_space

[
"->"
"="
(match_guard)
] @prepend_space

(assignment
opr: _
rhs:
(pipeline
(pipe_element
(val_string
(raw_string_begin)
)
)
) @prepend_space
)

(
"="
.
(pipeline
(pipe_element
(val_string
(raw_string_begin)
)
)
) @prepend_space
)

[
"->"
"=>"
"alias"
"as"
"break"
"catch"
"const"
"continue"
"def"
"do"
"else"
"error"
"export"
Expand All @@ -44,46 +74,36 @@
"for"
"hide"
"hide-env"
"if"
"in"
"let"
"loop"
"make"
"match"
"module"
"mut"
"not"
"new"
"overlay"
"return"
"source"
"source-env"
"try"
"use"
"where"
"while"
(comment)
] @prepend_space @append_space

;; add spaces to left & right sides of operators
(pipe_element
"|" @prepend_space @append_space @prepend_empty_softline
(pipeline
"|" @append_space @prepend_input_softline
)

;; add spaces to left & right sides of operators
(expr_binary
lhs: _ @append_space
opr: _ @append_spaced_softline ; multiline in expr_parenthesized
rhs: _ @prepend_space
opr: _ @append_input_softline @prepend_input_softline
)

(assignment
lhs: _ @append_space
opr: _
rhs: _ @prepend_space
opr: _ @prepend_space
)

(where_command
opr: _ @prepend_space @append_space
opr: _ @append_input_softline @prepend_input_softline
)

;; special flags
Expand All @@ -106,17 +126,19 @@
[
"["
"("
"...("
"...["
"...{"
] @append_indent_start @append_empty_softline

"{" @append_indent_start

[
"]"
"}"
")"
] @prepend_indent_end @prepend_empty_softline

; change line happens after || for closure
;;; change line happens after || for closure
"{" @append_indent_start
(
"{" @append_empty_softline
.
Expand Down Expand Up @@ -172,26 +194,24 @@
(block)? @prepend_space
)

;; forced new-line
[
(decl_def)
(decl_export)
(decl_extern)
(shebang)
] @append_hardline
;; new-line
(comment) @prepend_input_softline @append_hardline

[
(comment)
(pipeline)
(overlay_use)
(overlay_hide)
(overlay_list)
(overlay_new)
(hide_env)
(hide_mod)
(decl_use)
(stmt_source)
] @append_empty_softline
;; TODO: substantial slow down by duplicated rules
(nu_script
(_)
(_) @prepend_input_softline
)

(block
(_)
(_) @prepend_input_softline
)

(val_closure
(_)
(_) @prepend_input_softline
)

;; control flow
(ctrl_if
Expand All @@ -217,28 +237,22 @@
(default_arm)? @prepend_spaced_softline
)

(match_guard
"if" @prepend_space @append_space
)

;; data structures
(command_list
[(cmd_identifier) (val_string)] @append_space @prepend_spaced_softline
[
(cmd_identifier)
(val_string)
] @append_space @prepend_spaced_softline
)

(command
flag: _? @prepend_space
arg_str: _? @prepend_space
arg: _? @prepend_space
flag: _? @prepend_input_softline
arg_str: _? @prepend_input_softline
arg_spread: _? @prepend_input_softline
arg: _? @prepend_input_softline
redir: (_
file_path: _? @prepend_space
)? @prepend_space
)

(command
arg_str: _
.
(expr_parenthesized)? @do_nothing
file_path: _? @prepend_input_softline
)? @prepend_input_softline
)

(list_body
Expand Down
3 changes: 1 addition & 2 deletions test/expected_command.nu
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# local command
ls | get -i name
| length;
ls # multiline command
| length; ls # multiline command
| length
# external command
^git add (
Expand Down
21 changes: 10 additions & 11 deletions test/expected_ctrl.nu
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@
for i in [1 2 3] {
# if
if (true or false) {
print "break";
break # break
print "break"; break # break
} else if not false {
print "continue";
continue # continue
print "continue"; continue # continue
}
return 1 # return
} # alias
}
# alias
alias ll = ls -l # alias comment
# where
ls | where $in.name == 'foo'
| where {|e| $e.item.name !~ $'^($e.index + 1)'}
# match
let foo = { name: 'bar' count: 7 }
match $foo {
{ name: 'bar' count: $it } if $it < 5 => ($it + 3), # match arm comment
{ name: 'bar' count: $it } if $it < 5 => ($it + 3) # match arm comment
# match comment
{ name: 'bar' count: $it } if not ($it >= 5) => ($it + 7),
{ name: 'bar' count: $it } if not ($it >= 5) => ($it + 7)
_ => {exit 0}
}
# while
mut x = 0; while $x < 10 {$x = $x + 1}; $x # while comment
# loop
loop {
if $x > 10 { break };
if $x > 10 {break};
$x = $x + 1
} # try
}
# try
try {
# error
error make -u {
msg: 'Some error info'
}
};
print 'Resuming'
}; print 'Resuming'
4 changes: 4 additions & 0 deletions test/expected_decl.nu
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def "hi there" [where: string]: nothing -> string {
# decl_use
use greetings.nu hello
export use greetings.nu *
use module [ foo bar ]
use module [ "foo" "bar" ]
use module [ foo "bar" ]
use module [ "foo" bar ]

# decl_module
module greetings {
Expand Down
1 change: 1 addition & 0 deletions test/expected_exe.nu
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use constants.nu [
colors
get_icon_by_app_name
"foo bar"
]

const animate_frames = 30
Expand Down
7 changes: 5 additions & 2 deletions test/input_decl.nu
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ $env.hello = 'hello'
}

# decl_def
def "hi there" [where: string]:
nothing -> string {
def "hi there" [where: string]: nothing -> string {
$"hi ($where)!"
}

# decl_use
use greetings.nu hello
export use greetings.nu *
use module [foo bar]
use module ["foo" "bar"]
use module [foo "bar"]
use module ["foo" bar]

# decl_module
module greetings {
Expand Down
1 change: 1 addition & 0 deletions test/input_exe.nu
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use constants.nu [
colors
get_icon_by_app_name
"foo bar"
]

const animate_frames = 30
Expand Down
3 changes: 2 additions & 1 deletion test/input_string.nu
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ const hybrid_help_cmd = ("Multiline
string" +
($external_tldr_cmd | str replace '{}' '(foo)') +
"another multiline
string" + ($help_preview_cmd | str replace '{}' '(bar)')
string" +
($help_preview_cmd | str replace '{}' '(bar)')
)

0 comments on commit 6f0657d

Please sign in to comment.