Skip to content

Commit 00cce7b

Browse files
cushongoogle-java-format Team
authored andcommitted
Place the trailing """ of a text block to appear on its own line
PiperOrigin-RevId: 690618067
1 parent 6586afe commit 00cce7b

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565

6666
/** Wraps string literals that exceed the column limit. */
6767
public 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
}

core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public void textBlock() throws Exception {
6565
" String str =",
6666
" \"\"\"",
6767
"{\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint"
68-
+ "\":\"ri.something.1-1.object-internal.2\",\"typeId\":\"typeId\"}\"\"\";",
68+
+ "\":\"ri.something.1-1.object-internal.2\",\"typeId\":\"typeId\"}\\",
69+
"\"\"\";",
6970
" myString = str;",
7071
" }",
7172
"}");

core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class RSLs {
2323
""";
2424
String f =
2525
"""
26-
ipsum""";
26+
ipsum\
27+
""";
2728
String g =
2829
"""
2930
lorem\

0 commit comments

Comments
 (0)