From 973cbff100aa1277ea97f26ffdbc2afd9cb0cb07 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 24 Feb 2023 13:44:10 -0800 Subject: [PATCH] Allow modifier reordering to be disabled This is currently the only non-whitespace change made as part of the core formatting logic. This is a requirement for updating the IntelliJ plugin to use the new supported plugin API. PiperOrigin-RevId: 512153706 --- .../googlejavaformat/java/Formatter.java | 4 +- .../java/JavaFormatterOptions.java | 50 +++++++------------ .../googlejavaformat/java/MainTest.java | 23 +++++++++ .../GoogleJavaFormatCodeStyleManager.java | 3 +- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java index 841e88a8a..9ff702d5a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java @@ -262,7 +262,9 @@ public ImmutableList getFormatReplacements( // TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace, // and doesn't change the replacements that are output. This is not true in general for // 'de-linting' changes (e.g. import ordering). - javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges); + if (options.reorderModifiers()) { + javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges); + } String lineSeparator = Newlines.guessLineSeparator(input); JavaOutput javaOutput = diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java index fbb6fe7e4..67c13d0b3 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java @@ -14,6 +14,7 @@ package com.google.googlejavaformat.java; +import com.google.auto.value.AutoValue; import com.google.errorprone.annotations.Immutable; /** @@ -27,7 +28,8 @@ * preferences, and in fact it would work directly against our primary goals. */ @Immutable -public class JavaFormatterOptions { +@AutoValue +public abstract class JavaFormatterOptions { public enum Style { /** The default Google Java Style configuration. */ @@ -47,27 +49,17 @@ int indentationMultiplier() { } } - private final Style style; - private final boolean formatJavadoc; - - private JavaFormatterOptions(Style style, boolean formatJavadoc) { - this.style = style; - this.formatJavadoc = formatJavadoc; - } - /** Returns the multiplier for the unit of indent. */ public int indentationMultiplier() { - return style.indentationMultiplier(); + return style().indentationMultiplier(); } - public boolean formatJavadoc() { - return formatJavadoc; - } + public abstract boolean formatJavadoc(); + + public abstract boolean reorderModifiers(); /** Returns the code style. */ - public Style style() { - return style; - } + public abstract Style style(); /** Returns the default formatting options. */ public static JavaFormatterOptions defaultOptions() { @@ -76,28 +68,22 @@ public static JavaFormatterOptions defaultOptions() { /** Returns a builder for {@link JavaFormatterOptions}. */ public static Builder builder() { - return new Builder(); + return new AutoValue_JavaFormatterOptions.Builder() + .style(Style.GOOGLE) + .formatJavadoc(true) + .reorderModifiers(true); } /** A builder for {@link JavaFormatterOptions}. */ - public static class Builder { - private Style style = Style.GOOGLE; - private boolean formatJavadoc = true; + @AutoValue.Builder + public abstract static class Builder { - private Builder() {} + public abstract Builder style(Style style); - public Builder style(Style style) { - this.style = style; - return this; - } + public abstract Builder formatJavadoc(boolean formatJavadoc); - public Builder formatJavadoc(boolean formatJavadoc) { - this.formatJavadoc = formatJavadoc; - return this; - } + public abstract Builder reorderModifiers(boolean reorderModifiers); - public JavaFormatterOptions build() { - return new JavaFormatterOptions(style, formatJavadoc); - } + public abstract JavaFormatterOptions build(); } } diff --git a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java index d4a410904..ad91bfa26 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/MainTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/MainTest.java @@ -613,4 +613,27 @@ public void noFormatJavadoc() throws Exception { assertThat(main.format("--skip-javadoc-formatting", "-")).isEqualTo(0); assertThat(out.toString()).isEqualTo(joiner.join(input)); } + + @Test + public void reorderModifiersOptionTest() throws Exception { + String[] input = { + "class Test {", // + " static public void main(String... args) {}", + "}", + "", + }; + String[] fixed = { + "class Test {", // + " public static void main(String... args) {}", + "}", + "", + }; + String source = joiner.join(input); + assertThat(new Formatter(JavaFormatterOptions.builder().build()).formatSource(source)) + .isEqualTo(joiner.join(fixed)); + assertThat( + new Formatter(JavaFormatterOptions.builder().reorderModifiers(false).build()) + .formatSource(source)) + .isEqualTo(source); + } } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java index 3d56743ee..c9aa64a67 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java @@ -145,7 +145,8 @@ private void formatInternal(PsiFile file, Collection ranges */ private void format(Document document, Collection ranges) { Style style = GoogleJavaFormatSettings.getInstance(getProject()).getStyle(); - Formatter formatter = new Formatter(JavaFormatterOptions.builder().style(style).build()); + Formatter formatter = + new Formatter(JavaFormatterOptions.builder().style(style).reorderModifiers(false).build()); performReplacements( document, FormatterUtil.getReplacements(formatter, document.getText(), ranges)); }