diff --git a/core/src/main/java/com/google/googlejavaformat/Input.java b/core/src/main/java/com/google/googlejavaformat/Input.java index d059111d9..66a392190 100644 --- a/core/src/main/java/com/google/googlejavaformat/Input.java +++ b/core/src/main/java/com/google/googlejavaformat/Input.java @@ -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. */ diff --git a/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java b/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java index 008be11f3..a45e83b9e 100644 --- a/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java +++ b/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java @@ -534,7 +534,7 @@ public final ImmutableList build() { space = tokBefore.isSlashStarComment(); newlines = 0; lastWasComment = true; - if (tokBefore.isJavadocComment() || token.getTok().isTextBlock()) { + if (tokBefore.isJavadocComment()) { tokOps.put(j, Doc.Break.makeForced()); } allowBlankAfterLastComment = diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java index 6be39ab42..01c617776 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java @@ -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) diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index af9810114..01f9a3e05 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -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; @@ -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(); diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index ab4093a11..42099d6b8 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -190,10 +190,15 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { private void indentTextBlocks( TreeRangeMap replacements, List 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 @@ -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(); diff --git a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java index bc1a93384..7854126c4 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java @@ -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\"}\\", "\"\"\";", diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B377585941.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B377585941.output index 7f57dfbef..bd3107be4 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B377585941.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B377585941.output @@ -1,8 +1,7 @@ class T { { f( - /* foo */ - """ + /* foo */ """ hello """); } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output index 9993f5aa2..6e9a3ae5b 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output @@ -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 """; @@ -66,12 +66,10 @@ ipsum """ .formatted("world"); f( - /* foo= */ - """ + /* foo= */ """ foo """, - /* bar= */ - """ + /* bar= */ """ bar """); """ @@ -87,11 +85,10 @@ ipsum bar """; String t = -""" + """ foo """ - + -""" + + """ bar """; String u =