Skip to content
Draft
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
4 changes: 2 additions & 2 deletions format.nu
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const script_path = path self .
@example "Format files (in-place replacement)" { format.nu foo.nu bar.nu }
@example "Path overriding" { format.nu -c /path/to/topiary-nushell foo.nu bar.nu }
def main [
--config_dir (-c): path # Root of the topiary-nushell repo, defaults to the parent directory of this script
...files: path # Files to format
--config_dir (-c): path, # Root of the topiary-nushell repo, defaults to the parent directory of this script
...files: path, # Files to format
]: [nothing -> nothing string -> string] {
let config_dir = $config_dir | default $script_path
$env.TOPIARY_CONFIG_FILE = ($config_dir | path join languages.ncl)
Expand Down
158 changes: 127 additions & 31 deletions languages/nu.scm
Original file line number Diff line number Diff line change
Expand Up @@ -141,33 +141,54 @@
) @append_indent_end
)

;; space/newline between parameters
(parameter_pipes
(
(parameter) @append_space
.
(parameter)
)?
) @append_space @append_spaced_softline
(parameter_pipes) @append_space @append_spaced_softline

(parameter_bracks
(parameter) @append_space
.
(parameter) @prepend_empty_softline
(parameter
flag_capsule: _ @prepend_space
)

(parameter_parens
(parameter) @append_space
.
(parameter) @prepend_empty_softline
;; Comma/newline between parameters
;; NOTE: Currently, the parameter node contains the comma separator,
;; as well as its doc comment. We want the comma to appear before the comment.
(parameter
"," @delete
)

(parameter
flag_capsule: _ @prepend_space
;; need to be explicit to avoid
;; commas between consecutive comments
[
(flag_capsule)
(param_completer)
(param_long_flag)
(param_opt)
(param_short_flag)
(param_type)
(param_value)
] @append_delimiter
.
(comment)
(#delimiter! ",")
)

(parameter
"," @delete
;; FIXME: comma after the last parameter for multi-line lists
;; https://github.com/nushell/tree-sitter-nu/issues/215
(
(parameter
(comment)? @do_nothing
) @append_delimiter
.
(parameter)
(#delimiter! ", ")
(#single_line_only!)
)

(
(parameter
(comment)? @do_nothing
) @append_delimiter @append_spaced_softline
(#delimiter! ",")
(#multi_line_only!)
)

;; attributes
Expand Down Expand Up @@ -287,9 +308,19 @@
(ctrl_match
"match" @append_space
scrutinee: _? @append_space
(match_arm)? @prepend_spaced_softline
(default_arm)? @prepend_spaced_softline
"}"? @prepend_spaced_softline
"{" @append_spaced_softline
"}" @prepend_spaced_softline
)

(ctrl_match
[
(match_arm)
(default_arm)
] @append_delimiter
.
(_)
(#delimiter! ", ")
(#single_line_only!)
)

(match_pattern "|" @prepend_spaced_softline @append_space)
Expand All @@ -313,27 +344,92 @@
file_path: _? @prepend_input_softline
) @prepend_input_softline

;; FIXME: comma after the last entry for multi-line expressions
;; A workaround given:
;; 1. https://github.com/nushell/tree-sitter-nu/issues/215
;; 2. https://github.com/tree-sitter/tree-sitter/discussions/3967
;; 3. Comments between entries should be kept unmoved
(list_body
entry: _ @append_space
entry: _ @append_delimiter
.
entry: _ @prepend_spaced_softline
entry: _
(#delimiter! ", ")
(#single_line_only!)
)

(record_body
entry: _ @append_space
entry: _ @append_delimiter
.
entry: _ @prepend_spaced_softline
entry: _
(#delimiter! ", ")
(#single_line_only!)
)

;; match_arm
(val_list
(list_body)
(list_body
entry: _ @append_delimiter
(#delimiter! ",")
(#multi_line_only!)
)

(list_body
entry: _ @append_begin_scope
.
rest: _ @prepend_spaced_softline
entry: _ @prepend_spaced_softline @prepend_end_scope
(#scope_id! "consecutive_scope")
(#multi_line_only!)
)

(record_body
entry: _ @append_delimiter
(#delimiter! ",")
(#multi_line_only!)
)

(record_body
entry: _ @append_begin_scope
.
entry: _ @prepend_spaced_softline @prepend_end_scope
(#scope_id! "consecutive_scope")
(#multi_line_only!)
)

(val_table
row: _ @append_delimiter
.
row: _
(#delimiter! ", ")
(#single_line_only!)
)

(val_table
row: _ @prepend_spaced_softline
row: _ @append_delimiter @prepend_spaced_softline
(#delimiter! ",")
(#multi_line_only!)
)

(ctrl_match
[
(match_arm (block)? @do_nothing)
(default_arm (block)? @do_nothing)
] @append_delimiter
(#delimiter! ",")
(#multi_line_only!)
)

(ctrl_match
[
(match_arm)
(default_arm)
] @prepend_spaced_softline
(#multi_line_only!)
)

;; match_arm
(val_list
(list_body) @append_delimiter
.
rest: _ @prepend_spaced_softline
(#delimiter! ",")
)

;; type notation
Expand Down
38 changes: 28 additions & 10 deletions run_test.nu
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,33 @@ let files = glob test/input_*.nu
$env.TOPIARY_CONFIG_FILE = (pwd | path join languages.ncl)
$env.TOPIARY_LANGUAGE_DIR = (pwd | path join languages)

for f in $files {
print $"(ansi green)Testing: (ansi yellow)($f)(ansi reset)"
cp $f $temp_file
topiary format $temp_file
let expected_file = $f | str replace --regex '/input_' '/expected_'
try {
assert ((open $temp_file) == (open $expected_file))
} catch {
delta $temp_file $expected_file
def main [
--update (-u), # force updating `expected_xxx` files
] {
for f in $files {
print $"(ansi green)Testing: (ansi yellow)($f)(ansi reset)"
cp $f $temp_file
topiary format $temp_file
let expected_file = $f | str replace --regex '/input_' '/expected_'
try {
if ((which delta) | is-empty) {
assert ((open $temp_file) == (open $expected_file))
} else {
delta $temp_file $expected_file --paging never
}
} catch {
if $update and (
[yes, no]
| input list --index --fuzzy $"Update expected results in file (
$expected_file
| path basename
)?"
) == 0 {
cp $temp_file $expected_file
} else {
exit 1
}
}
rm $temp_file
}
rm $temp_file
}
6 changes: 3 additions & 3 deletions test/expected_attribute.nu
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def test [] { }
path add {linux: "foo"}
}
export def --env "path add" [
--ret (-r) # return $env.PATH, useful in pipelines to avoid scoping.
--append (-a) # append to $env.PATH instead of prepending to.
p
--ret (-r), # return $env.PATH, useful in pipelines to avoid scoping.
--append (-a), # append to $env.PATH instead of prepending to.
p,
] { }

@search-terms multiply times
Expand Down
13 changes: 7 additions & 6 deletions test/expected_comment.nu
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
# multiline
def foo_bar [
# comment at [
foo: string # comment for arg
bar: int # another comment
foo: string, # comment for arg
# 2nd comment for arg foo
bar: int, # another comment
] {
# comment at {
# comment in body
[
foo # comment in list
foo, # comment in list
# another comment
bar
bar,
];
{
foo: foo # comment in record
foo: foo, # comment in record
# another comment
bar: bar
bar: bar,
} # comment at }
(
# comment at (
Expand Down
22 changes: 11 additions & 11 deletions test/expected_ctrl.nu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# for
for i in [1 2 3] {
for i in [1, 2, 3] {
# if
if (true or false) {
print "break"; break # break
Expand All @@ -14,25 +14,25 @@ alias ll = ls -l # alias comment
ls | where $in.name == 'foo'
| where {|e| $e.item.name !~ $'^($e.index + 1)' }
# match
let foo = {name: 'bar' count: 7}
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 }
}
match $foo {
[a b c] => 0
a | b | c => 42
[a, b, c] => 0,
a | b | c => 42,
a
| b
| c => 42
{$bar $baz} => $baz
| c => 42,
{$bar, $baz} => $baz,
# ..rest pattern
[$x ..$y] if $x == 1 => { 'good list' }
[$x, ..$y] if $x == 1 => { 'good list' }
[..$y] => { $y }
}
match $foo { null => { return "default" } $val => $val }
match $foo { null => { return "default" }, $val => $val }
# while
mut x = 0; while $x < 10 { $x = $x + 1 }; $x # while comment
# loop
Expand All @@ -44,6 +44,6 @@ loop {
try {
# error
error make -u {
msg: 'Some error info'
msg: 'Some error info',
}
}; print 'Resuming'
Loading