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
24 changes: 1 addition & 23 deletions core/src/main/java/com/google/googlejavaformat/Doc.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,7 @@ public enum FillMode {
public static final int MAX_LINE_WIDTH = 1000;

/** State for writing. */
public static final class State {
final int lastIndent;
final int indent;
final int column;
final boolean mustBreak;

State(int lastIndent, int indent, int column, boolean mustBreak) {
this.lastIndent = lastIndent;
this.indent = indent;
this.column = column;
this.mustBreak = mustBreak;
}

public record State(int lastIndent, int indent, int column, boolean mustBreak) {
public State(int indent0, int column0) {
this(indent0, indent0, column0, false);
}
Expand All @@ -98,16 +86,6 @@ State withColumn(int column) {
State withMustBreak(boolean mustBreak) {
return new State(lastIndent, indent, column, mustBreak);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("lastIndent", lastIndent)
.add("indent", indent)
.add("column", column)
.add("mustBreak", mustBreak)
.toString();
}
}

private static final Range<Integer> EMPTY_RANGE = Range.closedOpen(-1, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,36 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/** An error that prevented formatting from succeeding. */
public class FormatterDiagnostic {
private final int lineNumber;
private final String message;
private final int column;

public static FormatterDiagnostic create(String message) {
return new FormatterDiagnostic(-1, -1, message);
}

public static FormatterDiagnostic create(int lineNumber, int column, String message) {
checkArgument(lineNumber >= 0);
checkArgument(column >= 0);
/**
* An error that prevented formatting from succeeding.
*
* @param line the line number on which the error occurred, or {@code -1} if the error does not have
* a line number.
* @param column the 1-indexed column number on which the error occurred, or {@code -1} if the error
* does not have a column.
* @param message a description of the problem that prevented formatting from succeeding.
*/
public record FormatterDiagnostic(int line, int column, String message) {
public FormatterDiagnostic {
checkArgument(line >= -1);
checkArgument(column >= -1);
checkNotNull(message);
return new FormatterDiagnostic(lineNumber, column, message);
}

private FormatterDiagnostic(int lineNumber, int column, String message) {
this.lineNumber = lineNumber;
this.column = column;
this.message = message;
}

/**
* Returns the line number on which the error occurred, or {@code -1} if the error does not have a
* line number.
*/
public int line() {
return lineNumber;
}

/**
* Returns the 1-indexed column number on which the error occurred, or {@code -1} if the error
* does not have a column.
*/
public int column() {
return column;
}

/** Returns a description of the problem that prevented formatting from succeeding. */
public String message() {
return message;
public FormatterDiagnostic(String message) {
this(-1, -1, message);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (lineNumber >= 0) {
sb.append(lineNumber).append(':');
if (line >= 0) {
sb.append(line).append(':');
}
if (column >= 0) {
sb.append(column).append(':');
}
if (lineNumber >= 0 || column >= 0) {
if (line >= 0 || column >= 0) {
sb.append(' ');
}
sb.append("error: ").append(message);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/googlejavaformat/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public String toString() {
* numbers.
*/
public FormatterDiagnostic createDiagnostic(int inputPosition, String message) {
return FormatterDiagnostic.create(
return new FormatterDiagnostic(
getLineNumber(inputPosition), getColumnNumber(inputPosition), message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;
import java.util.Map;

/** This interface defines methods common to an {@link Input} or an {@link Output}. */
/** This class defines methods common to an {@link Input} or an {@link Output}. */
public abstract class InputOutput {
private ImmutableList<String> lines = ImmutableList.of();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static Result create(
private final CommandLineOptions parameters;
private final JavaFormatterOptions options;

public FormatFileCallable(
FormatFileCallable(
CommandLineOptions parameters, Path path, String input, JavaFormatterOptions options) {
this.path = path;
this.input = input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class FormatterException extends Exception {
private final ImmutableList<FormatterDiagnostic> diagnostics;

public FormatterException(String message) {
this(FormatterDiagnostic.create(message));
this(new FormatterDiagnostic(message));
}

public FormatterException(FormatterDiagnostic diagnostic) {
Expand All @@ -57,7 +57,7 @@ public static FormatterException fromJavacDiagnostics(
}

private static FormatterDiagnostic toFormatterDiagnostic(Diagnostic<?> input) {
return FormatterDiagnostic.create(
return new FormatterDiagnostic(
(int) input.getLineNumber(), (int) input.getColumnNumber(), input.getMessage(ENGLISH));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ public static String reorderImports(String text) throws FormatterException {
}

private String reorderImports() throws FormatterException {
int firstImportStart;
Optional<Integer> maybeFirstImport = findIdentifier(0, IMPORT_OR_CLASS_START);
if (!maybeFirstImport.isPresent() || !tokenAt(maybeFirstImport.get()).equals("import")) {
// No imports, so nothing to do.
return text;
}
firstImportStart = maybeFirstImport.get();
int firstImportStart = maybeFirstImport.get();
int unindentedFirstImportStart = unindent(firstImportStart);

ImportsAndIndex imports = scanImports(firstImportStart);
Expand Down Expand Up @@ -189,37 +188,28 @@ private ImportOrderer(String text, ImmutableList<Tok> toks, Style style) {
}
}

enum ImportType {
private enum ImportType {
STATIC,
MODULE,
NORMAL
}

/** An import statement. */
class Import {
private final String imported;
private final String trailing;
private final ImportType importType;

Import(String imported, String trailing, ImportType importType) {
this.imported = imported;
this.trailing = trailing;
this.importType = importType;
}

/** The name being imported, for example {@code java.util.List}. */
String imported() {
return imported;
}

/** Returns the {@link ImportType}. */
ImportType importType() {
return importType;
}

/**
* An import statement.
*
* @param imported the name being imported, for example {@code java.util.List}.
* @param trailing the {@code //} comment lines after the final {@code ;}, up to and including the
* line terminator of the last one. Note: In case two imports were separated by a space (which
* is disallowed by the style guide), the trailing whitespace of the first import does not
* include a line terminator.
* @param importType the {@link ImportType} of the import.
* @param lineSeparator the line separator to use when formatting the import.
*/
private record Import(
String imported, String trailing, ImportType importType, String lineSeparator) {
/** The top-level package of the import. */
String topLevel() {
return DOT_SPLITTER.split(imported()).iterator().next();
return DOT_SPLITTER.split(imported).iterator().next();
}

/** True if this is an Android import per AOSP style. */
Expand All @@ -236,18 +226,8 @@ boolean isJava() {
};
}

/**
* The {@code //} comment lines after the final {@code ;}, up to and including the line
* terminator of the last one. Note: In case two imports were separated by a space (which is
* disallowed by the style guide), the trailing whitespace of the first import does not include
* a line terminator.
*/
String trailing() {
return trailing;
}

/** True if this is a third-party import per AOSP style. */
public boolean isThirdParty() {
boolean isThirdParty() {
return !(isAndroid() || isJava());
}

Expand Down Expand Up @@ -280,15 +260,7 @@ private String tokString(int start, int end) {
return sb.toString();
}

private static class ImportsAndIndex {
final ImmutableSortedSet<Import> imports;
final int index;

ImportsAndIndex(ImmutableSortedSet<Import> imports, int index) {
this.imports = imports;
this.index = index;
}
}
private record ImportsAndIndex(ImmutableSortedSet<Import> imports, int index) {}

/**
* Scans a sequence of import lines. The parsing uses this approximate grammar:
Expand Down Expand Up @@ -366,7 +338,7 @@ private ImportsAndIndex scanImports(int i) throws FormatterException {
// Extra semicolons are not allowed by the JLS but are accepted by javac.
i++;
}
imports.add(new Import(importedName, trailing.toString(), importType));
imports.add(new Import(importedName, trailing.toString(), importType, lineSeparator));
// Remember the position just after the import we just saw, before skipping blank lines.
// If the next thing after the blank lines is not another import then we don't want to
// include those blank lines in the text to be replaced.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import java.util.regex.Pattern;

/** {@code JavaCommentsHelper} extends {@link CommentsHelper} to rewrite Java comments. */
public final class JavaCommentsHelper implements CommentsHelper {
final class JavaCommentsHelper implements CommentsHelper {

private final String lineSeparator;
private final JavaFormatterOptions options;

public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
this.lineSeparator = lineSeparator;
this.options = options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import org.jspecify.annotations.Nullable;

/** {@code JavaInput} extends {@link Input} to represent a Java input document. */
public final class JavaInput extends Input {
final class JavaInput extends Input {
/**
* A {@code JavaInput} is a sequence of {@link Tok}s that cover the Java input. A {@link Tok} is
* either a token (if {@code isToken()}), or a non-token, which is a comment (if {@code
Expand Down Expand Up @@ -275,7 +275,7 @@ public String toString() {
* @param text the input text
* @throws FormatterException if the input cannot be parsed
*/
public JavaInput(String text) throws FormatterException {
JavaInput(String text) throws FormatterException {
this.text = checkNotNull(text);
setLines(ImmutableList.copyOf(Newlines.lineIterator(text)));
ImmutableList<Tok> toks = buildToks(text);
Expand Down Expand Up @@ -608,7 +608,7 @@ private static boolean isParamComment(Tok tok) {
* @return the {@code 0}-based {@link Range} of tokens
* @throws FormatterException if the upper endpoint of the range is outside the file
*/
Range<Integer> characterRangeToTokenRange(Range<Integer> characterRange)
private Range<Integer> characterRangeToTokenRange(Range<Integer> characterRange)
throws FormatterException {
if (characterRange.upperEndpoint() > text.length()) {
throw new FormatterException(
Expand Down Expand Up @@ -699,11 +699,11 @@ public int getColumnNumber(int inputPosition) {

// TODO(cushon): refactor JavaInput so the CompilationUnit can be passed into
// the constructor.
public void setCompilationUnit(JCCompilationUnit unit) {
void setCompilationUnit(JCCompilationUnit unit) {
this.unit = unit;
}

public RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>> characterRanges)
RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>> characterRanges)
throws FormatterException {
RangeSet<Integer> tokenRangeSet = TreeRangeSet.create();
for (Range<Integer> characterRange : characterRanges) {
Expand Down
Loading
Loading