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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ offsets (`--offset`), passing through to standard-out (default) or altered
in-place (`--replace`).

Option `--help` will print full usage details; including built-in documentation
about other flags, such as `--aosp`, `--fix-imports-only`,
about other flags, such as `--style`, `--fix-imports-only`,
`--skip-sorting-imports`, `--skip-removing-unused-import`,
`--skip-reflowing-long-strings`, `--skip-javadoc-formatting`, or the `--dry-run`
and `--set-exit-if-changed`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import java.util.Optional;

/**
Expand All @@ -28,7 +29,7 @@
* @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 style The style to format with.
* @param version Print the version.
* @param help Print usage information.
* @param stdin Format input from stdin.
Expand All @@ -47,7 +48,7 @@ record CommandLineOptions(
ImmutableRangeSet<Integer> lines,
ImmutableList<Integer> offsets,
ImmutableList<Integer> lengths,
boolean aosp,
Style style,
boolean version,
boolean help,
boolean stdin,
Expand All @@ -73,7 +74,7 @@ static Builder builder() {
.reflowLongStrings(true)
.formatJavadoc(true)
.reorderModifiers(true)
.aosp(false)
.style(Style.GOOGLE)
.version(false)
.help(false)
.stdin(false)
Expand Down Expand Up @@ -108,7 +109,7 @@ default Builder addLength(Integer length) {
return this;
}

Builder aosp(boolean aosp);
Builder style(Style style);

Builder version(boolean version);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -71,7 +73,9 @@ static CommandLineOptions parse(Iterable<String> options) {
parseRangeSet(linesBuilder, getValue(flag, it, value));
case "--offset", "-offset" -> optionsBuilder.addOffset(parseInteger(it, flag, value));
case "--length", "-length" -> optionsBuilder.addLength(parseInteger(it, flag, value));
case "--aosp", "-aosp", "-a" -> optionsBuilder.aosp(true);
case "--style", "-style" ->
optionsBuilder.style(parseStyle(flag, getValue(flag, it, value)));
case "--aosp", "-aosp", "-a" -> optionsBuilder.style(Style.AOSP);
case "--version", "-version", "-v" -> optionsBuilder.version(true);
case "--help", "-help", "-h" -> optionsBuilder.help(true);
case "--fix-imports-only" -> optionsBuilder.fixImportsOnly(true);
Expand All @@ -92,6 +96,16 @@ static CommandLineOptions parse(Iterable<String> options) {
return optionsBuilder.build();
}

private static Style parseStyle(String flag, String value) {
return switch (Ascii.toLowerCase(value)) {
case "google" -> Style.GOOGLE;
case "aosp" -> Style.AOSP;
default ->
throw new IllegalArgumentException(
String.format("invalid value for %s: %s (expected google or aosp)", flag, value));
};
}

private static Integer parseInteger(Iterator<String> it, String flag, String value) {
try {
return Integer.valueOf(getValue(flag, it, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -118,7 +117,7 @@ public int format(String... args) throws UsageException {

JavaFormatterOptions options =
JavaFormatterOptions.builder()
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
.style(parameters.style())
.formatJavadoc(parameters.formatJavadoc())
.reorderModifiers(parameters.reorderModifiers())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ final class UsageException extends Exception {
Format stdin -> stdout
--assume-filename, -assume-filename
File name to use for diagnostics when formatting standard input (default is <stdin>).
--style, -style
The style to format with, either google (2-space indentation) or aosp (4-space
indentation). The default is google; if given more than once, the last one wins.
--aosp, -aosp, -a
Use AOSP style instead of Google Style (4-space indentation).
Alias for --style=aosp.
--fix-imports-only
Fix import order and remove any unused imports, but do no other formatting.
--skip-sorting-imports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -40,7 +42,7 @@ public void defaults() {
CommandLineOptions options = CommandLineOptionsParser.parse(ImmutableList.of());
assertThat(options.files()).isEmpty();
assertThat(options.stdin()).isFalse();
assertThat(options.aosp()).isFalse();
assertThat(options.style()).isEqualTo(Style.GOOGLE);
assertThat(options.help()).isFalse();
assertThat(options.lengths()).isEmpty();
assertThat(options.lines().asRanges()).isEmpty();
Expand Down Expand Up @@ -74,7 +76,41 @@ public void stdin() {

@Test
public void aosp() {
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-aosp")).aosp()).isTrue();
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-aosp")).style())
.isEqualTo(Style.AOSP);
}

@Test
public void style() {
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--style=google")).style())
.isEqualTo(Style.GOOGLE);
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--style", "aosp")).style())
.isEqualTo(Style.AOSP);
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-style=AOSP")).style())
.isEqualTo(Style.AOSP);
}

@Test
public void lastStyleWins() {
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--aosp", "--style=google")).style())
.isEqualTo(Style.GOOGLE);
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--style=google", "--aosp")).style())
.isEqualTo(Style.AOSP);
assertThat(
CommandLineOptionsParser.parse(Arrays.asList("--style=google", "--style=aosp")).style())
.isEqualTo(Style.AOSP);
assertThat(
CommandLineOptionsParser.parse(Arrays.asList("--style=aosp", "--style=google")).style())
.isEqualTo(Style.GOOGLE);
}

@Test
public void unknownStyle() {
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class,
() -> CommandLineOptionsParser.parse(Arrays.asList("--style=llvm")));
assertThat(e).hasMessageThat().contains("invalid value for --style: llvm");
}

@Test
Expand Down
Loading