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 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(); })