diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index 5bce89e7a..e678efaba 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -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) { @@ -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(), @@ -1199,7 +1193,6 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) { } else { scan(node.getBody(), null); } - builder.close(); return null; } @@ -2759,7 +2752,10 @@ void addTypeArguments(List typeArguments, Indent plusIndent) { * @param plusIndent the extra indent for the arguments */ void addArguments(List 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) { @@ -2793,12 +2789,16 @@ void addArguments(List 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 arguments) { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20128760.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20128760.output index 0e043fe32..e9ccfde0f 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20128760.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B20128760.output @@ -89,19 +89,12 @@ class B20128760 { { Stream 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)))); } } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21305044.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21305044.output index 32296d215..aba5c4735 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21305044.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B21305044.output @@ -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"); } } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22873322.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22873322.output index 776838319..b529afaae 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22873322.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B22873322.output @@ -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; }); diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B33358723.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B33358723.output index da0000ce1..7fc3f4fc7 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B33358723.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B33358723.output @@ -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")); } }