diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index 6814054a2..c0b4dc1db 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -65,6 +65,9 @@ /** Wraps string literals that exceed the column limit. */ public final class StringWrapper { + + public static final String TEXT_BLOCK_DELIMITER = "\"\"\""; + /** Reflows long string literals in the given Java source code. */ public static String wrap(String input, Formatter formatter) throws FormatterException { return StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, input, formatter); @@ -162,7 +165,7 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { return null; } int pos = getStartPosition(literalTree); - if (input.substring(pos, min(input.length(), pos + 3)).equals("\"\"\"")) { + if (input.substring(pos, min(input.length(), pos + 3)).equals(TEXT_BLOCK_DELIMITER)) { textBlocks.add(literalTree); return null; } @@ -206,7 +209,7 @@ private void indentTextBlocks( ? "" : " ".repeat(startColumn - 1); - StringBuilder output = new StringBuilder("\"\"\""); + StringBuilder output = new StringBuilder(TEXT_BLOCK_DELIMITER); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); String trimmed = line.stripLeading().stripTrailing(); @@ -215,11 +218,16 @@ private void indentTextBlocks( // Don't add incidental leading whitespace to empty lines output.append(prefix); } - if (i == lines.size() - 1 && trimmed.equals("\"\"\"")) { - // If the trailing line is just """, indenting is more than the prefix of incidental + if (i == lines.size() - 1) { + String withoutDelimiter = + trimmed.substring(0, trimmed.length() - TEXT_BLOCK_DELIMITER.length()); + if (!withoutDelimiter.isEmpty()) { + output.append(withoutDelimiter).append('\\').append(separator).append(prefix); + } + // If the trailing line is just """, indenting it more than the prefix of incidental // whitespace has no effect, and results in a javac text-blocks warning that 'trailing // white space will be removed'. - output.append("\"\"\""); + output.append(TEXT_BLOCK_DELIMITER); } else { output.append(line); } @@ -482,7 +490,7 @@ private static boolean needWrapping(int columnLimit, String input) { Iterator it = Newlines.lineIterator(input); while (it.hasNext()) { String line = it.next(); - if (line.length() > columnLimit || line.contains("\"\"\"")) { + if (line.length() > columnLimit || line.contains(TEXT_BLOCK_DELIMITER)) { return true; } } diff --git a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java index fd176ed4e..7854126c4 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java @@ -65,7 +65,8 @@ public void textBlock() throws Exception { " String str =", " \"\"\"", "{\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint" - + "\":\"ri.something.1-1.object-internal.2\",\"typeId\":\"typeId\"}\"\"\";", + + "\":\"ri.something.1-1.object-internal.2\",\"typeId\":\"typeId\"}\\", + "\"\"\";", " myString = str;", " }", "}"); diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output index 5ca1fb8cc..5f59bc5ea 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output @@ -23,7 +23,8 @@ class RSLs { """; String f = """ - ipsum"""; + ipsum\ + """; String g = """ lorem\