Skip to content

Commit

Permalink
Allow modifier reordering to be disabled
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cushon authored and google-java-format Team committed Feb 24, 2023
1 parent 8c85ac1 commit 973cbff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ public ImmutableList<Replacement> 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.googlejavaformat.java;

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;

/**
Expand All @@ -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. */
Expand All @@ -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() {
Expand All @@ -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();
}
}
23 changes: 23 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 @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ private void formatInternal(PsiFile file, Collection<? extends TextRange> ranges
*/
private void format(Document document, Collection<? extends TextRange> 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));
}
Expand Down

0 comments on commit 973cbff

Please sign in to comment.