Skip to content

Commit

Permalink
Back out text block special cases
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 695363436
  • Loading branch information
cushon authored and google-java-format Team committed Nov 11, 2024
1 parent fc31690 commit 02b6ad1
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 37 deletions.
5 changes: 0 additions & 5 deletions core/src/main/java/com/google/googlejavaformat/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public interface Tok {

/** Is the {@code Tok} a comment? */
boolean isComment();

/** Is the {@code Tok} a text block? */
default boolean isTextBlock() {
return false;
}
}

/** A {@code Token} is a language-level token. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public final ImmutableList<Op> build() {
space = tokBefore.isSlashStarComment();
newlines = 0;
lastWasComment = true;
if (tokBefore.isJavadocComment() || token.getTok().isTextBlock()) {
if (tokBefore.isJavadocComment()) {
tokOps.put(j, Doc.Break.makeForced());
}
allowBlankAfterLastComment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ public boolean isComment() {
return isSlashSlashComment() || isSlashStarComment();
}

@Override
public boolean isTextBlock() {
return originalText.startsWith("\"\"\"");
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
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 @@ -1668,15 +1667,6 @@ 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,10 +190,15 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
private void indentTextBlocks(
TreeRangeMap<Integer, String> replacements, List<Tree> textBlocks) {
for (Tree tree : textBlocks) {
int startPosition = lineMap.getStartPosition(lineMap.getLineNumber(getStartPosition(tree)));
int startPosition = getStartPosition(tree);
int endPosition = getEndPosition(unit, tree);
String text = input.substring(startPosition, endPosition);
int startColumn = CharMatcher.whitespace().negate().indexIn(text) + 1;
int lineStartPosition = lineMap.getStartPosition(lineMap.getLineNumber(startPosition));
int startColumn =
CharMatcher.whitespace()
.negate()
.indexIn(input.substring(lineStartPosition, endPosition))
+ 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 @@ -210,7 +215,7 @@ private void indentTextBlocks(
? ""
: " ".repeat(startColumn - 1);

StringBuilder output = new StringBuilder(prefix).append(initialLines.get(0).stripLeading());
StringBuilder output = new StringBuilder(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 @@ -9,7 +9,7 @@ class T {
# No implicit input file, because they can only be created outside a symbolic macro,
""";
String c =
"""
"""
# No implicit input file, because they can only be created outside a symbolic macro,
""";
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class T {
{
f(
/* foo */
"""
/* foo */ """
hello
""");
}
Expand Down
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 @@ -66,12 +66,10 @@ ipsum
"""
.formatted("world");
f(
/* foo= */
"""
/* foo= */ """
foo
""",
/* bar= */
"""
/* bar= */ """
bar
""");
"""
Expand All @@ -87,11 +85,10 @@ ipsum
bar
""";
String t =
"""
"""
foo
"""
+
"""
+ """
bar
""";
String u =
Expand Down

0 comments on commit 02b6ad1

Please sign in to comment.