diff --git a/README.md b/README.md index 0a5123c..f1f81b4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ## Status -* Supposed to work well with all language features of nushell v0.106 +* Supposed to work well with all language features of nushell v0.107 * Except for some known issues of `tree-sitter-nu` > [!NOTE] @@ -124,6 +124,24 @@ cat foo.nu | topiary format --language nu +### Locally Disable Formatting for Certain Expression + +If you don't like the formatted output of certain parts of your code, +you can choose to disable it locally with a preceding `topiary: disable` comment: + +```nushell +... +# topiary: disable +let foo = [foo, bar + baz, ] +... +``` + +This will keep the let assignment as it is while formatting the rest of the code. + +> [!NOTE] +> We do recommend reporting code style issues before resorting to this workaround. + ## Editor Integration
diff --git a/languages/nu.scm b/languages/nu.scm index 667e64a..b6306c6 100644 --- a/languages/nu.scm +++ b/languages/nu.scm @@ -18,6 +18,9 @@ (path) ] @leaf +;; TODO: new feature of the next topiary release +;; (unescaped_interpolated_content) @keep_whitespaces + ;; keep empty lines (_) @allow_blank_line_before @@ -188,16 +191,39 @@ (#scope_id! "consecutive_scope") ) +(val_closure + (_) @append_begin_scope + . + (_) @prepend_end_scope @prepend_input_softline + (#scope_id! "consecutive_scope") +) + (block "{" @append_space "}" @prepend_space ) +;; HACK: temporarily disable formatting after special comment +;; abuse capture `@do_nothing` for the predicate +(nu_script + (comment) @do_nothing + . + (_) @leaf + (#match? @do_nothing "topiary: disable") +) + +(block + (comment) @do_nothing + . + (_) @leaf + (#match? @do_nothing "topiary: disable") +) + (val_closure - (_) @append_begin_scope + (comment) @do_nothing . - (_) @prepend_end_scope @prepend_input_softline - (#scope_id! "consecutive_scope") + (_) @leaf + (#match? @do_nothing "topiary: disable") ) (val_closure diff --git a/test/expected_disable.nu b/test/expected_disable.nu new file mode 100644 index 0000000..0a77dbd --- /dev/null +++ b/test/expected_disable.nu @@ -0,0 +1,17 @@ +# topiary: disable +let foo = {} + +{|| + # other comments + let foo = 1 + # topiary: disable + let bar = 2 +} + +module foo { + + #topiary: disable + def should_not_be_formatted [] { } + + def should_be_formatted [] { } +} diff --git a/test/input_disable.nu b/test/input_disable.nu new file mode 100644 index 0000000..d12ed2d --- /dev/null +++ b/test/input_disable.nu @@ -0,0 +1,17 @@ +# topiary: disable +let foo = {} + +{ || +# other comments + let foo = 1 + # topiary: disable + let bar = 2 +} + +module foo { + + #topiary: disable + def should_not_be_formatted [] { } + + def should_be_formatted [] { } +}