Skip to content

Commit 8ff95bf

Browse files
dansanduleacbulldozer-bot[bot]
authored andcommitted
Improvement: better inlining of method reference (#44)
Prefer inlining method references after whatever expression they follow.
1 parent 8fed14b commit 8ff95bf

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

changelog/@unreleased/pr-44.v2.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: improvement
2+
improvement:
3+
description: Prefer inlining method references after whatever expression they follow.
4+
links:
5+
- https://github.com/palantir/palantir-java-format/pull/44

palantir-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private Optional<State> tryBreakLastLevel(
296296
// In this case, recurse rather than go into computeBreaks.
297297
if (lastLevel.breakabilityIfLastLevel == LastLevelBreakability.CHECK_INNER) {
298298
// Try to fit the entire inner prefix if it's that kind of level.
299-
Optional<Optional<State>> couldBreakRecursively = BreakBehaviours.caseOf(lastLevel.breakBehaviour)
299+
return BreakBehaviours.caseOf(lastLevel.breakBehaviour)
300300
.preferBreakingLastInnerLevel(keepIndentWhenInlined -> {
301301
State state2 = state1;
302302
if (keepIndentWhenInlined) {
@@ -305,10 +305,7 @@ private Optional<State> tryBreakLastLevel(
305305
return lastLevel.tryBreakLastLevel(commentsHelper, maxWidth, state2, true);
306306
})
307307
// We don't know how to fit the inner level on the same line, so bail out.
308-
.otherwiseEmpty();
309-
if (couldBreakRecursively.isPresent()) {
310-
return couldBreakRecursively.get();
311-
}
308+
.otherwise_(Optional.empty());
312309

313310
} else if (lastLevel.breakabilityIfLastLevel == LastLevelBreakability.ONLY_IF_FIRST_LEVEL_FITS) {
314311
// Otherwise, we may be able to check if the first inner level of the lastLevel fits.

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,15 @@ public boolean visitEnumDeclaration(ClassTree node) {
899899
@Override
900900
public Void visitMemberReference(MemberReferenceTree node, Void unused) {
901901
sync(node);
902-
builder.open(plusFour);
902+
builder.open(OpenOp.builder()
903+
.plusIndent(plusFour)
904+
.debugName("methodReference")
905+
// Would like to use CHECK_INNER but we'd have to check in the _first_ level rather than the last
906+
// level, which the current logic can't do yet.
907+
.breakabilityIfLastLevel(LastLevelBreakability.BREAK_HERE)
908+
.build());
903909
scan(node.getQualifierExpression(), null);
910+
builder.open(ZERO);
904911
builder.breakOp();
905912
builder.op("::");
906913
addTypeArguments(node.getTypeArguments(), plusFour);
@@ -915,6 +922,7 @@ public Void visitMemberReference(MemberReferenceTree node, Void unused) {
915922
throw new AssertionError(node.getMode());
916923
}
917924
builder.close();
925+
builder.close();
918926
return null;
919927
}
920928

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
class PalantirGcv1 {
22
public static void main(String[] args) {
3-
constraintsProperty.addAll(project.provider(
4-
Suppliers.memoize(() -> {
5-
log.debug(
6-
"Computing publish constraints for {} by resolving {}",
7-
configuration.get(),
8-
configurationForFiltering.get());
9-
Set<ModuleIdentifier> modulesToInclude =
10-
configurationForFiltering
11-
.get()
12-
.getIncoming()
13-
.getResolutionResult()
14-
.getAllComponents()
15-
.stream()
16-
.map(ResolvedComponentResult::getModuleVersion)
17-
.filter(Objects::nonNull)
18-
.map(ModuleVersionIdentifier::getModule)
19-
.collect(Collectors.toSet());
20-
return Collections2.filter(publishConstraints, constraint ->
21-
modulesToInclude.contains(constraint.getModule()));
22-
})
23-
::get));
3+
constraintsProperty.addAll(project.provider(Suppliers.memoize(() -> {
4+
log.debug(
5+
"Computing publish constraints for {} by resolving {}",
6+
configuration.get(),
7+
configurationForFiltering.get());
8+
Set<ModuleIdentifier> modulesToInclude =
9+
configurationForFiltering.get().getIncoming().getResolutionResult().getAllComponents().stream()
10+
.map(ResolvedComponentResult::getModuleVersion)
11+
.filter(Objects::nonNull)
12+
.map(ModuleVersionIdentifier::getModule)
13+
.collect(Collectors.toSet());
14+
return Collections2.filter(
15+
publishConstraints, constraint -> modulesToInclude.contains(constraint.getModule()));
16+
})::get));
2417
}
2518
}

0 commit comments

Comments
 (0)