Skip to content

Commit 8165092

Browse files
hferreirogoogle-java-format Team
authored andcommitted
Add --google-style to select Google 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 --google-style flag, and allow overriding a previously given style. When both --aosp and --google-style are given, the last one wins. Fixes #1415 COPYBARA_INTEGRATE_REVIEW=#1415 from hferreiro:no-aosp 0d1c53e PiperOrigin-RevId: 982482322
1 parent ed87207 commit 8165092

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static CommandLineOptions parse(Iterable<String> options) {
7171
parseRangeSet(linesBuilder, getValue(flag, it, value));
7272
case "--offset", "-offset" -> optionsBuilder.addOffset(parseInteger(it, flag, value));
7373
case "--length", "-length" -> optionsBuilder.addLength(parseInteger(it, flag, value));
74+
case "--google-style", "-google-style" -> optionsBuilder.aosp(false);
7475
case "--aosp", "-aosp", "-a" -> optionsBuilder.aosp(true);
7576
case "--version", "-version", "-v" -> optionsBuilder.version(true);
7677
case "--help", "-help", "-h" -> optionsBuilder.help(true);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ 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+
--google-style, -google-style
38+
Use Google Style (2-space indentation). This is the default; if both --aosp and
39+
--google-style are given, the last one wins.
3740
--aosp, -aosp, -a
3841
Use AOSP style instead of Google Style (4-space indentation).
3942
--fix-imports-only

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ public void aosp() {
7777
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-aosp")).aosp()).isTrue();
7878
}
7979

80+
@Test
81+
public void googleStyle() {
82+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--google-style")).aosp()).isFalse();
83+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-google-style")).aosp()).isFalse();
84+
}
85+
86+
@Test
87+
public void lastStyleWins() {
88+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--aosp", "--google-style")).aosp())
89+
.isFalse();
90+
assertThat(CommandLineOptionsParser.parse(Arrays.asList("--google-style", "--aosp")).aosp())
91+
.isTrue();
92+
}
93+
8094
@Test
8195
public void help() {
8296
assertThat(CommandLineOptionsParser.parse(Arrays.asList("-help")).help()).isTrue();

0 commit comments

Comments
 (0)