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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* 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.
* @param reorderModifiers Reorder modifiers into the JLS-recommended order.
*/
record CommandLineOptions(
ImmutableList<String> files,
Expand All @@ -57,7 +58,8 @@ record CommandLineOptions(
boolean setExitIfChanged,
Optional<String> assumeFilename,
boolean reflowLongStrings,
boolean formatJavadoc) {
boolean formatJavadoc,
boolean reorderModifiers) {

/** Returns true if partial formatting was selected. */
boolean isSelection() {
Expand All @@ -70,6 +72,7 @@ static Builder builder() {
.removeUnusedImports(true)
.reflowLongStrings(true)
.formatJavadoc(true)
.reorderModifiers(true)
.aosp(false)
.version(false)
.help(false)
Expand Down Expand Up @@ -129,6 +132,8 @@ default Builder addLength(Integer length) {

Builder formatJavadoc(boolean formatJavadoc);

Builder reorderModifiers(boolean reorderModifiers);

CommandLineOptions build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static CommandLineOptions parse(Iterable<String> options) {
case "--skip-removing-unused-imports" -> optionsBuilder.removeUnusedImports(false);
case "--skip-reflowing-long-strings" -> optionsBuilder.reflowLongStrings(false);
case "--skip-javadoc-formatting" -> optionsBuilder.formatJavadoc(false);
case "--skip-reordering-modifiers" -> optionsBuilder.reorderModifiers(false);
case "-" -> optionsBuilder.stdin(true);
case "-n", "--dry-run" -> optionsBuilder.dryRun(true);
case "--set-exit-if-changed" -> optionsBuilder.setExitIfChanged(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public int format(String... args) throws UsageException {
JavaFormatterOptions.builder()
.style(parameters.aosp() ? Style.AOSP : Style.GOOGLE)
.formatJavadoc(parameters.formatJavadoc())
.reorderModifiers(parameters.reorderModifiers())
.build();

if (parameters.stdin()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Use AOSP style instead of Google Style (4-space indentation).
Do not reflow string literals that exceed the column limit.
--skip-javadoc-formatting
Do not reformat javadoc.
--skip-reordering-modifiers
Do not reorder modifiers into the JLS-recommended order.
--dry-run, -n
Prints the paths of the files whose contents would change if the formatter were run normally.
--set-exit-if-changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void defaults() {
assertThat(options.setExitIfChanged()).isFalse();
assertThat(options.reflowLongStrings()).isTrue();
assertThat(options.formatJavadoc()).isTrue();
assertThat(options.reorderModifiers()).isTrue();
}

@Test
Expand Down Expand Up @@ -200,4 +201,12 @@ public void skipJavadocFormatting() {
.formatJavadoc())
.isFalse();
}

@Test
public void skipReorderingModifiers() {
assertThat(
CommandLineOptionsParser.parse(Arrays.asList("--skip-reordering-modifiers"))
.reorderModifiers())
.isFalse();
}
}
19 changes: 19 additions & 0 deletions core/src/test/java/com/google/googlejavaformat/java/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,25 @@ public static void main(String... args) {}
.isEqualTo(input);
}

@Test
public void noReorderModifiers() throws Exception {
String input =
"""
class Test {
static public void main(String... args) {}
}
""";
InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
in);
assertThat(main.format("--skip-reordering-modifiers", "-")).isEqualTo(0);
assertThat(out.toString()).isEqualTo(input);
}

@Test
public void syntaxError() throws Exception {
Path path = testFolder.newFile("Test.java").toPath();
Expand Down