Skip to content

Commit 4e0eb5a

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Text block formatting refinements
* Don't automatically deindent long text blocks to start at column 0. For text blocks that are already deindented the existing choice will be preserved, but it won't happen automatically. * For deindented text blocks, put the opening `"""` at column 0 instead of indenting it PiperOrigin-RevId: 739966701
1 parent dae09ec commit 4e0eb5a

File tree

6 files changed

+47
-25
lines changed

6 files changed

+47
-25
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
4343
import static com.sun.source.tree.Tree.Kind.UNION_TYPE;
4444
import static com.sun.source.tree.Tree.Kind.VARIABLE;
45+
import static java.util.stream.Collectors.joining;
4546
import static java.util.stream.Collectors.toList;
4647

4748
import com.google.auto.value.AutoOneOf;
@@ -71,6 +72,7 @@
7172
import com.google.googlejavaformat.FormattingError;
7273
import com.google.googlejavaformat.Indent;
7374
import com.google.googlejavaformat.Input;
75+
import com.google.googlejavaformat.Newlines;
7476
import com.google.googlejavaformat.Op;
7577
import com.google.googlejavaformat.OpenOp;
7678
import com.google.googlejavaformat.OpsBuilder;
@@ -1753,6 +1755,23 @@ public Void visitMemberSelect(MemberSelectTree node, Void unused) {
17531755
public Void visitLiteral(LiteralTree node, Void unused) {
17541756
sync(node);
17551757
String sourceForNode = getSourceForNode(node, getCurrentPath());
1758+
if (sourceForNode.startsWith("\"\"\"")) {
1759+
String separator = Newlines.guessLineSeparator(sourceForNode);
1760+
ImmutableList<String> initialLines = sourceForNode.lines().collect(toImmutableList());
1761+
String stripped = initialLines.stream().skip(1).collect(joining(separator)).stripIndent();
1762+
// Use the last line of the text block to determine if it is deindented to column 0, by
1763+
// comparing the length of the line in the input source with the length after processing
1764+
// the text block contents with stripIndent().
1765+
boolean deindent =
1766+
getLast(initialLines).stripTrailing().length()
1767+
== Streams.findLast(stripped.lines()).orElseThrow().stripTrailing().length();
1768+
if (deindent) {
1769+
Indent indent = Indent.Const.make(Integer.MIN_VALUE / indentMultiplier, indentMultiplier);
1770+
builder.breakOp(indent);
1771+
}
1772+
token(sourceForNode);
1773+
return null;
1774+
}
17561775
if (isUnaryMinusLiteral(sourceForNode)) {
17571776
token("-");
17581777
sourceForNode = sourceForNode.substring(1).trim();

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,24 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
188188
private void indentTextBlocks(
189189
TreeRangeMap<Integer, String> replacements, List<Tree> textBlocks) {
190190
for (Tree tree : textBlocks) {
191-
int startPosition = getStartPosition(tree);
191+
int startPosition = lineMap.getStartPosition(lineMap.getLineNumber(getStartPosition(tree)));
192192
int endPosition = getEndPosition(unit, tree);
193193
String text = input.substring(startPosition, endPosition);
194-
int lineStartPosition = lineMap.getStartPosition(lineMap.getLineNumber(startPosition));
195-
int startColumn =
196-
CharMatcher.whitespace()
197-
.negate()
198-
.indexIn(input.substring(lineStartPosition, endPosition))
199-
+ 1;
194+
int leadingWhitespace = CharMatcher.whitespace().negate().indexIn(text);
200195

201196
// Find the source code of the text block with incidental whitespace removed.
202197
// The first line of the text block is always """, and it does not affect incidental
203198
// whitespace.
204199
ImmutableList<String> initialLines = text.lines().collect(toImmutableList());
205200
String stripped = initialLines.stream().skip(1).collect(joining(separator)).stripIndent();
206201
ImmutableList<String> lines = stripped.lines().collect(toImmutableList());
207-
int deindent =
202+
boolean deindent =
208203
getLast(initialLines).stripTrailing().length()
209-
- getLast(lines).stripTrailing().length();
204+
== getLast(lines).stripTrailing().length();
210205

211-
String prefix =
212-
(deindent == 0
213-
|| lines.stream().anyMatch(x -> x.length() + startColumn - 1 > columnLimit))
214-
? ""
215-
: " ".repeat(startColumn - 1);
206+
String prefix = deindent ? "" : " ".repeat(leadingWhitespace);
216207

217-
StringBuilder output = new StringBuilder(initialLines.get(0).stripLeading());
208+
StringBuilder output = new StringBuilder(prefix).append(initialLines.get(0).stripLeading());
218209
for (int i = 0; i < lines.size(); i++) {
219210
String line = lines.get(i);
220211
String trimmed = line.stripTrailing();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void textBlock() throws Exception {
6363
" private String myString;",
6464
" private ReproBug() {",
6565
" String str =",
66-
" \"\"\"",
66+
"\"\"\"",
6767
"{\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint"
6868
+ "\":\"ri.something.1-1.object-internal.2\",\"typeId\":\"typeId\"}\\",
6969
"\"\"\";",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ class T {
1010
""";
1111
String c =
1212
"""
13-
# No implicit input file, because they can only be created outside a symbolic macro,
14-
""";
13+
# No implicit input file, because they can only be created outside a symbolic macro,
14+
""";
1515
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class RSLs {
4040
lorem
4141
ipsum
4242
""";
43+
String l =
44+
"""
45+
foo
46+
bar
47+
baz""";
4348
{
4449
f("""
4550
lorem

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,20 @@ class RSLs {
4343
""";
4444
String j =
4545
"""
46-
lorem
47-
one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt
48-
ipsum
49-
""";
46+
lorem
47+
one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt
48+
ipsum
49+
""";
5050
String k =
51-
"""
51+
"""
5252
lorem
5353
ipsum
54+
""";
55+
String l =
56+
"""
57+
foo
58+
bar
59+
baz\
5460
""";
5561

5662
{
@@ -85,10 +91,11 @@ ipsum
8591
bar
8692
""";
8793
String t =
88-
"""
94+
"""
8995
foo
9096
"""
91-
+ """
97+
+
98+
"""
9299
bar
93100
""";
94101
String u =

0 commit comments

Comments
 (0)