diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java index f51ad6c59..ecf5f18b4 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java @@ -83,9 +83,12 @@ String writeFormatReplacements(List replacements) throws FormatterE private String formatFile(Formatter formatter) throws FormatterException { String formatted = formatter.formatSource(input, characterRanges(input).asRanges()); + boolean unchanged = formatted.equals(input); formatted = fixImports(formatted); if (parameters.reflowLongStrings()) { - formatted = StringWrapper.wrap(options.maxLineLength(), formatted, formatter); + formatted = unchanged && formatted.equals(input) + ? StringWrapper.wrapFormatted(options.maxLineLength(), formatted, formatter) + : StringWrapper.wrap(options.maxLineLength(), formatted, formatter); } return formatted; } diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java b/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java index d26d8d621..19388409e 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java @@ -308,7 +308,9 @@ public String formatSourceAndFixImports(String input) throws FormatterException input = ImportOrderer.reorderImports(input, options.style()); input = RemoveUnusedImports.removeUnusedImports(input); String formatted = formatSource(input); - return StringWrapper.wrap(options.maxLineLength(), formatted, this); + return formatted.equals(input) + ? StringWrapper.wrapFormatted(options.maxLineLength(), formatted, this) + : StringWrapper.wrap(options.maxLineLength(), formatted, this); } /** diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java b/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java index 110a7067f..205881096 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java @@ -74,7 +74,18 @@ public final class StringWrapper { * adjacent string literals that share a line. */ static String wrap(final int columnLimit, String input, Formatter formatter) throws FormatterException { - return wrap(columnLimit, new Source(input, null), formatter).text(); + return wrap(columnLimit, new Source(input, null), formatter, false).text(); + } + + /** + * Reflows, as {@link #wrap(int, String, Formatter)} does, a file that formatting leaves as it is: one the + * formatter has returned unchanged. Formatting the lines of its strings would change nothing either, so the first + * round does not. The output of formatting is not enough: formatting it again can still lay out a chain of + * {@code +} another way. + */ + static String wrapFormatted(final int columnLimit, String formatted, Formatter formatter) + throws FormatterException { + return wrap(columnLimit, new Source(formatted, null), formatter, true).text(); } /** @@ -84,19 +95,23 @@ static String wrap(final int columnLimit, String input, Formatter formatter) thr */ static String wrap(final int columnLimit, String input, RangeSet characterRanges, Formatter formatter) throws FormatterException { - return wrap(columnLimit, new Source(input, characterRanges), formatter).text(); + return wrap(columnLimit, new Source(input, characterRanges), formatter, false) + .text(); } - private static Source wrap(final int columnLimit, Source input, Formatter formatter) throws FormatterException { - Source result = wrapOnce(columnLimit, input, formatter); + private static Source wrap(final int columnLimit, Source input, Formatter formatter, boolean formatted) + throws FormatterException { + Source result = wrapOnce(columnLimit, input, formatter, formatted); for (int round = 1; round < MAX_ROUNDS && !result.text().equals(input.text()); round++) { input = result; - result = wrapOnce(columnLimit, input, formatter); + // A round may reflow again what its second pass formatted, so what it leaves is not always formatted. + result = wrapOnce(columnLimit, input, formatter, false); } return result; } - private static Source wrapOnce(final int columnLimit, Source input, Formatter formatter) throws FormatterException { + private static Source wrapOnce(final int columnLimit, Source input, Formatter formatter, boolean formatted) + throws FormatterException { if (!needWrapping(columnLimit, input.text())) { // fast path return input; @@ -110,13 +125,25 @@ private static Source wrapOnce(final int columnLimit, Source input, Formatter fo // needWrapping is an over-approximation, so there may be nothing to do after all. return input; } - Source firstPass = input.format(formatter, replacements.asMapOfRanges().keySet()); - if (!firstPass.text().equals(input.text())) { - // If formatting the replacement ranges resulted in a change, recalculate the replacements on - // the updated input. - input = firstPass; - replacements = input.reflowReplacements(columnLimit, continuationIndent); + // Whether formatting the replacement ranges leaves the input as it is. + var rangesFormatted = formatted; + if (!rangesFormatted) { + Source firstPass = + input.format(formatter, replacements.asMapOfRanges().keySet()); + if (firstPass.text().equals(input.text())) { + rangesFormatted = true; + } else { + // If formatting the replacement ranges resulted in a change, recalculate the replacements on + // the updated input. + input = firstPass; + replacements = input.reflowReplacements(columnLimit, continuationIndent); + } + } + if (rangesFormatted && input.unchangedBy(replacements)) { + // Nothing to change, such as text blocks that are indented already: the second pass would format the same + // ranges of the same text again, and the check below would compare the text with itself. + return input; } Source result = input.apply(replacements); @@ -176,6 +203,15 @@ Source format(Formatter formatter, Collection> rangesToFormat) th return new Source(Utils.applyReplacements(text, replacements), moved(replacements)); } + /** Whether every replacement is the text it replaces. */ + boolean unchangedBy(TreeRangeMap replacements) { + return replacements.asMapOfRanges().entrySet().stream() + .allMatch(entry -> text.substring( + entry.getKey().lowerEndpoint(), + entry.getKey().upperEndpoint()) + .equals(entry.getValue())); + } + Source apply(TreeRangeMap replacements) { List applied = replacements.asMapOfRanges().entrySet().stream() .map(entry -> Replacement.create( @@ -1055,6 +1091,14 @@ private static boolean isOctalDigit(char character) { return character >= '0' && character <= '7'; } + /** + * Whether a line may hold a string literal past the column limit: it is longer than the limit and has a quote on + * it. A long line without one, such as a comment with a URL or an import, holds no literal to reflow. + */ + private static boolean mayHaveLongLiteral(int columnLimit, String line) { + return line.length() > columnLimit && line.indexOf('"') != -1; + } + /** * Whether a line may hold two adjacent string literals to join: a {@code +} with a quote right before and after * it, blanks aside. A cheap over-approximation that keeps the fast paths fast; the {@link Reflower} decides what @@ -1094,15 +1138,15 @@ private static int getStartPosition(Tree tree) { } /** - * Returns true if any lines in the given Java source exceed the column limit, contain text blocks or may hold - * adjacent string literals to join. Keep this method and {@code linesNeedWrapping} in line. + * Returns true if any lines in the given Java source may hold string literals past the column limit, contain text + * blocks or may hold adjacent string literals to join. Keep this method and {@code linesNeedWrapping} in line. * */ private static boolean needWrapping(int columnLimit, String input) { // TODO(cushon): consider adding Newlines.lineIterable? Iterator it = Newlines.lineIterator(input); while (it.hasNext()) { String line = it.next(); - if (line.length() > columnLimit + if (mayHaveLongLiteral(columnLimit, line) || line.contains(TEXT_BLOCK_DELIMITER) // Literals joined into a line that fits leave no over-long line to set off the wrapping. || mayHaveJoinableLiterals(line)) { @@ -1113,9 +1157,10 @@ private static boolean needWrapping(int columnLimit, String input) { } /** - * Returns true if the lines containing the {@code initialRangesToChange} in the given Java source exceed the - * column limit, contain text blocks or may hold adjacent string literals to join. Used by the Intellij plugin to - * check if we need to run StringWrapper.wrap. Keep this method and {@code needWrapping} in line. + * Returns true if the lines containing the {@code initialRangesToChange} in the given Java source may hold string + * literals past the column limit, contain text blocks or may hold adjacent string literals to join. Used by the + * Intellij plugin to check if we need to run StringWrapper.wrap. Keep this method and {@code needWrapping} in + * line. * */ public static boolean linesNeedWrapping(int columnLimit, String input, RangeSet initialRangesToChange) { // TODO(cushon): consider adding Newlines.lineIterable? @@ -1125,7 +1170,7 @@ public static boolean linesNeedWrapping(int columnLimit, String input, RangeSet< boolean insideTextBlock = false; while (it.hasNext()) { String line = it.next(); - if (line.length() > columnLimit || mayHaveJoinableLiterals(line)) { + if (mayHaveLongLiteral(columnLimit, line) || mayHaveJoinableLiterals(line)) { linesToChange.add(Range.closedOpen(i, i + 1)); } if (!insideTextBlock && line.contains(TEXT_BLOCK_DELIMITER)) { diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java index 34e466178..fcb3ff7ee 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java @@ -16,6 +16,7 @@ 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; @@ -126,6 +127,82 @@ class T { .isEqualTo(StringWrapper.wrap(100, input, Formatter.create())); } + @Test + void stopsAReflowThatWouldMoveACastToTheFirstPiece() { + // From google-java-format#1533: in `(Object) "a" + "b"` the cast takes only "a". + String input = """ + class T { + Object o = + (Object) + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt"; + } + """; + + FormatterException e = + assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create())); + + assertThat(e).hasMessageThat().contains("Something has gone terribly wrong"); + } + + @Test + void stopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece() { + // From google-java-format#1533: in `"a" + "b".new Inner()` the creation takes only "b". + String input = """ + class T { + Object o = + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt" + .new Inner(); + } + """; + + FormatterException e = + assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create())); + + assertThat(e).hasMessageThat().contains("Something has gone terribly wrong"); + } + + @Test + void longLinesWithoutAQuoteNeedNoWrapping() { + String comment = """ + class T { + // https://example.com/one/long/incredibly/unbroken/path/moving/from/topic/to/topic/so/that/no/one/had/a/chance + } + """; + String literal = """ + class T { + String s = "https://example.com/one/long/incredibly/unbroken/path/moving/from/topic/to/topic/so/that/no/one"; + } + """; + + assertThat(StringWrapper.linesNeedWrapping(100, comment, wholeFile(comment))) + .isFalse(); + assertThat(StringWrapper.linesNeedWrapping(100, literal, wholeFile(literal))) + .isTrue(); + } + + @Test + void wrapsAFileThatFormattingLeavesAsItIsAsItWrapsAnyOther() throws Exception { + // wrapFormatted leaves out formatting the lines of the strings first, which changes nothing in such a file. + Formatter formatter = Formatter.create(); + String formatted = formatter.formatSource(""" + class T { + String s = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance"; + String t = \""" + a text block + \"""; + void f() { + foo("one long incredibly unbroken sentence moving from topic to topic so that no one had a chance", t); + } + } + """); + assertThat(formatter.formatSource(formatted)).isEqualTo(formatted); + + String output = StringWrapper.wrapFormatted(100, formatted, formatter); + + assertThat(output).isNotEqualTo(formatted); + assertThat(output).isEqualTo(StringWrapper.wrap(100, formatted, formatter)); + } + @Test void movesRangesAlongWithTheReplacements() { // Characters 4 to 8 become two, and two are inserted at 12. @@ -172,6 +249,10 @@ private static String concatenation(ExpressionTree expression) { return (String) ((LiteralTree) expression).getValue(); } + private static RangeSet wholeFile(String input) { + return TreeRangeSet.create(List.of(Range.closedOpen(0, input.length()))); + } + private static String lines(String... line) { return Joiner.on('\n').join(line) + '\n'; }