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

Break on lambda argument in method chain #707

Merged
merged 4 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
16 changes: 16 additions & 0 deletions changelog/@unreleased/pr-707.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type: improvement
improvement:
description: "Break on lambda argument in method chain\n\n## Before this PR\nChained
calls involving lambda expressions don't line break, which conflicts with checkstyle
(at least the gradle-baseline checkstyle config)\n\n```java\nclass PalantirLambdaInliningPrefersBreak
{\n void foo() {\n return hello.read(txn -> {\n doSomeWork();\n
\ doSomeMoreWork();\n })\n .chainedCall(()
-> {\n doSomeWork();\n });\n }\n}\n\n```\n\n##
After this PR\n\n```java\nclass PalantirLambdaInliningPrefersBreak {\n void
foo() {\n return hello\n .read(txn -> {\n doSomeWork();\n
\ doSomeMoreWork();\n })\n .chainedCall(()
-> {\n doSomeWork();\n });\n }\n}\n```\n\n##
Possible downsides?\n\U0001F937‍♂️ I've also added a test to show that non-chained
lambda calls don't include a line break"
links:
- https://github.com/palantir/palantir-java-format/pull/707
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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 @@ -2783,7 +2784,11 @@ private void visitRegularDot(List<ExpressionTree> items, boolean needDot) {
int length = needDot0 ? minLength : 0;
for (ExpressionTree e : items) {
if (needDot) {
if (length > minLength) {
// Also break if invoked with a lambda -- palantir-break-lambda-arg
// foo
// .doSomething(() -> bar())
// .doSomethingElse();
if (length > minLength || methodHasLambdaArg(e)) {
builder.breakOp(Break.builder()
.fillMode(FillMode.UNIFIED)
.flat("")
Expand All @@ -2808,6 +2813,14 @@ private void visitRegularDot(List<ExpressionTree> items, boolean needDot) {
}
}

private boolean methodHasLambdaArg(ExpressionTree e) {
if (!e.getKind().equals(METHOD_INVOCATION)) {
return false;
}
return ((MethodInvocationTree) e)
.getArguments().stream().anyMatch(argExpr -> argExpr.getKind().equals(LAMBDA_EXPRESSION));
}

// avoid formattings like:
//
// when(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class PalantirLambdaBreakChain {
void foo() {
return hello.read(txn -> {
doSomeWork();
doSomeMoreWork();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class PalantirLambdaBreakChain {
void foo() {
return hello.read(txn -> {
doSomeWork();
doSomeMoreWork();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class PalantirLambdaBreakChain {
void foo() {
return hello.read(txn -> {
doSomeWork();
doSomeMoreWork();
})
.chainedCall(() -> {
doSomeWork();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class PalantirLambdaBreakChain {
void foo() {
return hello
.read(txn -> {
doSomeWork();
doSomeMoreWork();
})
.chainedCall(() -> {
doSomeWork();
});
}
}