Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,16 @@ 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", ".");
runCommandInRepo("git", "commit", "-m", "A class");

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.";
}
""");
Expand All @@ -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.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface FormatterService {
/**
* Emit a list of {@link Replacement}s to convert from input to formatted output.
*
* <p>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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,21 @@ public List<Replacement> getFormatReplacements(String input, Collection<Range<In
}

/**
* 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.
* Emit the {@link Replacement}s that format the given character ranges and reflow the long strings in the code
* that formatting them rewrites; the imports, and the strings elsewhere, stay as they are. The command line prints
* these 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<Replacement> getFormatAndReflowReplacements(String input, RangeSet<Integer> characterRanges)
throws FormatterException {
List<Replacement> 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<Integer> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -72,53 +74,67 @@ 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<Integer> 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;
}

// 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<Integer, String> replacements = getReflowReplacements(columnLimit, continuationIndent, input);
TreeRangeMap<Integer, String> 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: "
Expand All @@ -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<Integer> ranges) {

/** The replacements of {@link #getReflowReplacements}, less those for literals outside the ranges. */
TreeRangeMap<Integer, String> reflowReplacements(int columnLimit, int continuationIndent)
throws FormatterException {
TreeRangeMap<Integer, String> replacements = getReflowReplacements(columnLimit, continuationIndent, text);
RangeSet<Integer> within = ranges;
if (within == null) {
return replacements;
}
TreeRangeMap<Integer, String> 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<Range<Integer>> rangesToFormat) throws FormatterException {
List<Replacement> replacements = formatter.getFormatReplacements(text, rangesToFormat);
return new Source(Utils.applyReplacements(text, replacements), moved(replacements));
}

Source apply(TreeRangeMap<Integer, String> replacements) {
List<Replacement> 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<Integer> moved(List<Replacement> 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<Integer> moveRanges(RangeSet<Integer> ranges, List<Replacement> replacements) {
RangeSet<Integer> moved = TreeRangeSet.create();
for (Range<Integer> range : ranges.asRanges()) {
Range<Integer> 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<Replacement> 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<Range<Integer>> rangesAfterAppliedReplacements(TreeRangeMap<Integer, String> replacements) {
var outputRanges = new LinkedHashSet<Range<Integer>>();
int offset = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Integer> insideLine =
TreeRangeSet.create(List.of(Range.closedOpen(inside, input.indexOf('\n', inside))));
RangeSet<Integer> 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<Replacement> 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<Range<Integer>> moved(Range<Integer> range, List<Replacement> 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) {
Expand Down
Loading