diff --git a/core/src/main/java/com/google/googlejavaformat/java/CleanupPass.java b/core/src/main/java/com/google/googlejavaformat/java/CleanupPass.java index e92ed4d32..25e9639de 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/CleanupPass.java +++ b/core/src/main/java/com/google/googlejavaformat/java/CleanupPass.java @@ -15,6 +15,7 @@ package com.google.googlejavaformat.java; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableRangeSet; /// Standard cleanup passes applied before formatting. enum CleanupPass { @@ -30,22 +31,31 @@ ImmutableList findReplacements(Cleanups cleanups) throws FormatterE AST_CLEANUPS { @Override ImmutableList findReplacements(Cleanups cleanups) throws FormatterException { - return CleanupScanner.scan(cleanups.unit(), cleanups.contents(), cleanups.ranges()); + return CleanupScanner.scan( + cleanups.unit(), + cleanups.contents(), + cleanups.javaInput().getToks(), + cleanups.ranges(), + ImmutableRangeSet.of()); } }, - /// Reorders modifiers into JLS order. - REORDER_MODIFIERS { + /// Reorders imports into canonical style order. + SORT_IMPORTS { @Override ImmutableList findReplacements(Cleanups cleanups) throws FormatterException { - JavaInput javaInput = new JavaInput(cleanups.contents()); - return ModifierOrderer.findModifierReplacements(javaInput, cleanups.ranges()); + JavaInput cached = cleanups.cachedJavaInput(); + if (cached != null) { + return ImportOrderer.findReplacements( + cleanups.contents(), cached.getToks(), cleanups.style()); + } + return ImportOrderer.findReplacements(cleanups.contents(), cleanups.style()); } }, - /// Reorders imports into canonical style order. - SORT_IMPORTS { + /// Reorders modifiers into JLS order. + REORDER_MODIFIERS { @Override ImmutableList findReplacements(Cleanups cleanups) throws FormatterException { - return ImportOrderer.findReplacements(cleanups.contents(), cleanups.style()); + return ModifierOrderer.findModifierReplacements(cleanups.javaInput(), cleanups.ranges()); } }; diff --git a/core/src/main/java/com/google/googlejavaformat/java/CleanupScanner.java b/core/src/main/java/com/google/googlejavaformat/java/CleanupScanner.java index b36167630..49b4e6ac5 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/CleanupScanner.java +++ b/core/src/main/java/com/google/googlejavaformat/java/CleanupScanner.java @@ -23,6 +23,7 @@ import static java.lang.Math.min; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableRangeSet; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Range; import com.google.common.collect.RangeSet; @@ -59,10 +60,25 @@ final class CleanupScanner extends TreePathScanner<@Nullable Void, @Nullable Voi static ImmutableList scan( JCCompilationUnit unit, String contents, Collection> ranges) throws FormatterException { + return scan( + unit, + contents, + JavaInput.buildToks(contents, ImmutableSet.of()), + ranges, + ImmutableRangeSet.of()); + } + + static ImmutableList scan( + JCCompilationUnit unit, + String contents, + ImmutableList toks, + Collection> ranges, + RangeSet preExistingDeletions) + throws FormatterException { RangeSet targetRanges = TreeRangeSet.create(ranges); - ImmutableList toks = JavaInput.buildToks(contents, ImmutableSet.of()); RangeSet activeDeletions = TreeRangeSet.create(); - new CleanupScanner(contents, unit, toks, targetRanges, activeDeletions).scan(unit, null); + new CleanupScanner(contents, unit, toks, targetRanges, activeDeletions, preExistingDeletions) + .scan(unit, null); if (activeDeletions.isEmpty()) { return ImmutableList.of(); } @@ -122,18 +138,21 @@ private static void verifyCommentsPreserved( private final ImmutableList toks; private final RangeSet targetRanges; private final RangeSet activeDeletions; + private final RangeSet preExistingDeletions; CleanupScanner( String contents, JCCompilationUnit unit, ImmutableList toks, RangeSet targetRanges, - RangeSet activeDeletions) { + RangeSet activeDeletions, + RangeSet preExistingDeletions) { this.contents = contents; this.unit = unit; this.toks = toks; this.targetRanges = targetRanges; this.activeDeletions = activeDeletions; + this.preExistingDeletions = preExistingDeletions; } private boolean inRange(int startPos, int endPos) { @@ -142,7 +161,9 @@ private boolean inRange(int startPos, int endPos) { private void deleteRedundantSemicolon(int semiPos) { int start = semiPos; - while (start > 0 && isHorizontalWhitespace(contents.charAt(start - 1))) { + while (start > 0 + && isHorizontalWhitespace(contents.charAt(start - 1)) + && !preExistingDeletions.contains(start - 1)) { start--; } int lineBegin = start; @@ -153,8 +174,7 @@ private void deleteRedundantSemicolon(int semiPos) { while (end < contents.length() && isHorizontalWhitespace(contents.charAt(end))) { end++; } - boolean nothingBeforeOnLine = - start == lineBegin || activeDeletions.encloses(Range.closedOpen(lineBegin, start)); + boolean nothingBeforeOnLine = start == lineBegin || isDeletedRange(lineBegin, start); if (nothingBeforeOnLine && end < contents.length()) { if (contents.charAt(end) == '\r') { end++; @@ -168,6 +188,19 @@ private void deleteRedundantSemicolon(int semiPos) { activeDeletions.add(Range.closedOpen(start, end)); } + private boolean isDeletedRange(int start, int end) { + Range range = Range.closedOpen(start, end); + if (activeDeletions.encloses(range)) { + return true; + } + if (preExistingDeletions.isEmpty()) { + return false; + } + RangeSet combined = TreeRangeSet.create(activeDeletions.subRangeSet(range)); + combined.addAll(preExistingDeletions.subRangeSet(range)); + return combined.encloses(range); + } + private static boolean isHorizontalWhitespace(char c) { return c == ' ' || c == '\t'; } diff --git a/core/src/main/java/com/google/googlejavaformat/java/Cleanups.java b/core/src/main/java/com/google/googlejavaformat/java/Cleanups.java index baaa45d3e..be636818b 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Cleanups.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Cleanups.java @@ -16,6 +16,7 @@ import static java.lang.Math.max; import static java.lang.Math.min; +import static java.util.Comparator.comparingInt; import com.google.common.base.CharMatcher; import com.google.common.collect.ImmutableList; @@ -43,11 +44,13 @@ final class Cleanups { /// The cleaned contents, the requested character ranges remapped onto them (plus any modified - /// regions), and the javac unit for `contents` if it can be reused for formatting. + /// regions), and the javac unit and lexed [JavaInput] for `contents` if they can be reused for + /// formatting. record Result( String contents, Collection> characterRanges, - @Nullable JCCompilationUnit unit) {} + @Nullable JCCompilationUnit unit, + @Nullable JavaInput javaInput) {} private record Parsed(Context context, JCCompilationUnit unit) {} @@ -78,7 +81,7 @@ static Result apply( throws FormatterException { JavaInput.validateRanges(characterRanges, contents.length()); if (passes.isEmpty()) { - return new Result(contents, characterRanges, null); + return new Result(contents, characterRanges, null, null); } RangeSet normalized = TreeRangeSet.create(); for (Range range : characterRanges) { @@ -88,8 +91,17 @@ static Result apply( : range); } Cleanups cleanups = new Cleanups(contents, ImmutableRangeSet.copyOf(normalized), style); + boolean fuseAstPasses = + passes.contains(CleanupPass.REMOVE_UNUSED_IMPORTS) + && passes.contains(CleanupPass.AST_CLEANUPS); for (CleanupPass pass : passes) { - cleanups.applyReplacements(pass.findReplacements(cleanups)); + if (fuseAstPasses && pass == CleanupPass.REMOVE_UNUSED_IMPORTS) { + cleanups.applyFusedAstPasses(); + } else if (fuseAstPasses && pass == CleanupPass.AST_CLEANUPS) { + // Already applied together with REMOVE_UNUSED_IMPORTS on the same parsed AST. + } else { + cleanups.applyReplacements(pass.findReplacements(cleanups)); + } } // An empty range can't be represented in a RangeSet, so restore it if the passes removed all // contents (or the input was empty). @@ -98,12 +110,16 @@ static Result apply( ? ImmutableList.of(Range.closedOpen(0, 0)) : cleanups.ranges.asRanges(); return new Result( - cleanups.contents, resultRanges, cleanups.parsed == null ? null : cleanups.parsed.unit()); + cleanups.contents, + resultRanges, + cleanups.parsed == null ? null : cleanups.parsed.unit(), + cleanups.javaInput); } private String contents; private ImmutableRangeSet ranges; private @Nullable Parsed parsed; + private @Nullable JavaInput javaInput; private final Style style; private Cleanups(String contents, ImmutableRangeSet ranges, Style style) { @@ -148,6 +164,48 @@ Context context() throws FormatterException { return parsed().context(); } + JavaInput javaInput() throws FormatterException { + if (javaInput == null) { + javaInput = new JavaInput(contents); + } + return javaInput; + } + + @Nullable JavaInput cachedJavaInput() { + return javaInput; + } + + /// Runs `REMOVE_UNUSED_IMPORTS` and `AST_CLEANUPS` against a single parsed [JCCompilationUnit] so + /// unused-import edits do not force an extra full-file parse before AST cleanups. + private void applyFusedAstPasses() throws FormatterException { + ImmutableList unusedImports = + RemoveUnusedImports.findUnusedImports(context(), unit(), contents); + RangeSet preExistingDeletions; + if (unusedImports.isEmpty()) { + preExistingDeletions = ImmutableRangeSet.of(); + } else { + RangeSet deletions = TreeRangeSet.create(); + for (Replacement r : unusedImports) { + deletions.add(r.replaceRange()); + } + preExistingDeletions = deletions; + } + ImmutableList astCleanups = + CleanupScanner.scan( + unit(), contents, javaInput().getToks(), ranges(), preExistingDeletions); + if (unusedImports.isEmpty()) { + applyReplacements(astCleanups); + } else if (astCleanups.isEmpty()) { + applyReplacements(unusedImports); + } else { + List merged = new ArrayList<>(unusedImports.size() + astCleanups.size()); + merged.addAll(unusedImports); + merged.addAll(astCleanups); + merged.sort(comparingInt(r -> r.replaceRange().lowerEndpoint())); + applyReplacements(merged); + } + } + /// Applies a pass's replacements to the current contents, remapping requested ranges and adding /// modified regions. private void applyReplacements(List replacements) { @@ -166,6 +224,7 @@ private void applyReplacements(List replacements) { this.contents = edit.contents(); this.ranges = ImmutableRangeSet.copyOf(newRanges); this.parsed = null; + this.javaInput = null; } /// Returns the range to reformat for the given span. diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java index 2b763c1fa..003acbe69 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java @@ -193,14 +193,24 @@ String formatSource( Edit.apply( cleaned.contents(), getFormatReplacements( - cleaned.contents(), cleaned.characterRanges(), options.cleanup(), cleaned.unit())); + cleaned.contents(), + cleaned.characterRanges(), + options.cleanup(), + cleaned.unit(), + cleaned.javaInput())); if (reflowLongStrings) { // Only reflow long strings in the requested ranges, mapped onto the formatted output. + boolean wholeFile = + characterRanges.size() == 1 + && characterRanges.iterator().next().equals(Range.closedOpen(0, input.length())); + JCCompilationUnit reusableUnit = formatted.spans().isEmpty() ? cleaned.unit() : null; return StringWrapper.wrap( options.maxLineLength(), formatted.contents(), this, - formatted.mapRanges(cleaned.characterRanges())); + formatted.mapRanges(cleaned.characterRanges()), + reusableUnit, + wholeFile); } return formatted.contents(); } @@ -265,7 +275,8 @@ public ImmutableList getFormatReplacements( ImmutableList getFormatReplacements( String input, Collection> characterRanges, boolean reindent) throws FormatterException { - return getFormatReplacements(input, characterRanges, reindent, /* unit= */ null); + return getFormatReplacements( + input, characterRanges, reindent, /* unit= */ null, /* cachedJavaInput= */ null); } ImmutableList getFormatReplacements( @@ -274,7 +285,18 @@ ImmutableList getFormatReplacements( boolean reindent, @Nullable JCCompilationUnit unit) throws FormatterException { - JavaInput javaInput = new JavaInput(input); + return getFormatReplacements( + input, characterRanges, reindent, unit, /* cachedJavaInput= */ null); + } + + ImmutableList getFormatReplacements( + String input, + Collection> characterRanges, + boolean reindent, + @Nullable JCCompilationUnit unit, + @Nullable JavaInput cachedJavaInput) + throws FormatterException { + JavaInput javaInput = cachedJavaInput != null ? cachedJavaInput : new JavaInput(input); if (unit != null) { javaInput.setCompilationUnit(unit); } diff --git a/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java b/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java index 27d2fde68..ce84dabef 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java +++ b/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java @@ -76,6 +76,12 @@ public static String reorderImports(String text) throws FormatterException { static ImmutableList findReplacements(String text, Style style) throws FormatterException { ImmutableList toks = JavaInput.buildToks(text, CLASS_START); + return findReplacements(text, toks, style); + } + + /// Variant of [#findReplacements(String, Style)] that reuses an already-lexed token stream. + static ImmutableList findReplacements( + String text, ImmutableList toks, Style style) throws FormatterException { return new ImportOrderer(text, toks, style).findReplacements(); } diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java index edfb2ab10..55b839bf5 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java @@ -251,6 +251,7 @@ public String toString() { // `/*` comments can lie on multiple lines. These data structures (along with equivalent ones for // the formatted output) let us compute correspondences between the input and output. + private final ImmutableList toks; // All Toks (including whitespace and comments). private final ImmutableMap positionToColumnMap; // Map Tok position to column. private final ImmutableList tokens; // The Tokens for this input. private final ImmutableRangeMap positionTokenMap; // Map position to Token. @@ -265,7 +266,7 @@ public String toString() { JavaInput(String text) throws FormatterException { this.text = checkNotNull(text); setLines(ImmutableList.copyOf(Newlines.lineIterator(text))); - ImmutableList toks = buildToks(text); + this.toks = buildToks(text); positionToColumnMap = makePositionToColumnMap(toks); tokens = buildTokens(toks); ImmutableRangeMap.Builder tokenLocations = ImmutableRangeMap.builder(); @@ -751,6 +752,11 @@ public Token getToken(int k) { return kToToken[k]; } + /// Get all input toks (tokens, comments, whitespace, and EOF). + ImmutableList getToks() { + return toks; + } + /// Get the input tokens. /// /// @return the input tokens diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index c87ca449b..ef42a1180 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -27,11 +27,29 @@ import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeMap; import com.google.googlejavaformat.Newlines; +import com.sun.source.tree.AnnotationTree; +import com.sun.source.tree.AssertTree; +import com.sun.source.tree.AssignmentTree; import com.sun.source.tree.BinaryTree; +import com.sun.source.tree.CaseTree; +import com.sun.source.tree.ClassTree; +import com.sun.source.tree.CompoundAssignmentTree; +import com.sun.source.tree.ConditionalExpressionTree; +import com.sun.source.tree.ExpressionStatementTree; +import com.sun.source.tree.LambdaExpressionTree; import com.sun.source.tree.LiteralTree; import com.sun.source.tree.MemberSelectTree; +import com.sun.source.tree.MethodInvocationTree; +import com.sun.source.tree.NewArrayTree; +import com.sun.source.tree.NewClassTree; +import com.sun.source.tree.ParenthesizedTree; +import com.sun.source.tree.ReturnTree; +import com.sun.source.tree.SwitchExpressionTree; +import com.sun.source.tree.ThrowTree; import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; +import com.sun.source.tree.VariableTree; +import com.sun.source.tree.YieldTree; import com.sun.source.util.TreePath; import com.sun.source.util.TreePathScanner; import com.sun.tools.javac.tree.JCTree; @@ -40,12 +58,13 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; -import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import javax.tools.Diagnostic; import javax.tools.JavaFileObject; +import org.jspecify.annotations.Nullable; /// Wraps string literals that exceed the column limit. public final class StringWrapper { @@ -69,51 +88,142 @@ static String wrap(final int columnLimit, String input, Formatter formatter) static String wrap( final int columnLimit, String input, Formatter formatter, RangeSet characterRanges) throws FormatterException { + return wrap( + columnLimit, + input, + formatter, + characterRanges, + /* parsedUnit= */ null, + /* alreadyFormattedWholeFile= */ false); + } + + static String wrap( + final int columnLimit, + String input, + Formatter formatter, + RangeSet characterRanges, + JCTree.@Nullable JCCompilationUnit parsedUnit, + boolean alreadyFormattedWholeFile) + throws FormatterException { JavaFormatterOptions options = formatter.options(); - if (!needWrapping(columnLimit, input, options)) { + if (!needWrapping(columnLimit, input, options, characterRanges)) { // fast path return input; } - TreeRangeMap replacements = - getReflowReplacements(columnLimit, input, options, characterRanges); - if (replacements.asMapOfRanges().isEmpty()) { + ReflowResult reflow = + getReflowReplacements(columnLimit, input, options, characterRanges, parsedUnit); + if (reflow.replacements().asMapOfRanges().isEmpty()) { return input; } - Edit firstPass = - Edit.apply( - input, formatter.getFormatReplacements(input, replacements.asMapOfRanges().keySet())); - - if (!firstPass.contents().equals(input)) { - // If formatting the replacement ranges resulted in a change, recalculate the replacements on - // the updated input. - input = firstPass.contents(); - characterRanges = firstPass.mapRanges(characterRanges.asRanges()); - replacements = getReflowReplacements(columnLimit, input, options, characterRanges); + if (!alreadyFormattedWholeFile) { + Edit firstPass = + Edit.apply( + input, + formatter.getFormatReplacements( + input, + reflow.replacements().asMapOfRanges().keySet(), + /* reindent= */ false, + reflow.unit())); + + if (!firstPass.contents().equals(input)) { + // If formatting the replacement ranges resulted in a change, recalculate the replacements + // on the updated input. + input = firstPass.contents(); + characterRanges = firstPass.mapRanges(characterRanges.asRanges()); + reflow = + getReflowReplacements( + columnLimit, input, options, characterRanges, /* parsedUnit= */ null); + if (reflow.replacements().asMapOfRanges().isEmpty()) { + return input; + } + } } - String result = applyReplacements(input, 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; - try { - actual = parse(result, /* allowStringFolding= */ true).toString(); - } catch (FormatterException e) { - // Diagnostics for the reflowed output don't correspond to positions in the original input, - // and the error is a bug in this pass rather than in the input, so report it as such. - throw somethingHasGoneTerriblyWrong( - "it would produce code that does not parse (" + e.getMessage() + ")", result, input); + String result = applyReplacements(input, reflow.replacements()); + + verifyReplacements(input, result, reflow); + + return result; + } + + /// Verifies that reflowing string literals did not produce unparseable code or change the AST. + /// + /// When all replacements occur in syntactic contexts where replacing a string literal with a + /// string `+` concatenation cannot change operator precedence, we parse a tiny synthetic class + /// containing only the replacement expressions (with `allowStringFolding=true`) and verify each + /// folds to the exact original string literal value. Otherwise, we fall back to full-file AST + /// comparison. + private static void verifyReplacements(String input, String result, ReflowResult reflow) + throws FormatterException { + if (reflow.needsFullAstVerification()) { + verifyFullAst(input, result); + return; + } + Map, String> replacementMap = reflow.replacements().asMapOfRanges(); + StringBuilder synthetic = new StringBuilder(); + synthetic.append("class C {\n"); + List expectedValues = new ArrayList<>(replacementMap.size()); + int idx = 0; + for (Map.Entry, String> entry : replacementMap.entrySet()) { + synthetic + .append(" String s") + .append(idx++) + .append(" = ") + .append(entry.getValue()) + .append(";\n"); + expectedValues.add(reflow.expectedValues().get(entry.getKey())); + } + synthetic.append("}\n"); + + JCTree.JCCompilationUnit syntheticUnit; + try { + syntheticUnit = parse(synthetic.toString(), /* allowStringFolding= */ true); + } catch (FormatterException e) { + throw somethingHasGoneTerriblyWrong( + "it would produce code that does not parse (" + e.getMessage() + ")", result, input); + } + + if (syntheticUnit.getTypeDecls().size() == 1 + && syntheticUnit.getTypeDecls().get(0) instanceof ClassTree classTree + && classTree.getMembers().size() == expectedValues.size()) { + boolean allMatch = true; + for (int i = 0; i < expectedValues.size(); i++) { + Tree member = classTree.getMembers().get(i); + if (member instanceof VariableTree varTree + && varTree.getInitializer() instanceof LiteralTree literalTree + && literalTree.getKind() == Kind.STRING_LITERAL + && expectedValues.get(i).equals(literalTree.getValue())) { + continue; + } + allMatch = false; + break; } - if (!expected.equals(actual)) { - throw somethingHasGoneTerriblyWrong( - "it would unexpectedly change the AST", actual, expected); + if (allMatch) { + return; } } + // Unexpected mismatch in synthetic verification; run full AST comparison to produce the full + // diagnostic diff. + verifyFullAst(input, result); + } - return result; + private static void verifyFullAst(String input, String result) throws FormatterException { + // 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; + try { + actual = parse(result, /* allowStringFolding= */ true).toString(); + } catch (FormatterException e) { + // Diagnostics for the reflowed output don't correspond to positions in the original input, + // and the error is a bug in this pass rather than in the input, so report it as such. + throw somethingHasGoneTerriblyWrong( + "it would produce code that does not parse (" + e.getMessage() + ")", result, input); + } + if (!expected.equals(actual)) { + throw somethingHasGoneTerriblyWrong("it would unexpectedly change the AST", actual, expected); + } } private static FormatterException somethingHasGoneTerriblyWrong( @@ -128,13 +238,21 @@ private static FormatterException somethingHasGoneTerriblyWrong( reason, actual, expected)); } - private static TreeRangeMap getReflowReplacements( + private record ReflowResult( + TreeRangeMap replacements, + JCTree.JCCompilationUnit unit, + Map, String> expectedValues, + boolean needsFullAstVerification) {} + + private static ReflowResult getReflowReplacements( int columnLimit, final String input, JavaFormatterOptions options, - RangeSet characterRanges) + RangeSet characterRanges, + JCTree.@Nullable JCCompilationUnit parsedUnit) throws FormatterException { - return new Reflower(columnLimit, input, options, characterRanges).getReflowReplacements(); + return new Reflower(columnLimit, input, options, characterRanges, parsedUnit) + .getReflowReplacements(); } private static class Reflower { @@ -151,14 +269,15 @@ private static class Reflower { int columnLimit, String input, JavaFormatterOptions options, - RangeSet characterRanges) + RangeSet characterRanges, + JCTree.@Nullable JCCompilationUnit parsedUnit) throws FormatterException { this.columnLimit = columnLimit; this.input = input; this.options = options; this.characterRanges = characterRanges; this.separator = Newlines.guessLineSeparator(input); - this.unit = parse(input, /* allowStringFolding= */ false); + this.unit = parsedUnit != null ? parsedUnit : parse(input, /* allowStringFolding= */ false); this.lineMap = unit.getLineMap(); } @@ -167,13 +286,15 @@ private int visualColumn(int position) { return options.visualLength(input, lineStart, position); } - TreeRangeMap getReflowReplacements() { + ReflowResult getReflowReplacements() { // Paths to string literals that extend past the column limit. List longStringLiterals = new ArrayList<>(); new LongStringsScanner(longStringLiterals).scan(new TreePath(unit), null); TreeRangeMap replacements = TreeRangeMap.create(); - wrapLongStrings(replacements, longStringLiterals); - return replacements; + Map, String> expectedValues = new LinkedHashMap<>(); + boolean needsFullAstVerification = + wrapLongStrings(replacements, expectedValues, longStringLiterals); + return new ReflowResult(replacements, unit, expectedValues, needsFullAstVerification); } private class LongStringsScanner extends TreePathScanner { @@ -214,8 +335,11 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { } } - private void wrapLongStrings( - TreeRangeMap replacements, List longStringLiterals) { + private boolean wrapLongStrings( + TreeRangeMap replacements, + Map, String> expectedValues, + List longStringLiterals) { + boolean needsFullAstVerification = false; for (TreePath path : longStringLiterals) { // Find the outermost contiguous enclosing concatenation expression TreePath enclosing = path; @@ -227,9 +351,16 @@ private void wrapLongStrings( // We need this information to handle continuation indents. AtomicBoolean first = new AtomicBoolean(false); // Finds the set of string literals in the concat expression that includes the one that - // needs - // to be wrapped. + // needs to be wrapped. List flat = flatten(input, unit, path, enclosing, first); + Range replaceRange = + Range.closedOpen(getStartPosition(flat.get(0)), getEndPosition(getLast(flat), unit)); + if (replacements.get(replaceRange.lowerEndpoint()) != null) { + continue; + } + if (!isSafeParentForConcat(enclosing)) { + needsFullAstVerification = true; + } // Zero-indexed start column int startColumn = visualColumn(getStartPosition(flat.get(0))); @@ -245,11 +376,51 @@ private void wrapLongStrings( // Get the original source text of the string literals, excluding `"` and `+`. ImmutableList components = stringComponents(input, unit, flat); replacements.put( - Range.closedOpen(getStartPosition(flat.get(0)), getEndPosition(getLast(flat), unit)), + replaceRange, reflow( separator, columnLimit, startColumn, trailing, components, first.get(), options)); + StringBuilder expected = new StringBuilder(); + for (Tree literal : flat) { + expected.append(((LiteralTree) literal).getValue()); + } + expectedValues.put(replaceRange, expected.toString()); } + return needsFullAstVerification; + } + } + + /// Returns true if replacing a string literal (or `+` chain of string literals) with a `+` + /// concatenation under `enclosing`'s parent cannot alter surrounding operator precedence. + private static boolean isSafeParentForConcat(TreePath enclosing) { + TreePath parentPath = enclosing.getParentPath(); + if (parentPath == null) { + return false; + } + Tree parent = parentPath.getLeaf(); + if (parent instanceof BinaryTree binaryTree) { + return switch (binaryTree.getKind()) { + case MULTIPLY, DIVIDE, REMAINDER, MINUS, PLUS -> false; + default -> true; + }; } + return parent instanceof VariableTree + || parent instanceof AssignmentTree + || parent instanceof CompoundAssignmentTree + || parent instanceof MethodInvocationTree + || (parent instanceof NewClassTree newClassTree + && newClassTree.getArguments().contains(enclosing.getLeaf())) + || parent instanceof ReturnTree + || parent instanceof ExpressionStatementTree + || parent instanceof ConditionalExpressionTree + || parent instanceof ParenthesizedTree + || parent instanceof NewArrayTree + || parent instanceof AnnotationTree + || parent instanceof CaseTree + || parent instanceof YieldTree + || parent instanceof ThrowTree + || parent instanceof AssertTree + || parent instanceof LambdaExpressionTree + || parent instanceof SwitchExpressionTree; } /// Returns the source text of the given string literal trees, excluding the leading and trailing @@ -454,14 +625,42 @@ private static boolean noComments( private static final CharMatcher STRING_CONCAT_DELIMITER = CharMatcher.whitespace().or(CharMatcher.anyOf("\"+")); - /// Returns true if any lines in the given Java source exceed the column limit. - private static boolean needWrapping(int columnLimit, String input, JavaFormatterOptions options) { - // TODO(cushon): consider adding Newlines.lineIterable? - Iterator it = Newlines.lineIterator(input); - while (it.hasNext()) { - String line = it.next(); - if (options.visualLength(line) > columnLimit) { - return true; + /// Returns true if any line in the given Java source exceeds the column limit, contains a double + /// quote (so it might contain a string literal), and intersects `characterRanges`. + private static boolean needWrapping( + int columnLimit, + String input, + JavaFormatterOptions options, + RangeSet characterRanges) { + int len = input.length(); + int lineStart = 0; + while (lineStart < len) { + int lineEnd = lineStart; + while (lineEnd < len) { + char c = input.charAt(lineEnd); + if (c == '\n' || c == '\r') { + break; + } + lineEnd++; + } + if (options.visualLength(input, lineStart, lineEnd) > columnLimit) { + int quoteIdx = input.indexOf('"', lineStart); + if (quoteIdx != -1 + && quoteIdx < lineEnd + && characterRanges.intersects(Range.closedOpen(lineStart, lineEnd))) { + return true; + } + } + if (lineEnd < len) { + if (input.charAt(lineEnd) == '\r' + && lineEnd + 1 < len + && input.charAt(lineEnd + 1) == '\n') { + lineStart = lineEnd + 2; + } else { + lineStart = lineEnd + 1; + } + } else { + break; } } return false; diff --git a/core/src/test/java/com/google/googlejavaformat/java/CleanupsTest.java b/core/src/test/java/com/google/googlejavaformat/java/CleanupsTest.java index 3e685ed8a..2fc8fd3b6 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/CleanupsTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/CleanupsTest.java @@ -607,11 +607,12 @@ class Test { ImmutableList.of(Range.closedOpen(0, input.length())), Style.GOOGLE, Cleanups.enabledPasses( - /* cleanup= */ false, + /* cleanup= */ true, /* removeUnusedImports= */ true, - /* reorderModifiers= */ false, - /* sortImports= */ false)); + /* reorderModifiers= */ true, + /* sortImports= */ true)); assertThat(result.unit()).isNotNull(); + assertThat(result.javaInput()).isNotNull(); assertThat(result.contents()).isEqualTo(input); } @@ -634,6 +635,7 @@ class Test { /* reorderModifiers= */ true, /* sortImports= */ false)); assertThat(result.unit()).isNull(); + assertThat(result.javaInput()).isNull(); assertThat(result.contents()).contains("public static int x;"); } @@ -671,12 +673,12 @@ class Test { } @Test - public void parseReusedWhenLaterPassParsesAfterEarlierEdits() throws Exception { + public void fusedAstPassesInvalidateUnitOnEditAndReuseJavaInputInLaterPass() throws Exception { String input = """ package test; - import java.util.ArrayList; + import java.util.ArrayList;; class Test { void f(boolean a) { @@ -694,10 +696,15 @@ void f(boolean a) { Cleanups.enabledPasses( /* cleanup= */ true, /* removeUnusedImports= */ true, - /* reorderModifiers= */ false, - /* sortImports= */ false)); - assertThat(result.unit()).isNotNull(); + /* reorderModifiers= */ true, + /* sortImports= */ true)); + // REMOVE_UNUSED_IMPORTS and AST_CLEANUPS ran on a single shared parse and removed both the + // unused import and its redundant semicolon, invalidating the parsed unit; REORDER_MODIFIERS + // then lexed once with no edits so javaInput is cached for Formatter. + assertThat(result.unit()).isNull(); + assertThat(result.javaInput()).isNotNull(); assertThat(result.contents()).doesNotContain("ArrayList"); + assertThat(result.contents()).doesNotContain(";;"); assertThat(result.contents()).contains("if (a) {"); } diff --git a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java index 7ef7b553d..857f01a08 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java @@ -15,6 +15,7 @@ package com.google.googlejavaformat.java; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import static org.junit.Assume.assumeTrue; import com.google.common.collect.ImmutableRangeSet; @@ -252,4 +253,40 @@ class T { } """); } + + @Test + public void wrap_typeCastParentFallsBackToFullAstCheck() { + String input = + """ + class T { + Object o = + (Object) + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt"; + } + """; + // Splitting a cast string literal changes precedence from `(Object) (a + b)` to + // `((Object) a) + b`; the full-AST fallback must catch this and abort. + FormatterException e = + assertThrows( + FormatterException.class, () -> StringWrapper.wrap(100, input, new Formatter())); + assertThat(e).hasMessageThat().contains("unexpectedly change the AST"); + } + + @Test + public void wrap_newClassEnclosingExpressionFallsBackToFullAstCheck() { + String input = + """ + class T { + Object o = + "one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt" + .new Inner(); + } + """; + // Splitting the enclosing expression of `.new Inner()` changes precedence from + // `(a + b).new Inner()` to `a + (b.new Inner())`; the full-AST fallback must catch this. + FormatterException e = + assertThrows( + FormatterException.class, () -> StringWrapper.wrap(100, input, new Formatter())); + assertThat(e).hasMessageThat().contains("unexpectedly change the AST"); + } }