From 5077653485fb7bfda129464dbb040481e9d34f30 Mon Sep 17 00:00:00 2001 From: blindfs Date: Sun, 15 Dec 2024 12:36:03 +0800 Subject: [PATCH 1/2] feat: adapted to the latest parser --- languages.ncl | 11 ++-- languages/{nushell.scm => nu.scm} | 99 ++++++++++++++++--------------- test/expected_command.nu | 6 +- test/expected_ctrl.nu | 24 ++++---- test/expected_decl.nu | 7 ++- test/expected_exe.nu | 7 ++- test/input_decl.nu | 7 ++- test/input_exe.nu | 1 + 8 files changed, 88 insertions(+), 74 deletions(-) rename languages/{nushell.scm => nu.scm} (80%) diff --git a/languages.ncl b/languages.ncl index 7d46e8c..bcec7b3 100644 --- a/languages.ncl +++ b/languages.ncl @@ -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", }, }, }, diff --git a/languages/nushell.scm b/languages/nu.scm similarity index 80% rename from languages/nushell.scm rename to languages/nu.scm index edccae4..b7cc4a8 100644 --- a/languages/nushell.scm +++ b/languages/nu.scm @@ -2,7 +2,6 @@ [ (cell_path) (comment) - (long_flag_equals_value) (shebang) (unquoted) (val_binary) @@ -24,16 +23,18 @@ ";" ] @append_space +;; TODO: temp solution for the whitespace issue [ - "->" "=" +] @prepend_space + +[ + "->" "=>" "alias" "as" - "break" "catch" "const" - "continue" "def" "do" "else" @@ -52,7 +53,6 @@ "match" "module" "mut" - "not" "new" "overlay" "return" @@ -62,24 +62,35 @@ "use" "where" "while" - (comment) ] @prepend_space @append_space -;; add spaces to left & right sides of operators -(pipe_element +(pipeline "|" @prepend_space @append_space @prepend_empty_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_space @prepend_space +) + +(expr_parenthesized + (pipeline + (pipe_element + (expr_binary + opr: _ @append_empty_softline + ) + ) + ) +) + +(expr_binary + (expr_binary + opr: _ @append_empty_softline + ) ) (assignment - lhs: _ @append_space - opr: _ - rhs: _ @prepend_space + opr: _ @prepend_space ) (where_command @@ -106,17 +117,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 . @@ -172,26 +185,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 of duplicated rules +(nu_script + (_) + (_) @prepend_input_softline +) + +(block + (_) + (_) @prepend_input_softline +) + +(val_closure + (_) + (_) @prepend_input_softline +) ;; control flow (ctrl_if @@ -217,30 +228,24 @@ (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_spread: _? @prepend_space arg: _? @prepend_space redir: (_ file_path: _? @prepend_space )? @prepend_space ) -(command - arg_str: _ - . - (expr_parenthesized)? @do_nothing -) - (list_body entry: _ @append_space . diff --git a/test/expected_command.nu b/test/expected_command.nu index 512d9e5..9d0015b 100644 --- a/test/expected_command.nu +++ b/test/expected_command.nu @@ -1,7 +1,7 @@ # local command -ls | get -i name -| length; -ls # multiline command +ls +| get -i name +| length; ls # multiline command | length # external command ^git add ( diff --git a/test/expected_ctrl.nu b/test/expected_ctrl.nu index 3178621..757aaaf 100644 --- a/test/expected_ctrl.nu +++ b/test/expected_ctrl.nu @@ -2,37 +2,37 @@ 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' +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' diff --git a/test/expected_decl.nu b/test/expected_decl.nu index 9772be1..1f209e6 100644 --- a/test/expected_decl.nu +++ b/test/expected_decl.nu @@ -21,7 +21,8 @@ with-env { ABC: 'hello' } { let cls = {|foo bar baz| ( $foo + - $bar + $baz + $bar + + $baz ) } @@ -38,6 +39,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 { diff --git a/test/expected_exe.nu b/test/expected_exe.nu index fcb45e8..72ef7fa 100644 --- a/test/expected_exe.nu +++ b/test/expected_exe.nu @@ -3,6 +3,7 @@ use constants.nu [ colors get_icon_by_app_name + "foo bar" ] const animate_frames = 30 @@ -13,9 +14,11 @@ def modify_args_per_workspace [ ]: nothing -> list { let icons = ( aerospace list-windows --workspace $sid --json - | from json | get app-name + | from json + | get app-name | each {$in | get_icon_by_app_name} - | uniq | sort + | uniq + | sort | str join ' ' ) let extra = ( diff --git a/test/input_decl.nu b/test/input_decl.nu index f6c66f6..bdbc282 100644 --- a/test/input_decl.nu +++ b/test/input_decl.nu @@ -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 { diff --git a/test/input_exe.nu b/test/input_exe.nu index 7d1a9b4..986b93e 100644 --- a/test/input_exe.nu +++ b/test/input_exe.nu @@ -3,6 +3,7 @@ use constants.nu [ colors get_icon_by_app_name + "foo bar" ] const animate_frames = 30 From ac17f646fe17865d732736e785b6dab09db4737c Mon Sep 17 00:00:00 2001 From: blindfs Date: Sun, 15 Dec 2024 13:42:40 +0800 Subject: [PATCH 2/2] fix: assignment with raw_string --- languages/nu.scm | 73 ++++++++++++++++++++++------------------ test/expected_command.nu | 3 +- test/expected_ctrl.nu | 3 +- test/expected_decl.nu | 3 +- test/expected_exe.nu | 6 ++-- test/input_string.nu | 3 +- 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/languages/nu.scm b/languages/nu.scm index b7cc4a8..5d7fa02 100644 --- a/languages/nu.scm +++ b/languages/nu.scm @@ -18,16 +18,46 @@ ;; keep empty lines (_) @allow_blank_line_before +;; TODO: temp workaround for the whitespace issue [ ":" ";" + "do" + "if" + "match" + "try" + "while" ] @append_space -;; TODO: temp solution for the whitespace issue [ "=" + (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 +) + [ "->" "=>" @@ -36,7 +66,6 @@ "catch" "const" "def" - "do" "else" "error" "export" @@ -45,12 +74,10 @@ "for" "hide" "hide-env" - "if" "in" "let" "loop" "make" - "match" "module" "mut" "new" @@ -58,35 +85,17 @@ "return" "source" "source-env" - "try" "use" "where" - "while" ] @prepend_space @append_space (pipeline - "|" @prepend_space @append_space @prepend_empty_softline + "|" @append_space @prepend_input_softline ) ;; add spaces to left & right sides of operators (expr_binary - opr: _ @append_space @prepend_space -) - -(expr_parenthesized - (pipeline - (pipe_element - (expr_binary - opr: _ @append_empty_softline - ) - ) - ) -) - -(expr_binary - (expr_binary - opr: _ @append_empty_softline - ) + opr: _ @append_input_softline @prepend_input_softline ) (assignment @@ -94,7 +103,7 @@ ) (where_command - opr: _ @prepend_space @append_space + opr: _ @append_input_softline @prepend_input_softline ) ;; special flags @@ -188,7 +197,7 @@ ;; new-line (comment) @prepend_input_softline @append_hardline -;; TODO: substantial slow down of duplicated rules +;; TODO: substantial slow down by duplicated rules (nu_script (_) (_) @prepend_input_softline @@ -237,13 +246,13 @@ ) (command - flag: _? @prepend_space - arg_str: _? @prepend_space - arg_spread: _? @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 + file_path: _? @prepend_input_softline + )? @prepend_input_softline ) (list_body diff --git a/test/expected_command.nu b/test/expected_command.nu index 9d0015b..2afbcac 100644 --- a/test/expected_command.nu +++ b/test/expected_command.nu @@ -1,6 +1,5 @@ # local command -ls -| get -i name +ls | get -i name | length; ls # multiline command | length # external command diff --git a/test/expected_ctrl.nu b/test/expected_ctrl.nu index 757aaaf..216d240 100644 --- a/test/expected_ctrl.nu +++ b/test/expected_ctrl.nu @@ -11,8 +11,7 @@ for i in [1 2 3] { # alias alias ll = ls -l # alias comment # where -ls -| where $in.name == 'foo' +ls | where $in.name == 'foo' | where {|e| $e.item.name !~ $'^($e.index + 1)'} # match let foo = { name: 'bar' count: 7 } diff --git a/test/expected_decl.nu b/test/expected_decl.nu index 1f209e6..203dbea 100644 --- a/test/expected_decl.nu +++ b/test/expected_decl.nu @@ -21,8 +21,7 @@ with-env { ABC: 'hello' } { let cls = {|foo bar baz| ( $foo + - $bar + - $baz + $bar + $baz ) } diff --git a/test/expected_exe.nu b/test/expected_exe.nu index 72ef7fa..cc85a9e 100644 --- a/test/expected_exe.nu +++ b/test/expected_exe.nu @@ -14,11 +14,9 @@ def modify_args_per_workspace [ ]: nothing -> list { let icons = ( aerospace list-windows --workspace $sid --json - | from json - | get app-name + | from json | get app-name | each {$in | get_icon_by_app_name} - | uniq - | sort + | uniq | sort | str join ' ' ) let extra = ( diff --git a/test/input_string.nu b/test/input_string.nu index 9da9d99..9f8293f 100644 --- a/test/input_string.nu +++ b/test/input_string.nu @@ -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)') )