Skip to content

Commit

Permalink
Improve lambda formatting
Browse files Browse the repository at this point in the history
google#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.
  • Loading branch information
novalis committed Jun 8, 2017
1 parent 02de39a commit df5909b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1185,11 +1185,7 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
builder.space();
builder.op("->");
builder.open(statementBody ? ZERO : plusFour);
if (statementBody) {
builder.space();
} else {
builder.breakOp(" ");
}
builder.space();
if (node.getBody().getKind() == Tree.Kind.BLOCK) {
visitBlock(
(BlockTree) node.getBody(),
Expand Down Expand Up @@ -2793,7 +2789,13 @@ void addArguments(List<? extends ExpressionTree> arguments, Indent plusIndent) {
builder.close();
builder.close();
} else {
builder.breakOp();
if (arguments.size() == 1) {
if (!(arguments.get(0) instanceof JCTree.JCLambda)) {
builder.breakOp();
}
} else {
builder.breakOp();
}
argList(arguments);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,13 @@ class B20128760 {
{
Stream<ItemKey> itemIdsStream =
stream(members)
.flatMap(
m ->
m.getFieldValues()
.flatMap(m -> m.getFieldValues()
.entrySet()
.stream()
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
.flatMap(
fv ->
FieldDTO.deserializeStringToListOfStrings(fv.getValue())
.flatMap(fv -> FieldDTO.deserializeStringToListOfStrings(fv.getValue())
.stream()
.map(
id ->
new ItemKey(
.map(id -> new ItemKey(
fieldsById.get(fv.getKey()).getItemTypeId(), id))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class B21305044 {
{
Function f = () -> moderatelyLongResult;
Function f =
() ->
breakableResult
() -> breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
Expand All @@ -48,11 +47,10 @@ class B21305044 {
+ breakableResult
+ breakableResult;
Function f =
() ->
System.err.println(
() -> System.err.println(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Function f =
(someParam) ->
System.err.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
(someParam) -> System.err.println(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
class B22873322 {
{
f(
param ->
veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
f(param -> veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
param));
f(
(param1, param2) ->
veryLooooooooooooooooooooooooooooooooooooooooooooooooongExpr(param1, param2));
f(
(int param) ->
veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(param));
f(
(param1, param2) -> {
f((param1, param2) -> veryLooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
param1, param2));
f((int param) -> veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
param));
f((param1, param2) -> {
return expr(param1, param2);
});
f(
(param1, param2) -> {
f((param1, param2) -> {
Object foo = expr(param1, param2);
return foo;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ class B33358723 {
{
f(
//
x ->
System.err.println(
x -> System.err.println(
//
"hello"));
f(
//
( //
x) ->
System.err.println(
x) -> System.err.println(
//
"hello"));
f(
//
(int //
x) ->
System.err.println(
x) -> System.err.println(
//
"hello"));
}
Expand Down

0 comments on commit df5909b

Please sign in to comment.