Skip to content

Commit

Permalink
Improve lambda formatting
Browse files Browse the repository at this point in the history
#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 4594013
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 66 deletions.
Expand Up @@ -1163,7 +1163,6 @@ public Void visitLabeledStatement(LabeledStatementTree node, Void unused) {
@Override
public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
sync(node);
boolean statementBody = node.getBodyKind() == LambdaExpressionTree.BodyKind.STATEMENT;
boolean parens = builder.peekToken().equals(Optional.of("("));
builder.open(parens ? plusFour : ZERO);
if (parens) {
Expand All @@ -1184,12 +1183,7 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
builder.close();
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 All @@ -1199,7 +1193,6 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
} else {
scan(node.getBody(), null);
}
builder.close();
return null;
}

Expand Down Expand Up @@ -2759,7 +2752,10 @@ void addTypeArguments(List<? extends Tree> typeArguments, Indent plusIndent) {
* @param plusIndent the extra indent for the arguments
*/
void addArguments(List<? extends ExpressionTree> arguments, Indent plusIndent) {
builder.open(plusIndent);
boolean singleLambdaArg = arguments.size() == 1 && arguments.get(0) instanceof JCTree.JCLambda;
if (!singleLambdaArg) {
builder.open(plusIndent);
}
token("(");
if (!arguments.isEmpty()) {
if (arguments.size() % 2 == 0 && argumentsAreTabular(arguments) == 2) {
Expand Down Expand Up @@ -2793,12 +2789,16 @@ void addArguments(List<? extends ExpressionTree> arguments, Indent plusIndent) {
builder.close();
builder.close();
} else {
builder.breakOp();
if (!singleLambdaArg) {
builder.breakOp();
}
argList(arguments);
}
}
token(")");
builder.close();
if (!singleLambdaArg) {
builder.close();
}
}

private void argList(List<? extends ExpressionTree> arguments) {
Expand Down
Expand Up @@ -89,19 +89,12 @@ class B20128760 {
{
Stream<ItemKey> itemIdsStream =
stream(members)
.flatMap(
m ->
m.getFieldValues()
.entrySet()
.stream()
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
.flatMap(
fv ->
FieldDTO.deserializeStringToListOfStrings(fv.getValue())
.stream()
.map(
id ->
new ItemKey(
fieldsById.get(fv.getKey()).getItemTypeId(), id))));
.flatMap(m -> m.getFieldValues()
.entrySet()
.stream()
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
.flatMap(fv -> FieldDTO.deserializeStringToListOfStrings(fv.getValue())
.stream()
.map(id -> new ItemKey(fieldsById.get(fv.getKey()).getItemTypeId(), id))));
}
}
Expand Up @@ -37,22 +37,20 @@ class B21305044 {
{
Function f = () -> moderatelyLongResult;
Function f =
() ->
breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult;
() -> breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult
+ breakableResult;
Function f =
() ->
System.err.println(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
() -> System.err.println(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Function f =
(someParam) ->
System.err.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
(someParam) -> System.err.println(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
@@ -1,21 +1,15 @@
class B22873322 {
{
f(
param ->
veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
param));
f(
(param1, param2) ->
veryLooooooooooooooooooooooooooooooooooooooooooooooooongExpr(param1, param2));
f(
(int param) ->
veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(param));
f(
(param1, param2) -> {
f(param -> veryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongExpr(
param));
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
Expand Up @@ -2,23 +2,20 @@ class B33358723 {
{
f(
//
x ->
System.err.println(
//
"hello"));
x -> System.err.println(
//
"hello"));
f(
//
( //
x) ->
System.err.println(
//
"hello"));
x) -> System.err.println(
//
"hello"));
f(
//
(int //
x) ->
System.err.println(
//
"hello"));
x) -> System.err.println(
//
"hello"));
}
}

0 comments on commit 4594013

Please sign in to comment.