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 ba6613aa2..e250a3e60 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
@@ -96,6 +96,7 @@ void reflows_the_long_string_of_a_changed_line(FormatterService formatterService
Path longString = repo.resolve("LongString.java");
Files.writeString(longString, """
class LongString {
+ String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
}
""");
runCommandInRepo("git", "add", ".");
@@ -103,6 +104,8 @@ class LongString {
Files.writeString(longString, """
class LongString {
+ String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
}
""");
@@ -111,6 +114,8 @@ class LongString {
assertThat(longString).hasContent("""
class LongString {
+ String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
+
String changed =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
+ " dolore magna.";
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 186c375c4..a59aa4299 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
@@ -102,11 +102,15 @@ public void main( String aaaa ) {
void reflows_the_long_string_of_the_range(FormatterService formatterService) throws FormatterException {
String input = """
class S {
+ String outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
+
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 outside = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.";
+
String changed =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
+ " dolore magna.";
diff --git a/open-java-format-spi/src/main/java/com/palantir/javaformat/java/FormatterService.java b/open-java-format-spi/src/main/java/com/palantir/javaformat/java/FormatterService.java
index 292549467..a314ac57a 100644
--- a/open-java-format-spi/src/main/java/com/palantir/javaformat/java/FormatterService.java
+++ b/open-java-format-spi/src/main/java/com/palantir/javaformat/java/FormatterService.java
@@ -28,6 +28,9 @@ public interface FormatterService {
/**
* Emit a list of {@link Replacement}s to convert from input to formatted output.
*
+ *
The ranges are formatted, and the long strings in the code that formatting them rewrites are reflowed; the
+ * imports, and the strings elsewhere, stay as they are.
+ *
* @param input the input compilation unit
* @param ranges the character ranges to reformat
* @return a list of {@link Replacement}s, sorted from low index to high index, without overlaps
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 215094cd8..d26d8d621 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
@@ -366,19 +366,21 @@ public List getFormatReplacements(String input, Collection 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)) {
+ RangeSet formattedRanges = StringWrapper.moveRanges(characterRanges, replacements);
+ // avoid trying to wrap the input unless the lines needing wrapping are part of the ranges
+ if (!StringWrapper.linesNeedWrapping(options.maxLineLength(), formatted, formattedRanges)) {
return replacements;
}
- String wrapped = StringWrapper.wrap(options.maxLineLength(), formatted, this);
+ String wrapped = StringWrapper.wrap(options.maxLineLength(), formatted, formattedRanges, this);
// if no wrapping change happened, return the initial replacements
if (wrapped.equals(formatted)) {
return replacements;
diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java b/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java
index c6a54eb3a..110a7067f 100644
--- a/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java
+++ b/open-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java
@@ -19,6 +19,7 @@
import com.google.common.base.CharMatcher;
import com.google.common.base.Verify;
+import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeMap;
@@ -41,6 +42,7 @@
import com.sun.tools.javac.util.Position.LineMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
@@ -72,16 +74,30 @@ public final class StringWrapper {
* adjacent string literals that share a line.
*/
static String wrap(final int columnLimit, String input, Formatter formatter) throws FormatterException {
- String result = wrapOnce(columnLimit, input, formatter);
- for (int round = 1; round < MAX_ROUNDS && !result.equals(input); round++) {
+ return wrap(columnLimit, new Source(input, null), formatter).text();
+ }
+
+ /**
+ * Reflows, as {@link #wrap(int, String, Formatter)} does, only the string literals and text blocks that overlap
+ * the given character ranges: when part of a file is formatted, the long strings in the rest of it stay as they
+ * are.
+ */
+ static String wrap(final int columnLimit, String input, RangeSet characterRanges, Formatter formatter)
+ throws FormatterException {
+ return wrap(columnLimit, new Source(input, characterRanges), formatter).text();
+ }
+
+ private static Source wrap(final int columnLimit, Source input, Formatter formatter) throws FormatterException {
+ Source result = wrapOnce(columnLimit, input, formatter);
+ for (int round = 1; round < MAX_ROUNDS && !result.text().equals(input.text()); round++) {
input = result;
result = wrapOnce(columnLimit, input, formatter);
}
return result;
}
- private static String wrapOnce(final int columnLimit, String input, Formatter formatter) throws FormatterException {
- if (!needWrapping(columnLimit, input)) {
+ private static Source wrapOnce(final int columnLimit, Source input, Formatter formatter) throws FormatterException {
+ if (!needWrapping(columnLimit, input.text())) {
// fast path
return input;
}
@@ -89,36 +105,36 @@ private static String wrapOnce(final int columnLimit, String input, Formatter fo
// The pieces continue the way the formatter continues a chain of +: four columns times the multiplier of its
// style, eight in OJF.
var continuationIndent = 4 * formatter.options().indentationMultiplier();
- TreeRangeMap replacements = getReflowReplacements(columnLimit, continuationIndent, input);
+ TreeRangeMap replacements = input.reflowReplacements(columnLimit, continuationIndent);
if (replacements.asMapOfRanges().isEmpty()) {
// needWrapping is an over-approximation, so there may be nothing to do after all.
return input;
}
- String firstPass =
- formatter.formatSource(input, replacements.asMapOfRanges().keySet());
+ Source firstPass = input.format(formatter, replacements.asMapOfRanges().keySet());
- if (!firstPass.equals(input)) {
+ if (!firstPass.text().equals(input.text())) {
// If formatting the replacement ranges resulted in a change, recalculate the replacements on
// the updated input.
input = firstPass;
- replacements = getReflowReplacements(columnLimit, continuationIndent, input);
+ replacements = input.reflowReplacements(columnLimit, continuationIndent);
}
- String result = applyReplacements(input, replacements);
+ Source result = input.apply(replacements);
// Format again, because broken strings might now fit on the first line in case of assignments
- String secondPass = formatter.formatSource(result, rangesAfterAppliedReplacements(replacements));
+ Source secondPass = result.format(formatter, rangesAfterAppliedReplacements(replacements));
- if (!secondPass.equals(result)) {
- replacements = getReflowReplacements(columnLimit, continuationIndent, secondPass);
- result = applyReplacements(secondPass, replacements);
+ if (!secondPass.text().equals(result.text())) {
+ replacements = secondPass.reflowReplacements(columnLimit, continuationIndent);
+ result = secondPass.apply(replacements);
}
{
// We really don't want bugs in this pass to change the behaviour of programs we're
// formatting, so check that the pretty-printed AST is the same before and after reformatting.
- String expected = parse(input, /* allowStringFolding= */ true).toString();
- String actual = parse(result, /* allowStringFolding= */ true).toString();
+ String expected =
+ parse(input.text(), /* allowStringFolding= */ true).toString();
+ String actual = parse(result.text(), /* allowStringFolding= */ true).toString();
if (!expected.equals(actual)) {
throw new FormatterException(String.format(
"Something has gone terribly wrong. Please file a bug: "
@@ -131,6 +147,82 @@ private static String wrapOnce(final int columnLimit, String input, Formatter fo
return result;
}
+ /**
+ * The source between two passes of {@link #wrapOnce}, with the character ranges of it that the passes may change:
+ * the ranges being formatted, moved along with every change, or {@code null} when the whole file is.
+ */
+ private record Source(String text, @Nullable RangeSet ranges) {
+
+ /** The replacements of {@link #getReflowReplacements}, less those for literals outside the ranges. */
+ TreeRangeMap reflowReplacements(int columnLimit, int continuationIndent)
+ throws FormatterException {
+ TreeRangeMap replacements = getReflowReplacements(columnLimit, continuationIndent, text);
+ RangeSet within = ranges;
+ if (within == null) {
+ return replacements;
+ }
+ TreeRangeMap inRanges = TreeRangeMap.create();
+ replacements.asMapOfRanges().forEach((literal, replacement) -> {
+ if (within.intersects(literal)) {
+ inRanges.put(literal, replacement);
+ }
+ });
+ return inRanges;
+ }
+
+ /** Formats the given ranges, as {@link Formatter#formatSource(String, Collection)} does. */
+ Source format(Formatter formatter, Collection> rangesToFormat) throws FormatterException {
+ List replacements = formatter.getFormatReplacements(text, rangesToFormat);
+ return new Source(Utils.applyReplacements(text, replacements), moved(replacements));
+ }
+
+ Source apply(TreeRangeMap replacements) {
+ List applied = replacements.asMapOfRanges().entrySet().stream()
+ .map(entry -> Replacement.create(
+ entry.getKey().lowerEndpoint(), entry.getKey().upperEndpoint(), entry.getValue()))
+ .toList();
+ return new Source(applyReplacements(text, replacements), moved(applied));
+ }
+
+ private @Nullable RangeSet moved(List replacements) {
+ return ranges == null ? null : moveRanges(ranges, replacements);
+ }
+ }
+
+ /**
+ * Where the given character ranges are once the replacements, sorted and without overlaps, have been applied. A
+ * range grows to take in all of a replacement that overlaps it, or that inserts text at its start.
+ */
+ static RangeSet moveRanges(RangeSet ranges, List replacements) {
+ RangeSet moved = TreeRangeSet.create();
+ for (Range range : ranges.asRanges()) {
+ Range canonical = range.canonical(DiscreteDomain.integers());
+ moved.add(Range.closedOpen(
+ movePosition(canonical.lowerEndpoint(), replacements, /* start= */ true),
+ movePosition(canonical.upperEndpoint(), replacements, /* start= */ false)));
+ }
+ return moved;
+ }
+
+ private static int movePosition(int position, List replacements, boolean start) {
+ int shift = 0;
+ for (Replacement replacement : replacements) {
+ int lower = replacement.getReplaceRange().lowerEndpoint();
+ int upper = replacement.getReplaceRange().upperEndpoint();
+ int length = replacement.getReplacementString().length();
+ if (upper < position || (upper == position && lower < position)) {
+ // Before the position.
+ shift += length - (upper - lower);
+ } else if (lower < position) {
+ // Around the position, which moves to the replacement's start or end.
+ return lower + shift + (start ? 0 : length);
+ } else {
+ break;
+ }
+ }
+ return position + shift;
+ }
+
private static Set> rangesAfterAppliedReplacements(TreeRangeMap replacements) {
var outputRanges = new LinkedHashSet>();
int offset = 0;
diff --git a/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java b/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java
index 1379bd871..34e466178 100644
--- a/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java
+++ b/open-java-format/src/test/java/com/palantir/javaformat/java/StringWrapperTest.java
@@ -19,6 +19,9 @@
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
+import com.google.common.collect.Range;
+import com.google.common.collect.RangeSet;
+import com.google.common.collect.TreeRangeSet;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
@@ -29,6 +32,7 @@
import java.io.IOException;
import java.net.URI;
import java.util.List;
+import java.util.Set;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
@@ -94,6 +98,57 @@ void wrappingKeepsTheValueOfAStringWithEscapes() throws Exception {
assertThat(stringValue(output)).isEqualTo(stringValue(input));
}
+ @Test
+ void wrapsOnlyTheStringsInTheRanges() throws Exception {
+ // As when one line of the file is formatted: the long string on the other line stays as it is.
+ String input = """
+ class T {
+ String outside = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance";
+ String inside = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance";
+ }
+ """;
+ String output = """
+ class T {
+ String outside = "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance";
+ String inside =
+ "one long incredibly unbroken sentence moving from topic to topic so that no one had a"
+ + " chance";
+ }
+ """;
+ int inside = input.indexOf(" String inside");
+ RangeSet insideLine =
+ TreeRangeSet.create(List.of(Range.closedOpen(inside, input.indexOf('\n', inside))));
+ RangeSet wholeFile = TreeRangeSet.create(List.of(Range.closedOpen(0, input.length())));
+
+ assertThat(StringWrapper.wrap(100, input, insideLine, Formatter.create()))
+ .isEqualTo(output);
+ assertThat(StringWrapper.wrap(100, input, wholeFile, Formatter.create()))
+ .isEqualTo(StringWrapper.wrap(100, input, Formatter.create()));
+ }
+
+ @Test
+ void movesRangesAlongWithTheReplacements() {
+ // Characters 4 to 8 become two, and two are inserted at 12.
+ List replacements = List.of(Replacement.create(4, 8, "ab"), Replacement.create(12, 12, "cd"));
+
+ // Before the replacements, and after the first one.
+ assertThat(moved(Range.closedOpen(0, 2), replacements)).containsExactly(Range.closedOpen(0, 2));
+ assertThat(moved(Range.closedOpen(9, 11), replacements)).containsExactly(Range.closedOpen(7, 9));
+ // Over the start or the end of a replacement: the range takes in all of it.
+ assertThat(moved(Range.closedOpen(2, 5), replacements)).containsExactly(Range.closedOpen(2, 6));
+ assertThat(moved(Range.closedOpen(6, 10), replacements)).containsExactly(Range.closedOpen(4, 8));
+ // Text inserted at the start of a range goes in, at its end it does not.
+ assertThat(moved(Range.closedOpen(12, 14), replacements)).containsExactly(Range.closedOpen(10, 14));
+ assertThat(moved(Range.closedOpen(10, 12), replacements)).containsExactly(Range.closedOpen(8, 10));
+ // A closed range moves as the half-open one with the same characters.
+ assertThat(moved(Range.closed(9, 10), replacements)).containsExactly(Range.closedOpen(7, 9));
+ }
+
+ private static Set> moved(Range range, List replacements) {
+ return StringWrapper.moveRanges(TreeRangeSet.create(List.of(range)), replacements)
+ .asRanges();
+ }
+
/** The value javac gives the initializer of the only field: a string literal, or literals joined with +. */
private static String stringValue(String source) throws IOException {
JavaFileObject file = new SimpleJavaFileObject(URI.create("string:///T.java"), JavaFileObject.Kind.SOURCE) {