diff --git a/changelog/@unreleased/pr-44.v2.yml b/changelog/@unreleased/pr-44.v2.yml new file mode 100644 index 000000000..74e0d7f97 --- /dev/null +++ b/changelog/@unreleased/pr-44.v2.yml @@ -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 diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/doc/Level.java b/palantir-java-format/src/main/java/com/palantir/javaformat/doc/Level.java index 436ce0e44..456e57c95 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/doc/Level.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/doc/Level.java @@ -296,7 +296,7 @@ private Optional 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> couldBreakRecursively = BreakBehaviours.caseOf(lastLevel.breakBehaviour) + return BreakBehaviours.caseOf(lastLevel.breakBehaviour) .preferBreakingLastInnerLevel(keepIndentWhenInlined -> { State state2 = state1; if (keepIndentWhenInlined) { @@ -305,10 +305,7 @@ private Optional 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. diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java index 61147e00d..ac88960ea 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java @@ -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); @@ -915,6 +922,7 @@ public Void visitMemberReference(MemberReferenceTree node, Void unused) { throw new AssertionError(node.getMode()); } builder.close(); + builder.close(); return null; } diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-gcv-1.output b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-gcv-1.output index 6de10b4b1..3b67b5047 100644 --- a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-gcv-1.output +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-gcv-1.output @@ -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 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 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)); } }