Skip to content

Commit e2142e5

Browse files
authored
Merge pull request #46 from openjavaformat/upstream-pr-1784
Bring over upstream palantir#1784: run the string wrapping pass to a fixed point
2 parents f38b759 + 95c738a commit e2142e5

7 files changed

Lines changed: 110 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,25 @@ public final class StringWrapper {
5656

5757
public static final String TEXT_BLOCK_DELIMITER = "\"\"\"";
5858

59+
/**
60+
* How many times {@link #wrapOnce} may be re-run while it is still changing the source. The indentation chosen
61+
* for a text block is derived from the layout around it, which this pass can itself move, so a single round is
62+
* not always a fixed point and the formatter would not be idempotent. See
63+
* <a href="https://github.com/palantir/open-java-format/issues/1343">#1343</a>.
64+
*/
65+
private static final int MAX_ROUNDS = 5;
66+
5967
/** Reflows string literals in the given Java source code that extend past the given column limit. */
6068
static String wrap(final int columnLimit, String input, Formatter formatter) throws FormatterException {
69+
String result = wrapOnce(columnLimit, input, formatter);
70+
for (int round = 1; round < MAX_ROUNDS && !result.equals(input); round++) {
71+
input = result;
72+
result = wrapOnce(columnLimit, input, formatter);
73+
}
74+
return result;
75+
}
76+
77+
private static String wrapOnce(final int columnLimit, String input, Formatter formatter) throws FormatterException {
6178
if (!needWrapping(columnLimit, input)) {
6279
// fast path
6380
return input;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class T {
2+
void f() {
3+
LOGGER.warning(String.format("""
4+
@Gauge is configured on a bean %s that is neither ApplicationScoped nor \
5+
Singleton. This is most likely a bug. You may set 'metrics.warn-dependent' \
6+
configuration option to 'false' to remove this warning.""", clazz.getName()));
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class T {
2+
void f() {
3+
LOGGER.warning(String.format("""
4+
@Gauge is configured on a bean %s that is neither ApplicationScoped nor \
5+
Singleton. This is most likely a bug. You may set 'metrics.warn-dependent' \
6+
configuration option to 'false' to remove this warning.\
7+
""", clazz.getName()));
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class TernaryBug {
2+
boolean flag = true;
3+
4+
String a = flag
5+
? """
6+
yes
7+
"""
8+
: """
9+
no
10+
""";
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class TernaryBug {
2+
boolean flag = true;
3+
4+
String a = flag ? """
5+
yes
6+
""" : """
7+
no
8+
""";
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class PalantirIssue1343TextBlockIndentStable {
2+
String[][] withCommentFirstLine = {
3+
{
4+
"""
5+
// tmp fix
6+
@Deprecated
7+
class Test {}
8+
"""
9+
},
10+
{
11+
"""
12+
13+
@Deprecated
14+
class Test {}
15+
"""
16+
}
17+
};
18+
19+
String[][] blankLineInTheMiddle = {
20+
{
21+
"""
22+
import java.util.List;
23+
24+
@Deprecated
25+
class Test {}
26+
"""
27+
},
28+
{
29+
"""
30+
31+
class Test {}
32+
"""
33+
}
34+
};
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class PalantirIssue1343TextBlockIndentStable {
2+
String[][] withCommentFirstLine = {{"""
3+
// tmp fix
4+
@Deprecated
5+
class Test {}
6+
"""}, {"""
7+
8+
@Deprecated
9+
class Test {}
10+
"""}};
11+
12+
String[][] blankLineInTheMiddle = {{"""
13+
import java.util.List;
14+
15+
@Deprecated
16+
class Test {}
17+
"""}, {"""
18+
19+
class Test {}
20+
"""}};
21+
}

0 commit comments

Comments
 (0)