Skip to content

Migrate to CommandLineOptions to a record and AutoBuilder #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
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,291 +14,121 @@

package com.google.googlejavaformat.java;

import com.google.auto.value.AutoBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Optional;

/**
* Command line options for google-java-format.
*
* <p>google-java-format doesn't depend on AutoValue, to allow AutoValue to depend on
* google-java-format.
* @param files The files to format.
* @param inPlace Format files in place.
* @param lines Line ranges to format.
* @param offsets Character offsets for partial formatting, paired with {@code lengths}.
* @param lengths Partial formatting region lengths, paired with {@code offsets}.
* @param aosp Use AOSP style instead of Google Style (4-space indentation).
* @param version Print the version.
* @param help Print usage information.
* @param stdin Format input from stdin.
* @param fixImportsOnly Fix imports, but do no formatting.
* @param sortImports Sort imports.
* @param removeUnusedImports Remove unused imports.
* @param dryRun Print the paths of the files whose contents would change if the formatter were run
* normally.
* @param setExitIfChanged Return exit code 1 if there are any formatting changes.
* @param assumeFilename Return the name to use for diagnostics when formatting standard input.
*/
final class CommandLineOptions {

private final ImmutableList<String> files;
private final boolean inPlace;
private final ImmutableRangeSet<Integer> lines;
private final ImmutableList<Integer> offsets;
private final ImmutableList<Integer> lengths;
private final boolean aosp;
private final boolean version;
private final boolean help;
private final boolean stdin;
private final boolean fixImportsOnly;
private final boolean sortImports;
private final boolean removeUnusedImports;
private final boolean dryRun;
private final boolean setExitIfChanged;
private final Optional<String> assumeFilename;
private final boolean reflowLongStrings;
private final boolean formatJavadoc;

CommandLineOptions(
ImmutableList<String> files,
boolean inPlace,
ImmutableRangeSet<Integer> lines,
ImmutableList<Integer> offsets,
ImmutableList<Integer> lengths,
boolean aosp,
boolean version,
boolean help,
boolean stdin,
boolean fixImportsOnly,
boolean sortImports,
boolean removeUnusedImports,
boolean dryRun,
boolean setExitIfChanged,
Optional<String> assumeFilename,
boolean reflowLongStrings,
boolean formatJavadoc) {
this.files = files;
this.inPlace = inPlace;
this.lines = lines;
this.offsets = offsets;
this.lengths = lengths;
this.aosp = aosp;
this.version = version;
this.help = help;
this.stdin = stdin;
this.fixImportsOnly = fixImportsOnly;
this.sortImports = sortImports;
this.removeUnusedImports = removeUnusedImports;
this.dryRun = dryRun;
this.setExitIfChanged = setExitIfChanged;
this.assumeFilename = assumeFilename;
this.reflowLongStrings = reflowLongStrings;
this.formatJavadoc = formatJavadoc;
}

/** The files to format. */
ImmutableList<String> files() {
return files;
}

/** Format files in place. */
boolean inPlace() {
return inPlace;
}

/** Line ranges to format. */
ImmutableRangeSet<Integer> lines() {
return lines;
}

/** Character offsets for partial formatting, paired with {@code lengths}. */
ImmutableList<Integer> offsets() {
return offsets;
}

/** Partial formatting region lengths, paired with {@code offsets}. */
ImmutableList<Integer> lengths() {
return lengths;
}

/** Use AOSP style instead of Google Style (4-space indentation). */
boolean aosp() {
return aosp;
}

/** Print the version. */
boolean version() {
return version;
}

/** Print usage information. */
boolean help() {
return help;
}

/** Format input from stdin. */
boolean stdin() {
return stdin;
}

/** Fix imports, but do no formatting. */
boolean fixImportsOnly() {
return fixImportsOnly;
}

/** Sort imports. */
boolean sortImports() {
return sortImports;
}

/** Remove unused imports. */
boolean removeUnusedImports() {
return removeUnusedImports;
}

/**
* Print the paths of the files whose contents would change if the formatter were run normally.
*/
boolean dryRun() {
return dryRun;
}

/** Return exit code 1 if there are any formatting changes. */
boolean setExitIfChanged() {
return setExitIfChanged;
}

/** Return the name to use for diagnostics when formatting standard input. */
Optional<String> assumeFilename() {
return assumeFilename;
}

boolean reflowLongStrings() {
return reflowLongStrings;
}
record CommandLineOptions(
ImmutableList<String> files,
boolean inPlace,
ImmutableRangeSet<Integer> lines,
ImmutableList<Integer> offsets,
ImmutableList<Integer> lengths,
boolean aosp,
boolean version,
boolean help,
boolean stdin,
boolean fixImportsOnly,
boolean sortImports,
boolean removeUnusedImports,
boolean dryRun,
boolean setExitIfChanged,
Optional<String> assumeFilename,
boolean reflowLongStrings,
boolean formatJavadoc) {

/** Returns true if partial formatting was selected. */
boolean isSelection() {
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
}

boolean formatJavadoc() {
return formatJavadoc;
}

static Builder builder() {
return new Builder();
return new AutoBuilder_CommandLineOptions_Builder()
.sortImports(true)
.removeUnusedImports(true)
.reflowLongStrings(true)
.formatJavadoc(true)
.aosp(false)
.version(false)
.help(false)
.stdin(false)
.fixImportsOnly(false)
.dryRun(false)
.setExitIfChanged(false)
.inPlace(false);
}

static class Builder {
@AutoBuilder
interface Builder {

private final ImmutableList.Builder<String> files = ImmutableList.builder();
private final RangeSet<Integer> lines = TreeRangeSet.create();
private final ImmutableList.Builder<Integer> offsets = ImmutableList.builder();
private final ImmutableList.Builder<Integer> lengths = ImmutableList.builder();
private boolean inPlace = false;
private boolean aosp = false;
private boolean version = false;
private boolean help = false;
private boolean stdin = false;
private boolean fixImportsOnly = false;
private boolean sortImports = true;
private boolean removeUnusedImports = true;
private boolean dryRun = false;
private boolean setExitIfChanged = false;
private Optional<String> assumeFilename = Optional.empty();
private boolean reflowLongStrings = true;
private boolean formatJavadoc = true;
ImmutableList.Builder<String> filesBuilder();

ImmutableList.Builder<String> filesBuilder() {
return files;
}
Builder inPlace(boolean inPlace);

Builder inPlace(boolean inPlace) {
this.inPlace = inPlace;
return this;
}
Builder lines(ImmutableRangeSet<Integer> lines);

RangeSet<Integer> linesBuilder() {
return lines;
}
ImmutableList.Builder<Integer> offsetsBuilder();

Builder addOffset(Integer offset) {
offsets.add(offset);
@CanIgnoreReturnValue
default Builder addOffset(Integer offset) {
offsetsBuilder().add(offset);
return this;
}

Builder addLength(Integer length) {
lengths.add(length);
return this;
}
ImmutableList.Builder<Integer> lengthsBuilder();

Builder aosp(boolean aosp) {
this.aosp = aosp;
@CanIgnoreReturnValue
default Builder addLength(Integer length) {
lengthsBuilder().add(length);
return this;
}

Builder version(boolean version) {
this.version = version;
return this;
}
Builder aosp(boolean aosp);

Builder help(boolean help) {
this.help = help;
return this;
}
Builder version(boolean version);

Builder stdin(boolean stdin) {
this.stdin = stdin;
return this;
}
Builder help(boolean help);

Builder fixImportsOnly(boolean fixImportsOnly) {
this.fixImportsOnly = fixImportsOnly;
return this;
}
Builder stdin(boolean stdin);

Builder sortImports(boolean sortImports) {
this.sortImports = sortImports;
return this;
}
Builder fixImportsOnly(boolean fixImportsOnly);

Builder removeUnusedImports(boolean removeUnusedImports) {
this.removeUnusedImports = removeUnusedImports;
return this;
}
Builder sortImports(boolean sortImports);

Builder dryRun(boolean dryRun) {
this.dryRun = dryRun;
return this;
}
Builder removeUnusedImports(boolean removeUnusedImports);

Builder setExitIfChanged(boolean setExitIfChanged) {
this.setExitIfChanged = setExitIfChanged;
return this;
}
Builder dryRun(boolean dryRun);

Builder assumeFilename(String assumeFilename) {
this.assumeFilename = Optional.of(assumeFilename);
return this;
}
Builder setExitIfChanged(boolean setExitIfChanged);

Builder reflowLongStrings(boolean reflowLongStrings) {
this.reflowLongStrings = reflowLongStrings;
return this;
}
Builder assumeFilename(String assumeFilename);

Builder formatJavadoc(boolean formatJavadoc) {
this.formatJavadoc = formatJavadoc;
return this;
}
Builder reflowLongStrings(boolean reflowLongStrings);

CommandLineOptions build() {
return new CommandLineOptions(
files.build(),
inPlace,
ImmutableRangeSet.copyOf(lines),
offsets.build(),
lengths.build(),
aosp,
version,
help,
stdin,
fixImportsOnly,
sortImports,
removeUnusedImports,
dryRun,
setExitIfChanged,
assumeFilename,
reflowLongStrings,
formatJavadoc);
}
Builder formatJavadoc(boolean formatJavadoc);

CommandLineOptions build();
}
}
Loading