Skip to content

Commit

Permalink
Improvement: better inlining of method reference (#44)
Browse files Browse the repository at this point in the history
Prefer inlining method references after whatever expression they follow.
  • Loading branch information
dansanduleac authored and bulldozer-bot[bot] committed Oct 21, 2019
1 parent 8fed14b commit 8ff95bf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-44.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Prefer inlining method references after whatever expression they follow.
links:
- https://github.com/palantir/palantir-java-format/pull/44
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private Optional<State> tryBreakLastLevel(
// In this case, recurse rather than go into computeBreaks.
if (lastLevel.breakabilityIfLastLevel == LastLevelBreakability.CHECK_INNER) {
// Try to fit the entire inner prefix if it's that kind of level.
Optional<Optional<State>> couldBreakRecursively = BreakBehaviours.caseOf(lastLevel.breakBehaviour)
return BreakBehaviours.caseOf(lastLevel.breakBehaviour)
.preferBreakingLastInnerLevel(keepIndentWhenInlined -> {
State state2 = state1;
if (keepIndentWhenInlined) {
Expand All @@ -305,10 +305,7 @@ private Optional<State> tryBreakLastLevel(
return lastLevel.tryBreakLastLevel(commentsHelper, maxWidth, state2, true);
})
// We don't know how to fit the inner level on the same line, so bail out.
.otherwiseEmpty();
if (couldBreakRecursively.isPresent()) {
return couldBreakRecursively.get();
}
.otherwise_(Optional.empty());

} else if (lastLevel.breakabilityIfLastLevel == LastLevelBreakability.ONLY_IF_FIRST_LEVEL_FITS) {
// Otherwise, we may be able to check if the first inner level of the lastLevel fits.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,15 @@ public boolean visitEnumDeclaration(ClassTree node) {
@Override
public Void visitMemberReference(MemberReferenceTree node, Void unused) {
sync(node);
builder.open(plusFour);
builder.open(OpenOp.builder()
.plusIndent(plusFour)
.debugName("methodReference")
// Would like to use CHECK_INNER but we'd have to check in the _first_ level rather than the last
// level, which the current logic can't do yet.
.breakabilityIfLastLevel(LastLevelBreakability.BREAK_HERE)
.build());
scan(node.getQualifierExpression(), null);
builder.open(ZERO);
builder.breakOp();
builder.op("::");
addTypeArguments(node.getTypeArguments(), plusFour);
Expand All @@ -915,6 +922,7 @@ public Void visitMemberReference(MemberReferenceTree node, Void unused) {
throw new AssertionError(node.getMode());
}
builder.close();
builder.close();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
class PalantirGcv1 {
public static void main(String[] args) {
constraintsProperty.addAll(project.provider(
Suppliers.memoize(() -> {
log.debug(
"Computing publish constraints for {} by resolving {}",
configuration.get(),
configurationForFiltering.get());
Set<ModuleIdentifier> modulesToInclude =
configurationForFiltering
.get()
.getIncoming()
.getResolutionResult()
.getAllComponents()
.stream()
.map(ResolvedComponentResult::getModuleVersion)
.filter(Objects::nonNull)
.map(ModuleVersionIdentifier::getModule)
.collect(Collectors.toSet());
return Collections2.filter(publishConstraints, constraint ->
modulesToInclude.contains(constraint.getModule()));
})
::get));
constraintsProperty.addAll(project.provider(Suppliers.memoize(() -> {
log.debug(
"Computing publish constraints for {} by resolving {}",
configuration.get(),
configurationForFiltering.get());
Set<ModuleIdentifier> modulesToInclude =
configurationForFiltering.get().getIncoming().getResolutionResult().getAllComponents().stream()
.map(ResolvedComponentResult::getModuleVersion)
.filter(Objects::nonNull)
.map(ModuleVersionIdentifier::getModule)
.collect(Collectors.toSet());
return Collections2.filter(
publishConstraints, constraint -> modulesToInclude.contains(constraint.getModule()));
})::get));
}
}

0 comments on commit 8ff95bf

Please sign in to comment.