Skip to content

Commit 02b6ad1

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Back out text block special cases
PiperOrigin-RevId: 695363436
1 parent fc31690 commit 02b6ad1

File tree

9 files changed

+18
-37
lines changed

9 files changed

+18
-37
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ public interface Tok {
7171

7272
/** Is the {@code Tok} a comment? */
7373
boolean isComment();
74-
75-
/** Is the {@code Tok} a text block? */
76-
default boolean isTextBlock() {
77-
return false;
78-
}
7974
}
8075

8176
/** A {@code Token} is a language-level token. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ public final ImmutableList<Op> build() {
534534
space = tokBefore.isSlashStarComment();
535535
newlines = 0;
536536
lastWasComment = true;
537-
if (tokBefore.isJavadocComment() || token.getTok().isTextBlock()) {
537+
if (tokBefore.isJavadocComment()) {
538538
tokOps.put(j, Doc.Break.makeForced());
539539
}
540540
allowBlankAfterLastComment =

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,6 @@ public boolean isComment() {
166166
return isSlashSlashComment() || isSlashStarComment();
167167
}
168168

169-
@Override
170-
public boolean isTextBlock() {
171-
return originalText.startsWith("\"\"\"");
172-
}
173-
174169
@Override
175170
public String toString() {
176171
return MoreObjects.toStringHelper(this)

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import com.google.googlejavaformat.FormattingError;
7171
import com.google.googlejavaformat.Indent;
7272
import com.google.googlejavaformat.Input;
73-
import com.google.googlejavaformat.Newlines;
7473
import com.google.googlejavaformat.Op;
7574
import com.google.googlejavaformat.OpenOp;
7675
import com.google.googlejavaformat.OpsBuilder;
@@ -1668,15 +1667,6 @@ public Void visitMemberSelect(MemberSelectTree node, Void unused) {
16681667
public Void visitLiteral(LiteralTree node, Void unused) {
16691668
sync(node);
16701669
String sourceForNode = getSourceForNode(node, getCurrentPath());
1671-
if (sourceForNode.endsWith("\"\"\"")
1672-
&& (Newlines.hasNewlineAt(sourceForNode, sourceForNode.length() - 4) != -1)) {
1673-
// If the closing delimiter of a text block starts at the margin, outdent the opening
1674-
// delimiter as well by adding a break with negative indentation. Outdenting for text blocks
1675-
// with wide contents is also handled by StringWrapper, but this means the behaviour for
1676-
// the opening delimiter is consistent if string wrapping is disabled, and also effectively
1677-
// preserves user choice about which text blocks stay de-indented.
1678-
builder.breakOp(Indent.Const.make(Integer.MIN_VALUE / indentMultiplier, indentMultiplier));
1679-
}
16801670
if (isUnaryMinusLiteral(sourceForNode)) {
16811671
token("-");
16821672
sourceForNode = sourceForNode.substring(1).trim();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,15 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
190190
private void indentTextBlocks(
191191
TreeRangeMap<Integer, String> replacements, List<Tree> textBlocks) {
192192
for (Tree tree : textBlocks) {
193-
int startPosition = lineMap.getStartPosition(lineMap.getLineNumber(getStartPosition(tree)));
193+
int startPosition = getStartPosition(tree);
194194
int endPosition = getEndPosition(unit, tree);
195195
String text = input.substring(startPosition, endPosition);
196-
int startColumn = CharMatcher.whitespace().negate().indexIn(text) + 1;
196+
int lineStartPosition = lineMap.getStartPosition(lineMap.getLineNumber(startPosition));
197+
int startColumn =
198+
CharMatcher.whitespace()
199+
.negate()
200+
.indexIn(input.substring(lineStartPosition, endPosition))
201+
+ 1;
197202

198203
// Find the source code of the text block with incidental whitespace removed.
199204
// The first line of the text block is always """, and it does not affect incidental
@@ -210,7 +215,7 @@ private void indentTextBlocks(
210215
? ""
211216
: " ".repeat(startColumn - 1);
212217

213-
StringBuilder output = new StringBuilder(prefix).append(initialLines.get(0).stripLeading());
218+
StringBuilder output = new StringBuilder(initialLines.get(0).stripLeading());
214219
for (int i = 0; i < lines.size(); i++) {
215220
String line = lines.get(i);
216221
String trimmed = line.stripLeading().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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class T {
99
# No implicit input file, because they can only be created outside a symbolic macro,
1010
""";
1111
String c =
12-
"""
12+
"""
1313
# No implicit input file, because they can only be created outside a symbolic macro,
1414
""";
1515
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class T {
22
{
33
f(
4-
/* foo */
5-
"""
4+
/* foo */ """
65
hello
76
""");
87
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ class RSLs {
4242
ipsum
4343
""";
4444
String j =
45-
"""
45+
"""
4646
lorem
4747
one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt
4848
ipsum
4949
""";
5050
String k =
51-
"""
51+
"""
5252
lorem
5353
ipsum
5454
""";
@@ -66,12 +66,10 @@ ipsum
6666
"""
6767
.formatted("world");
6868
f(
69-
/* foo= */
70-
"""
69+
/* foo= */ """
7170
foo
7271
""",
73-
/* bar= */
74-
"""
72+
/* bar= */ """
7573
bar
7674
""");
7775
"""
@@ -87,11 +85,10 @@ ipsum
8785
bar
8886
""";
8987
String t =
90-
"""
88+
"""
9189
foo
9290
"""
93-
+
94-
"""
91+
+ """
9592
bar
9693
""";
9794
String u =

0 commit comments

Comments
 (0)