Skip to content
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 @@ -42,6 +42,7 @@
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
import static com.sun.source.tree.Tree.Kind.UNION_TYPE;
import static com.sun.source.tree.Tree.Kind.VARIABLE;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

import com.google.auto.value.AutoOneOf;
Expand Down Expand Up @@ -71,6 +72,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 @@ -1753,6 +1755,23 @@ public Void visitMemberSelect(MemberSelectTree node, Void unused) {
public Void visitLiteral(LiteralTree node, Void unused) {
sync(node);
String sourceForNode = getSourceForNode(node, getCurrentPath());
if (sourceForNode.startsWith("\"\"\"")) {
String separator = Newlines.guessLineSeparator(sourceForNode);
ImmutableList<String> initialLines = sourceForNode.lines().collect(toImmutableList());
String stripped = initialLines.stream().skip(1).collect(joining(separator)).stripIndent();
// Use the last line of the text block to determine if it is deindented to column 0, by
// comparing the length of the line in the input source with the length after processing
// the text block contents with stripIndent().
boolean deindent =
getLast(initialLines).stripTrailing().length()
== Streams.findLast(stripped.lines()).orElseThrow().stripTrailing().length();
if (deindent) {
Indent indent = Indent.Const.make(Integer.MIN_VALUE / indentMultiplier, indentMultiplier);
builder.breakOp(indent);
}
token(sourceForNode);
return null;
}
if (isUnaryMinusLiteral(sourceForNode)) {
token("-");
sourceForNode = sourceForNode.substring(1).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,33 +188,24 @@ 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 lineStartPosition = lineMap.getStartPosition(lineMap.getLineNumber(startPosition));
int startColumn =
CharMatcher.whitespace()
.negate()
.indexIn(input.substring(lineStartPosition, endPosition))
+ 1;
int leadingWhitespace = CharMatcher.whitespace().negate().indexIn(text);

// 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
// whitespace.
ImmutableList<String> initialLines = text.lines().collect(toImmutableList());
String stripped = initialLines.stream().skip(1).collect(joining(separator)).stripIndent();
ImmutableList<String> lines = stripped.lines().collect(toImmutableList());
int deindent =
boolean deindent =
getLast(initialLines).stripTrailing().length()
- getLast(lines).stripTrailing().length();
== getLast(lines).stripTrailing().length();

String prefix =
(deindent == 0
|| lines.stream().anyMatch(x -> x.length() + startColumn - 1 > columnLimit))
? ""
: " ".repeat(startColumn - 1);
String prefix = deindent ? "" : " ".repeat(leadingWhitespace);

StringBuilder output = new StringBuilder(initialLines.get(0).stripLeading());
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.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 @@ -10,6 +10,6 @@ class T {
""";
String c =
"""
# No implicit input file, because they can only be created outside a symbolic macro,
""";
# No implicit input file, because they can only be created outside a symbolic macro,
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class RSLs {
lorem
ipsum
""";
String l =
"""
foo
bar
baz""";
{
f("""
lorem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ class RSLs {
""";
String j =
"""
lorem
one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt
ipsum
""";
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
""";
String l =
"""
foo
bar
baz\
""";

{
Expand Down Expand Up @@ -85,10 +91,11 @@ ipsum
bar
""";
String t =
"""
"""
foo
"""
+ """
+
"""
bar
""";
String u =
Expand Down
Loading