Skip to content

Commit

Permalink
Fix lambda compactness regression (#728)
Browse files Browse the repository at this point in the history
Fix lambda compactness regression
  • Loading branch information
carterkozak authored Apr 28, 2022
1 parent 923d577 commit 9c353ec
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-728.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Fix lambda compactness regression
links:
- https://github.com/palantir/palantir-java-format/pull/728
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2785,13 +2783,7 @@ private void visitRegularDot(List<ExpressionTree> 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("")
Expand All @@ -2816,17 +2808,6 @@ private void visitRegularDot(List<ExpressionTree> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class PalantirLambdaBreakChain {
void foo() {
return hello
.read(txn -> {
return hello.read(txn -> {
doSomeWork();
doSomeMoreWork();
})
Expand Down

0 comments on commit 9c353ec

Please sign in to comment.