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

Only apply line break to lambdas with statements #708

Merged
merged 3 commits into from
Apr 13, 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
12 changes: 12 additions & 0 deletions changelog/@unreleased/pr-708.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type: fix
fix:
description: |-
Only apply line break to lambdas with statements

## Before this PR
Previous PR #707 was a little aggressive and inserted line breaks for non-statement lambdas.

## After this PR
Only multi-line lambdas cause a line-break in the method chain.
links:
- https://github.com/palantir/palantir-java-format/pull/708
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
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 @@ -2784,11 +2785,13 @@ 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 lambda -- palantir-break-lambda-arg
// Also break if invoked with a multi-statement lambda -- palantir-break-lambda-arg
// foo
// .doSomething(() -> bar())
// .doSomething(() -> {
// bar();
// })
// .doSomethingElse();
if (length > minLength || methodHasLambdaArg(e)) {
if (length > minLength || methodHasMultiStatementLambdaArg(e)) {
builder.breakOp(Break.builder()
.fillMode(FillMode.UNIFIED)
.flat("")
Expand All @@ -2813,12 +2816,15 @@ private void visitRegularDot(List<ExpressionTree> items, boolean needDot) {
}
}

private boolean methodHasLambdaArg(ExpressionTree e) {
private boolean methodHasMultiStatementLambdaArg(ExpressionTree e) {
if (!e.getKind().equals(METHOD_INVOCATION)) {
return false;
}
return ((MethodInvocationTree) e)
.getArguments().stream().anyMatch(argExpr -> argExpr.getKind().equals(LAMBDA_EXPRESSION));
.getArguments().stream()
.filter(argExpr -> argExpr.getKind().equals(LAMBDA_EXPRESSION))
.map(argExpr -> (LambdaExpressionTree) argExpr)
.anyMatch(lambda -> lambda.getBodyKind().equals(BodyKind.STATEMENT));
}

// avoid formattings like:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PalantirLambdaBreakChain {
void foo() {
return hello
.read(txn -> doSomeWork())
.doMoreWork();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PalantirLambdaBreakChain {
void foo() {
return hello.read(txn -> doSomeWork()).doMoreWork();
}
}