1
1
/*
2
- * Copyright 2016-2023 DiffPlug
2
+ * Copyright 2016-2024 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
18
18
import static com .diffplug .spotless .kotlin .KtfmtStep .Style .DEFAULT ;
19
19
20
- import java .io .IOException ;
21
20
import java .io .Serializable ;
22
21
import java .lang .reflect .Constructor ;
23
22
import java .lang .reflect .InvocationTargetException ;
30
29
import com .diffplug .spotless .FormatterStep ;
31
30
import com .diffplug .spotless .JarState ;
32
31
import com .diffplug .spotless .Provisioner ;
32
+ import com .diffplug .spotless .RoundedStep ;
33
33
import com .diffplug .spotless .ThrowingEx ;
34
34
35
35
/**
36
36
* Wraps up <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> as a FormatterStep.
37
37
*/
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 ;
42
40
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
+ }
46
64
47
65
/**
48
66
* 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) {
136
154
public static FormatterStep create (String version , Provisioner provisioner , @ Nullable Style style , @ Nullable KtfmtFormattingOptions options ) {
137
155
Objects .requireNonNull (version , "version" );
138
156
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 );
141
161
}
142
162
143
163
public static String defaultVersion () {
144
164
return DEFAULT_VERSION ;
145
165
}
146
166
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
+ }
149
170
171
+ private static final class State implements Serializable {
172
+ private static final long serialVersionUID = 1L ;
173
+ private static final String PACKAGE = "com.facebook.ktfmt" ;
150
174
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
- */
156
175
@ Nullable
157
176
private final Style style ;
158
- /**
159
- *
160
- */
161
177
@ Nullable
162
178
private final KtfmtFormattingOptions options ;
163
- /** The jar that contains the formatter. */
164
- final JarState jarState ;
179
+ private final JarState jarState ;
165
180
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 ) {
167
185
this .version = version ;
168
186
this .options = options ;
169
- this .pkg = PACKAGE ;
170
187
this .style = style ;
171
- this .jarState = JarState . from ( MAVEN_COORDINATE + version , provisioner ) ;
188
+ this .jarState = jarState ;
172
189
}
173
190
174
191
FormatterFunc createFormat () throws Exception {
@@ -259,7 +276,7 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
259
276
260
277
// fallback to old, pre-0.19 ktfmt interface.
261
278
if (style == Style .DEFAULT || style == Style .DROPBOX ) {
262
- Class <?> formattingOptionsCompanionClazz = classLoader .loadClass (pkg + ".ktfmt .FormattingOptions$Companion" );
279
+ Class <?> formattingOptionsCompanionClazz = classLoader .loadClass (PACKAGE + ".FormattingOptions$Companion" );
263
280
Object companion = formattingOptionsCompanionClazz .getConstructors ()[0 ].newInstance ((Object ) null );
264
281
Method formattingOptionsMethod = formattingOptionsCompanionClazz .getDeclaredMethod ("dropboxStyle" );
265
282
return formattingOptionsMethod .invoke (companion );
@@ -271,19 +288,19 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
271
288
private Class <?> getFormatterClazz (ClassLoader classLoader ) throws Exception {
272
289
Class <?> formatterClazz ;
273
290
if (BadSemver .version (version ) >= BadSemver .version (0 , 31 )) {
274
- formatterClazz = classLoader .loadClass (pkg + ".ktfmt .format.Formatter" );
291
+ formatterClazz = classLoader .loadClass (PACKAGE + ".format.Formatter" );
275
292
} else {
276
- formatterClazz = classLoader .loadClass (pkg + ".ktfmt .FormatterKt" );
293
+ formatterClazz = classLoader .loadClass (PACKAGE + ".FormatterKt" );
277
294
}
278
295
return formatterClazz ;
279
296
}
280
297
281
298
private Class <?> getFormattingOptionsClazz (ClassLoader classLoader ) throws Exception {
282
299
Class <?> formattingOptionsClazz ;
283
300
if (BadSemver .version (version ) >= BadSemver .version (0 , 31 )) {
284
- formattingOptionsClazz = classLoader .loadClass (pkg + ".ktfmt .format.FormattingOptions" );
301
+ formattingOptionsClazz = classLoader .loadClass (PACKAGE + ".format.FormattingOptions" );
285
302
} else {
286
- formattingOptionsClazz = classLoader .loadClass (pkg + ".ktfmt .FormattingOptions" );
303
+ formattingOptionsClazz = classLoader .loadClass (PACKAGE + ".FormattingOptions" );
287
304
}
288
305
return formattingOptionsClazz ;
289
306
}
0 commit comments