Skip to content

Commit 1db1183

Browse files
committed
feat: locally disable formatting with specific comment
1 parent aca3a4f commit 1db1183

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## Status
1212

13-
* Supposed to work well with all language features of nushell v0.106
13+
* Supposed to work well with all language features of nushell v0.107
1414
* Except for some known issues of `tree-sitter-nu`
1515

1616
> [!NOTE]
@@ -124,6 +124,24 @@ cat foo.nu | topiary format --language nu
124124

125125
</details>
126126

127+
### Locally disable formatting for certain expression
128+
129+
If you don't like the formatted output of certain parts of your code,
130+
you can choose to disable it locally with a preceding `Topiary: disable` comment:
131+
132+
```nushell
133+
...
134+
# Topiary: disable
135+
let foo = [foo, bar
136+
baz, ]
137+
...
138+
```
139+
140+
This will keep the let assignment as it is while formatting the rest of the code.
141+
142+
> [!NOTE]
143+
> We do recommend reporting code style issues before resorting to this workaround.
144+
127145
## Editor Integration
128146

129147
<details>

languages/nu.scm

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
(path)
1919
] @leaf
2020

21+
;; TODO: new feature of the next topiary release
22+
;; (unescaped_interpolated_content) @keep_whitespaces
23+
2124
;; keep empty lines
2225
(_) @allow_blank_line_before
2326

@@ -188,16 +191,39 @@
188191
(#scope_id! "consecutive_scope")
189192
)
190193

194+
(val_closure
195+
(_) @append_begin_scope
196+
.
197+
(_) @prepend_end_scope @prepend_input_softline
198+
(#scope_id! "consecutive_scope")
199+
)
200+
191201
(block
192202
"{" @append_space
193203
"}" @prepend_space
194204
)
195205

206+
;; HACK: temporarily disable formatting after special comment
207+
;; abuse capture `@do_nothing` for the predicate
208+
(nu_script
209+
(comment) @do_nothing
210+
.
211+
(_) @leaf
212+
(#match? @do_nothing "Topiary: disable")
213+
)
214+
215+
(block
216+
(comment) @do_nothing
217+
.
218+
(_) @leaf
219+
(#match? @do_nothing "Topiary: disable")
220+
)
221+
196222
(val_closure
197-
(_) @append_begin_scope
223+
(comment) @do_nothing
198224
.
199-
(_) @prepend_end_scope @prepend_input_softline
200-
(#scope_id! "consecutive_scope")
225+
(_) @leaf
226+
(#match? @do_nothing "Topiary: disable")
201227
)
202228

203229
(val_closure

test/expected_disable.nu

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Topiary: disable
2+
let foo = {}
3+
4+
{||
5+
# other comments
6+
let foo = 1
7+
# Topiary: disable
8+
let bar = 2
9+
}
10+
11+
module foo {
12+
13+
#Topiary: disable
14+
def should_not_be_formatted [] { }
15+
16+
def should_be_formatted [] { }
17+
}

test/input_disable.nu

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Topiary: disable
2+
let foo = {}
3+
4+
{ ||
5+
# other comments
6+
let foo = 1
7+
# Topiary: disable
8+
let bar = 2
9+
}
10+
11+
module foo {
12+
13+
#Topiary: disable
14+
def should_not_be_formatted [] { }
15+
16+
def should_be_formatted [] { }
17+
}

0 commit comments

Comments
 (0)