Skip to content

Commit 6e6ceaa

Browse files
committed
Make KtfmtStep round-trippable
1 parent 4e10fd4 commit 6e6ceaa

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

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

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 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.
@@ -17,7 +17,6 @@
1717

1818
import static com.diffplug.spotless.kotlin.KtfmtStep.Style.DEFAULT;
1919

20-
import java.io.IOException;
2120
import java.io.Serializable;
2221
import java.lang.reflect.Constructor;
2322
import java.lang.reflect.InvocationTargetException;
@@ -30,19 +29,38 @@
3029
import com.diffplug.spotless.FormatterStep;
3130
import com.diffplug.spotless.JarState;
3231
import com.diffplug.spotless.Provisioner;
32+
import com.diffplug.spotless.RoundedStep;
3333
import com.diffplug.spotless.ThrowingEx;
3434

3535
/**
3636
* Wraps up <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> as a FormatterStep.
3737
*/
38-
public class KtfmtStep {
39-
// prevent direct instantiation
40-
private KtfmtStep() {}
41-
38+
public class KtfmtStep implements RoundedStep {
39+
private static final long serialVersionUID = 1L;
4240
private static final String DEFAULT_VERSION = "0.46";
43-
static final String NAME = "ktfmt";
44-
static final String PACKAGE = "com.facebook";
45-
static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:";
41+
private static final String NAME = "ktfmt";
42+
private static final String MAVEN_COORDINATE = "com.facebook:ktfmt:";
43+
44+
private final String version;
45+
/**
46+
* Option that allows to apply formatting options to perform a 4-space block and continuation indent.
47+
*/
48+
@Nullable
49+
private final Style style;
50+
@Nullable
51+
private final KtfmtFormattingOptions options;
52+
/** The jar that contains the formatter. */
53+
private final JarState.Promised jarState;
54+
55+
private KtfmtStep(String version,
56+
JarState.Promised jarState,
57+
@Nullable Style style,
58+
@Nullable KtfmtFormattingOptions options) {
59+
this.version = Objects.requireNonNull(version, "version");
60+
this.style = style;
61+
this.options = options;
62+
this.jarState = Objects.requireNonNull(jarState, "jarState");
63+
}
4664

4765
/**
4866
* Used to allow multiple style option through formatting options and since when is each of them available.
@@ -136,39 +154,38 @@ public static FormatterStep create(String version, Provisioner provisioner) {
136154
public static FormatterStep create(String version, Provisioner provisioner, @Nullable Style style, @Nullable KtfmtFormattingOptions options) {
137155
Objects.requireNonNull(version, "version");
138156
Objects.requireNonNull(provisioner, "provisioner");
139-
return FormatterStep.createLazy(
140-
NAME, () -> new State(version, provisioner, style, options), State::createFormat);
157+
return FormatterStep.create(NAME,
158+
new KtfmtStep(version, JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner)), style, options),
159+
KtfmtStep::equalityState,
160+
State::createFormat);
141161
}
142162

143163
public static String defaultVersion() {
144164
return DEFAULT_VERSION;
145165
}
146166

147-
static final class State implements Serializable {
148-
private static final long serialVersionUID = 1L;
167+
private State equalityState() {
168+
return new State(version, jarState.get(), style, options);
169+
}
149170

171+
private static final class State implements Serializable {
172+
private static final long serialVersionUID = 1L;
173+
private static final String PACKAGE = "com.facebook.ktfmt";
150174
private final String version;
151-
152-
private final String pkg;
153-
/**
154-
* Option that allows to apply formatting options to perform a 4 spaces block and continuation indent.
155-
*/
156175
@Nullable
157176
private final Style style;
158-
/**
159-
*
160-
*/
161177
@Nullable
162178
private final KtfmtFormattingOptions options;
163-
/** The jar that contains the formatter. */
164-
final JarState jarState;
179+
private final JarState jarState;
165180

166-
State(String version, Provisioner provisioner, @Nullable Style style, @Nullable KtfmtFormattingOptions options) throws IOException {
181+
State(String version,
182+
JarState jarState,
183+
@Nullable Style style,
184+
@Nullable KtfmtFormattingOptions options) {
167185
this.version = version;
168186
this.options = options;
169-
this.pkg = PACKAGE;
170187
this.style = style;
171-
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
188+
this.jarState = jarState;
172189
}
173190

174191
FormatterFunc createFormat() throws Exception {
@@ -259,7 +276,7 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
259276

260277
// fallback to old, pre-0.19 ktfmt interface.
261278
if (style == Style.DEFAULT || style == Style.DROPBOX) {
262-
Class<?> formattingOptionsCompanionClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions$Companion");
279+
Class<?> formattingOptionsCompanionClazz = classLoader.loadClass(PACKAGE + ".FormattingOptions$Companion");
263280
Object companion = formattingOptionsCompanionClazz.getConstructors()[0].newInstance((Object) null);
264281
Method formattingOptionsMethod = formattingOptionsCompanionClazz.getDeclaredMethod("dropboxStyle");
265282
return formattingOptionsMethod.invoke(companion);
@@ -271,19 +288,19 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
271288
private Class<?> getFormatterClazz(ClassLoader classLoader) throws Exception {
272289
Class<?> formatterClazz;
273290
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
274-
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.format.Formatter");
291+
formatterClazz = classLoader.loadClass(PACKAGE + ".format.Formatter");
275292
} else {
276-
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.FormatterKt");
293+
formatterClazz = classLoader.loadClass(PACKAGE + ".FormatterKt");
277294
}
278295
return formatterClazz;
279296
}
280297

281298
private Class<?> getFormattingOptionsClazz(ClassLoader classLoader) throws Exception {
282299
Class<?> formattingOptionsClazz;
283300
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
284-
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.format.FormattingOptions");
301+
formattingOptionsClazz = classLoader.loadClass(PACKAGE + ".format.FormattingOptions");
285302
} else {
286-
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions");
303+
formattingOptionsClazz = classLoader.loadClass(PACKAGE + ".FormattingOptions");
287304
}
288305
return formattingOptionsClazz;
289306
}

0 commit comments

Comments
 (0)