Skip to content

Commit 60e0c49

Browse files
committed
Skip the StringWrapper work that cannot change the file
A long line without a quote on it holds no string literal to reflow, so needWrapping and linesNeedWrapping no longer parse the file for a long comment or import. When every replacement is the text it replaces, as for a text block that is indented already, and formatting their ranges changed nothing, wrapOnce returns: the second pass would format the same ranges of the same text, and the AST check would compare the text with itself. The first pass is left out as well when formatting returned the file unchanged, which formatSourceAndFixImports and the command line know. The formatter's output alone is not enough to leave out the first pass, as google-java-format#1533 does: formatting it again lays out a chain of + another way in 5 of 6170 files of the JDK and of dependency sources, and the first pass picks that up. Adds the two tests of google-java-format#1533 for the AST check.
1 parent 3453f13 commit 60e0c49

4 files changed

Lines changed: 152 additions & 21 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ String writeFormatReplacements(List<Replacement> replacements) throws FormatterE
8383

8484
private String formatFile(Formatter formatter) throws FormatterException {
8585
String formatted = formatter.formatSource(input, characterRanges(input).asRanges());
86+
boolean unchanged = formatted.equals(input);
8687
formatted = fixImports(formatted);
8788
if (parameters.reflowLongStrings()) {
88-
formatted = StringWrapper.wrap(options.maxLineLength(), formatted, formatter);
89+
formatted = unchanged && formatted.equals(input)
90+
? StringWrapper.wrapFormatted(options.maxLineLength(), formatted, formatter)
91+
: StringWrapper.wrap(options.maxLineLength(), formatted, formatter);
8992
}
9093
return formatted;
9194
}

‎open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ public String formatSourceAndFixImports(String input) throws FormatterException
308308
input = ImportOrderer.reorderImports(input, options.style());
309309
input = RemoveUnusedImports.removeUnusedImports(input);
310310
String formatted = formatSource(input);
311-
return StringWrapper.wrap(options.maxLineLength(), formatted, this);
311+
return formatted.equals(input)
312+
? StringWrapper.wrapFormatted(options.maxLineLength(), formatted, this)
313+
: StringWrapper.wrap(options.maxLineLength(), formatted, this);
312314
}
313315

314316
/**

‎open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java‎

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,18 @@ public final class StringWrapper {
7474
* adjacent string literals that share a line.
7575
*/
7676
static String wrap(final int columnLimit, String input, Formatter formatter) throws FormatterException {
77-
return wrap(columnLimit, new Source(input, null), formatter).text();
77+
return wrap(columnLimit, new Source(input, null), formatter, false).text();
78+
}
79+
80+
/**
81+
* Reflows, as {@link #wrap(int, String, Formatter)} does, a file that formatting leaves as it is: one the
82+
* formatter has returned unchanged. Formatting the lines of its strings would change nothing either, so the first
83+
* round does not. The output of formatting is not enough: formatting it again can still lay out a chain of
84+
* {@code +} another way.
85+
*/
86+
static String wrapFormatted(final int columnLimit, String formatted, Formatter formatter)
87+
throws FormatterException {
88+
return wrap(columnLimit, new Source(formatted, null), formatter, true).text();
7889
}
7990

8091
/**
@@ -84,19 +95,23 @@ static String wrap(final int columnLimit, String input, Formatter formatter) thr
8495
*/
8596
static String wrap(final int columnLimit, String input, RangeSet<Integer> characterRanges, Formatter formatter)
8697
throws FormatterException {
87-
return wrap(columnLimit, new Source(input, characterRanges), formatter).text();
98+
return wrap(columnLimit, new Source(input, characterRanges), formatter, false)
99+
.text();
88100
}
89101

90-
private static Source wrap(final int columnLimit, Source input, Formatter formatter) throws FormatterException {
91-
Source result = wrapOnce(columnLimit, input, formatter);
102+
private static Source wrap(final int columnLimit, Source input, Formatter formatter, boolean formatted)
103+
throws FormatterException {
104+
Source result = wrapOnce(columnLimit, input, formatter, formatted);
92105
for (int round = 1; round < MAX_ROUNDS && !result.text().equals(input.text()); round++) {
93106
input = result;
94-
result = wrapOnce(columnLimit, input, formatter);
107+
// A round may reflow again what its second pass formatted, so what it leaves is not always formatted.
108+
result = wrapOnce(columnLimit, input, formatter, false);
95109
}
96110
return result;
97111
}
98112

99-
private static Source wrapOnce(final int columnLimit, Source input, Formatter formatter) throws FormatterException {
113+
private static Source wrapOnce(final int columnLimit, Source input, Formatter formatter, boolean formatted)
114+
throws FormatterException {
100115
if (!needWrapping(columnLimit, input.text())) {
101116
// fast path
102117
return input;
@@ -110,13 +125,25 @@ private static Source wrapOnce(final int columnLimit, Source input, Formatter fo
110125
// needWrapping is an over-approximation, so there may be nothing to do after all.
111126
return input;
112127
}
113-
Source firstPass = input.format(formatter, replacements.asMapOfRanges().keySet());
114128

115-
if (!firstPass.text().equals(input.text())) {
116-
// If formatting the replacement ranges resulted in a change, recalculate the replacements on
117-
// the updated input.
118-
input = firstPass;
119-
replacements = input.reflowReplacements(columnLimit, continuationIndent);
129+
// Whether formatting the replacement ranges leaves the input as it is.
130+
var rangesFormatted = formatted;
131+
if (!rangesFormatted) {
132+
Source firstPass =
133+
input.format(formatter, replacements.asMapOfRanges().keySet());
134+
if (firstPass.text().equals(input.text())) {
135+
rangesFormatted = true;
136+
} else {
137+
// If formatting the replacement ranges resulted in a change, recalculate the replacements on
138+
// the updated input.
139+
input = firstPass;
140+
replacements = input.reflowReplacements(columnLimit, continuationIndent);
141+
}
142+
}
143+
if (rangesFormatted && input.unchangedBy(replacements)) {
144+
// Nothing to change, such as text blocks that are indented already: the second pass would format the same
145+
// ranges of the same text again, and the check below would compare the text with itself.
146+
return input;
120147
}
121148

122149
Source result = input.apply(replacements);
@@ -176,6 +203,15 @@ Source format(Formatter formatter, Collection<Range<Integer>> rangesToFormat) th
176203
return new Source(Utils.applyReplacements(text, replacements), moved(replacements));
177204
}
178205

206+
/** Whether every replacement is the text it replaces. */
207+
boolean unchangedBy(TreeRangeMap<Integer, String> replacements) {
208+
return replacements.asMapOfRanges().entrySet().stream()
209+
.allMatch(entry -> text.substring(
210+
entry.getKey().lowerEndpoint(),
211+
entry.getKey().upperEndpoint())
212+
.equals(entry.getValue()));
213+
}
214+
179215
Source apply(TreeRangeMap<Integer, String> replacements) {
180216
List<Replacement> applied = replacements.asMapOfRanges().entrySet().stream()
181217
.map(entry -> Replacement.create(
@@ -1055,6 +1091,14 @@ private static boolean isOctalDigit(char character) {
10551091
return character >= '0' && character <= '7';
10561092
}
10571093

1094+
/**
1095+
* Whether a line may hold a string literal past the column limit: it is longer than the limit and has a quote on
1096+
* it. A long line without one, such as a comment with a URL or an import, holds no literal to reflow.
1097+
*/
1098+
private static boolean mayHaveLongLiteral(int columnLimit, String line) {
1099+
return line.length() > columnLimit && line.indexOf('"') != -1;
1100+
}
1101+
10581102
/**
10591103
* Whether a line may hold two adjacent string literals to join: a {@code +} with a quote right before and after
10601104
* 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) {
10941138
}
10951139

10961140
/**
1097-
* Returns true if any lines in the given Java source exceed the column limit, contain text blocks or may hold
1098-
* adjacent string literals to join. Keep this method and {@code linesNeedWrapping} in line.
1141+
* Returns true if any lines in the given Java source may hold string literals past the column limit, contain text
1142+
* blocks or may hold adjacent string literals to join. Keep this method and {@code linesNeedWrapping} in line.
10991143
* */
11001144
private static boolean needWrapping(int columnLimit, String input) {
11011145
// TODO(cushon): consider adding Newlines.lineIterable?
11021146
Iterator<String> it = Newlines.lineIterator(input);
11031147
while (it.hasNext()) {
11041148
String line = it.next();
1105-
if (line.length() > columnLimit
1149+
if (mayHaveLongLiteral(columnLimit, line)
11061150
|| line.contains(TEXT_BLOCK_DELIMITER)
11071151
// Literals joined into a line that fits leave no over-long line to set off the wrapping.
11081152
|| mayHaveJoinableLiterals(line)) {
@@ -1113,9 +1157,10 @@ private static boolean needWrapping(int columnLimit, String input) {
11131157
}
11141158

11151159
/**
1116-
* Returns true if the lines containing the {@code initialRangesToChange} in the given Java source exceed the
1117-
* column limit, contain text blocks or may hold adjacent string literals to join. Used by the Intellij plugin to
1118-
* check if we need to run StringWrapper.wrap. Keep this method and {@code needWrapping} in line.
1160+
* Returns true if the lines containing the {@code initialRangesToChange} in the given Java source may hold string
1161+
* literals past the column limit, contain text blocks or may hold adjacent string literals to join. Used by the
1162+
* Intellij plugin to check if we need to run StringWrapper.wrap. Keep this method and {@code needWrapping} in
1163+
* line.
11191164
* */
11201165
public static boolean linesNeedWrapping(int columnLimit, String input, RangeSet<Integer> initialRangesToChange) {
11211166
// TODO(cushon): consider adding Newlines.lineIterable?
@@ -1125,7 +1170,7 @@ public static boolean linesNeedWrapping(int columnLimit, String input, RangeSet<
11251170
boolean insideTextBlock = false;
11261171
while (it.hasNext()) {
11271172
String line = it.next();
1128-
if (line.length() > columnLimit || mayHaveJoinableLiterals(line)) {
1173+
if (mayHaveLongLiteral(columnLimit, line) || mayHaveJoinableLiterals(line)) {
11291174
linesToChange.add(Range.closedOpen(i, i + 1));
11301175
}
11311176
if (!insideTextBlock && line.contains(TEXT_BLOCK_DELIMITER)) {

‎open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

2021
import com.google.common.base.Joiner;
2122
import com.google.common.collect.Iterables;
@@ -126,6 +127,82 @@ class T {
126127
.isEqualTo(StringWrapper.wrap(100, input, Formatter.create()));
127128
}
128129

130+
@Test
131+
void stopsAReflowThatWouldMoveACastToTheFirstPiece() {
132+
// From google-java-format#1533: in `(Object) "a" + "b"` the cast takes only "a".
133+
String input = """
134+
class T {
135+
Object o =
136+
(Object)
137+
"one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt";
138+
}
139+
""";
140+
141+
FormatterException e =
142+
assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create()));
143+
144+
assertThat(e).hasMessageThat().contains("Something has gone terribly wrong");
145+
}
146+
147+
@Test
148+
void stopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece() {
149+
// From google-java-format#1533: in `"a" + "b".new Inner()` the creation takes only "b".
150+
String input = """
151+
class T {
152+
Object o =
153+
"one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt"
154+
.new Inner();
155+
}
156+
""";
157+
158+
FormatterException e =
159+
assertThrows(FormatterException.class, () -> StringWrapper.wrap(100, input, Formatter.create()));
160+
161+
assertThat(e).hasMessageThat().contains("Something has gone terribly wrong");
162+
}
163+
164+
@Test
165+
void longLinesWithoutAQuoteNeedNoWrapping() {
166+
String comment = """
167+
class T {
168+
// https://example.com/one/long/incredibly/unbroken/path/moving/from/topic/to/topic/so/that/no/one/had/a/chance
169+
}
170+
""";
171+
String literal = """
172+
class T {
173+
String s = "https://example.com/one/long/incredibly/unbroken/path/moving/from/topic/to/topic/so/that/no/one";
174+
}
175+
""";
176+
177+
assertThat(StringWrapper.linesNeedWrapping(100, comment, wholeFile(comment)))
178+
.isFalse();
179+
assertThat(StringWrapper.linesNeedWrapping(100, literal, wholeFile(literal)))
180+
.isTrue();
181+
}
182+
183+
@Test
184+
void wrapsAFileThatFormattingLeavesAsItIsAsItWrapsAnyOther() throws Exception {
185+
// wrapFormatted leaves out formatting the lines of the strings first, which changes nothing in such a file.
186+
Formatter formatter = Formatter.create();
187+
String formatted = formatter.formatSource("""
188+
class T {
189+
String s = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance";
190+
String t = \"""
191+
a text block
192+
\""";
193+
void f() {
194+
foo("one long incredibly unbroken sentence moving from topic to topic so that no one had a chance", t);
195+
}
196+
}
197+
""");
198+
assertThat(formatter.formatSource(formatted)).isEqualTo(formatted);
199+
200+
String output = StringWrapper.wrapFormatted(100, formatted, formatter);
201+
202+
assertThat(output).isNotEqualTo(formatted);
203+
assertThat(output).isEqualTo(StringWrapper.wrap(100, formatted, formatter));
204+
}
205+
129206
@Test
130207
void movesRangesAlongWithTheReplacements() {
131208
// Characters 4 to 8 become two, and two are inserted at 12.
@@ -172,6 +249,10 @@ private static String concatenation(ExpressionTree expression) {
172249
return (String) ((LiteralTree) expression).getValue();
173250
}
174251

252+
private static RangeSet<Integer> wholeFile(String input) {
253+
return TreeRangeSet.create(List.of(Range.closedOpen(0, input.length())));
254+
}
255+
175256
private static String lines(String... line) {
176257
return Joiner.on('\n').join(line) + '\n';
177258
}

0 commit comments

Comments
 (0)