Skip to content

Commit 5039d41

Browse files
committed
Add --style to select the formatting style
--aosp was the only style switch, with no way to select Google Style once it had been passed. Wrapper scripts such as Chromium's google-java-format launcher append --aosp unconditionally, leaving projects that format through them no way to opt back into Google Style. Add a --style=<google|aosp> flag, and allow overriding a previously given style. --aosp is kept as an alias for --style=aosp.
1 parent e4cef58 commit 5039d41

6 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ offsets (`--offset`), passing through to standard-out (default) or altered
2727
in-place (`--replace`).
2828

2929
Option `--help` will print full usage details; including built-in documentation
30-
about other flags, such as `--aosp`, `--fix-imports-only`,
30+
about other flags, such as `--style`, `--fix-imports-only`,
3131
`--skip-sorting-imports`, `--skip-removing-unused-import`,
3232
`--skip-reflowing-long-strings`, `--skip-javadoc-formatting`, or the `--dry-run`
3333
and `--set-exit-if-changed`.

‎core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.ImmutableList;
1919
import com.google.common.collect.ImmutableRangeSet;
2020
import com.google.errorprone.annotations.CanIgnoreReturnValue;
21+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
2122
import java.util.Optional;
2223

2324
/**
@@ -28,7 +29,7 @@
2829
* @param lines Line ranges to format.
2930
* @param offsets Character offsets for partial formatting, paired with {@code lengths}.
3031
* @param lengths Partial formatting region lengths, paired with {@code offsets}.
31-
* @param aosp Use AOSP style instead of Google Style (4-space indentation).
32+
* @param style The style to format with.
3233
* @param version Print the version.
3334
* @param help Print usage information.
3435
* @param stdin Format input from stdin.
@@ -47,7 +48,7 @@ record CommandLineOptions(
4748
ImmutableRangeSet<Integer> lines,
4849
ImmutableList<Integer> offsets,
4950
ImmutableList<Integer> lengths,
50-
boolean aosp,
51+
Style style,
5152
boolean version,
5253
boolean help,
5354
boolean stdin,
@@ -73,7 +74,7 @@ static Builder builder() {
7374
.reflowLongStrings(true)
7475
.formatJavadoc(true)
7576
.reorderModifiers(true)
76-
.aosp(false)
77+
.style(Style.GOOGLE)
7778
.version(false)
7879
.help(false)
7980
.stdin(false)
@@ -108,7 +109,7 @@ default Builder addLength(Integer length) {
108109
return this;
109110
}
110111

111-
Builder aosp(boolean aosp);
112+
Builder style(Style style);
112113

113114
Builder version(boolean version);
114115

‎core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

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

19+
import com.google.common.base.Ascii;
1920
import com.google.common.base.CharMatcher;
2021
import com.google.common.base.Splitter;
2122
import com.google.common.collect.ImmutableRangeSet;
2223
import com.google.common.collect.Range;
2324
import com.google.common.collect.RangeSet;
2425
import com.google.common.collect.TreeRangeSet;
26+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
2527
import java.io.IOException;
2628
import java.io.UncheckedIOException;
2729
import java.nio.file.Files;
@@ -71,7 +73,9 @@ static CommandLineOptions parse(Iterable<String> options) {
7173
parseRangeSet(linesBuilder, getValue(flag, it, value));
7274
case "--offset", "-offset" -> optionsBuilder.addOffset(parseInteger(it, flag, value));
7375
case "--length", "-length" -> optionsBuilder.addLength(parseInteger(it, flag, value));
74-
case "--aosp", "-aosp", "-a" -> optionsBuilder.aosp(true);
76+
case "--style", "-style" ->
77+
optionsBuilder.style(parseStyle(flag, getValue(flag, it, value)));
78+
case "--aosp", "-aosp", "-a" -> optionsBuilder.style(Style.AOSP);
7579
case "--version", "-version", "-v" -> optionsBuilder.version(true);
7680
case "--help", "-help", "-h" -> optionsBuilder.help(true);
7781
case "--fix-imports-only" -> optionsBuilder.fixImportsOnly(true);
@@ -92,6 +96,16 @@ static CommandLineOptions parse(Iterable<String> options) {
9296
return optionsBuilder.build();
9397
}
9498

99+
private static Style parseStyle(String flag, String value) {
100+
return switch (Ascii.toLowerCase(value)) {
101+
case "google" -> Style.GOOGLE;
102+
case "aosp" -> Style.AOSP;
103+
default ->
104+
throw new IllegalArgumentException(
105+
String.format("invalid value for %s: %s (expected google or aosp)", flag, value));
106+
};
107+
}
108+
95109
private static Integer parseInteger(Iterator<String> it, String flag, String value) {
96110
try {
97111
return Integer.valueOf(getValue(flag, it, value));

‎core/src/main/java/com/google/googlejavaformat/java/Main.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.common.io.ByteStreams;
2222
import com.google.common.util.concurrent.MoreExecutors;
23-
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
2423
import java.io.IOError;
2524
import java.io.IOException;
2625
import java.io.InputStream;
@@ -118,7 +117,7 @@ public int format(String... args) throws UsageException {
118117

119118
JavaFormatterOptions options =
120119
JavaFormatterOptions.builder()
121-
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
120+
.style(parameters.style())
122121
.formatJavadoc(parameters.formatJavadoc())
123122
.reorderModifiers(parameters.reorderModifiers())
124123
.build();

‎core/src/main/java/com/google/googlejavaformat/java/UsageException.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ final class UsageException extends Exception {
3434
Format stdin -> stdout
3535
--assume-filename, -assume-filename
3636
File name to use for diagnostics when formatting standard input (default is <stdin>).
37+
--style, -style
38+
The style to format with, either google (2-space indentation) or aosp (4-space
39+
indentation). The default is google; if given more than once, the last one wins.
3740
--aosp, -aosp, -a
38-
Use AOSP style instead of Google Style (4-space indentation).
41+
Alias for --style=aosp.
3942
--fix-imports-only
4043
Fix import order and remove any unused imports, but do no other formatting.
4144
--skip-sorting-imports

‎core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
import static com.google.common.truth.Truth.assertThat;
1818
import static java.nio.charset.StandardCharsets.UTF_8;
19+
import static org.junit.Assert.assertThrows;
1920

2021
import com.google.common.collect.ImmutableList;
2122
import com.google.common.collect.Range;
23+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
2224
import java.io.IOException;
2325
import java.nio.file.Files;
2426
import java.nio.file.Path;
@@ -40,7 +42,7 @@ public void defaults() {
4042
CommandLineOptions options = CommandLineOptionsParser.parse(ImmutableList.of());
4143
assertThat(options.files()).isEmpty();
4244
assertThat(options.stdin()).isFalse();
43-
assertThat(options.aosp()).isFalse();
45+
assertThat(options.style()).isEqualTo(Style.GOOGLE);
4446
assertThat(options.help()).isFalse();
4547
assertThat(options.lengths()).isEmpty();
4648
assertThat(options.lines().asRanges()).isEmpty();
@@ -74,7 +76,41 @@ public void stdin() {
7476

7577
@Test
7678
public void aosp() {
77-
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-aosp")).aosp()).isTrue();
79+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-aosp")).style())
80+
.isEqualTo(Style.AOSP);
81+
}
82+
83+
@Test
84+
public void style() {
85+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--style=google")).style())
86+
.isEqualTo(Style.GOOGLE);
87+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--style", "aosp")).style())
88+
.isEqualTo(Style.AOSP);
89+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-style=AOSP")).style())
90+
.isEqualTo(Style.AOSP);
91+
}
92+
93+
@Test
94+
public void lastStyleWins() {
95+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--aosp", "--style=google")).style())
96+
.isEqualTo(Style.GOOGLE);
97+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--style=google", "--aosp")).style())
98+
.isEqualTo(Style.AOSP);
99+
assertThat(
100+
CommandLineOptionsParser.parse(Arrays.asList("--style=google", "--style=aosp")).style())
101+
.isEqualTo(Style.AOSP);
102+
assertThat(
103+
CommandLineOptionsParser.parse(Arrays.asList("--style=aosp", "--style=google")).style())
104+
.isEqualTo(Style.GOOGLE);
105+
}
106+
107+
@Test
108+
public void unknownStyle() {
109+
IllegalArgumentException e =
110+
assertThrows(
111+
IllegalArgumentException.class,
112+
() -> CommandLineOptionsParser.parse(Arrays.asList("--style=llvm")));
113+
assertThat(e).hasMessageThat().contains("invalid value for --style: llvm");
78114
}
79115

80116
@Test

0 commit comments

Comments
 (0)