Skip to content

Commit 1bc6129

Browse files
committed
Keep an annotated lambda parameter on its line
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.
1 parent 2fbf175 commit 1bc6129

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,10 +1247,10 @@ protected void visitLambdaExpression(LambdaExpressionTree node, boolean statemen
12471247
token(",");
12481248
builder.breakOp(" ");
12491249
}
1250-
visitVariables(
1251-
ImmutableList.of(parameter),
1252-
DeclarationKind.NONE,
1253-
inlineAnnotationDirection(parameter.getModifiers()));
1250+
// A lambda parameter is a parameter: its annotations stay on its line, as a method parameter's do. The
1251+
// vertical direction, a forced break after an annotation with arguments, is for local variable
1252+
// declarations.
1253+
visitVariables(ImmutableList.of(parameter), DeclarationKind.NONE, Direction.HORIZONTAL);
12541254
first = false;
12551255
}
12561256
if (parens) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.function.BiFunction;
2+
import java.util.function.Consumer;
3+
import java.util.function.Function;
4+
5+
class AnnotatedLambdaParameters {
6+
BiFunction<Integer, Integer, Integer> withArgs =
7+
(final var x,
8+
@SuppressWarnings("unused")
9+
var y) -> x + y;
10+
11+
Function<String, Integer> typed = ( @SuppressWarnings( "unused" ) String s )->s.length();
12+
13+
BiFunction<Integer, Integer, Integer> two = (@Deprecated @SuppressWarnings("unused")
14+
var x, var y) -> x + y;
15+
16+
BiFunction<Integer, Integer, Integer> marker = (@Deprecated var x,
17+
@Deprecated final var y) -> x + y;
18+
19+
Consumer<String> block = (@SuppressWarnings({"unused", "rawtypes"})
20+
String s) -> { System.out.println(s); };
21+
22+
BiFunction<Integer, Integer, Integer> tooLongForOneLine = (@SuppressWarnings("unused") Integer first, @SuppressWarnings("unused") Integer second) -> first + second;
23+
24+
BiFunction<Integer, Integer, Integer> parametersDoNotFitOnOneLine = (@SuppressWarnings("unused") Integer firstParameterWithAVeryLongName, @SuppressWarnings("unused") Integer secondParameterWithAVeryLongName) -> firstParameterWithAVeryLongName + secondParameterWithAVeryLongName;
25+
26+
void method(@SuppressWarnings("unused") int y, @Deprecated int z) {
27+
@SuppressWarnings("unchecked") Function<Object, String> local = o -> (String) o;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.function.BiFunction;
2+
import java.util.function.Consumer;
3+
import java.util.function.Function;
4+
5+
class AnnotatedLambdaParameters {
6+
BiFunction<Integer, Integer, Integer> withArgs = (final var x, @SuppressWarnings("unused") var y) -> x + y;
7+
8+
Function<String, Integer> typed = (@SuppressWarnings("unused") String s) -> s.length();
9+
10+
BiFunction<Integer, Integer, Integer> two = (@Deprecated @SuppressWarnings("unused") var x, var y) -> x + y;
11+
12+
BiFunction<Integer, Integer, Integer> marker = (@Deprecated var x, @Deprecated final var y) -> x + y;
13+
14+
Consumer<String> block = (@SuppressWarnings({"unused", "rawtypes"}) String s) -> {
15+
System.out.println(s);
16+
};
17+
18+
BiFunction<Integer, Integer, Integer> tooLongForOneLine =
19+
(@SuppressWarnings("unused") Integer first, @SuppressWarnings("unused") Integer second) -> first + second;
20+
21+
BiFunction<Integer, Integer, Integer> parametersDoNotFitOnOneLine =
22+
(@SuppressWarnings("unused") Integer firstParameterWithAVeryLongName,
23+
@SuppressWarnings("unused") Integer secondParameterWithAVeryLongName) ->
24+
firstParameterWithAVeryLongName + secondParameterWithAVeryLongName;
25+
26+
void method(@SuppressWarnings("unused") int y, @Deprecated int z) {
27+
@SuppressWarnings("unchecked")
28+
Function<Object, String> local = o -> (String) o;
29+
}
30+
}

0 commit comments

Comments
 (0)