Skip to content
Closed
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
58 changes: 52 additions & 6 deletions languages/nu.scm
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,22 @@
scrutinee: _? @append_space
(match_arm)? @prepend_spaced_softline
(default_arm)? @prepend_spaced_softline
"}"? @prepend_spaced_softline
)

; append delimiter after match_arms in single line match expressions
(ctrl_match
"match"
scrutinee: _
(_) @append_delimiter
.
(_)
(#delimiter! ",")
(#single_line_only!)
)

(match_pattern "|" @prepend_spaced_softline @append_space )

;; data structures
(command_list
[
Expand All @@ -280,28 +294,60 @@
) @prepend_input_softline

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

;; match_arm
(val_list
entry: _ @append_space
entry: (_) @append_delimiter
;; unlike list expressions, list patterns include commas as anonymous nodes.
;; this is required to avoid adding multiple commas
","* @do_nothing
entry: (_)
(#delimiter! ",")
)
(val_list
.
entry: _ @prepend_spaced_softline
entry: (_)
entry: (_) @prepend_spaced_softline
)
Comment on lines 310 to 322
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(val_list
entry: _ @append_space
entry: (_) @append_delimiter
;; unlike list expressions, list patterns include commas as anonymous nodes.
;; this is required to avoid adding multiple commas
","* @do_nothing
entry: (_)
(#delimiter! ",")
)
(val_list
.
entry: _ @prepend_spaced_softline
entry: (_)
entry: (_) @prepend_spaced_softline
)
(val_list
entry: (_) @append_delimiter
;; unlike list expressions, list patterns include commas as anonymous nodes.
;; this is required to avoid adding multiple commas
","* @do_nothing
.
entry: (_)
(#delimiter! ",")
)
(val_list
entry: (_) @append_begin_scope
.
entry: (_) @prepend_spaced_softline
(#scope_id! "consecutive_scope")
)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost forgot, there's a hidden ts issue here:

;; TODO: pseudo scope_id to cope with
;; https://github.com/tree-sitter/tree-sitter/discussions/3967

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bahex Here's a hack to workaround the case in your previous comment, sorry I accidentally deleted it.

And the GitHub webpage is not clever enough to let me commit it. Maybe you'll have to manually do it (same for the val_record) later when we come to a conclusion of the desired behavior.

Copy link
Contributor

@mkatychev mkatychev Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blindFS you can always suggest a git patch:

```patch
# paste git diff output here
```

tweag/topiary#921 (comment)


(val_table
row: _ @prepend_spaced_softline
)

(val_record
entry: (record_entry) @append_space
entry: (_) @append_delimiter
;; unlike record expressions, record patterns include commas as anonymous nodes.
;; this is required to avoid adding multiple commas
","* @do_nothing
entry: (_)
(#delimiter! ",")
)
(val_record
.
entry: (record_entry) @prepend_spaced_softline
;; record patterns can use `{ $foo }` as a shorthand for `{ foo: $foo }`
;; so we can's use record_entry here
entry: (_)
entry: (_) @prepend_spaced_softline
)

(record_body
entry: (record_entry) @append_delimiter
.
entry: (record_entry)
(#delimiter! ",")
(#single_line_only!)
)
(record_body
entry: (record_entry) @append_space
.
Expand Down
14 changes: 9 additions & 5 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,16 +14,20 @@ 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)
# record shorthand
{$name, $count} => $'($name): ($count)'
_ => { exit 0 }
}
match $foo {
[a b c] => 0
[a, b, c] => 0
a | b | c => 42
}
match $foo { null => { return "default" }, 1 => "one", 2 => "two", $val => $val }
# while
mut x = 0; while $x < 10 { $x = $x + 1 }; $x # while comment
# loop
Expand Down
10 changes: 5 additions & 5 deletions test/expected_data.nu
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const config = {
{
name: ws_listener
pos: left
events: [aerospace_workspace_change space_windows_change]
events: [aerospace_workspace_change, space_windows_change]
args: {
updates: on
drawing: off
Expand All @@ -48,7 +48,7 @@ export const config = {
{
name: front_app
pos: left
events: [front_app_switched aerospace_mode_change]
events: [front_app_switched, aerospace_mode_change]
args: {
label.color: $colors.black
icon.color: $colors.black
Expand Down Expand Up @@ -112,7 +112,7 @@ export const config = {
}
{
name: battery
events: [system_woke power_source_change]
events: [system_woke, power_source_change]
args: {
update_freq: 120
script: '{}/battery.nu'
Expand Down Expand Up @@ -247,7 +247,7 @@ export const config = {
]
}

const table = [[a b]; [1 2] [2 [4 4]]]
const table_no_row = [[a b]; ]
const table = [[a, b]; [1, 2] [2, [4, 4]]]
const table_no_row = [[a, b]; ]

mut foo: record<"a", b, "c": int, d: list<any>> = {}
10 changes: 5 additions & 5 deletions test/expected_exe.nu
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def modify_args_per_workspace [
)
let extra = (
if $sid == $focused_sid {
{highlight: on border_color: $colors.green}
{highlight: on, border_color: $colors.green}
} else {
{highlight: off border_color: $colors.fg}
{highlight: off, border_color: $colors.fg}
}
)

['--set' $"space.($sid)"]
['--set', $"space.($sid)"]
| append (
if (($icons | is-empty) and ($sid != $focused_sid)) {
[
Expand Down Expand Up @@ -58,14 +58,14 @@ def workspace_modification_args [
let ids_to_modify = (
if ($last_sid | is-empty) { (aerospace list-workspaces --all | lines) }
else {
[$focused_sid $last_sid]
[$focused_sid, $last_sid]
}
)
$ids_to_modify
| uniq
| each { modify_args_per_workspace $in $focused_sid }
| flatten
| append ["--set" $name $"label=($focused_sid)"]
| append ["--set", $name, $"label=($focused_sid)"]
}

# remained for other possible signals
Expand Down
4 changes: 4 additions & 0 deletions test/input_ctrl.nu
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ match $foo {
{ name: 'bar' count: $it } if $it < 5 => ($it + 3), # match arm comment
# match comment
{ name: 'bar' count: $it } if not ($it >= 5) => ($it + 7),
# record shorthand
{ $name $count } => $'($name): ($count)',
_ => {exit 0}
}
match $foo {
[ a b c] => 0
a|b|c => 42
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test for a multiline pattern? But I don't even know if that's valid...

}
match $foo {null => {return "default"} 1 => "one" 2 => "two" $val => $val}
# while
mut x = 0; while $x < 10 { $x = $x + 1 }; $x # while comment
# loop
Expand Down