Repository navigation
Hug lambda arguments in google-java-format. - #1485
Merged
Merged
Conversation
When a call doesn't fit on one line and its only argument is a lambda, keep the
lambda's head on the call's line and indent its body relative to that line:
```java
executor.submit(() -> {
run();
});
```
instead of
```java
executor.submit(
() -> {
run();
});
```
The lambda is hugged only if everything up to `->` fits on the line; otherwise
the existing layout is used. However, if breaking before the lambda allows the
entire lambda to fit flat on the next line, prefer breaking before the lambda
rather than hugging `params ->` onto the call line and breaking inside the
lambda body. Lambdas preceded by other arguments are not hugged, since the
preceding arguments wouldn't line up with the lambda's body. This addresses
#19.
This adds a `Doc.Choice` node to the layout IR, which chooses between a
preferred and a fallback layout with the same flat text. `LayoutBuilder`
derives the preferred layout by flattening the argument list up to the hug
point.
PiperOrigin-RevId: 1000770039
copybara-service
Bot
force-pushed
the
test_987328159
branch
from
October 16, 2026 11:34
cf8fd34 to
212d5fe
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hug lambda arguments in
google-java-format.When a call doesn't fit on one line and its only argument is a lambda, keep the
lambda's head on the call's line and indent its body relative to that line:
instead of
The lambda is hugged only if everything up to
->fits on the line; otherwisethe existing layout is used. However, if breaking before the lambda allows the
entire lambda to fit flat on the next line, prefer breaking before the lambda
rather than hugging
params ->onto the call line and breaking inside thelambda body. Lambdas preceded by other arguments are not hugged, since the
preceding arguments wouldn't line up with the lambda's body. This addresses
#19.
This adds a
Doc.Choicenode to the layout IR, which chooses between apreferred and a fallback layout with the same flat text.
LayoutBuilderderives the preferred layout by flattening the argument list up to the hug
point.