Repository navigation
Commit bf9e0eb
committed
Do not inline a lambda body whose closing paren carries a comment
A "//" comment on its own line before the "))" that close a lambda's
parenthesized body and the call around it was joined onto the code
line before it, and the "))" ended up inside the comment, so the output
no longer parsed (#62, palantir#1792). The
command line noticed only because its import pass parses the result
again and reported a position in the output; a caller of formatSource
got the text as it was.
An expression lambda's body is laid out by
handle_breakOnlyIfInnerLevelsThenFitOnOneLine: when the body does not
fit after "->" but its first line does, tryInlinePrefixOntoCurrentLine
lays the body's level out "on one line" through
tryToLayOutLevelOnOneLine, which marks every break of that level as
not taken and only lets the inner levels break. The comments before
the closing ")" belong to that same level, with forced breaks around
them, and a forced break laid out flat is what put the comment on the
code line. The width of the docs before the last inner level was
checked, and a forced break there fails that check; the docs after it
were not checked at all.
The check is now made for the trailing docs too, the way
tryBreakInnerLevel already refuses a suffix of infinite width: a body
with a comment before its closing token is not inlined and breaks
normally, so the body starts on the line after "->" and the comments
keep their own lines at the body's indent, as they already did when
the body's first line did not fit. A block comment in the same place
was written without a space in front of it and moved on a second run;
it now gets its own line as well.
The golden holds the reporter's input, the same shape as a plain call,
and the block comment. The 15,747 files of the JDK 21 sources format
exactly as before: none of them has a comment before "))".1 parent 7f2a32d commit bf9e0eb
3 files changed
Lines changed: 70 additions & 1 deletion
File tree
- open-java-format/src
- main/java/com/palantir/javaformat/doc
- test/resources/com/palantir/javaformat/java/testdata
Lines changed: 14 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
339 | 339 | | |
340 | 340 | | |
341 | 341 | | |
342 | | - | |
| 342 | + | |
| 343 | + | |
343 | 344 | | |
344 | 345 | | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
345 | 355 | | |
346 | 356 | | |
347 | 357 | | |
| |||
592 | 602 | | |
593 | 603 | | |
594 | 604 | | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
595 | 608 | | |
596 | 609 | | |
597 | 610 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
0 commit comments