Skip to content

Commit 238fd08

Browse files
authored
Merge pull request #469 from ZacSweers/z/updateKtLint
Support KtLint 0.34+
2 parents 2ca2107 + 7832883 commit 238fd08

File tree

11 files changed

+77
-27
lines changed

11 files changed

+77
-27
lines changed

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ You might be looking for:
77

88
### Version 1.25.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))
99

10+
* Add support for ktlint `0.34+`, and bump default version from `0.32.0` to `0.34.2`. ([#469](https://github.com/diffplug/spotless/pull/469))
11+
1012
### Version 1.24.3 - September 23rd 2019 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.24.3/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.24.3/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
1113

1214
* Update jgit from `5.3.2.201906051522-r` to `5.5.0.201909110433-r`. ([#445](https://github.com/diffplug/spotless/pull/445))
@@ -200,7 +202,7 @@ You might be looking for:
200202
### Version 1.1.0 - February 27th 2017 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.1.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.1.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
201203

202204
* Added support for Scala via [scalafmt](https://github.com/olafurpg/scalafmt).
203-
* Added support for Kotlin via [ktlint](https://github.com/shyiko/ktlint).
205+
* Added support for Kotlin via [ktlint](https://github.com/pinterest/ktlint).
204206
* Better error messages for JarState.
205207
* Improved test harnessing.
206208
* Formatter now has pluggable exception policies,

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Once someone has filled in one square of the formatter/build system matrix, it's
108108
- Thanks to [Oliver Horn](https://github.com/ohorn) for adding AOSP support for Spotless' google-java-format integration.
109109
- Formatting by Eclipse
110110
- Special thanks to [Mateusz Matela](https://waynebeaton.wordpress.com/2015/03/15/great-fixes-for-mars-winners-part-i/) for huge improvements to the eclipse code formatter!
111-
- Thanks to [Zac Sweers](https://github.com/ZacSweers) for multiple build updates and fixing a gradle deprecation warning ([#434](https://github.com/diffplug/spotless/pull/434) and others).
111+
- Thanks to [Zac Sweers](https://github.com/ZacSweers) for fixing the highly requested ktlint 0.34+ support ([#469](https://github.com/diffplug/spotless/pull/469)), multiple build updates and fixing a gradle deprecation warning ([#434](https://github.com/diffplug/spotless/pull/434) and others).
112112
- Thanks to [Nelson Osacky](https://github.com/runningcode) for android doc improvements, versions bump, and a build improvement.
113113
- Thanks to [Stanley Shyiko](https://github.com/shyiko) for his help integrating [ktlint](https://github.com/shyiko/ktlint).
114114
- Thanks to [Jonathan Leitschuh](https://github.com/JLLeitschuh) for adding [ktlint](https://github.com/shyiko/ktlint) support for [Gradle Kotlin DSL](https://github.com/gradle/kotlin-dsl) files.

lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.IOException;
1919
import java.io.Serializable;
20+
import java.lang.reflect.Constructor;
2021
import java.lang.reflect.InvocationTargetException;
2122
import java.lang.reflect.Method;
2223
import java.lang.reflect.Proxy;
@@ -33,13 +34,13 @@
3334
import com.diffplug.spotless.Provisioner;
3435
import com.diffplug.spotless.ThrowingEx;
3536

36-
/** Wraps up [ktlint](https://github.com/shyiko/ktlint) as a FormatterStep. */
37+
/** Wraps up [ktlint](https://github.com/pinterest/ktlint) as a FormatterStep. */
3738
public class KtLintStep {
3839
// prevent direct instantiation
3940
private KtLintStep() {}
4041

41-
private static final Pattern VERSION_PRE_0_32 = Pattern.compile("0\\.(\\d+)\\.\\d+");
42-
private static final String DEFAULT_VERSION = "0.32.0";
42+
private static final Pattern VERSION_MATCHER = Pattern.compile("0\\.(\\d+)\\.\\d+");
43+
private static final String DEFAULT_VERSION = "0.34.2";
4344
static final String NAME = "ktlint";
4445
static final String PACKAGE_PRE_0_32 = "com.github.shyiko";
4546
static final String PACKAGE = "com.pinterest";
@@ -87,18 +88,21 @@ static final class State implements Serializable {
8788
/** The jar that contains the eclipse formatter. */
8889
final JarState jarState;
8990
private final TreeMap<String, String> userData;
91+
private final boolean useParams;
9092

9193
State(String version, Provisioner provisioner, boolean isScript, Map<String, String> userData) throws IOException {
9294
this.userData = new TreeMap<>(userData);
9395
String coordinate;
94-
Matcher matcher = VERSION_PRE_0_32.matcher(version);
95-
if (matcher.matches() && Integer.parseInt(matcher.group(1)) < 32) {
96+
Matcher matcher = VERSION_MATCHER.matcher(version);
97+
boolean matches = matcher.matches();
98+
if (matches && Integer.parseInt(matcher.group(1)) < 32) {
9699
coordinate = MAVEN_COORDINATE_PRE_0_32;
97100
this.pkg = PACKAGE_PRE_0_32;
98101
} else {
99102
coordinate = MAVEN_COORDINATE;
100103
this.pkg = PACKAGE;
101104
}
105+
this.useParams = matches && Integer.parseInt(matcher.group(1)) >= 34;
102106
this.jarState = JarState.from(coordinate + version, provisioner);
103107
this.isScript = isScript;
104108
}
@@ -135,18 +139,56 @@ FormatterFunc createFormat() throws Exception {
135139
// grab the KtLint singleton
136140
Class<?> ktlintClass = classLoader.loadClass(pkg + ".ktlint.core.KtLint");
137141
Object ktlint = ktlintClass.getDeclaredField("INSTANCE").get(null);
138-
// and its format method
139-
String formatterMethodName = isScript ? "formatScript" : "format";
140-
Method formatterMethod = ktlintClass.getMethod(formatterMethodName, String.class, Iterable.class, Map.class, function2Interface);
141-
142-
return input -> {
143-
try {
144-
String formatted = (String) formatterMethod.invoke(ktlint, input, ruleSets, userData, formatterCallback);
145-
return formatted;
146-
} catch (InvocationTargetException e) {
147-
throw ThrowingEx.unwrapCause(e);
148-
}
149-
};
142+
FormatterFunc formatterFunc;
143+
if (useParams) {
144+
//
145+
// In KtLint 0.34+ there is a new "format(params: Params)" function. We create an
146+
// instance of the Params class with our configuration and invoke it here.
147+
//
148+
149+
// grab the Params class
150+
Class<?> paramsClass = classLoader.loadClass(pkg + ".ktlint.core.KtLint$Params");
151+
// and its constructor
152+
Constructor<?> constructor = paramsClass.getConstructor(
153+
/* fileName, nullable */ String.class,
154+
/* text */ String.class,
155+
/* ruleSets */ Iterable.class,
156+
/* userData */ Map.class,
157+
/* callback */ function2Interface,
158+
/* script */ boolean.class,
159+
/* editorConfigPath, nullable */ String.class,
160+
/* debug */ boolean.class);
161+
Method formatterMethod = ktlintClass.getMethod("format", paramsClass);
162+
formatterFunc = input -> {
163+
try {
164+
Object params = constructor.newInstance(
165+
/* fileName, nullable */ null,
166+
/* text */ input,
167+
/* ruleSets */ ruleSets,
168+
/* userData */ userData,
169+
/* callback */ formatterCallback,
170+
/* script */ isScript,
171+
/* editorConfigPath, nullable */ null,
172+
/* debug */ false);
173+
return (String) formatterMethod.invoke(ktlint, params);
174+
} catch (InvocationTargetException e) {
175+
throw ThrowingEx.unwrapCause(e);
176+
}
177+
};
178+
} else {
179+
// and its format method
180+
String formatterMethodName = isScript ? "formatScript" : "format";
181+
Method formatterMethod = ktlintClass.getMethod(formatterMethodName, String.class, Iterable.class, Map.class, function2Interface);
182+
formatterFunc = input -> {
183+
try {
184+
return (String) formatterMethod.invoke(ktlint, input, ruleSets, userData, formatterCallback);
185+
} catch (InvocationTargetException e) {
186+
throw ThrowingEx.unwrapCause(e);
187+
}
188+
};
189+
}
190+
191+
return formatterFunc;
150192
}
151193
}
152194
}

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Spotless no longer breaks configuration avoidance for other tasks (specifically the `check` task and all of its dependees) ([#463](https://github.com/diffplug/spotless/pull/463)).
66
* Important change: **Formerly, Spotless did not create its tasks until the `afterEvaluate` phase. Spotless now creates them as soon as the plugin is applied**, and it creates the format-specific tasks as soon as the formats are defined. There is no performance degradation associated with this change, and it makes configuring Spotless easier.
7+
* Add support for ktlint `0.34+`, and bump default version from `0.32.0` to `0.34.2`. ([#469](https://github.com/diffplug/spotless/pull/469))
78

89
### Version 3.24.3 - September 23rd 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.24.3/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.24.3))
910

@@ -218,7 +219,7 @@
218219
### Version 3.1.0 - February 27th 2017 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.1.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.1.0))
219220

220221
* Added support for Scala via [scalafmt](https://github.com/olafurpg/scalafmt).
221-
* Added support for Kotlin via [ktlint](https://github.com/shyiko/ktlint).
222+
* Added support for Kotlin via [ktlint](https://github.com/pinterest/ktlint).
222223
* Added `FormatExtension::replaceStep`.
223224
* `paddedCell()` is no longer required if a misbehaving rule converges.
224225
* Any errors in a step will now fail the build - previously they were only warned.

plugin-gradle/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Spotless can check and apply formatting to any plain-text file, using simple rul
8383
* Google's [google-java-format](https://github.com/google/google-java-format)
8484
* [Groovy Eclipse](#groovy-eclipse)'s groovy code formatter
8585
* [FreshMark](https://github.com/diffplug/freshmark) (markdown with variables)
86-
* [ktlint](https://github.com/shyiko/ktlint)
86+
* [ktlint](https://github.com/pinterest/ktlint)
8787
* [scalafmt](https://github.com/olafurpg/scalafmt)
8888
* [DBeaver sql format](https://dbeaver.jkiss.org/)
8989
* [Prettier: An opinionated code formatter](https://prettier.io)
@@ -252,7 +252,7 @@ spotless {
252252

253253
<a name="ktlint"></a>
254254

255-
## Applying [ktlint](https://github.com/shyiko/ktlint) to Kotlin files
255+
## Applying [ktlint](https://github.com/pinterest/ktlint) to Kotlin files
256256

257257
```gradle
258258
spotless {

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
4646
return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER);
4747
}
4848

49-
/** Adds the specified version of [ktlint](https://github.com/shyiko/ktlint). */
49+
/** Adds the specified version of [ktlint](https://github.com/pinterest/ktlint). */
5050
public KotlinFormatExtension ktlint(String version) {
5151
Objects.requireNonNull(version);
5252
return new KotlinFormatExtension(version, Collections.emptyMap());

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public KotlinGradleExtension(SpotlessExtension rootExtension) {
3232
super(rootExtension);
3333
}
3434

35-
/** Adds the specified version of [ktlint](https://github.com/shyiko/ktlint). */
35+
/** Adds the specified version of [ktlint](https://github.com/pinterest/ktlint). */
3636
public KotlinFormatExtension ktlint(String version) {
3737
Objects.requireNonNull(version, "version");
3838
return new KotlinFormatExtension(version, Collections.emptyMap());

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void integration_default() throws IOException {
7474
}
7575

7676
@Test
77-
public void integration_shyiko() throws IOException {
77+
public void integration_pinterest() throws IOException {
7878
setFile("build.gradle").toLines(
7979
"plugins {",
8080
" id 'nebula.kotlin' version '1.0.6'",

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Version 1.25.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))
44

5+
* Add support for ktlint `0.34+`, and bump default version from `0.32.0` to `0.34.2`. ([#469](https://github.com/diffplug/spotless/pull/469))
6+
57
### Version 1.24.3 - September 23rd 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.24.3/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.24.3))
68

79
* Update jgit from `5.3.2.201906051522-r` to `5.5.0.201909110433-r`. ([#445](https://github.com/diffplug/spotless/pull/445))

plugin-maven/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ By default, all files matching `src/main/kotlin/**/*.kt` and `src/test/kotlin/**
162162
<endWithNewLine/>
163163
<trimTrailingWhitespace/>
164164
<ktlint>
165-
<!-- Optional, available versions: https://github.com/shyiko/ktlint/releases -->
165+
<!-- Optional, available versions: https://github.com/pinterest/ktlint/releases -->
166166
<version>0.14.0</version>
167167
</ktlint>
168168
</kotlin>

testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ public void worksShyiko() throws Exception {
5252
});
5353
}
5454

55+
// Regression test to ensure it works on the version it switched to Pinterest (version 0.32.0)
56+
// but before 0.34.
57+
// https://github.com/diffplug/spotless/issues/419
5558
@Test
56-
public void worksPinterest() throws Exception {
59+
public void worksPinterestAndPre034() throws Exception {
5760
// Must use jcenter because `com.andreapivetta.kolor:kolor:0.0.2` isn't available on mavenCentral.
5861
// It is a dependency of ktlint.
5962
FormatterStep step = KtLintStep.create("0.32.0", TestProvisioner.jcenter());

0 commit comments

Comments
 (0)