Skip to content
Open
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 @@ -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 {
Expand All @@ -30,22 +31,31 @@ ImmutableList<Replacement> findReplacements(Cleanups cleanups) throws FormatterE
AST_CLEANUPS {
@Override
ImmutableList<Replacement> 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<Replacement> 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<Replacement> findReplacements(Cleanups cleanups) throws FormatterException {
return ImportOrderer.findReplacements(cleanups.contents(), cleanups.style());
return ModifierOrderer.findModifierReplacements(cleanups.javaInput(), cleanups.ranges());
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,10 +60,25 @@ final class CleanupScanner extends TreePathScanner<@Nullable Void, @Nullable Voi
static ImmutableList<Replacement> scan(
JCCompilationUnit unit, String contents, Collection<Range<Integer>> ranges)
throws FormatterException {
return scan(
unit,
contents,
JavaInput.buildToks(contents, ImmutableSet.of()),
ranges,
ImmutableRangeSet.of());
}

static ImmutableList<Replacement> scan(
JCCompilationUnit unit,
String contents,
ImmutableList<Tok> toks,
Collection<Range<Integer>> ranges,
RangeSet<Integer> preExistingDeletions)
throws FormatterException {
RangeSet<Integer> targetRanges = TreeRangeSet.create(ranges);
ImmutableList<Tok> toks = JavaInput.buildToks(contents, ImmutableSet.of());
RangeSet<Integer> 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();
}
Expand Down Expand Up @@ -122,18 +138,21 @@ private static void verifyCommentsPreserved(
private final ImmutableList<Tok> toks;
private final RangeSet<Integer> targetRanges;
private final RangeSet<Integer> activeDeletions;
private final RangeSet<Integer> preExistingDeletions;

CleanupScanner(
String contents,
JCCompilationUnit unit,
ImmutableList<Tok> toks,
RangeSet<Integer> targetRanges,
RangeSet<Integer> activeDeletions) {
RangeSet<Integer> activeDeletions,
RangeSet<Integer> 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) {
Expand All @@ -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;
Expand All @@ -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++;
Expand All @@ -168,6 +188,19 @@ private void deleteRedundantSemicolon(int semiPos) {
activeDeletions.add(Range.closedOpen(start, end));
}

private boolean isDeletedRange(int start, int end) {
Range<Integer> range = Range.closedOpen(start, end);
if (activeDeletions.encloses(range)) {
return true;
}
if (preExistingDeletions.isEmpty()) {
return false;
}
RangeSet<Integer> 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';
}
Expand Down
69 changes: 64 additions & 5 deletions core/src/main/java/com/google/googlejavaformat/java/Cleanups.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Range<Integer>> characterRanges,
@Nullable JCCompilationUnit unit) {}
@Nullable JCCompilationUnit unit,
@Nullable JavaInput javaInput) {}

private record Parsed(Context context, JCCompilationUnit unit) {}

Expand Down Expand Up @@ -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<Integer> normalized = TreeRangeSet.create();
for (Range<Integer> range : characterRanges) {
Expand All @@ -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).
Expand All @@ -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<Integer> ranges;
private @Nullable Parsed parsed;
private @Nullable JavaInput javaInput;
private final Style style;

private Cleanups(String contents, ImmutableRangeSet<Integer> ranges, Style style) {
Expand Down Expand Up @@ -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<Replacement> unusedImports =
RemoveUnusedImports.findUnusedImports(context(), unit(), contents);
RangeSet<Integer> preExistingDeletions;
if (unusedImports.isEmpty()) {
preExistingDeletions = ImmutableRangeSet.of();
} else {
RangeSet<Integer> deletions = TreeRangeSet.create();
for (Replacement r : unusedImports) {
deletions.add(r.replaceRange());
}
preExistingDeletions = deletions;
}
ImmutableList<Replacement> astCleanups =
CleanupScanner.scan(
unit(), contents, javaInput().getToks(), ranges(), preExistingDeletions);
if (unusedImports.isEmpty()) {
applyReplacements(astCleanups);
} else if (astCleanups.isEmpty()) {
applyReplacements(unusedImports);
} else {
List<Replacement> 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<Replacement> replacements) {
Expand All @@ -166,6 +224,7 @@ private void applyReplacements(List<Replacement> 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.
Expand Down
30 changes: 26 additions & 4 deletions core/src/main/java/com/google/googlejavaformat/java/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -265,7 +275,8 @@ public ImmutableList<Replacement> getFormatReplacements(
ImmutableList<Replacement> getFormatReplacements(
String input, Collection<Range<Integer>> characterRanges, boolean reindent)
throws FormatterException {
return getFormatReplacements(input, characterRanges, reindent, /* unit= */ null);
return getFormatReplacements(
input, characterRanges, reindent, /* unit= */ null, /* cachedJavaInput= */ null);
}

ImmutableList<Replacement> getFormatReplacements(
Expand All @@ -274,7 +285,18 @@ ImmutableList<Replacement> getFormatReplacements(
boolean reindent,
@Nullable JCCompilationUnit unit)
throws FormatterException {
JavaInput javaInput = new JavaInput(input);
return getFormatReplacements(
input, characterRanges, reindent, unit, /* cachedJavaInput= */ null);
}

ImmutableList<Replacement> getFormatReplacements(
String input,
Collection<Range<Integer>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public static String reorderImports(String text) throws FormatterException {
static ImmutableList<Replacement> findReplacements(String text, Style style)
throws FormatterException {
ImmutableList<Tok> 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<Replacement> findReplacements(
String text, ImmutableList<Tok> toks, Style style) throws FormatterException {
return new ImportOrderer(text, toks, style).findReplacements();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
// `/*` 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<Tok> toks; // All Toks (including whitespace and comments).
private final ImmutableMap<Integer, Integer> positionToColumnMap; // Map Tok position to column.
private final ImmutableList<Token> tokens; // The Tokens for this input.
private final ImmutableRangeMap<Integer, Token> positionTokenMap; // Map position to Token.
Expand All @@ -265,7 +266,7 @@
JavaInput(String text) throws FormatterException {
this.text = checkNotNull(text);
setLines(ImmutableList.copyOf(Newlines.lineIterator(text)));
ImmutableList<Tok> toks = buildToks(text);
this.toks = buildToks(text);
positionToColumnMap = makePositionToColumnMap(toks);
tokens = buildTokens(toks);
ImmutableRangeMap.Builder<Integer, Token> tokenLocations = ImmutableRangeMap.builder();
Expand Down Expand Up @@ -447,7 +448,7 @@
// cannot be lexed without syntactic knowledge.
isToken = true;
isNumbered = true;
for (char c : tokText.toCharArray()) {

Check warning on line 451 in core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[LoopOverCharArray] toCharArray allocates a new array, using charAt is more efficient

Check warning on line 451 in core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[LoopOverCharArray] toCharArray allocates a new array, using charAt is more efficient

Check warning on line 451 in core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[LoopOverCharArray] toCharArray allocates a new array, using charAt is more efficient

Check warning on line 451 in core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[LoopOverCharArray] toCharArray allocates a new array, using charAt is more efficient

Check warning on line 451 in core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[LoopOverCharArray] toCharArray allocates a new array, using charAt is more efficient

Check warning on line 451 in core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[LoopOverCharArray] toCharArray allocates a new array, using charAt is more efficient
strings.add(String.valueOf(c));
}
}
Expand Down Expand Up @@ -751,6 +752,11 @@
return kToToken[k];
}

/// Get all input toks (tokens, comments, whitespace, and EOF).
ImmutableList<Tok> getToks() {
return toks;
}

/// Get the input tokens.
///
/// @return the input tokens
Expand Down
Loading
Loading