Skip to content

Commit 054e3ca

Browse files
committed
Improve lambda formatting
#19 The style guide says: "A line is never broken adjacent to the arrow in a lambda, except that a break may come immediately after the arrow if the body of the lambda consists of a single unbraced expression." There are two changes here: 1. Don't put a newline after the arrow. 2. When the only argument to a function is a lambda, don't put a newline after the open-paren of the function. I think this newline was going in because a lambda is a single expression that is longer than (the remainder of) a line. But generally, it's prettier to break inside the lambda.
1 parent 02de39a commit 054e3ca

File tree

5 files changed

+25
-40
lines changed

5 files changed

+25
-40
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -1185,11 +1185,7 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
11851185
builder.space();
11861186
builder.op("->");
11871187
builder.open(statementBody ? ZERO : plusFour);
1188-
if (statementBody) {
1189-
builder.space();
1190-
} else {
1191-
builder.breakOp(" ");
1192-
}
1188+
builder.space();
11931189
if (node.getBody().getKind() == Tree.Kind.BLOCK) {
11941190
visitBlock(
11951191
(BlockTree) node.getBody(),
@@ -2793,7 +2789,13 @@ void addArguments(List<? extends ExpressionTree> arguments, Indent plusIndent) {
27932789
builder.close();
27942790
builder.close();
27952791
} else {
2796-
builder.breakOp();
2792+
if (arguments.size() == 1) {
2793+
if (!(arguments.get(0) instanceof JCTree.JCLambda)) {
2794+
builder.breakOp();
2795+
}
2796+
} else {
2797+
builder.breakOp();
2798+
}
27972799
argList(arguments);
27982800
}
27992801
}

core/src/test/resources/com/google/googlejavaformat/java/testdata/B20128760.output

+3-9
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,13 @@ class B20128760 {
8989
{
9090
Stream<ItemKey> itemIdsStream =
9191
stream(members)
92-
.flatMap(
93-
m ->
94-
m.getFieldValues()
92+
.flatMap(m -> m.getFieldValues()
9593
.entrySet()
9694
.stream()
9795
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
98-
.flatMap(
99-
fv ->
100-
FieldDTO.deserializeStringToListOfStrings(fv.getValue())
96+
.flatMap(fv -> FieldDTO.deserializeStringToListOfStrings(fv.getValue())
10197
.stream()
102-
.map(
103-
id ->
104-
new ItemKey(
98+
.map(id -> new ItemKey(
10599
fieldsById.get(fv.getKey()).getItemTypeId(), id))));
106100
}
107101
}

core/src/test/resources/com/google/googlejavaformat/java/testdata/B21305044.output

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class B21305044 {
3737
{
3838
Function f = () -> moderatelyLongResult;
3939
Function f =
40-
() ->
41-
breakableResult
40+
() -> breakableResult
4241
+ breakableResult
4342
+ breakableResult
4443
+ breakableResult
@@ -48,11 +47,10 @@ class B21305044 {
4847
+ breakableResult
4948
+ breakableResult;
5049
Function f =
51-
() ->
52-
System.err.println(
50+
() -> System.err.println(
5351
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
5452
Function f =
55-
(someParam) ->
56-
System.err.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
53+
(someParam) -> System.err.println(
54+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
5755
}
5856
}

core/src/test/resources/com/google/googlejavaformat/java/testdata/B22873322.output

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
class B22873322 {
22
{
3-
f(
4-
param ->
5-
veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
3+
f(param -> veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
64
param));
7-
f(
8-
(param1, param2) ->
9-
veryLooooooooooooooooooooooooooooooooooooooooooooooooongExpr(param1, param2));
10-
f(
11-
(int param) ->
12-
veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(param));
13-
f(
14-
(param1, param2) -> {
5+
f((param1, param2) -> veryLooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
6+
param1, param2));
7+
f((int param) -> veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
8+
param));
9+
f((param1, param2) -> {
1510
return expr(param1, param2);
1611
});
17-
f(
18-
(param1, param2) -> {
12+
f((param1, param2) -> {
1913
Object foo = expr(param1, param2);
2014
return foo;
2115
});

core/src/test/resources/com/google/googlejavaformat/java/testdata/B33358723.output

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ class B33358723 {
22
{
33
f(
44
//
5-
x ->
6-
System.err.println(
5+
x -> System.err.println(
76
//
87
"hello"));
98
f(
109
//
1110
( //
12-
x) ->
13-
System.err.println(
11+
x) -> System.err.println(
1412
//
1513
"hello"));
1614
f(
1715
//
1816
(int //
19-
x) ->
20-
System.err.println(
17+
x) -> System.err.println(
2118
//
2219
"hello"));
2320
}

0 commit comments

Comments
 (0)