Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import com.palantir.javaformat.Utils;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.TypeCastTree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.tree.JCTree;
Expand Down Expand Up @@ -437,9 +440,7 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
textBlocks.add(getCurrentPath());
return null;
}
Tree parent = getCurrentPath().getParentPath().getLeaf();
if (parent instanceof MemberSelectTree memberSelectTree
&& memberSelectTree.getExpression().equals(literalTree)) {
if (bindsTighterThanPlus(getCurrentPath().getParentPath().getLeaf(), literalTree)) {
return null;
}
int endPosition = getEndPosition(unit, literalTree);
Expand All @@ -455,6 +456,22 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
}
}

/**
* Whether the parent of a literal applies to it more tightly than {@code +} does: as a member select
* ({@code "…".length()}), a cast, a qualified {@code .new} or a method reference. Reflowed into
* {@code "a" + "b"}, the literal would leave that parent only one of its pieces, as in
* {@code (Object) "a" + "b"}, so it stays whole.
*/
private static boolean bindsTighterThanPlus(Tree parent, LiteralTree literal) {
return (parent instanceof MemberSelectTree select
&& select.getExpression().equals(literal))
|| (parent instanceof TypeCastTree cast
&& cast.getExpression().equals(literal))
|| (parent instanceof NewClassTree newClass && literal.equals(newClass.getEnclosingExpression()))
|| (parent instanceof MemberReferenceTree reference
&& reference.getQualifierExpression().equals(literal));
}

private void indentTextBlocks(TreeRangeMap<Integer, String> replacements, List<TreePath> textBlocks) {
// pjf specific - compute textBlock's parents & store indentation info for each parent
Map<TreePath, String> textBlockToIndent = computeCustomTextBlocksIndent(textBlocks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.truth.Truth.assertThat;
import static com.palantir.javaformat.java.JavaFormatterOptions.Style;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -128,8 +127,8 @@ class T {
}

@Test
void stopsAReflowThatWouldMoveACastToTheFirstPiece() {
// From google-java-format#1533: in `(Object) "a" + "b"` the cast takes only "a".
void stopsAReflowThatWouldMoveACastToTheFirstPiece() throws Exception {
// From google-java-format#1533: in `(Object) "a" + "b"` the cast takes only "a", so the literal stays whole.
String input = """
class T {
Object o =
Expand All @@ -138,15 +137,13 @@ class T {
}
""";

FormatterException e =
assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create()));

assertThat(e).hasMessageThat().contains("Something has gone terribly wrong");
assertThat(StringWrapper.wrap(100, input, Formatter.create())).isEqualTo(input);
}

@Test
void stopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece() {
// From google-java-format#1533: in `"a" + "b".new Inner()` the creation takes only "b".
void stopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece() throws Exception {
// From google-java-format#1533: in `"a" + "b".new Inner()` the creation takes only "b", so the literal stays
// whole.
String input = """
class T {
Object o =
Expand All @@ -155,10 +152,21 @@ class T {
}
""";

FormatterException e =
assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create()));
assertThat(StringWrapper.wrap(100, input, Formatter.create())).isEqualTo(input);
}

@Test
void stopsAReflowThatWouldMoveAMethodReferenceToTheLastPiece() throws Exception {
// In `"a" + "b"::length` the method reference takes only "b", so the literal stays whole.
String input = """
class T {
java.util.function.IntSupplier s =
"one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt"
::length;
}
""";

assertThat(e).hasMessageThat().contains("Something has gone terribly wrong");
assertThat(StringWrapper.wrap(100, input, Formatter.create())).isEqualTo(input);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# A long literal stays whole under a cast, as the enclosing expression of a qualified .new and as the qualifier of a
# method reference, as it does as the expression of a member select: these bind tighter than +, so each would take
# only one piece of the reflowed literal, as in (Object) "a" + "b". A literal in parentheses, or under a +, is still
# reflowed.
----------
class T {
Object o = (Object) "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything";
Object p = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything".new Inner();
java.util.function.IntSupplier s = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"::length;
Object q = (Object) ("one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything");
String fine = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything";
}
----------
class T {
Object o = (Object)
"one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything";
Object p =
"one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"
.new Inner();
java.util.function.IntSupplier s =
"one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt or to say anything"
::length;
Object q =
(Object) ("one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to"
+ " interrupt or to say anything");
String fine =
"one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt"
+ " or to say anything";
}
Loading