6565
6666/** Wraps string literals that exceed the column limit. */
6767public final class StringWrapper {
68+
69+ public static final String TEXT_BLOCK_DELIMITER = "\" \" \" " ;
70+
6871 /** Reflows long string literals in the given Java source code. */
6972 public static String wrap (String input , Formatter formatter ) throws FormatterException {
7073 return StringWrapper .wrap (Formatter .MAX_LINE_LENGTH , input , formatter );
@@ -162,7 +165,7 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
162165 return null ;
163166 }
164167 int pos = getStartPosition (literalTree );
165- if (input .substring (pos , min (input .length (), pos + 3 )).equals (" \" \" \" " )) {
168+ if (input .substring (pos , min (input .length (), pos + 3 )).equals (TEXT_BLOCK_DELIMITER )) {
166169 textBlocks .add (literalTree );
167170 return null ;
168171 }
@@ -206,7 +209,7 @@ private void indentTextBlocks(
206209 ? ""
207210 : " " .repeat (startColumn - 1 );
208211
209- StringBuilder output = new StringBuilder (" \" \" \" " );
212+ StringBuilder output = new StringBuilder (TEXT_BLOCK_DELIMITER );
210213 for (int i = 0 ; i < lines .size (); i ++) {
211214 String line = lines .get (i );
212215 String trimmed = line .stripLeading ().stripTrailing ();
@@ -215,11 +218,16 @@ private void indentTextBlocks(
215218 // Don't add incidental leading whitespace to empty lines
216219 output .append (prefix );
217220 }
218- if (i == lines .size () - 1 && trimmed .equals ("\" \" \" " )) {
219- // If the trailing line is just """, indenting is more than the prefix of incidental
221+ if (i == lines .size () - 1 ) {
222+ String withoutDelimiter =
223+ trimmed .substring (0 , trimmed .length () - TEXT_BLOCK_DELIMITER .length ());
224+ if (!withoutDelimiter .isEmpty ()) {
225+ output .append (withoutDelimiter ).append ('\\' ).append (separator ).append (prefix );
226+ }
227+ // If the trailing line is just """, indenting it more than the prefix of incidental
220228 // whitespace has no effect, and results in a javac text-blocks warning that 'trailing
221229 // white space will be removed'.
222- output .append (" \" \" \" " );
230+ output .append (TEXT_BLOCK_DELIMITER );
223231 } else {
224232 output .append (line );
225233 }
@@ -482,7 +490,7 @@ private static boolean needWrapping(int columnLimit, String input) {
482490 Iterator <String > it = Newlines .lineIterator (input );
483491 while (it .hasNext ()) {
484492 String line = it .next ();
485- if (line .length () > columnLimit || line .contains (" \" \" \" " )) {
493+ if (line .length () > columnLimit || line .contains (TEXT_BLOCK_DELIMITER )) {
486494 return true ;
487495 }
488496 }
0 commit comments