-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better lambda/assignment inlining logic (#169)
Reduce situations where an inlined lambda can get a newline after the arrow, and allow inlining new Class() calls in more contexts. This is a two pronged change: * **When part of a simple inlining**: prefer partially inlining (instead of putting entire lambda body expression on the 2nd line) only if this results in fewer lines used, *or* it's a long method chain, like a builder. * **When part of a complex inlining**: partially inline iff it fits and the expression being inlined is complex, otherwise abort the inlining chain -- simple / complex refer to the complexity of the levels partially inlined so far in the given inlining chain, where if any of the levels is complex, the inlining is considered complex.
- Loading branch information
1 parent
a088988
commit 98ebec2
Showing
15 changed files
with
150 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: improvement | ||
improvement: | ||
description: Reduce situations where an inlined lambda can get a newline after the | ||
arrow, and allow inlining new Class() calls in more contexts. | ||
links: | ||
- https://github.com/palantir/palantir-java-format/pull/169 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...tir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/B18479811.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...tir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/B32114928.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class B32114928 { | ||
{ | ||
Class<T> tClass = (Class<T>) | ||
verifyNotNull((ParameterizedType) getClass().getGenericSuperclass()) | ||
Class<T> tClass = | ||
(Class<T>) verifyNotNull((ParameterizedType) getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[0]; | ||
} | ||
} |
5 changes: 2 additions & 3 deletions
5
...ir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-1.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
class Palantir1 { | ||
void foo() { | ||
for (Double d : Lists.newArrayList(5.0, 10.0, 1.0, 0.0)) { | ||
list.add( | ||
new BinaryNumericConstantColumnInfoV1( | ||
operation, SparkIntegrationTestRuleSets.column("x"), d, Optional.empty())); | ||
list.add(new BinaryNumericConstantColumnInfoV1( | ||
operation, SparkIntegrationTestRuleSets.column("x"), d, Optional.empty())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 11 additions & 14 deletions
25
...ir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-3.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
class Palantir3 { | ||
void foo() { | ||
operations.put(FunctionDefinition.ARRAY_REGEX_REPLACE, (func, helper) -> { | ||
return new Column( | ||
new ArrayTransform( | ||
helper.expressionAsColumn(func.arguments().get(0)).expr(), | ||
new LambdaFunction( | ||
new RegExpReplace( | ||
var, | ||
helper.expressionAsColumn( | ||
func.arguments().get(1)) | ||
.expr(), | ||
helper.expressionAsColumn( | ||
func.arguments().get(2)) | ||
.expr()), | ||
JavaConversions.asScalaBuffer(Arrays.asList(var)), | ||
false))); | ||
return new Column(new ArrayTransform( | ||
helper.expressionAsColumn(func.arguments().get(0)).expr(), | ||
new LambdaFunction( | ||
new RegExpReplace( | ||
var, | ||
helper.expressionAsColumn(func.arguments().get(1)) | ||
.expr(), | ||
helper.expressionAsColumn(func.arguments().get(2)) | ||
.expr()), | ||
JavaConversions.asScalaBuffer(Arrays.asList(var)), | ||
false))); | ||
}); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
...ir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-7.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
class Palantir7 { | ||
void foo() { | ||
when(graphService.getPathFromRootToNode(any(GraphId.class), any(NodeId.class))) | ||
.thenAnswer(invocation -> invocation.getArguments().length == 2 | ||
&& invocation.getArguments()[1] != null | ||
? ImmutableList.of(invocation.getArgument(1)) | ||
: ImmutableList.of(NodeId.create())); | ||
.thenAnswer(invocation -> | ||
invocation.getArguments().length == 2 && invocation.getArguments()[1] != null | ||
? ImmutableList.of(invocation.getArgument(1)) | ||
: ImmutableList.of(NodeId.create())); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...tir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-8.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Palantir8 { | ||
void foo() { | ||
assertThatServiceExceptionThrownBy( | ||
new Aggregation.Builder() | ||
.max(max) | ||
.aggregations(ImmutableMap.of("bountiful", bountifulAggregation))::build) | ||
.hasType(MY_ERROR_TYPE); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-8.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Palantir8 { | ||
void foo() { | ||
assertThatServiceExceptionThrownBy(new Aggregation.Builder() | ||
.max(max) | ||
.aggregations(ImmutableMap.of("bountiful", bountifulAggregation))::build) | ||
.hasType(MY_ERROR_TYPE); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
...t/src/test/resources/com/palantir/javaformat/java/testdata/palantir-chains-lambdas.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
public class Foo { | ||
public static void main(String[] args) { | ||
foo.bar().baz(argargargargargargargargargargargargargargargargargargargargarg, param -> | ||
doStuff().method1().method2(arljhfaldjfhalfjhalfjahflahflahfaljfhadlfjhljsahljhfsaljfhlfajhfljh)); | ||
foo.bar().baz(argargargargargargargargargargargargargargargargargargargargarg, param -> doStuff() | ||
.method1() | ||
.method2(arljhfaldjfhalfjhalfjahflahflahfaljfhadlfjhljsahljhfsaljfhlfajhfljh)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ources/com/palantir/javaformat/java/testdata/palantir-lambda-inlining-prefers-break.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class PalantirLambdaInliningPrefersBreak { | ||
void foo() { | ||
endpointDefinition.getDeprecated().ifPresent(documentation -> endpointBuilder.addMethod( | ||
MethodSpec.methodBuilder("deprecated") | ||
.addModifiers(Modifier.PUBLIC) | ||
.addAnnotation(Override.class) | ||
.returns(ParameterizedTypeName.get(ClassName.get(Optional.class), ClassName.get(String.class))) | ||
.addStatement("return $1T.of($2S)", Optional.class, documentation) | ||
.build())); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...urces/com/palantir/javaformat/java/testdata/palantir-lambda-inlining-prefers-break.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class PalantirLambdaInliningPrefersBreak { | ||
void foo() { | ||
endpointDefinition | ||
.getDeprecated() | ||
.ifPresent(documentation -> endpointBuilder.addMethod(MethodSpec.methodBuilder("deprecated") | ||
.addModifiers(Modifier.PUBLIC) | ||
.addAnnotation(Override.class) | ||
.returns(ParameterizedTypeName.get(ClassName.get(Optional.class), ClassName.get(String.class))) | ||
.addStatement("return $1T.of($2S)", Optional.class, documentation) | ||
.build())); | ||
} | ||
} |