Skip to content

Hug lambda arguments in google-java-format. - #1485

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_987328159
Oct 16, 2026
Merged

copybara-service[bot] merged 1 commit into
masterfrom
test_987328159

Conversation

@copybara-service

Copy link
Copy Markdown

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:

executor.submit(() -> {
  run();
});

instead of

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.

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
copybara-service Bot merged commit 212d5fe into master Oct 16, 2026
4 checks passed
@copybara-service
copybara-service Bot deleted the test_987328159 branch October 16, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant