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

Improvement: better inlining of method reference #44

Merged
merged 3 commits into from
Oct 21, 2019
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
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));
}
}