Skip to content

Commit bac4aa5

Browse files
committed
Return what --output-replacements prints from the in-process service
formatDiff on the JVM loads FormatterServiceImpl into the build's JVM, and its getFormatReplacements called Formatter.getFormatReplacements, which never reflows strings. With the native binary, and in IntelliJ IDEA, the bootstrapping and native services run Main --output-replacements, which does. A long string on a changed line stayed on one line on the JVM only, and the GitHub Action failed on the result (#219). What FormatFileCallable did for --output-replacements moves, as it is, to Formatter.getFormatAndReflowReplacements, and FormatterServiceImpl.getFormatReplacements returns what it gives. Each case of FormatterCasesTest now also holds the two to each other: the lines formatting changes, taken as a range, get the same replacements from the service and from Main. On the code before, 78 of the 915 cases fail.
1 parent bbfd2c1 commit bac4aa5

4 files changed

Lines changed: 108 additions & 21 deletions

File tree

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.palantir.javaformat.Utils;
2525
import java.io.UncheckedIOException;
2626
import java.util.List;
27-
import java.util.Set;
2827
import java.util.concurrent.Callable;
2928

3029
/** Encapsulates information about a file to be formatted, including which parts of the file to format. */
@@ -51,23 +50,10 @@ public String call() throws FormatterException {
5150
Formatter formatter = Formatter.createFormatter(options);
5251
if (parameters.outputReplacements()) {
5352
RangeSet<Integer> characterRanges = characterRanges(input);
54-
Set<Range<Integer>> rangesToChange = characterRanges.asRanges();
55-
List<Replacement> replacements = formatter.getFormatReplacements(input, rangesToChange);
56-
if (parameters.reflowLongStrings()) {
57-
String formattedText = Utils.applyReplacements(input, replacements);
58-
// avoid trying to wrap the input unless the lines needing wrapping are part of the "characterRanges"
59-
if (!StringWrapper.linesNeedWrapping(options.maxLineLength(), formattedText, characterRanges)) {
60-
return writeFormatReplacements(replacements);
61-
}
62-
String wrappedFormattedText = StringWrapper.wrap(options.maxLineLength(), formattedText, formatter);
63-
// if no wrapping change happened, return the initial replacements
64-
if (wrappedFormattedText.equals(formattedText)) {
65-
return writeFormatReplacements(replacements);
66-
}
67-
return writeFormatReplacements(List.of(Replacement.create(0, input.length(), wrappedFormattedText)));
68-
} else {
69-
return writeFormatReplacements(replacements);
70-
}
53+
return writeFormatReplacements(
54+
parameters.reflowLongStrings()
55+
? formatter.getFormatAndReflowReplacements(input, characterRanges)
56+
: formatter.getFormatReplacements(input, characterRanges.asRanges()));
7157
}
7258
return formatFile(formatter);
7359
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,25 @@ public List<Replacement> getFormatReplacements(String input, Collection<Range<In
364364
RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges);
365365
return javaOutput.getFormatReplacements(tokenRangeSet);
366366
}
367+
368+
/**
369+
* Emit the {@link Replacement}s that format the given character ranges and reflow long strings, as the command line
370+
* prints them for {@code --output-replacements} and {@link FormatterServiceImpl#getFormatReplacements} returns
371+
* them: in the build's JVM, out of process and with the native binary alike.
372+
*/
373+
List<Replacement> getFormatAndReflowReplacements(String input, RangeSet<Integer> characterRanges)
374+
throws FormatterException {
375+
List<Replacement> replacements = getFormatReplacements(input, characterRanges.asRanges());
376+
String formatted = Utils.applyReplacements(input, replacements);
377+
// avoid trying to wrap the input unless the lines needing wrapping are part of the "characterRanges"
378+
if (!StringWrapper.linesNeedWrapping(options.maxLineLength(), formatted, characterRanges)) {
379+
return replacements;
380+
}
381+
String wrapped = StringWrapper.wrap(options.maxLineLength(), formatted, this);
382+
// if no wrapping change happened, return the initial replacements
383+
if (wrapped.equals(formatted)) {
384+
return replacements;
385+
}
386+
return List.of(Replacement.create(0, input.length(), wrapped));
387+
}
367388
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.auto.service.AutoService;
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.Range;
21+
import com.google.common.collect.TreeRangeSet;
2122
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
2223
import java.util.Collection;
2324

@@ -36,7 +37,8 @@ public FormatterServiceImpl() {
3637
@SuppressForbidden(reason = "FormatterService, kept in the SPI for compatibility, returns ImmutableList")
3738
public ImmutableList<Replacement> getFormatReplacements(String text, Collection<Range<Integer>> toRanges)
3839
throws FormatterException {
39-
return ImmutableList.copyOf(formatter.getFormatReplacements(text, toRanges));
40+
// What the command line prints for --output-replacements, which the bootstrapping and native services run.
41+
return ImmutableList.copyOf(formatter.getFormatAndReflowReplacements(text, TreeRangeSet.create(toRanges)));
4042
}
4143

4244
@Override

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@
1414

1515
package com.palantir.javaformat.java;
1616

17+
import static java.nio.charset.StandardCharsets.UTF_8;
1718
import static java.util.Objects.requireNonNull;
1819
import static org.assertj.core.api.Assertions.assertThat;
1920

21+
import com.fasterxml.jackson.core.type.TypeReference;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import com.fasterxml.jackson.databind.json.JsonMapper;
24+
import com.fasterxml.jackson.datatype.guava.GuavaModule;
25+
import com.google.common.collect.Range;
2026
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
27+
import java.io.ByteArrayInputStream;
28+
import java.io.PrintWriter;
29+
import java.io.StringWriter;
2130
import java.nio.file.Files;
2231
import java.nio.file.Path;
2332
import java.util.ArrayList;
@@ -26,13 +35,18 @@
2635

2736
/**
2837
* One test for each {@link CaseFile} in the given folders on the test classpath: formatting the before block gives the
29-
* after block, and formatting that again changes nothing. Adding a case is adding a file. The cases are in a folder of
30-
* the JDK they need, and each JDK has a class that runs the folders it can parse, such as
38+
* after block, and formatting that again changes nothing. For the lines that formatting changes, taken as a range, the
39+
* service that formats in this JVM, as {@code formatDiff} does without the native binary, gives the replacements that
40+
* the command line prints, which the bootstrapping and native services run. Adding a case is adding a file. The cases
41+
* are in a folder of the JDK they need, and each JDK has a class that runs the folders it can parse, such as
3142
* {@link FormatterCasesJava21Test}.
3243
*/
3344
final class FormatterCasesTest {
3445
private static final Formatter FORMATTER = Formatter.createFormatter(
3546
JavaFormatterOptions.builder().style(Style.OJF).build());
47+
private static final FormatterService SERVICE = new FormatterServiceImpl();
48+
private static final ObjectMapper MAPPER =
49+
JsonMapper.builder().addModule(new GuavaModule()).build();
3650

3751
/** The tests of the case files under {@code folders} on the test classpath. */
3852
static List<DynamicTest> casesIn(String... folders) throws Exception {
@@ -64,10 +78,74 @@ static List<DynamicTest> casesIn(String... folders) throws Exception {
6478
assertThat(twice)
6579
.as(() -> folder + "/" + path + ", second run, " + testCase.firstDifference(twice))
6680
.isEqualTo(once);
81+
82+
var lines = changedLines(testCase);
83+
List<Replacement> replacements = MAPPER.readValue(
84+
commandLine(
85+
testCase.before(),
86+
"--character-ranges",
87+
lines.lowerEndpoint() + ":" + (lines.upperEndpoint() - 1),
88+
"--output-replacements",
89+
"-"),
90+
new TypeReference<>() {});
91+
92+
assertThat(SERVICE.getFormatReplacements(testCase.before(), List.of(lines)))
93+
.as(() -> folder + "/" + path + ", the in-process service on characters " + lines)
94+
.isEqualTo(replacements);
6795
}));
6896
}
6997
}
7098

7199
return tests;
72100
}
101+
102+
/**
103+
* The lines of the before block that formatting changes, from the first that differs from the after block to the
104+
* last, as one range of characters without the last line break; the whole block when no line differs.
105+
*/
106+
private static Range<Integer> changedLines(CaseFile testCase) {
107+
var before = testCase.before();
108+
var beforeLines = List.of(before.split("(?<=\n)"));
109+
var afterLines = List.of(testCase.after().split("(?<=\n)"));
110+
var first = 0;
111+
while (first < beforeLines.size()
112+
&& first < afterLines.size()
113+
&& beforeLines.get(first).equals(afterLines.get(first))) {
114+
first++;
115+
}
116+
var last = 0;
117+
while (last < beforeLines.size() - first
118+
&& last < afterLines.size() - first
119+
&& beforeLines
120+
.get(beforeLines.size() - 1 - last)
121+
.equals(afterLines.get(afterLines.size() - 1 - last))) {
122+
last++;
123+
}
124+
var start = 0;
125+
for (var i = 0; i < first; i++) {
126+
start += beforeLines.get(i).length();
127+
}
128+
var end = start;
129+
for (var i = first; i < beforeLines.size() - last; i++) {
130+
end += beforeLines.get(i).length();
131+
}
132+
while (end > start && (before.charAt(end - 1) == '\n' || before.charAt(end - 1) == '\r')) {
133+
end--;
134+
}
135+
return end > start ? Range.closedOpen(start, end) : Range.closedOpen(0, before.length());
136+
}
137+
138+
/** What {@code open-java-format} prints for the source on standard input. */
139+
static String commandLine(String source, String... args) throws UsageException {
140+
var out = new StringWriter();
141+
var err = new StringWriter();
142+
var main = new Main(
143+
new PrintWriter(out, true),
144+
new PrintWriter(err, true),
145+
new ByteArrayInputStream(source.getBytes(UTF_8)));
146+
147+
assertThat(main.format(args)).as(err::toString).isZero();
148+
149+
return out.toString();
150+
}
73151
}

0 commit comments

Comments
 (0)