Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lambda compactness regression #728

Merged
merged 2 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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