From eb18b66e63cb7852dfda0da86544f0ca135bd83d Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Thu, 28 Apr 2022 08:35:37 -0400 Subject: [PATCH 1/2] Revert lambda compactness regression This reverts #708 and #707 which made expression lambdas less readable. --- .../javaformat/java/JavaInputAstVisitor.java | 21 +------------------ .../testdata/palantir-break-lambda-arg.output | 3 +-- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java index 8a6cdcd78..5f48f7fc5 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java @@ -35,7 +35,6 @@ import static com.sun.source.tree.Tree.Kind.BLOCK; import static com.sun.source.tree.Tree.Kind.EXTENDS_WILDCARD; import static com.sun.source.tree.Tree.Kind.IF; -import static com.sun.source.tree.Tree.Kind.LAMBDA_EXPRESSION; import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION; import static com.sun.source.tree.Tree.Kind.NEW_ARRAY; import static com.sun.source.tree.Tree.Kind.NEW_CLASS; @@ -107,7 +106,6 @@ import com.sun.source.tree.IntersectionTypeTree; import com.sun.source.tree.LabeledStatementTree; import com.sun.source.tree.LambdaExpressionTree; -import com.sun.source.tree.LambdaExpressionTree.BodyKind; import com.sun.source.tree.LiteralTree; import com.sun.source.tree.MemberReferenceTree; import com.sun.source.tree.MemberSelectTree; @@ -2785,13 +2783,7 @@ private void visitRegularDot(List items, boolean needDot) { int length = needDot0 ? minLength : 0; for (ExpressionTree e : items) { if (needDot) { - // Also break if invoked with a multi-statement lambda -- palantir-break-lambda-arg - // foo - // .doSomething(() -> { - // bar(); - // }) - // .doSomethingElse(); - if (length > minLength || methodHasMultiStatementLambdaArg(e)) { + if (length > minLength) { builder.breakOp(Break.builder() .fillMode(FillMode.UNIFIED) .flat("") @@ -2816,17 +2808,6 @@ private void visitRegularDot(List items, boolean needDot) { } } - private boolean methodHasMultiStatementLambdaArg(ExpressionTree e) { - if (!e.getKind().equals(METHOD_INVOCATION)) { - return false; - } - return ((MethodInvocationTree) e) - .getArguments().stream() - .filter(argExpr -> argExpr.getKind().equals(LAMBDA_EXPRESSION)) - .map(argExpr -> (LambdaExpressionTree) argExpr) - .anyMatch(lambda -> lambda.getBodyKind().equals(BodyKind.STATEMENT)); - } - // avoid formattings like: // // when( diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-break-lambda-arg.output b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-break-lambda-arg.output index 20c6bc7be..d4df67643 100644 --- a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-break-lambda-arg.output +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-break-lambda-arg.output @@ -1,7 +1,6 @@ class PalantirLambdaBreakChain { void foo() { - return hello - .read(txn -> { + return hello.read(txn -> { doSomeWork(); doSomeMoreWork(); }) From 7758acaba22e64e0fad3be49bbbf4ba78c2b5c8e Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Thu, 28 Apr 2022 12:48:32 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-728.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-728.v2.yml diff --git a/changelog/@unreleased/pr-728.v2.yml b/changelog/@unreleased/pr-728.v2.yml new file mode 100644 index 000000000..ac748641f --- /dev/null +++ b/changelog/@unreleased/pr-728.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Fix lambda compactness regression + links: + - https://github.com/palantir/palantir-java-format/pull/728