Skip to content

Commit 11d0c00

Browse files
committed
fix textBlock indentation
1 parent aa324c8 commit 11d0c00

7 files changed

Lines changed: 70 additions & 82 deletions

File tree

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import com.palantir.javaformat.Indent;
5656
import com.palantir.javaformat.Input;
5757
import com.palantir.javaformat.LastLevelBreakability;
58-
import com.palantir.javaformat.Newlines;
5958
import com.palantir.javaformat.Op;
6059
import com.palantir.javaformat.OpenOp;
6160
import com.palantir.javaformat.OpsBuilder;
@@ -144,7 +143,6 @@
144143
import java.util.Optional;
145144
import java.util.Set;
146145
import java.util.regex.Pattern;
147-
import java.util.stream.Collectors;
148146
import java.util.stream.Stream;
149147
import javax.annotation.Nullable;
150148
import javax.lang.model.element.Name;
@@ -1632,28 +1630,6 @@ public Void visitMemberSelect(MemberSelectTree node, Void unused) {
16321630
public Void visitLiteral(LiteralTree node, Void unused) {
16331631
sync(node);
16341632
String sourceForNode = getSourceForNode(node, getCurrentPath());
1635-
if (sourceForNode.startsWith("\"\"\"")) {
1636-
String separator = Newlines.guessLineSeparator(sourceForNode);
1637-
ImmutableList<String> initialLines = sourceForNode.lines().collect(ImmutableList.toImmutableList());
1638-
String stripped = initialLines.stream()
1639-
.skip(1)
1640-
.collect(Collectors.joining(separator))
1641-
.stripIndent();
1642-
// Use the last line of the text block to determine if it is deindented to column 0, by
1643-
// comparing the length of the line in the input source with the length after processing
1644-
// the text block contents with stripIndent().
1645-
boolean deindent = getLast(initialLines).stripTrailing().length()
1646-
== Streams.findLast(stripped.lines())
1647-
.orElseThrow()
1648-
.stripTrailing()
1649-
.length();
1650-
if (deindent) {
1651-
Indent indent = Indent.Const.make(Integer.MIN_VALUE / indentMultiplier, indentMultiplier);
1652-
builder.breakOp(indent);
1653-
}
1654-
token(sourceForNode);
1655-
return null;
1656-
}
16571633
// A negative numeric literal -n is usually represented as unary minus on n,
16581634
// but that doesn't work for integer or long MIN_VALUE. The parser works
16591635
// around that by representing it directly as a signed literal (with no

palantir-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private void indentTextBlocks(TreeRangeMap<Integer, String> replacements, List<T
194194
String text = input.substring(startPosition, endPosition);
195195
int lineStartPosition = lineMap.getStartPosition(lineMap.getLineNumber(startPosition));
196196
int startColumn =
197-
CharMatcher.whitespace().negate().indexIn(input.substring(lineStartPosition, endPosition)) + 1;
197+
CharMatcher.whitespace().negate().indexIn(input.substring(lineStartPosition, endPosition));
198198

199199
// Find the source code of the text block with incidental whitespace removed.
200200
// The first line of the text block is always """, and it does not affect incidental
@@ -205,15 +205,11 @@ private void indentTextBlocks(TreeRangeMap<Integer, String> replacements, List<T
205205
.collect(joining(separator))
206206
.stripIndent();
207207
ImmutableList<String> lines = stripped.lines().collect(ImmutableList.toImmutableList());
208-
int deindent = getLast(initialLines).stripTrailing().length()
209-
- getLast(lines).stripTrailing().length();
210208

211-
String prefix =
212-
(deindent == 0 || lines.stream().anyMatch(x -> x.length() + startColumn - 1 > columnLimit))
213-
? ""
214-
: " ".repeat(startColumn - 1);
209+
String prefix = (lineStartPosition + startColumn + 4 > startPosition ? "" : " ".repeat(4))
210+
+ " ".repeat(startColumn);
215211

216-
StringBuilder output = new StringBuilder(initialLines.get(0).stripLeading());
212+
StringBuilder output = new StringBuilder(initialLines.get(0));
217213
for (int i = 0; i < lines.size(); i++) {
218214
String line = lines.get(i);
219215
output.append(separator);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class T {
22
{
33
f(/* foo */ """
4-
hello
5-
""");
4+
hello
5+
""");
66
}
77
}

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/B380299722.output

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.helloworld;
33
class Foo {
44
void foo() {
55
var bar = """
6-
bar\
7-
bar\
8-
""";
6+
bar\
7+
bar\
8+
""";
99
}
1010
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public interface Foo {
22

33
private static String foo = """
4-
foo\
5-
bar\
6-
""";
4+
foo\
5+
bar\
6+
""";
77
}

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/RSL.input

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,21 @@ class RSLs {
7272
+ """
7373
bar
7474
""";
75-
String t =
75+
// TODO(crogoz): fix formatSource rendering of this block
76+
String broken =
7677
"""
7778
foo
7879
"""
7980
+ """
8081
bar
8182
""";
83+
String working =
84+
"""
85+
foo
86+
"""
87+
+ """
88+
bar
89+
""";
8290
String u =
8391
stringVariableOne
8492
+
@@ -90,6 +98,8 @@ class RSLs {
9098
...
9199
""";
92100
}
101+
102+
93103
String x = String.format("""
94104
this very long string that does something using arguments %s %s %s
95105
""",

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/RSL.output

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
class RSLs {
22
String a = """
3-
lorem
4-
ipsum
5-
""";
3+
lorem
4+
ipsum
5+
""";
66
String b = """
7-
lorem
8-
ipsum
9-
""";
7+
lorem
8+
ipsum
9+
""";
1010
String c = """
11-
lorem
12-
ipsum
13-
""";
11+
lorem
12+
ipsum
13+
""";
1414
String d = """
15-
ipsum
16-
""";
15+
ipsum
16+
""";
1717
String e = """
18-
""";
18+
""";
1919
String f = """
20-
ipsum\
21-
""";
20+
ipsum\
21+
""";
2222
String g = """
23-
lorem\
24-
ipsum
25-
""";
23+
lorem\
24+
ipsum
25+
""";
2626
String h = """
27-
lorem\
28-
ipsum\
29-
""";
27+
lorem\
28+
ipsum\
29+
""";
3030
String i = """
31-
lorem
31+
lorem
3232

33-
ipsum
34-
""";
33+
ipsum
34+
""";
3535
String j =
3636
"""
3737
lorem
3838
one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt
3939
ipsum
4040
""";
4141
String k = """
42-
lorem
43-
ipsum
44-
""";
42+
lorem
43+
ipsum
44+
""";
4545
String l = """
46-
foo
47-
bar
48-
baz\
49-
""";
46+
foo
47+
bar
48+
baz\
49+
""";
5050

5151
{
5252
f("""
@@ -58,23 +58,29 @@ class RSLs {
5858
hello %s
5959
""".formatted("world");
6060
f(/* foo= */ """
61-
foo
62-
""", /* bar= */ """
63-
bar
64-
""");
61+
foo
62+
""", /* bar= */ """
63+
bar
64+
""");
6565
"""
6666
hello
6767
""".codePoints().forEach(System.err::println);
6868
String s = """
69-
foo
70-
""" + """
69+
foo
70+
""" + """
71+
bar
72+
""";
73+
// TODO(crogoz): fix formatSource rendering of this block
74+
String broken = """
75+
foo
76+
""" + """
7177
bar
7278
""";
73-
String t = """
74-
foo
75-
""" + """
76-
bar
77-
""";
79+
String working = """
80+
foo
81+
""" + """
82+
bar
83+
""";
7884
String u = stringVariableOne
7985
+ """
8086
...

0 commit comments

Comments
 (0)