Skip to content

Commit c5d22d7

Browse files
authored
Merge pull request #232 from openjavaformat/abashev/Range-reflow-in-process
Reflow long strings in formatDiff on the JVM, as with the native binary
2 parents bbfd2c1 + 94e5117 commit c5d22d7

6 files changed

Lines changed: 181 additions & 26 deletions

File tree

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ void parsing_git_diff_output_works() throws IOException {
6969
@MethodSource("getFormatters")
7070
void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterService formatterService)
7171
throws IOException, InterruptedException {
72-
runCommandInRepo("git", "init");
73-
runCommandInRepo("git", "config", "user.name", "Test User");
74-
runCommandInRepo("git", "config", "user.email", "test-user@palantir.com");
75-
runCommandInRepo("git", "config", "commit.gpgsign", "false");
76-
runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init");
72+
initRepo();
7773

7874
Path subdir = repo.resolve("subdir");
7975
Files.createDirectories(subdir);
@@ -92,6 +88,44 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterServi
9288
assertThat(dontTouchMe).hasContent(" class DontTouchMe {}");
9389
}
9490

91+
@ParameterizedTest
92+
@MethodSource("getFormatters")
93+
void reflows_the_long_string_of_a_changed_line(FormatterService formatterService)
94+
throws IOException, InterruptedException {
95+
initRepo();
96+
Path longString = repo.resolve("LongString.java");
97+
Files.writeString(longString, """
98+
class LongString {
99+
}
100+
""");
101+
runCommandInRepo("git", "add", ".");
102+
runCommandInRepo("git", "commit", "-m", "A class");
103+
104+
Files.writeString(longString, """
105+
class LongString {
106+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
107+
}
108+
""");
109+
110+
FormatDiff.formatDiff(repo, formatterService);
111+
112+
assertThat(longString).hasContent("""
113+
class LongString {
114+
String changed =
115+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
116+
+ " dolore magna.";
117+
}
118+
""");
119+
}
120+
121+
private void initRepo() throws IOException, InterruptedException {
122+
runCommandInRepo("git", "init");
123+
runCommandInRepo("git", "config", "user.name", "Test User");
124+
runCommandInRepo("git", "config", "user.email", "test-user@palantir.com");
125+
runCommandInRepo("git", "config", "commit.gpgsign", "false");
126+
runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init");
127+
}
128+
95129
private void runCommandInRepo(String... args) throws IOException, InterruptedException {
96130
Process process =
97131
new ProcessBuilder().command(args).directory(repo.toFile()).start();
@@ -107,6 +141,9 @@ private void runCommandInRepo(String... args) throws IOException, InterruptedExc
107141

108142
private static Stream<FormatterService> getFormatters() throws IOException {
109143
return Stream.of(
144+
// In this JVM, as formatDiff runs it without the native binary.
145+
FormatterServiceLoader.load(
146+
getClasspath().stream().map(Path::toFile).toList()),
110147
new BootstrappingFormatterService(
111148
javaBinPath(), Runtime.version().feature(), getClasspath()),
112149
new NativeImageFormatterService(

‎open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.palantir.javaformat.Utils;
2525
import com.palantir.javaformat.java.FormatterException;
2626
import com.palantir.javaformat.java.FormatterService;
27+
import com.palantir.javaformat.java.FormatterServiceImpl;
2728
import com.palantir.javaformat.java.Replacement;
2829
import java.io.File;
2930
import java.net.URI;
@@ -96,13 +97,43 @@ public void main( String aaaa ) {
9697
.isEmpty();
9798
}
9899

100+
@ParameterizedTest
101+
@MethodSource("getFormattersAndTheInProcessOne")
102+
void reflows_the_long_string_of_the_range(FormatterService formatterService) throws FormatterException {
103+
String input = """
104+
class S {
105+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
106+
}
107+
""";
108+
String expectedOutput = """
109+
class S {
110+
String changed =
111+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
112+
+ " dolore magna.";
113+
}
114+
""";
115+
int start = input.indexOf(" String changed");
116+
int end = input.indexOf('\n', start);
117+
118+
List<Replacement> replacements =
119+
formatterService.getFormatReplacements(input, List.of(Range.closedOpen(start, end)));
120+
121+
assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput);
122+
}
123+
99124
private static Stream<FormatterService> getFormatters() {
100125
return Stream.of(
101126
new BootstrappingFormatterService(
102127
javaBinPath(), Runtime.version().feature(), getClasspath()),
103128
new NativeImageFormatterService(Path.of(System.getenv("NATIVE_IMAGE_CLASSPATH"))));
104129
}
105130

131+
// With the one that formats in this JVM, as formatDiff does without the native binary. On input that does not parse
132+
// it throws, where the other two return no replacements, so it is not among getFormatters.
133+
private static Stream<FormatterService> getFormattersAndTheInProcessOne() {
134+
return Stream.concat(Stream.of(new FormatterServiceImpl()), getFormatters());
135+
}
136+
106137
private String getTestResourceContent(String resourceName) {
107138
try {
108139
URI resource = getClass().getClassLoader().getResource(resourceName).toURI();

‎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)