Skip to content

Commit 012c135

Browse files
committed
Support GJF own import order
1 parent 54c6333 commit 012c135

File tree

8 files changed

+42
-15
lines changed

8 files changed

+42
-15
lines changed

Diff for: CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
15+
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
1516
### Fixed
1617
* Use latest versions of popular style guides for `eslint` tests to fix failing `useEslintXoStandardRules` test. ([#1761](https://github.com/diffplug/spotless/pull/1761), [#1756](https://github.com/diffplug/spotless/issues/1756))
1718
* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))

Diff for: lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import com.diffplug.spotless.FormatterFunc;
3131

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

3436
@Nonnull
@@ -42,10 +44,13 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc {
4244

4345
private final boolean reflowStrings;
4446

45-
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) {
47+
private final boolean reorderImports;
48+
49+
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) {
4650
this.version = Objects.requireNonNull(version);
4751
this.formatterStyle = Style.valueOf(Objects.requireNonNull(style));
4852
this.reflowStrings = reflowStrings;
53+
this.reorderImports = reorderImports;
4954

5055
this.formatter = new Formatter(JavaFormatterOptions.builder()
5156
.style(formatterStyle)
@@ -57,10 +62,7 @@ public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String st
5762
public String apply(@Nonnull String input) throws Exception {
5863
String formatted = formatter.formatSource(input);
5964
String removedUnused = RemoveUnusedImports.removeUnusedImports(formatted);
60-
// Issue #1679: we used to call ImportOrderer.reorderImports(String) here, but that is deprecated.
61-
// Replacing the call with (the correct) reorderImports(String, Style) causes issues for Style.AOSP,
62-
// so we force the style to GOOGLE for now (which is what the deprecated method did)
63-
String sortedImports = ImportOrderer.reorderImports(removedUnused, Style.GOOGLE);
65+
String sortedImports = ImportOrderer.reorderImports(removedUnused, reorderImports ? formatterStyle : Style.GOOGLE);
6466
return reflowLongStrings(sortedImports);
6567
}
6668

Diff for: lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private GoogleJavaFormatStep() {}
3232

3333
private static final String DEFAULT_STYLE = "GOOGLE";
3434
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
35+
private static final boolean DEFAULT_REORDER_IMPORTS = false;
3536
static final String NAME = "google-java-format";
3637
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
3738

@@ -55,8 +56,12 @@ public static FormatterStep create(String version, String style, Provisioner pro
5556
return create(MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
5657
}
5758

58-
/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
5959
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
60+
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false);
61+
}
62+
63+
/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
64+
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) {
6065
Objects.requireNonNull(groupArtifact, "groupArtifact");
6166
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
6267
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
@@ -65,7 +70,7 @@ public static FormatterStep create(String groupArtifact, String version, String
6570
Objects.requireNonNull(style, "style");
6671
Objects.requireNonNull(provisioner, "provisioner");
6772
return FormatterStep.createLazy(NAME,
68-
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings),
73+
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports),
6974
State::createFormat);
7075
}
7176

@@ -92,6 +97,10 @@ public static boolean defaultReflowLongStrings() {
9297
return DEFAULT_REFLOW_LONG_STRINGS;
9398
}
9499

100+
public static boolean defaultReorderImports() {
101+
return DEFAULT_REORDER_IMPORTS;
102+
}
103+
95104
static final class State implements Serializable {
96105
private static final long serialVersionUID = 1L;
97106

@@ -101,6 +110,7 @@ static final class State implements Serializable {
101110
final String version;
102111
final String style;
103112
final boolean reflowLongStrings;
113+
final boolean reorderImports;
104114

105115
State(String stepName, String version, Provisioner provisioner) throws Exception {
106116
this(stepName, version, DEFAULT_STYLE, provisioner);
@@ -111,24 +121,25 @@ static final class State implements Serializable {
111121
}
112122

113123
State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
114-
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
124+
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS);
115125
}
116126

117-
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
127+
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
118128
JVM_SUPPORT.assertFormatterSupported(version);
119129
ModuleHelper.doOpenInternalPackagesIfRequired();
120130
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
121131
this.stepName = stepName;
122132
this.version = version;
123133
this.style = style;
124134
this.reflowLongStrings = reflowLongStrings;
135+
this.reorderImports = reorderImports;
125136
}
126137

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

133144
return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc);
134145
}

Diff for: plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
8+
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
89
### Fixed
910
* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
1011
* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))

Diff for: plugin-gradle/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ spotless {
206206
// optional: you can specify a specific version (>= 1.8) and/or switch to AOSP style
207207
// and/or reflow long strings
208208
// and/or use custom group artifact (you probably don't need this)
209-
googleJavaFormat('1.8').aosp().reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format')
209+
googleJavaFormat('1.8').aosp().reflowLongStrings().reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format')
210210
```
211211

212212
### palantir-java-format

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public class GoogleJavaFormatConfig {
173173
String groupArtifact;
174174
String style;
175175
boolean reflowLongStrings;
176+
boolean reorderImports;
176177

177178
GoogleJavaFormatConfig(String version) {
178179
this.version = Objects.requireNonNull(version);
@@ -207,13 +208,19 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {
207208
return this;
208209
}
209210

211+
public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
212+
this.reorderImports = reorderImports;
213+
return this;
214+
}
215+
210216
private FormatterStep createStep() {
211217
return GoogleJavaFormatStep.create(
212218
groupArtifact,
213219
version,
214220
style,
215221
provisioner(),
216-
reflowLongStrings);
222+
reflowLongStrings,
223+
reorderImports);
217224
}
218225
}
219226

Diff for: plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
8+
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
89
### Fixed
910
* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
1011
* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))

Diff for: plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,12 +35,16 @@ public class GoogleJavaFormat implements FormatterStepFactory {
3535
@Parameter
3636
private Boolean reflowLongStrings;
3737

38+
@Parameter
39+
private Boolean reorderImports;
40+
3841
@Override
3942
public FormatterStep newFormatterStep(FormatterStepConfig config) {
4043
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : GoogleJavaFormatStep.defaultGroupArtifact();
4144
String version = this.version != null ? this.version : GoogleJavaFormatStep.defaultVersion();
4245
String style = this.style != null ? this.style : GoogleJavaFormatStep.defaultStyle();
4346
boolean reflowLongStrings = this.reflowLongStrings != null ? this.reflowLongStrings : GoogleJavaFormatStep.defaultReflowLongStrings();
44-
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings);
47+
boolean reorderImports = this.reorderImports != null ? this.reorderImports : GoogleJavaFormatStep.defaultReorderImports();
48+
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports);
4549
}
4650
}

0 commit comments

Comments
 (0)