Skip to content

Commit

Permalink
Add a flag to override sortedImports in GJF
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Aug 8, 2023
1 parent 8b1713d commit 973a561
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import com.diffplug.spotless.FormatterFunc;

// Used via reflection by the Gradle plugin.
@SuppressWarnings("unused")
public class GoogleJavaFormatFormatterFunc implements FormatterFunc {

@Nonnull
Expand All @@ -42,10 +44,13 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc {

private final boolean reflowStrings;

public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) {
private final boolean reorderImports;

public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) {
this.version = Objects.requireNonNull(version);
this.formatterStyle = Style.valueOf(Objects.requireNonNull(style));
this.reflowStrings = reflowStrings;
this.reorderImports = reorderImports;

this.formatter = new Formatter(JavaFormatterOptions.builder()
.style(formatterStyle)
Expand All @@ -60,7 +65,7 @@ public String apply(@Nonnull String input) throws Exception {
// Issue #1679: we used to call ImportOrderer.reorderImports(String) here, but that is deprecated.
// Replacing the call with (the correct) reorderImports(String, Style) causes issues for Style.AOSP,
// so we force the style to GOOGLE for now (which is what the deprecated method did)
String sortedImports = ImportOrderer.reorderImports(removedUnused, Style.GOOGLE);
String sortedImports = ImportOrderer.reorderImports(removedUnused, reorderImports ? formatterStyle : Style.GOOGLE);
return reflowLongStrings(sortedImports);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public static FormatterStep create(String version, String style, Provisioner pro
return create(MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
}

/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false);
}

/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) {
Objects.requireNonNull(groupArtifact, "groupArtifact");
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
Expand All @@ -65,7 +69,7 @@ public static FormatterStep create(String groupArtifact, String version, String
Objects.requireNonNull(style, "style");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings),
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports),
State::createFormat);
}

Expand Down Expand Up @@ -101,6 +105,7 @@ static final class State implements Serializable {
final String version;
final String style;
final boolean reflowLongStrings;
final boolean reorderImports;

State(String stepName, String version, Provisioner provisioner) throws Exception {
this(stepName, version, DEFAULT_STYLE, provisioner);
Expand All @@ -111,24 +116,25 @@ static final class State implements Serializable {
}

State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, false);
}

State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
JVM_SUPPORT.assertFormatterSupported(version);
ModuleHelper.doOpenInternalPackagesIfRequired();
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
this.stepName = stepName;
this.version = version;
this.style = style;
this.reflowLongStrings = reflowLongStrings;
this.reorderImports = reorderImports;
}

FormatterFunc createFormat() throws Exception {
final ClassLoader classLoader = jarState.getClassLoader();
Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.java.GoogleJavaFormatFormatterFunc");
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings);
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings, reorderImports);

return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public class GoogleJavaFormatConfig {
String groupArtifact;
String style;
boolean reflowLongStrings;
boolean reorderImports;

GoogleJavaFormatConfig(String version) {
this.version = Objects.requireNonNull(version);
Expand Down Expand Up @@ -207,13 +208,19 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {
return this;
}

public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
this.reorderImports = reorderImports;
return this;
}

private FormatterStep createStep() {
return GoogleJavaFormatStep.create(
groupArtifact,
version,
style,
provisioner(),
reflowLongStrings);
reflowLongStrings,
reorderImports);
}
}

Expand Down

0 comments on commit 973a561

Please sign in to comment.