Skip to content

Commit

Permalink
If a text block is outdented, outdent the opening """ too
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 693897793
  • Loading branch information
cushon authored and google-java-format Team committed Nov 8, 2024
1 parent 666fe33 commit d9e4925
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import com.google.googlejavaformat.FormattingError;
import com.google.googlejavaformat.Indent;
import com.google.googlejavaformat.Input;
import com.google.googlejavaformat.Newlines;
import com.google.googlejavaformat.Op;
import com.google.googlejavaformat.OpenOp;
import com.google.googlejavaformat.OpsBuilder;
Expand Down Expand Up @@ -1667,6 +1668,15 @@ public Void visitMemberSelect(MemberSelectTree node, Void unused) {
public Void visitLiteral(LiteralTree node, Void unused) {
sync(node);
String sourceForNode = getSourceForNode(node, getCurrentPath());
if (sourceForNode.endsWith("\"\"\"")
&& (Newlines.hasNewlineAt(sourceForNode, sourceForNode.length() - 4) != -1)) {
// If the closing delimiter of a text block starts at the margin, outdent the opening
// delimiter as well by adding a break with negative indentation. Outdenting for text blocks
// with wide contents is also handled by StringWrapper, but this means the behaviour for
// the opening delimiter is consistent if string wrapping is disabled, and also effectively
// preserves user choice about which text blocks stay de-indented.
builder.breakOp(Indent.Const.make(Integer.MIN_VALUE / indentMultiplier, indentMultiplier));
}
if (isUnaryMinusLiteral(sourceForNode)) {
token("-");
sourceForNode = sourceForNode.substring(1).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
private void indentTextBlocks(
TreeRangeMap<Integer, String> replacements, List<Tree> textBlocks) {
for (Tree tree : textBlocks) {
int startPosition = getStartPosition(tree);
int startPosition = lineMap.getStartPosition(lineMap.getLineNumber(getStartPosition(tree)));
int endPosition = getEndPosition(unit, tree);
String text = input.substring(startPosition, endPosition);
int startColumn = CharMatcher.whitespace().negate().indexIn(text) + 1;

// Find the source code of the text block with incidental whitespace removed.
// The first line of the text block is always """, and it does not affect incidental
Expand All @@ -203,13 +204,12 @@ private void indentTextBlocks(
int deindent =
initialLines.get(1).stripTrailing().length() - lines.get(0).stripTrailing().length();

int startColumn = lineMap.getColumnNumber(startPosition);
String prefix =
(deindent == 0 || lines.stream().anyMatch(x -> x.length() + startColumn > columnLimit))
? ""
: " ".repeat(startColumn - 1);

StringBuilder output = new StringBuilder(TEXT_BLOCK_DELIMITER);
StringBuilder output = new StringBuilder(prefix).append(initialLines.get(0).stripLeading());
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
String trimmed = line.stripLeading().stripTrailing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void textBlock() throws Exception {
" private String myString;",
" private ReproBug() {",
" String str =",
" \"\"\"",
"\"\"\"",
"{\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint"
+ "\":\"ri.something.1-1.object-internal.2\",\"typeId\":\"typeId\"}\\",
"\"\"\";",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class T {
{
f(
/* foo */ """
hello
""");
hello
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,12 @@ ipsum
hello %s
"""
.formatted("world");
f(
/* foo= */ """
foo
""",
/* bar= */ """
bar
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class RSLs {
ipsum
""";
String j =
"""
"""
lorem
one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt
ipsum
""";
String k =
"""
"""
lorem
ipsum
""";
Expand All @@ -65,5 +65,12 @@ ipsum
hello %s
"""
.formatted("world");
f(
/* foo= */ """
foo
""",
/* bar= */ """
bar
""");
}
}

0 comments on commit d9e4925

Please sign in to comment.