Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 973cbff

Browse files
cushongoogle-java-format Team
authored andcommitted
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
1 parent 8c85ac1 commit 973cbff

File tree

4 files changed

+46
-34
lines changed

4 files changed

+46
-34
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ public ImmutableList<Replacement> getFormatReplacements(
262262
// TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace,
263263
// and doesn't change the replacements that are output. This is not true in general for
264264
// 'de-linting' changes (e.g. import ordering).
265-
javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);
265+
if (options.reorderModifiers()) {
266+
javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);
267+
}
266268

267269
String lineSeparator = Newlines.guessLineSeparator(input);
268270
JavaOutput javaOutput =

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

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17+
import com.google.auto.value.AutoValue;
1718
import com.google.errorprone.annotations.Immutable;
1819

1920
/**
@@ -27,7 +28,8 @@
2728
* preferences, and in fact it would work directly against our primary goals.
2829
*/
2930
@Immutable
30-
public class JavaFormatterOptions {
31+
@AutoValue
32+
public abstract class JavaFormatterOptions {
3133

3234
public enum Style {
3335
/** The default Google Java Style configuration. */
@@ -47,27 +49,17 @@ int indentationMultiplier() {
4749
}
4850
}
4951

50-
private final Style style;
51-
private final boolean formatJavadoc;
52-
53-
private JavaFormatterOptions(Style style, boolean formatJavadoc) {
54-
this.style = style;
55-
this.formatJavadoc = formatJavadoc;
56-
}
57-
5852
/** Returns the multiplier for the unit of indent. */
5953
public int indentationMultiplier() {
60-
return style.indentationMultiplier();
54+
return style().indentationMultiplier();
6155
}
6256

63-
public boolean formatJavadoc() {
64-
return formatJavadoc;
65-
}
57+
public abstract boolean formatJavadoc();
58+
59+
public abstract boolean reorderModifiers();
6660

6761
/** Returns the code style. */
68-
public Style style() {
69-
return style;
70-
}
62+
public abstract Style style();
7163

7264
/** Returns the default formatting options. */
7365
public static JavaFormatterOptions defaultOptions() {
@@ -76,28 +68,22 @@ public static JavaFormatterOptions defaultOptions() {
7668

7769
/** Returns a builder for {@link JavaFormatterOptions}. */
7870
public static Builder builder() {
79-
return new Builder();
71+
return new AutoValue_JavaFormatterOptions.Builder()
72+
.style(Style.GOOGLE)
73+
.formatJavadoc(true)
74+
.reorderModifiers(true);
8075
}
8176

8277
/** A builder for {@link JavaFormatterOptions}. */
83-
public static class Builder {
84-
private Style style = Style.GOOGLE;
85-
private boolean formatJavadoc = true;
78+
@AutoValue.Builder
79+
public abstract static class Builder {
8680

87-
private Builder() {}
81+
public abstract Builder style(Style style);
8882

89-
public Builder style(Style style) {
90-
this.style = style;
91-
return this;
92-
}
83+
public abstract Builder formatJavadoc(boolean formatJavadoc);
9384

94-
public Builder formatJavadoc(boolean formatJavadoc) {
95-
this.formatJavadoc = formatJavadoc;
96-
return this;
97-
}
85+
public abstract Builder reorderModifiers(boolean reorderModifiers);
9886

99-
public JavaFormatterOptions build() {
100-
return new JavaFormatterOptions(style, formatJavadoc);
101-
}
87+
public abstract JavaFormatterOptions build();
10288
}
10389
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,4 +613,27 @@ public void noFormatJavadoc() throws Exception {
613613
assertThat(main.format("--skip-javadoc-formatting", "-")).isEqualTo(0);
614614
assertThat(out.toString()).isEqualTo(joiner.join(input));
615615
}
616+
617+
@Test
618+
public void reorderModifiersOptionTest() throws Exception {
619+
String[] input = {
620+
"class Test {", //
621+
" static public void main(String... args) {}",
622+
"}",
623+
"",
624+
};
625+
String[] fixed = {
626+
"class Test {", //
627+
" public static void main(String... args) {}",
628+
"}",
629+
"",
630+
};
631+
String source = joiner.join(input);
632+
assertThat(new Formatter(JavaFormatterOptions.builder().build()).formatSource(source))
633+
.isEqualTo(joiner.join(fixed));
634+
assertThat(
635+
new Formatter(JavaFormatterOptions.builder().reorderModifiers(false).build())
636+
.formatSource(source))
637+
.isEqualTo(source);
638+
}
616639
}

idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ private void formatInternal(PsiFile file, Collection<? extends TextRange> ranges
145145
*/
146146
private void format(Document document, Collection<? extends TextRange> ranges) {
147147
Style style = GoogleJavaFormatSettings.getInstance(getProject()).getStyle();
148-
Formatter formatter = new Formatter(JavaFormatterOptions.builder().style(style).build());
148+
Formatter formatter =
149+
new Formatter(JavaFormatterOptions.builder().style(style).reorderModifiers(false).build());
149150
performReplacements(
150151
document, FormatterUtil.getReplacements(formatter, document.getText(), ranges));
151152
}

0 commit comments

Comments
 (0)