Skip to content

Commit 740704f

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. FormatterServiceImplTest holds the two to each other: for each case, 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. FormatterServiceImplJava21Test, Java25Test and Java28Test run it on the case folders of their JDK, as the FormatterCases classes do; CaseFile.folder finds a folder for both.
1 parent 57bb4da commit 740704f

9 files changed

Lines changed: 259 additions & 25 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/CaseFile.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
package com.palantir.javaformat.java;
1616

17+
import static java.util.Objects.requireNonNull;
18+
19+
import com.google.common.collect.Range;
1720
import java.io.File;
1821
import java.io.IOException;
22+
import java.net.URISyntaxException;
1923
import java.nio.file.Files;
2024
import java.nio.file.Path;
2125
import java.util.List;
@@ -92,6 +96,48 @@ private static String lineOrEnd(List<String> lines, int index) {
9296
return index < lines.size() ? lines.get(index) : "(no more lines)";
9397
}
9498

99+
/**
100+
* The lines of the before block that formatting changes, from the first that differs from the after block to the
101+
* last, as one range of characters without the last line break; the whole block when no line differs.
102+
*/
103+
Range<Integer> changedLines() {
104+
var beforeLines = List.of(before.split("(?<=\n)"));
105+
var afterLines = List.of(after.split("(?<=\n)"));
106+
var first = 0;
107+
while (first < beforeLines.size()
108+
&& first < afterLines.size()
109+
&& beforeLines.get(first).equals(afterLines.get(first))) {
110+
first++;
111+
}
112+
var last = 0;
113+
while (last < beforeLines.size() - first
114+
&& last < afterLines.size() - first
115+
&& beforeLines
116+
.get(beforeLines.size() - 1 - last)
117+
.equals(afterLines.get(afterLines.size() - 1 - last))) {
118+
last++;
119+
}
120+
var start = 0;
121+
for (var i = 0; i < first; i++) {
122+
start += beforeLines.get(i).length();
123+
}
124+
var end = start;
125+
for (var i = first; i < beforeLines.size() - last; i++) {
126+
end += beforeLines.get(i).length();
127+
}
128+
while (end > start && (before.charAt(end - 1) == '\n' || before.charAt(end - 1) == '\r')) {
129+
end--;
130+
}
131+
return end > start ? Range.closedOpen(start, end) : Range.closedOpen(0, before.length());
132+
}
133+
134+
/** The folder {@code name} on the test classpath, where its case files are. */
135+
static Path folder(String name) throws URISyntaxException {
136+
var url = requireNonNull(
137+
CaseFile.class.getClassLoader().getResource(name), "no " + name + " folder on the test classpath");
138+
return Path.of(url.toURI());
139+
}
140+
95141
/** Returns the case files under {@code root}, as paths relative to it with {@code /} between names, in order. */
96142
static List<String> pathsIn(Path root) throws IOException {
97143
try (var files = Files.walk(root)) {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
package com.palantir.javaformat.java;
1616

17-
import static java.util.Objects.requireNonNull;
1817
import static org.assertj.core.api.Assertions.assertThat;
1918

2019
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
2120
import java.nio.file.Files;
22-
import java.nio.file.Path;
2321
import java.util.ArrayList;
2422
import java.util.List;
2523
import org.junit.jupiter.api.DynamicTest;
@@ -39,10 +37,7 @@ static List<DynamicTest> casesIn(String... folders) throws Exception {
3937
var tests = new ArrayList<DynamicTest>();
4038

4139
for (var folder : folders) {
42-
var url = requireNonNull(
43-
FormatterCasesTest.class.getClassLoader().getResource(folder),
44-
"no " + folder + " folder on the test classpath");
45-
var root = Path.of(url.toURI());
40+
var root = CaseFile.folder(folder);
4641
var paths = CaseFile.pathsIn(root);
4742

4843
// A wrong folder or a resource filter would otherwise pass with no tests at all.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.java;
16+
17+
import java.util.List;
18+
import org.junit.jupiter.api.DynamicTest;
19+
import org.junit.jupiter.api.TestFactory;
20+
import org.junit.jupiter.api.condition.EnabledForJreRange;
21+
import org.junit.jupiter.api.condition.JRE;
22+
import org.junit.jupiter.api.parallel.Execution;
23+
import org.junit.jupiter.api.parallel.ExecutionMode;
24+
25+
/** {@link FormatterServiceImplTest} on JDK 21 and later, with the cases of JDK 21. */
26+
@EnabledForJreRange(min = JRE.JAVA_21)
27+
@Execution(ExecutionMode.CONCURRENT)
28+
final class FormatterServiceImplJava21Test {
29+
@TestFactory
30+
List<DynamicTest> replacesTheChangedLinesAsTheCommandLineDoes() throws Exception {
31+
return FormatterServiceImplTest.changedLinesIn("test-cases-java21");
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.java;
16+
17+
import java.util.List;
18+
import org.junit.jupiter.api.DynamicTest;
19+
import org.junit.jupiter.api.TestFactory;
20+
import org.junit.jupiter.api.condition.EnabledForJreRange;
21+
import org.junit.jupiter.api.condition.JRE;
22+
import org.junit.jupiter.api.parallel.Execution;
23+
import org.junit.jupiter.api.parallel.ExecutionMode;
24+
25+
/** {@link FormatterServiceImplTest} on JDK 25 and later, with the cases of JDK 21 and 25. */
26+
@EnabledForJreRange(min = JRE.JAVA_25)
27+
@Execution(ExecutionMode.CONCURRENT)
28+
final class FormatterServiceImplJava25Test {
29+
@TestFactory
30+
List<DynamicTest> replacesTheChangedLinesAsTheCommandLineDoes() throws Exception {
31+
return FormatterServiceImplTest.changedLinesIn("test-cases-java25", "test-cases-java21");
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.java;
16+
17+
import java.util.List;
18+
import org.junit.jupiter.api.DynamicTest;
19+
import org.junit.jupiter.api.TestFactory;
20+
import org.junit.jupiter.api.condition.EnabledForJreRange;
21+
import org.junit.jupiter.api.condition.JRE;
22+
import org.junit.jupiter.api.parallel.Execution;
23+
import org.junit.jupiter.api.parallel.ExecutionMode;
24+
25+
/** {@link FormatterServiceImplTest} on JDK 28 and later, with the cases of JDK 21, 25 and 28. */
26+
@EnabledForJreRange(min = JRE.JAVA_28)
27+
@Execution(ExecutionMode.CONCURRENT)
28+
final class FormatterServiceImplJava28Test {
29+
@TestFactory
30+
List<DynamicTest> replacesTheChangedLinesAsTheCommandLineDoes() throws Exception {
31+
return FormatterServiceImplTest.changedLinesIn("test-cases-java28", "test-cases-java25", "test-cases-java21");
32+
}
33+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.java;
16+
17+
import static java.nio.charset.StandardCharsets.UTF_8;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import com.fasterxml.jackson.core.type.TypeReference;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.json.JsonMapper;
23+
import com.fasterxml.jackson.datatype.guava.GuavaModule;
24+
import com.google.common.collect.Range;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.PrintWriter;
27+
import java.io.StringWriter;
28+
import java.nio.file.Files;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
import org.junit.jupiter.api.DynamicTest;
32+
33+
/**
34+
* The service that formats in the build's own JVM, as {@code formatDiff} does without the native binary, against the
35+
* command line, which the bootstrapping and native services run: for each case, the lines that formatting changes,
36+
* taken as a range, get the same replacements from both. Each JDK has a class that runs the case folders it can parse,
37+
* such as {@link FormatterServiceImplJava21Test}.
38+
*/
39+
final class FormatterServiceImplTest {
40+
private static final FormatterService SERVICE = new FormatterServiceImpl();
41+
private static final ObjectMapper MAPPER =
42+
JsonMapper.builder().addModule(new GuavaModule()).build();
43+
44+
/** The tests of the case files under {@code folders} on the test classpath. */
45+
static List<DynamicTest> changedLinesIn(String... folders) throws Exception {
46+
var tests = new ArrayList<DynamicTest>();
47+
48+
for (var folder : folders) {
49+
var root = CaseFile.folder(folder);
50+
51+
for (var path : CaseFile.pathsIn(root)) {
52+
tests.add(DynamicTest.dynamicTest(path, () -> {
53+
var testCase = CaseFile.parse(path, Files.readString(root.resolve(path)));
54+
var lines = testCase.changedLines();
55+
56+
assertThat(SERVICE.getFormatReplacements(testCase.before(), List.of(lines)))
57+
.as(() -> folder + "/" + path + ", characters " + lines)
58+
.isEqualTo(commandLine(testCase.before(), lines));
59+
}));
60+
}
61+
}
62+
63+
return tests;
64+
}
65+
66+
/** What {@code open-java-format --character-ranges … --output-replacements -} prints for the source. */
67+
private static List<Replacement> commandLine(String source, Range<Integer> range) throws Exception {
68+
var out = new StringWriter();
69+
var err = new StringWriter();
70+
var main = new Main(
71+
new PrintWriter(out, true),
72+
new PrintWriter(err, true),
73+
new ByteArrayInputStream(source.getBytes(UTF_8)));
74+
75+
assertThat(main.format(
76+
"--character-ranges",
77+
range.lowerEndpoint() + ":" + (range.upperEndpoint() - 1),
78+
"--output-replacements",
79+
"-"))
80+
.as(err::toString)
81+
.isZero();
82+
83+
return MAPPER.readValue(out.toString(), new TypeReference<>() {});
84+
}
85+
}

0 commit comments

Comments
 (0)