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 @@ -14,6 +14,8 @@

package com.google.googlejavaformat.java;

import static java.util.Objects.requireNonNull;

import com.google.common.collect.ImmutableList;
import com.sun.source.tree.AnnotatedTypeTree;
import com.sun.source.tree.AnnotationTree;
Expand All @@ -25,25 +27,22 @@
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import org.jspecify.annotations.Nullable;

/**
* Utilities for working with array dimensions.
*
* <p>javac's parser does not preserve concrete syntax for mixed-notation arrays, so we have to
* re-lex the input to extra it.
* re-lex the input to extract it.
*
* <p>For example, {@code int [] a;} cannot be distinguished from {@code int [] a [];} in the AST.
*/
class DimensionHelpers {
final class DimensionHelpers {

/** The array dimension specifiers (including any type annotations) associated with a type. */
static class TypeWithDims {
final Tree node;
final ImmutableList<List<AnnotationTree>> dims;

public TypeWithDims(Tree node, ImmutableList<List<AnnotationTree>> dims) {
this.node = node;
this.dims = dims;
record TypeWithDims(@Nullable Tree node, ImmutableList<List<AnnotationTree>> dims) {
TypeWithDims {
requireNonNull(dims, "dims");
}
}

Expand Down Expand Up @@ -120,4 +119,6 @@ private static Tree extractDims(Deque<List<AnnotationTree>> dims, Tree node) {
default -> node;
};
}

private DimensionHelpers() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

package com.google.googlejavaformat.java;

import com.google.auto.value.AutoValue;
import static java.util.Objects.requireNonNull;

import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
Expand All @@ -28,26 +29,25 @@
*/
class FormatFileCallable implements Callable<FormatFileCallable.Result> {

@AutoValue
abstract static class Result {
abstract @Nullable Path path();

abstract String input();

abstract @Nullable String output();
record Result(
@Nullable Path path,
String input,
@Nullable String output,
@Nullable FormatterException exception) {
Result {
requireNonNull(input, "input");
}

boolean changed() {
return !input().equals(output());
}

abstract @Nullable FormatterException exception();

static Result create(
@Nullable Path path,
String input,
@Nullable String output,
@Nullable FormatterException exception) {
return new AutoValue_FormatFileCallable_Result(path, input, output, exception);
return new Result(path, input, output, exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package com.google.googlejavaformat.java;

import com.google.auto.value.AutoValue;
import static java.util.Objects.requireNonNull;

import com.google.auto.value.AutoBuilder;
import com.google.errorprone.annotations.Immutable;

/**
Expand All @@ -26,10 +28,14 @@
* <p>The goal of google-java-format is to provide consistent formatting, and to free developers
* from arguments over style choices. It is an explicit non-goal to support developers' individual
* preferences, and in fact it would work directly against our primary goals.
*
* @param style Returns the code style.
*/
@Immutable
@AutoValue
public abstract class JavaFormatterOptions {
public record JavaFormatterOptions(boolean formatJavadoc, boolean reorderModifiers, Style style) {
public JavaFormatterOptions {
requireNonNull(style, "style");
}

public enum Style {
/** The default Google Java Style configuration. */
Expand All @@ -54,28 +60,21 @@ public int indentationMultiplier() {
return style().indentationMultiplier();
}

public abstract boolean formatJavadoc();

public abstract boolean reorderModifiers();

/** Returns the code style. */
public abstract Style style();

/** Returns the default formatting options. */
public static JavaFormatterOptions defaultOptions() {
return builder().build();
}

/** Returns a builder for {@link JavaFormatterOptions}. */
public static Builder builder() {
return new AutoValue_JavaFormatterOptions.Builder()
return new AutoBuilder_JavaFormatterOptions_Builder()
.style(Style.GOOGLE)
.formatJavadoc(true)
.reorderModifiers(true);
}

/** A builder for {@link JavaFormatterOptions}. */
@AutoValue.Builder
@AutoBuilder
public abstract static class Builder {

public abstract Builder style(Style style);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
import static com.sun.source.tree.Tree.Kind.UNION_TYPE;
import static com.sun.source.tree.Tree.Kind.VARIABLE;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

import com.google.auto.value.AutoOneOf;
import com.google.auto.value.AutoValue;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
Expand Down Expand Up @@ -508,14 +508,14 @@ public Void visitNewArray(NewArrayTree node, Void unused) {
builder.space();

TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
Tree base = extractedDims.node;
Tree base = extractedDims.node();

Deque<ExpressionTree> dimExpressions = new ArrayDeque<>(node.getDimensions());

Deque<List<? extends AnnotationTree>> annotations = new ArrayDeque<>();
annotations.add(ImmutableList.copyOf(node.getAnnotations()));
annotations.addAll(node.getDimAnnotations());
annotations.addAll(extractedDims.dims);
annotations.addAll(extractedDims.dims());

scan(base, null);
builder.open(ZERO);
Expand Down Expand Up @@ -635,8 +635,8 @@ public Void visitArrayType(ArrayTypeTree node, Void unused) {
private void visitAnnotatedArrayType(Tree node) {
TypeWithDims extractedDims = DimensionHelpers.extractDims(node, SortedDims.YES);
builder.open(plusFour);
scan(extractedDims.node, null);
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims);
scan(extractedDims.node(), null);
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims());
maybeAddDims(dims);
Verify.verify(dims.isEmpty());
builder.close();
Expand Down Expand Up @@ -1088,7 +1088,8 @@ private static TypeWithDims variableFragmentDims(
}
TypeWithDims dims = DimensionHelpers.extractDims(type, SortedDims.NO);
return new TypeWithDims(
null, leadingDims > 0 ? dims.dims.subList(0, dims.dims.size() - leadingDims) : dims.dims);
null,
leadingDims > 0 ? dims.dims().subList(0, dims.dims().size() - leadingDims) : dims.dims());
}

@Override
Expand Down Expand Up @@ -1536,8 +1537,8 @@ public Void visitMethod(MethodTree node, Void unused) {
if (node.getReturnType() != null) {
TypeWithDims extractedDims =
DimensionHelpers.extractDims(node.getReturnType(), SortedDims.YES);
baseReturnType = extractedDims.node;
dims = new ArrayDeque<>(extractedDims.dims);
baseReturnType = extractedDims.node();
dims = new ArrayDeque<>(extractedDims.dims());
} else {
verticalAnnotations(typeAnnotations);
typeAnnotations = ImmutableList.of();
Expand Down Expand Up @@ -2554,17 +2555,18 @@ public int compareTo(AnnotationOrModifier o) {
* {@code @Deprecated public} as declaration modifiers, and {@code @Nullable} as a type annotation
* on the return type.
*/
@AutoValue
abstract static class DeclarationModifiersAndTypeAnnotations {
abstract ImmutableList<AnnotationOrModifier> declarationModifiers();

abstract ImmutableList<AnnotationTree> typeAnnotations();
record DeclarationModifiersAndTypeAnnotations(
ImmutableList<AnnotationOrModifier> declarationModifiers,
ImmutableList<AnnotationTree> typeAnnotations) {
DeclarationModifiersAndTypeAnnotations {
requireNonNull(declarationModifiers, "declarationModifiers");
requireNonNull(typeAnnotations, "typeAnnotations");
}

static DeclarationModifiersAndTypeAnnotations create(
ImmutableList<AnnotationOrModifier> declarationModifiers,
ImmutableList<AnnotationTree> typeAnnotations) {
return new AutoValue_JavaInputAstVisitor_DeclarationModifiersAndTypeAnnotations(
declarationModifiers, typeAnnotations);
return new DeclarationModifiersAndTypeAnnotations(declarationModifiers, typeAnnotations);
}

static DeclarationModifiersAndTypeAnnotations empty() {
Expand Down Expand Up @@ -2968,7 +2970,7 @@ private void visitToDeclare(
if (node.getType() != null) {
TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
typeWithDims = Optional.of(extractedDims);
type = extractedDims.node;
type = extractedDims.node();
} else {
typeWithDims = Optional.empty();
type = null;
Expand Down Expand Up @@ -3656,7 +3658,7 @@ protected int declareOne(
}

Deque<List<? extends AnnotationTree>> dims =
new ArrayDeque<>(typeWithDims.isPresent() ? typeWithDims.get().dims : ImmutableList.of());
new ArrayDeque<>(typeWithDims.isPresent() ? typeWithDims.get().dims() : ImmutableList.of());
int baseDims = 0;

// preprocess to separate declaration annotations + modifiers, type annotations
Expand Down Expand Up @@ -3688,8 +3690,8 @@ protected int declareOne(
visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES);
if (isVar) {
token("var");
} else if (typeWithDims.isPresent() && typeWithDims.get().node != null) {
scan(typeWithDims.get().node, null);
} else if (typeWithDims.isPresent() && typeWithDims.get().node() != null) {
scan(typeWithDims.get().node(), null);
int totalDims = dims.size();
builder.open(plusFour);
maybeAddDims(dims);
Expand Down Expand Up @@ -3838,8 +3840,8 @@ private void declareMany(List<VariableTree> fragments, Direction annotationDirec
builder.open(plusFour);
builder.open(ZERO);
TypeWithDims extractedDims = DimensionHelpers.extractDims(type, SortedDims.YES);
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims);
scan(extractedDims.node, null);
Deque<List<? extends AnnotationTree>> dims = new ArrayDeque<>(extractedDims.dims());
scan(extractedDims.node(), null);
int baseDims = dims.size();
maybeAddDims(dims);
baseDims = baseDims - dims.size();
Expand All @@ -3850,7 +3852,7 @@ private void declareMany(List<VariableTree> fragments, Direction annotationDirec
}
TypeWithDims fragmentDims =
variableFragmentDims(afterFirstToken, baseDims, fragment.getType());
dims = new ArrayDeque<>(fragmentDims.dims);
dims = new ArrayDeque<>(fragmentDims.dims());
builder.breakOp(" ");
builder.open(ZERO);
maybeAddDims(dims);
Expand Down