Skip to content

Commit

Permalink
Place the trailing """ of a text block to appear on its own line
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 689841044
  • Loading branch information
cushon authored and google-java-format Team committed Oct 28, 2024
1 parent 6586afe commit e49cf3f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -482,7 +490,7 @@ private static boolean needWrapping(int columnLimit, String input) {
Iterator<String> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;",
" }",
"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class RSLs {
""";
String f =
"""
ipsum""";
ipsum\
""";
String g =
"""
lorem\
Expand Down

0 comments on commit e49cf3f

Please sign in to comment.