Repository navigation
Commit 2303933
authored
Format a blank line around multi-line module items (#8961)
* Format a blank line around module items that span several lines
Why:
Dense code, often written by AI agents, runs multi-line modules and
functions together, which makes a file hard to scan (#8960). The
formatter already collapses several blank lines into one; it now also
separates an item that prints on more than one line from its
neighbours in structures and signatures. Blocks inside expressions
keep their layout.
Format modified (structure items):
Example:
module Details = {
let make = () => React.string("details")
}
module Output = Tool.TextResult.Output
let failed = Tool.TextResult.failed
Before (rescript):
module Details = {
let make = () => React.string("details")
}
module Output = Tool.TextResult.Output
let failed = Tool.TextResult.failed
After (rescript):
module Details = {
let make = () => React.string("details")
}
module Output = Tool.TextResult.Output
let failed = Tool.TextResult.failed
Format unchanged (one-line items with comments):
Example:
let a = 1
// about b
let b = 2 // trailing
let c = 3
After (rescript):
let a = 1
// about b
let b = 2 // trailing
let c = 3
Review: Is "multi-line" decided on the source or on the output? On the output: the new Res_doc.item_separator measures the items on each side with the printer's own layout decisions, stopping at the first line break, so an item that only breaks at the line width counts and formatting twice gives the same text. The source line span (`let x = someLongCall(a, b, c, d)` is one source line but prints on several) and Doc.will_break (a record that wraps by width is force-broken only on the second pass) would both add the blank line only on the second format. The measurement costs at most one output line per item. The roundtrip test formats every printer fixture twice and gets the same text.
Review: Why a Res_doc.comment marker? Leading comments print inside the item's document (a value binding carries them), so a `//` line above a one-line item would make it multi-line, even when the item is measured before print_list adds its own comments. The marker keeps comments out of the measurement; doc comments are attributes and still count.
Review: Why move own-line trailing comments below the blank line? A comment inside the previous item's source, such as `f(a // c => 1)`, prints on its own line after the item. Formatting again makes it a leading comment of the next item, after the blank line, so the first pass puts it there too.
Review: How are the measurement's branches tested? tests/syntax_tests/res_test.ml prints documents built for each case (line breaks, groups that fit and break, if_breaks, comments, trailing comments) and checks the output, as the printer fixtures do not reach every branch first.
Review: What else prints through it? The editor's code actions and `rescript-tools format-codeblocks` for code blocks in docstrings, so the Xform and FormatDocstringsTest2 snapshots gain a blank line.
Review: Do attributes count? Yes, an attribute or doc comment on its own line makes an item print on two lines, so runs of `@genType` or documented one-line items are spaced; an inline `@val external` is not. The fixtures cover both, and nested module bodies.
Review: Does a string literal with a line break count? Yes: it prints on several lines, like a template literal, so the line-ending test in res_test.ml separates such items with a blank line, in the file's own line endings.
Review: Is it opt-in? No: every `rescript format` uses it. The next commit formats this repository with it.
Signed-off-by: Roland Peelen <roland@magic.dev>
* Format the repository with multi-line item spacing
The previous commit makes the formatter put a blank line around module
items that span several lines. `make format` applies it to the
ReScript sources it covers: 353 files gain 1179 blank lines and lose
nothing. rewatch/testrepo/packages/dep02/src/Array.res gains one too:
the rewatch format test expects exactly the testrepo's 8 deliberately
unformatted files to change. The outputs that record source line
numbers follow: the super_errors snapshots, the `assert` locations in
the generated test JavaScript, the doc-extraction snapshots of
tests/tools_tests, and the locations in React.res and JsxEvent.res that
the completion and reanalyze snapshots of tests/analysis_tests point
to.
Signed-off-by: Roland Peelen <roland@magic.dev>
---------
Signed-off-by: Roland Peelen <roland@magic.dev>1 parent 6ea63e5 commit 2303933
627 files changed
Lines changed: 3623 additions & 1406 deletions
File tree
- changes
- compiler/syntax/src
- packages
- @rescript
- belt/src
- runtime
- dev-playground/src
- rewatch/testrepo/packages/dep02/src
- tests
- analysis_tests
- tests-reanalyze/deadcode/expected
- tests/src/expected
- belt_tests/src
- build_tests
- gpr_978/src
- jsx_preserve_semantics/cases
- option_transitive_dependency/dep
- react_ppx/src
- super_errors
- expected
- fixtures
- dependencies/rescript-react/src
- docstring_tests
- gentype_tests
- stdlib-no-shims/src
- typescript-react-example/src
- syntax_benchmarks/data
- syntax_tests
- data
- ast-mapping/expected
- conversion/reason/expected
- ppx/react/expected
- printer
- comments/expected
- expected
- expr/expected
- modExpr/expected
- modType/expected
- other/expected
- pattern/expected
- signature
- expected
- structure
- expected
- typeDef/expected
- typexpr/expected
- tests/src
- acyc
- conditional
- stdlib
- intl
- tools_tests
- ppx
- src
- expected
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| |||
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
| 57 | + | |
| 58 | + | |
55 | 59 | | |
56 | 60 | | |
57 | 61 | | |
| |||
75 | 79 | | |
76 | 80 | | |
77 | 81 | | |
78 | | - | |
| 82 | + | |
79 | 83 | | |
80 | | - | |
| 84 | + | |
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
| |||
115 | 119 | | |
116 | 120 | | |
117 | 121 | | |
118 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
119 | 125 | | |
120 | | - | |
| 126 | + | |
| 127 | + | |
121 | 128 | | |
122 | 129 | | |
123 | 130 | | |
| |||
129 | 136 | | |
130 | 137 | | |
131 | 138 | | |
132 | | - | |
| 139 | + | |
133 | 140 | | |
134 | | - | |
| 141 | + | |
135 | 142 | | |
136 | 143 | | |
137 | 144 | | |
| |||
170 | 177 | | |
171 | 178 | | |
172 | 179 | | |
| 180 | + | |
173 | 181 | | |
174 | 182 | | |
175 | 183 | | |
176 | 184 | | |
| 185 | + | |
177 | 186 | | |
178 | 187 | | |
179 | 188 | | |
| |||
203 | 212 | | |
204 | 213 | | |
205 | 214 | | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
206 | 268 | | |
207 | 269 | | |
208 | 270 | | |
| |||
220 | 282 | | |
221 | 283 | | |
222 | 284 | | |
| 285 | + | |
223 | 286 | | |
224 | 287 | | |
225 | 288 | | |
| |||
262 | 325 | | |
263 | 326 | | |
264 | 327 | | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
275 | 352 | | |
276 | 353 | | |
277 | 354 | | |
| |||
351 | 428 | | |
352 | 429 | | |
353 | 430 | | |
| 431 | + | |
| 432 | + | |
354 | 433 | | |
355 | 434 | | |
356 | 435 | | |
| |||
375 | 454 | | |
376 | 455 | | |
377 | 456 | | |
378 | | - | |
| 457 | + | |
379 | 458 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
21 | 30 | | |
22 | 31 | | |
23 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
| 340 | + | |
| 341 | + | |
344 | 342 | | |
345 | 343 | | |
346 | 344 | | |
| |||
398 | 396 | | |
399 | 397 | | |
400 | 398 | | |
401 | | - | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
402 | 402 | | |
403 | 403 | | |
404 | 404 | | |
| |||
414 | 414 | | |
415 | 415 | | |
416 | 416 | | |
417 | | - | |
418 | | - | |
419 | | - | |
| 417 | + | |
| 418 | + | |
420 | 419 | | |
421 | 420 | | |
422 | 421 | | |
| 422 | + | |
| 423 | + | |
423 | 424 | | |
424 | 425 | | |
425 | 426 | | |
| 427 | + | |
426 | 428 | | |
427 | 429 | | |
428 | 430 | | |
| |||
701 | 703 | | |
702 | 704 | | |
703 | 705 | | |
704 | | - | |
| 706 | + | |
705 | 707 | | |
706 | 708 | | |
707 | 709 | | |
| |||
1119 | 1121 | | |
1120 | 1122 | | |
1121 | 1123 | | |
1122 | | - | |
| 1124 | + | |
1123 | 1125 | | |
1124 | 1126 | | |
1125 | 1127 | | |
| |||
0 commit comments