Skip to content

Keep an annotated lambda parameter on its line - #103

Merged
abashev merged 1 commit into
mainfrom
lambda-parameter-annotations-inline
Oct 3, 2026
Merged

abashev merged 1 commit into
mainfrom
lambda-parameter-annotations-inline

Conversation

@abashev

@abashev abashev commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Closes #101.

An annotation with arguments on a lambda parameter, (final var x, @SuppressWarnings("unused") var y) -> x + y, forced a line break after the annotation, so a statement that fits in 120 columns came out as four lines:

        BiFunction<Integer, Integer, Integer> g =
                (final var x,
                        @SuppressWarnings("unused")
                        var y) -> x + y;

visitLambdaExpression declared each parameter with inlineAnnotationDirection, which answers VERTICAL, a forced break, for any annotation that has arguments. That rule is for local variable declarations, where the annotation above the type is the wanted shape. Method parameters are declared with Direction.HORIZONTAL and keep their annotations inline; lambda parameters now do the same. One line in JavaInputAstVisitor.

Golden

ojf-issue-101-annotated-lambda-parameters, every statement in a different input shape: the four-line output above, one line with odd spacing, two annotations split before the type, marker annotations (already inline, as a regression guard), a block body, a lambda too long for one line (it breaks after ->, as an unannotated one does) and one whose parameters do not fit on a line (one per line, each annotation on its parameter's line). A method parameter and a local variable serve as controls: the local keeps its annotation above the type. The golden fails on main in all four runs of the integration test and passes with the change.

Checks

  • :open-java-format:test on JDK 21 and 27: 0 failures.
  • The JDK 21 (15,747 files) and JDK 25 (15,368) sources, formatted with main's jar and this branch's jar, are identical: the JDK has no lambda parameter with an annotation that takes arguments.

Both upstreams carry the same code, palantir/palantir-java-format on develop and google/google-java-format, where the method is fieldAnnotationDirection; neither tracker has a report of it.

An annotation with arguments on a lambda parameter, such as
`(final var x, @SuppressWarnings("unused") var y) -> x + y`, forced a
line break after the annotation, so a statement that fits in 120
columns came out as four lines. visitLambdaExpression declared each
parameter with inlineAnnotationDirection, which answers VERTICAL, a
forced break, for any annotation that has arguments. That rule is for
local variable declarations, where the annotation above the type is the
wanted shape. Method parameters are declared with Direction.HORIZONTAL
and keep their annotations inline; lambda parameters now do the same.

Golden ojf-issue-101-annotated-lambda-parameters covers the broken
shape, a one-line input, two annotations, marker annotations, a block
body, a lambda too long for one line and one whose parameters do not
fit, with a method parameter and a local variable as controls. The JDK
21 and 25 sources format exactly as before: they have no lambda
parameter with an annotation that takes arguments.

Closes #101.
@abashev
abashev merged commit da8e6d2 into main Oct 3, 2026
16 checks passed
@abashev
abashev deleted the lambda-parameter-annotations-inline branch October 3, 2026 18:33
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.

An annotation with arguments on a lambda parameter forces a line break after it

1 participant