Skip to content

Hug a single block lambda on the first line of a chain, with an extra +4 indent #285

Not planned

Description

@abashev

Upstream: google/google-java-format#1548
Kind: idea | Priority: low

google-java-format#1548 (merged 2026-11-27, copybara export) reverses the choice made in google-java-format#1499, which stopped hugging a single block lambda when the call starts a dereference chain. Now such a lambda is hugged on the call's line again, like an expression lambda, and its body and closing }) get an extra +4 so they stand deeper than the chained calls. The same rule applies to constructor calls that start a chain (new Thread(() -> { ... }).start()), to chains whose first element is a static call, and to chains that already broke at an earlier dot. A call with a class body (anonymous class, enum constant) still keeps its lambda unhugged. Related earlier work: google-java-format#1489 and google-java-format#1506.

Why it matters to us: it only shows what upstream considers the settled layout for this shape, and it removes the 4-character receiver threshold inconsistency (item.withCallback(...) versus a long receiver breaking before .withCallback(...)). It is built on google's hug mechanism (an indent parameter on addArguments), which our palantir-based layout engine does not have, so it cannot be ported as a diff. Our own tests lay out a lambda at the start of a chain differently (lambda inlined, body +8, closing }) and following calls at +4, see palantir-break-lambda-arg.output). The examples below are upstream's 2-space Google-style output taken from the LambdaHug test data of the PR, not our output; we did not run our formatter.

Before (upstream behaviour before the change):

new Thread(
        () -> {
          assertThrows(CancellationException.class, future::get);
          successLatch.countDown();
        })
    .start();

After (with the change):

new Thread(() -> {
      assertThrows(CancellationException.class, future::get);
      successLatch.countDown();
    })
    .start();

Affected files in our repo:

  • open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java (visitNewClass, method-chain and argument handling)
  • open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-break-lambda-arg.input and .output, palantir-chains-lambdas.input and .output

Suggested action: watch. Fold into the open investigation in the tickets for google-java-format PRs 1489 and 1499 (run the LambdaHug chain cases through our formatter). Our output already differs structurally from google's, so adopt only if it shows a clearly worse shape, such as a closing }) flush with the statement or the body at the same indent as the chained call.


Filed from an upstream-watch ticket after manual review.

Activity

  1. abashev commented on Nov 28, 2026

    @abashev
    CollaboratorAuthor

    Checked: our formatter already lays these chains out the way google-java-format#1548 now does. I ran the LambdaHug input of that PR through a build of the current 21.X.X branch. Every block lambda chain whose layout the PR changed is already hugged on the call's line, in the same shape at our indents (4 per block and 8 per continuation, against google's 2 and 4): the closing }) one continuation deeper than the statement, in line with the chained calls, and the body one block indent deeper still.

    new Thread(() -> {
                assertThrows(CancellationException.class, future::get);
                successLatch.countDown();
            })
            .start();

    The same holds for constructor calls, static and unqualified calls, a receiver with a class body (new Receiver() { ... }.computeItems(...)), a chain already broken at an earlier dot (someLongReceiverName, then .withCallback(event -> {) and a chain inside an if condition. Formatting the output a second time changes nothing.

    Two corrections to the description above:

    • In palantir-break-lambda-arg, the closing }) and the chained calls stand at +8 and the body at +12. That is upstream's +4/+6 shape at our indents, not a different structure.
    • The receiver threshold is still there upstream: someLongReceiverName still breaks before .withCallback(...), as it does in ours.

    Two differences remain, and neither comes from the hug:

    • When the first line is already full (com.google.….MyClass.computeItems( at 120 columns, or item.<four long type arguments>of(), upstream breaks inside the qualified name or the type arguments and then hugs the lambda. We keep that line whole and put x -> { on the next one. The }) is not flush with the statement and the body stays deeper than the chain, so this is not the worse shape this ticket was watching for.
    • Upstream leaves the lambda unhugged in a call with a class body (new Thread(() -> { ... }) { ... }) and in a condition that is the chain itself (if (trapExceptions(() -> { ... }).isOk())). We hug it in both, as before.

    Nothing to port, so I'm closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions