diff --git a/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java b/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java index a0ebb03d2..ba6613aa2 100644 --- a/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java +++ b/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java @@ -69,11 +69,7 @@ void parsing_git_diff_output_works() throws IOException { @MethodSource("getFormatters") void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterService formatterService) throws IOException, InterruptedException { - runCommandInRepo("git", "init"); - runCommandInRepo("git", "config", "user.name", "Test User"); - runCommandInRepo("git", "config", "user.email", "test-user@palantir.com"); - runCommandInRepo("git", "config", "commit.gpgsign", "false"); - runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init"); + initRepo(); Path subdir = repo.resolve("subdir"); Files.createDirectories(subdir); @@ -92,6 +88,44 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterServi assertThat(dontTouchMe).hasContent(" class DontTouchMe {}"); } + @ParameterizedTest + @MethodSource("getFormatters") + void reflows_the_long_string_of_a_changed_line(FormatterService formatterService) + throws IOException, InterruptedException { + initRepo(); + Path longString = repo.resolve("LongString.java"); + Files.writeString(longString, """ + class LongString { + } + """); + runCommandInRepo("git", "add", "."); + runCommandInRepo("git", "commit", "-m", "A class"); + + Files.writeString(longString, """ + class LongString { + String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna."; + } + """); + + FormatDiff.formatDiff(repo, formatterService); + + assertThat(longString).hasContent(""" + class LongString { + String changed = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et" + + " dolore magna."; + } + """); + } + + private void initRepo() throws IOException, InterruptedException { + runCommandInRepo("git", "init"); + runCommandInRepo("git", "config", "user.name", "Test User"); + runCommandInRepo("git", "config", "user.email", "test-user@palantir.com"); + runCommandInRepo("git", "config", "commit.gpgsign", "false"); + runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init"); + } + private void runCommandInRepo(String... args) throws IOException, InterruptedException { Process process = new ProcessBuilder().command(args).directory(repo.toFile()).start(); @@ -107,6 +141,9 @@ private void runCommandInRepo(String... args) throws IOException, InterruptedExc private static Stream getFormatters() throws IOException { return Stream.of( + // In this JVM, as formatDiff runs it without the native binary. + FormatterServiceLoader.load( + getClasspath().stream().map(Path::toFile).toList()), new BootstrappingFormatterService( javaBinPath(), Runtime.version().feature(), getClasspath()), new NativeImageFormatterService( diff --git a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java index 5821fade2..186c375c4 100644 --- a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java +++ b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java @@ -24,6 +24,7 @@ import com.palantir.javaformat.Utils; import com.palantir.javaformat.java.FormatterException; import com.palantir.javaformat.java.FormatterService; +import com.palantir.javaformat.java.FormatterServiceImpl; import com.palantir.javaformat.java.Replacement; import java.io.File; import java.net.URI; @@ -96,6 +97,30 @@ public void main( String aaaa ) { .isEmpty(); } + @ParameterizedTest + @MethodSource("getFormattersAndTheInProcessOne") + void reflows_the_long_string_of_the_range(FormatterService formatterService) throws FormatterException { + String input = """ + class S { + String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna."; + } + """; + String expectedOutput = """ + class S { + String changed = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et" + + " dolore magna."; + } + """; + int start = input.indexOf(" String changed"); + int end = input.indexOf('\n', start); + + List replacements = + formatterService.getFormatReplacements(input, List.of(Range.closedOpen(start, end))); + + assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput); + } + private static Stream getFormatters() { return Stream.of( new BootstrappingFormatterService( @@ -103,6 +128,12 @@ private static Stream getFormatters() { new NativeImageFormatterService(Path.of(System.getenv("NATIVE_IMAGE_CLASSPATH")))); } + // With the one that formats in this JVM, as formatDiff does without the native binary. On input that does not parse + // it throws, where the other two return no replacements, so it is not among getFormatters. + private static Stream getFormattersAndTheInProcessOne() { + return Stream.concat(Stream.of(new FormatterServiceImpl()), getFormatters()); + } + private String getTestResourceContent(String resourceName) { try { URI resource = getClass().getClassLoader().getResource(resourceName).toURI(); diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java index 813e94310..1faeedf1a 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatFileCallable.java @@ -24,7 +24,6 @@ import com.palantir.javaformat.Utils; import java.io.UncheckedIOException; import java.util.List; -import java.util.Set; import java.util.concurrent.Callable; /** 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 { Formatter formatter = Formatter.createFormatter(options); if (parameters.outputReplacements()) { RangeSet characterRanges = characterRanges(input); - Set> rangesToChange = characterRanges.asRanges(); - List replacements = formatter.getFormatReplacements(input, rangesToChange); - if (parameters.reflowLongStrings()) { - String formattedText = Utils.applyReplacements(input, replacements); - // avoid trying to wrap the input unless the lines needing wrapping are part of the "characterRanges" - if (!StringWrapper.linesNeedWrapping(options.maxLineLength(), formattedText, characterRanges)) { - return writeFormatReplacements(replacements); - } - String wrappedFormattedText = StringWrapper.wrap(options.maxLineLength(), formattedText, formatter); - // if no wrapping change happened, return the initial replacements - if (wrappedFormattedText.equals(formattedText)) { - return writeFormatReplacements(replacements); - } - return writeFormatReplacements(List.of(Replacement.create(0, input.length(), wrappedFormattedText))); - } else { - return writeFormatReplacements(replacements); - } + return writeFormatReplacements( + parameters.reflowLongStrings() + ? formatter.getFormatAndReflowReplacements(input, characterRanges) + : formatter.getFormatReplacements(input, characterRanges.asRanges())); } return formatFile(formatter); } diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java b/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java index c3da6ce96..215094cd8 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java @@ -364,4 +364,25 @@ public List getFormatReplacements(String input, Collection tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges); return javaOutput.getFormatReplacements(tokenRangeSet); } + + /** + * Emit the {@link Replacement}s that format the given character ranges and reflow long strings, as the command line + * prints them for {@code --output-replacements} and {@link FormatterServiceImpl#getFormatReplacements} returns + * them: in the build's JVM, out of process and with the native binary alike. + */ + List getFormatAndReflowReplacements(String input, RangeSet characterRanges) + throws FormatterException { + List replacements = getFormatReplacements(input, characterRanges.asRanges()); + String formatted = Utils.applyReplacements(input, replacements); + // avoid trying to wrap the input unless the lines needing wrapping are part of the "characterRanges" + if (!StringWrapper.linesNeedWrapping(options.maxLineLength(), formatted, characterRanges)) { + return replacements; + } + String wrapped = StringWrapper.wrap(options.maxLineLength(), formatted, this); + // if no wrapping change happened, return the initial replacements + if (wrapped.equals(formatted)) { + return replacements; + } + return List.of(Replacement.create(0, input.length(), wrapped)); + } } diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java index 08681da2e..41644d0f1 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/FormatterServiceImpl.java @@ -18,6 +18,7 @@ import com.google.auto.service.AutoService; import com.google.common.collect.ImmutableList; import com.google.common.collect.Range; +import com.google.common.collect.TreeRangeSet; import com.palantir.javaformat.java.JavaFormatterOptions.Style; import java.util.Collection; @@ -36,7 +37,8 @@ public FormatterServiceImpl() { @SuppressForbidden(reason = "FormatterService, kept in the SPI for compatibility, returns ImmutableList") public ImmutableList getFormatReplacements(String text, Collection> toRanges) throws FormatterException { - return ImmutableList.copyOf(formatter.getFormatReplacements(text, toRanges)); + // What the command line prints for --output-replacements, which the bootstrapping and native services run. + return ImmutableList.copyOf(formatter.getFormatAndReflowReplacements(text, TreeRangeSet.create(toRanges))); } @Override diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java index 937e7743f..73a07ed03 100644 --- a/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java +++ b/open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java @@ -14,10 +14,19 @@ package com.palantir.javaformat.java; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Objects.requireNonNull; import static org.assertj.core.api.Assertions.assertThat; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.datatype.guava.GuavaModule; +import com.google.common.collect.Range; import com.palantir.javaformat.java.JavaFormatterOptions.Style; +import java.io.ByteArrayInputStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -26,13 +35,18 @@ /** * One test for each {@link CaseFile} in the given folders on the test classpath: formatting the before block gives the - * after block, and formatting that again changes nothing. Adding a case is adding a file. The cases are in a folder of - * the JDK they need, and each JDK has a class that runs the folders it can parse, such as + * after block, and formatting that again changes nothing. For the lines that formatting changes, taken as a range, the + * service that formats in this JVM, as {@code formatDiff} does without the native binary, gives the replacements that + * the command line prints, which the bootstrapping and native services run. Adding a case is adding a file. The cases + * are in a folder of the JDK they need, and each JDK has a class that runs the folders it can parse, such as * {@link FormatterCasesJava21Test}. */ final class FormatterCasesTest { private static final Formatter FORMATTER = Formatter.createFormatter( JavaFormatterOptions.builder().style(Style.OJF).build()); + private static final FormatterService SERVICE = new FormatterServiceImpl(); + private static final ObjectMapper MAPPER = + JsonMapper.builder().addModule(new GuavaModule()).build(); /** The tests of the case files under {@code folders} on the test classpath. */ static List casesIn(String... folders) throws Exception { @@ -64,10 +78,74 @@ static List casesIn(String... folders) throws Exception { assertThat(twice) .as(() -> folder + "/" + path + ", second run, " + testCase.firstDifference(twice)) .isEqualTo(once); + + var lines = changedLines(testCase); + List replacements = MAPPER.readValue( + commandLine( + testCase.before(), + "--character-ranges", + lines.lowerEndpoint() + ":" + (lines.upperEndpoint() - 1), + "--output-replacements", + "-"), + new TypeReference<>() {}); + + assertThat(SERVICE.getFormatReplacements(testCase.before(), List.of(lines))) + .as(() -> folder + "/" + path + ", the in-process service on characters " + lines) + .isEqualTo(replacements); })); } } return tests; } + + /** + * The lines of the before block that formatting changes, from the first that differs from the after block to the + * last, as one range of characters without the last line break; the whole block when no line differs. + */ + private static Range changedLines(CaseFile testCase) { + var before = testCase.before(); + var beforeLines = List.of(before.split("(?<=\n)")); + var afterLines = List.of(testCase.after().split("(?<=\n)")); + var first = 0; + while (first < beforeLines.size() + && first < afterLines.size() + && beforeLines.get(first).equals(afterLines.get(first))) { + first++; + } + var last = 0; + while (last < beforeLines.size() - first + && last < afterLines.size() - first + && beforeLines + .get(beforeLines.size() - 1 - last) + .equals(afterLines.get(afterLines.size() - 1 - last))) { + last++; + } + var start = 0; + for (var i = 0; i < first; i++) { + start += beforeLines.get(i).length(); + } + var end = start; + for (var i = first; i < beforeLines.size() - last; i++) { + end += beforeLines.get(i).length(); + } + while (end > start && (before.charAt(end - 1) == '\n' || before.charAt(end - 1) == '\r')) { + end--; + } + return end > start ? Range.closedOpen(start, end) : Range.closedOpen(0, before.length()); + } + + /** What {@code open-java-format} prints for the source on standard input. */ + static String commandLine(String source, String... args) throws UsageException { + var out = new StringWriter(); + var err = new StringWriter(); + var main = new Main( + new PrintWriter(out, true), + new PrintWriter(err, true), + new ByteArrayInputStream(source.getBytes(UTF_8))); + + assertThat(main.format(args)).as(err::toString).isZero(); + + return out.toString(); + } }