Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a text block is outdented, outdent the opening """ too #1178

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
""");
}
}
Loading