@@ -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 )) {
0 commit comments