Skip to content

Commit 0a44147

Browse files
authored
Bump default ktfmt 0.30 -> 0.31. (#1118)
2 parents 98d6ee9 + 9d3a4bd commit 0a44147

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Changed
14+
* Bump default ktfmt `0.30` -> `0.31` ([#1118](https://github.com/diffplug/spotless/pull/1118)).
1315

1416
## [2.22.1] - 2022-02-01
1517
### Changed

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

+26-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class KtfmtStep {
3232
// prevent direct instantiation
3333
private KtfmtStep() {}
3434

35-
private static final String DEFAULT_VERSION = "0.30";
35+
private static final String DEFAULT_VERSION = "0.31";
3636
static final String NAME = "ktfmt";
3737
static final String PACKAGE = "com.facebook";
3838
static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:";
@@ -120,18 +120,16 @@ static final class State implements Serializable {
120120

121121
FormatterFunc createFormat() throws Exception {
122122
ClassLoader classLoader = jarState.getClassLoader();
123-
Class<?> formatterClazz = classLoader.loadClass(pkg + ".ktfmt.FormatterKt");
124123
return input -> {
125124
try {
126125
if (style == DEFAULT) {
127-
Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
128-
return (String) formatterMethod.invoke(formatterClazz, input);
126+
Method formatterMethod = getFormatterClazz(classLoader).getMethod(FORMATTER_METHOD, String.class);
127+
return (String) formatterMethod.invoke(getFormatterClazz(classLoader), input);
129128
} else {
130-
Class<?> formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions");
131-
Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, formattingOptionsClazz,
129+
Method formatterMethod = getFormatterClazz(classLoader).getMethod(FORMATTER_METHOD, getFormattingOptionsClazz(classLoader),
132130
String.class);
133131
Object formattingOptions = getCustomFormattingOptions(classLoader, style);
134-
return (String) formatterMethod.invoke(formatterClazz, formattingOptions, input);
132+
return (String) formatterMethod.invoke(getFormatterClazz(classLoader), formattingOptions, input);
135133
}
136134
} catch (InvocationTargetException e) {
137135
throw ThrowingEx.unwrapCause(e);
@@ -146,7 +144,7 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
146144

147145
try {
148146
// ktfmt v0.19 and later
149-
return classLoader.loadClass(pkg + ".ktfmt.FormatterKt").getField(style.getFormat()).get(null);
147+
return getFormatterClazz(classLoader).getField(style.getFormat()).get(null);
150148
} catch (NoSuchFieldException ignored) {}
151149

152150
// fallback to old, pre-0.19 ktfmt interface.
@@ -159,5 +157,25 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
159157
throw new IllegalStateException("Versions pre-0.19 can only use Default and Dropbox styles");
160158
}
161159
}
160+
161+
private Class<?> getFormatterClazz(ClassLoader classLoader) throws Exception {
162+
Class<?> formatterClazz;
163+
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
164+
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.format.Formatter");
165+
} else {
166+
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.FormatterKt");
167+
}
168+
return formatterClazz;
169+
}
170+
171+
private Class<?> getFormattingOptionsClazz(ClassLoader classLoader) throws Exception {
172+
Class<?> formattingOptionsClazz;
173+
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
174+
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.format.FormattingOptions");
175+
} else {
176+
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions");
177+
}
178+
return formattingOptionsClazz;
179+
}
162180
}
163181
}

plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Changed
7+
* Bump default ktfmt `0.30` -> `0.31` ([#1118](https://github.com/diffplug/spotless/pull/1118)).
68

79
## [6.2.1] - 2022-02-01
810
### Changed

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Changed
7+
* Bump default ktfmt `0.30` -> `0.31` ([#1118](https://github.com/diffplug/spotless/pull/1118)).
68

79
## [2.20.1] - 2022-02-01
810
* Bump default versions of formatters ([#1095](https://github.com/diffplug/spotless/pull/1095)).

0 commit comments

Comments
 (0)