diff --git a/README.md b/README.md index b90dc4a91..6ce595439 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java index 698a3ea9f..1ee3bc7cb 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java @@ -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; /** @@ -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. @@ -47,7 +48,7 @@ record CommandLineOptions( ImmutableRangeSet lines, ImmutableList offsets, ImmutableList lengths, - boolean aosp, + Style style, boolean version, boolean help, boolean stdin, @@ -73,7 +74,7 @@ static Builder builder() { .reflowLongStrings(true) .formatJavadoc(true) .reorderModifiers(true) - .aosp(false) + .style(Style.GOOGLE) .version(false) .help(false) .stdin(false) @@ -108,7 +109,7 @@ default Builder addLength(Integer length) { return this; } - Builder aosp(boolean aosp); + Builder style(Style style); Builder version(boolean version); diff --git a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java index fddf9be8a..3a21bce64 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java +++ b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java @@ -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; @@ -71,7 +73,9 @@ static CommandLineOptions parse(Iterable 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); @@ -92,6 +96,16 @@ static CommandLineOptions parse(Iterable 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 it, String flag, String value) { try { return Integer.valueOf(getValue(flag, it, value)); diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java index bfe1ff51f..19165b24a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Main.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java @@ -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; @@ -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(); diff --git a/core/src/main/java/com/google/googlejavaformat/java/UsageException.java b/core/src/main/java/com/google/googlejavaformat/java/UsageException.java index f9c2eb80d..084e4605f 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/UsageException.java +++ b/core/src/main/java/com/google/googlejavaformat/java/UsageException.java @@ -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 ). + --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 diff --git a/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java b/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java index 3c0605823..8a0a0e57d 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java @@ -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; @@ -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(); @@ -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