Skip to content
Merged
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -124,6 +124,24 @@ cat foo.nu | topiary format --language nu

</details>

### 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

<details>
Expand Down
32 changes: 29 additions & 3 deletions languages/nu.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/expected_disable.nu
Original file line number Diff line number Diff line change
@@ -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 [] { }
}
17 changes: 17 additions & 0 deletions test/input_disable.nu
Original file line number Diff line number Diff line change
@@ -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 [] { }
}