From df5909b91a7338ef86136a47858a2715d4342a18 Mon Sep 17 00:00:00 2001 From: David Turner Date: Wed, 7 Jun 2017 23:27:35 -0400 Subject: [PATCH] Improve lambda formatting https://github.com/google/google-java-format/issues/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. --- .../java/JavaInputAstVisitor.java | 14 +++++++------ .../java/testdata/B20128760.output | 12 +++-------- .../java/testdata/B21305044.output | 10 ++++------ .../java/testdata/B22873322.output | 20 +++++++------------ .../java/testdata/B33358723.output | 9 +++------ 5 files changed, 25 insertions(+), 40 deletions(-) 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..7195b1097 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -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(), @@ -2793,7 +2789,13 @@ void addArguments(List 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); } } 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..12316c98e 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,13 @@ class B20128760 { { Stream 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)))); } } 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..1c77d554b 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,8 +37,7 @@ class B21305044 { { Function f = () -> moderatelyLongResult; Function f = - () -> - breakableResult + () -> breakableResult + breakableResult + breakableResult + breakableResult @@ -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"); } } 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..8c1825d85 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( + 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; }); 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..8bdc872c6 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,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")); }