Skip to content

Commit 50809be

Browse files
fix: format
1 parent 22e4d0e commit 50809be

File tree

6 files changed

+96
-102
lines changed

6 files changed

+96
-102
lines changed

lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 DiffPlug
2+
* Copyright 2022-2025 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.
@@ -28,8 +28,7 @@ public final class KtfmtFormatterFunc implements FormatterFunc {
2828
@Nonnull
2929
private final KtfmtStyle style;
3030

31-
@Nullable
32-
private final KtfmtFormattingOptions ktfmtFormattingOptions;
31+
@Nullable private final KtfmtFormattingOptions ktfmtFormattingOptions;
3332

3433
public KtfmtFormatterFunc() {
3534
this(KtfmtStyle.META, null);

lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormattingOptions.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 DiffPlug
2+
* Copyright 2022-2025 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.
@@ -22,27 +22,22 @@
2222

2323
public final class KtfmtFormattingOptions {
2424

25-
@Nullable
26-
private Integer maxWidth;
25+
@Nullable private Integer maxWidth;
2726

28-
@Nullable
29-
private Integer blockIndent;
27+
@Nullable private Integer blockIndent;
3028

31-
@Nullable
32-
private Integer continuationIndent;
29+
@Nullable private Integer continuationIndent;
3330

34-
@Nullable
35-
private Boolean removeUnusedImports;
31+
@Nullable private Boolean removeUnusedImports;
3632

37-
@Nullable
38-
private KtfmtTrailingCommaManagementStrategy trailingCommaManagementStrategy;
33+
@Nullable private KtfmtTrailingCommaManagementStrategy trailingCommaManagementStrategy;
3934

4035
public KtfmtFormattingOptions(
41-
@Nullable Integer maxWidth,
42-
@Nullable Integer blockIndent,
43-
@Nullable Integer continuationIndent,
44-
@Nullable Boolean removeUnusedImports,
45-
@Nullable KtfmtTrailingCommaManagementStrategy trailingCommaManagementStrategy) {
36+
@Nullable Integer maxWidth,
37+
@Nullable Integer blockIndent,
38+
@Nullable Integer continuationIndent,
39+
@Nullable Boolean removeUnusedImports,
40+
@Nullable KtfmtTrailingCommaManagementStrategy trailingCommaManagementStrategy) {
4641
this.maxWidth = maxWidth;
4742
this.blockIndent = blockIndent;
4843
this.continuationIndent = continuationIndent;

lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtTrailingCommaManagementStrategy.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.spotless.glue.ktfmt;
217

318
import com.facebook.ktfmt.format.TrailingCommaManagementStrategy;
419

520
public enum KtfmtTrailingCommaManagementStrategy {
6-
NONE,
7-
ONLY_ADD,
8-
COMPLETE;
21+
NONE, ONLY_ADD, COMPLETE;
922

1023
public TrailingCommaManagementStrategy toFormatterTrailingCommaManagementStrategy() {
1124
return switch (this) {

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

Lines changed: 65 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 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.
@@ -18,7 +18,6 @@
1818
import static com.diffplug.spotless.kotlin.KtfmtStep.Style.DEFAULT;
1919
import static com.diffplug.spotless.kotlin.KtfmtStep.Style.DROPBOX;
2020
import static com.diffplug.spotless.kotlin.KtfmtStep.Style.META;
21-
2221
import static com.diffplug.spotless.kotlin.KtfmtStep.TrailingCommaManagementStrategy.ONLY_ADD;
2322

2423
import java.io.Serializable;
@@ -49,19 +48,17 @@ public class KtfmtStep implements Serializable {
4948
/**
5049
* Option that allows to apply formatting options to perform a 4-space block and continuation indent.
5150
*/
52-
@Nullable
53-
private final Style style;
54-
@Nullable
55-
private final KtfmtFormattingOptions options;
51+
@Nullable private final Style style;
52+
@Nullable private final KtfmtFormattingOptions options;
5653
/**
5754
* The jar that contains the formatter.
5855
*/
5956
private final JarState.Promised jarState;
6057

6158
private KtfmtStep(String version,
62-
JarState.Promised jarState,
63-
@Nullable Style style,
64-
@Nullable KtfmtFormattingOptions options) {
59+
JarState.Promised jarState,
60+
@Nullable Style style,
61+
@Nullable KtfmtFormattingOptions options) {
6562
this.version = Objects.requireNonNull(version, "version");
6663
this.style = style;
6764
this.options = options;
@@ -110,8 +107,7 @@ String getSince() {
110107
/**
111108
* Last version (inclusive) that supports this style
112109
*/
113-
@Nullable
114-
String getUntil() {
110+
@Nullable String getUntil() {
115111
return until;
116112
}
117113
}
@@ -148,30 +144,24 @@ public static class KtfmtFormattingOptions implements Serializable {
148144

149145
private static final long serialVersionUID = 1L;
150146

151-
@Nullable
152-
private Integer maxWidth = null;
147+
@Nullable private Integer maxWidth = null;
153148

154-
@Nullable
155-
private Integer blockIndent = null;
149+
@Nullable private Integer blockIndent = null;
156150

157-
@Nullable
158-
private Integer continuationIndent = null;
151+
@Nullable private Integer continuationIndent = null;
159152

160-
@Nullable
161-
private Boolean removeUnusedImports = null;
153+
@Nullable private Boolean removeUnusedImports = null;
162154

163-
@Nullable
164-
private TrailingCommaManagementStrategy trailingCommaManagementStrategy;
155+
@Nullable private TrailingCommaManagementStrategy trailingCommaManagementStrategy;
165156

166-
public KtfmtFormattingOptions() {
167-
}
157+
public KtfmtFormattingOptions() {}
168158

169159
public KtfmtFormattingOptions(
170-
@Nullable Integer maxWidth,
171-
@Nullable Integer blockIndent,
172-
@Nullable Integer continuationIndent,
173-
@Nullable Boolean removeUnusedImports,
174-
@Nullable TrailingCommaManagementStrategy trailingCommaManagementStrategy) {
160+
@Nullable Integer maxWidth,
161+
@Nullable Integer blockIndent,
162+
@Nullable Integer continuationIndent,
163+
@Nullable Boolean removeUnusedImports,
164+
@Nullable TrailingCommaManagementStrategy trailingCommaManagementStrategy) {
175165
this.maxWidth = maxWidth;
176166
this.blockIndent = blockIndent;
177167
this.continuationIndent = continuationIndent;
@@ -221,9 +211,9 @@ public static FormatterStep create(String version, Provisioner provisioner, @Nul
221211
Objects.requireNonNull(version, "version");
222212
Objects.requireNonNull(provisioner, "provisioner");
223213
return FormatterStep.create(NAME,
224-
new KtfmtStep(version, JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner)), style, options),
225-
KtfmtStep::equalityState,
226-
State::createFormat);
214+
new KtfmtStep(version, JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner)), style, options),
215+
KtfmtStep::equalityState,
216+
State::createFormat);
227217
}
228218

229219
public static String defaultVersion() {
@@ -237,16 +227,14 @@ private State equalityState() {
237227
private static final class State implements Serializable {
238228
private static final long serialVersionUID = 1L;
239229
private final String version;
240-
@Nullable
241-
private final Style style;
242-
@Nullable
243-
private final KtfmtFormattingOptions options;
230+
@Nullable private final Style style;
231+
@Nullable private final KtfmtFormattingOptions options;
244232
private final JarState jarState;
245233

246234
State(String version,
247-
JarState jarState,
248-
@Nullable Style style,
249-
@Nullable KtfmtFormattingOptions options) {
235+
JarState jarState,
236+
@Nullable Style style,
237+
@Nullable KtfmtFormattingOptions options) {
250238
this.version = version;
251239
this.options = options;
252240
this.style = style;
@@ -279,14 +267,13 @@ FormatterFunc createFormat() throws Exception {
279267
}
280268

281269
final Constructor<?> optionsConstructor = ktfmtFormattingOptionsClass.getConstructor(
282-
Integer.class, Integer.class, Integer.class, Boolean.class, ktfmtTrailingCommaManagmentStrategyClass);
270+
Integer.class, Integer.class, Integer.class, Boolean.class, ktfmtTrailingCommaManagmentStrategyClass);
283271

284-
final Object ktfmtTrailingCommaManagementStrategy =
285-
options.trailingCommaManagementStrategy == null
272+
final Object ktfmtTrailingCommaManagementStrategy = options.trailingCommaManagementStrategy == null
286273
? null
287274
: Enum.valueOf((Class<? extends Enum>) ktfmtTrailingCommaManagmentStrategyClass, options.trailingCommaManagementStrategy.name());
288275
final Object ktfmtFormattingOptions = optionsConstructor.newInstance(
289-
options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports, ktfmtTrailingCommaManagementStrategy);
276+
options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports, ktfmtTrailingCommaManagementStrategy);
290277
if (style == null) {
291278
final Constructor<?> constructor = formatterFuncClass.getConstructor(ktfmtFormattingOptionsClass);
292279
return (FormatterFunc) constructor.newInstance(ktfmtFormattingOptions);
@@ -336,14 +323,14 @@ private void validateStyle() {
336323
*/
337324
private String getKtfmtStyleOption(Style style) {
338325
switch (style) {
339-
case META:
340-
return "META";
341-
case GOOGLE:
342-
return "GOOGLE";
343-
case KOTLINLANG:
344-
return "KOTLIN_LANG";
345-
default:
346-
throw new IllegalStateException("Unsupported style: " + style);
326+
case META:
327+
return "META";
328+
case GOOGLE:
329+
return "GOOGLE";
330+
case KOTLINLANG:
331+
return "KOTLIN_LANG";
332+
default:
333+
throw new IllegalStateException("Unsupported style: " + style);
347334
}
348335
}
349336
}
@@ -399,45 +386,45 @@ private Object getCustomFormattingOptions(Class<?> formatterClass) throws Except
399386
if (options != null) {
400387
if (BadSemver.version(version) < BadSemver.version(0, 17)) {
401388
formattingOptions = formattingOptions.getClass().getConstructor(int.class, int.class, int.class).newInstance(
402-
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
403-
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
404-
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)));
389+
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
390+
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
391+
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)));
405392
} else if (BadSemver.version(version) < BadSemver.version(0, 19)) {
406393
formattingOptions = formattingOptions.getClass().getConstructor(int.class, int.class, int.class, boolean.class, boolean.class).newInstance(
407-
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
408-
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
409-
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
410-
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
411-
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions));
394+
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
395+
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
396+
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
397+
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
398+
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions));
412399
} else if (BadSemver.version(version) < BadSemver.version(0, 47)) {
413400
Class<?> styleClass = classLoader.loadClass(formattingOptionsClass.getName() + "$Style");
414401
formattingOptions = formattingOptions.getClass().getConstructor(styleClass, int.class, int.class, int.class, boolean.class, boolean.class).newInstance(
415-
/* style = */ formattingOptionsClass.getMethod("getStyle").invoke(formattingOptions),
416-
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
417-
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
418-
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
419-
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
420-
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions));
402+
/* style = */ formattingOptionsClass.getMethod("getStyle").invoke(formattingOptions),
403+
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
404+
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
405+
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
406+
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
407+
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions));
421408
} else if (BadSemver.version(version) < BadSemver.version(0, 57)) {
422409
Class<?> styleClass = classLoader.loadClass(formattingOptionsClass.getName() + "$Style");
423410
formattingOptions = formattingOptions.getClass().getConstructor(styleClass, int.class, int.class, int.class, boolean.class, boolean.class, boolean.class).newInstance(
424-
/* style = */ formattingOptionsClass.getMethod("getStyle").invoke(formattingOptions),
425-
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
426-
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
427-
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
428-
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
429-
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions),
430-
/* manageTrailingCommas = */ Optional.ofNullable(getManageTrailingCommasFrom(options.trailingCommaManagementStrategy)).orElse((Boolean) formattingOptionsClass.getMethod("getManageTrailingCommas").invoke(formattingOptions)));
411+
/* style = */ formattingOptionsClass.getMethod("getStyle").invoke(formattingOptions),
412+
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
413+
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
414+
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
415+
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
416+
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions),
417+
/* manageTrailingCommas = */ Optional.ofNullable(getManageTrailingCommasFrom(options.trailingCommaManagementStrategy)).orElse((Boolean) formattingOptionsClass.getMethod("getManageTrailingCommas").invoke(formattingOptions)));
431418
} else {
432419
Class<?> styleClass = classLoader.loadClass(formattingOptionsClass.getName() + "$Style");
433420
formattingOptions = formattingOptions.getClass().getConstructor(styleClass, int.class, int.class, int.class, boolean.class, boolean.class, TrailingCommaManagementStrategy.class).newInstance(
434-
/* style = */ formattingOptionsClass.getMethod("getStyle").invoke(formattingOptions),
435-
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
436-
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
437-
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
438-
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
439-
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions),
440-
/* trailingCommaManagementStrategy */ Optional.ofNullable(options.trailingCommaManagementStrategy).orElse((TrailingCommaManagementStrategy) formattingOptionsClass.getMethod("getTrailingCommaManagementStrategy").invoke(formattingOptions)));
421+
/* style = */ formattingOptionsClass.getMethod("getStyle").invoke(formattingOptions),
422+
/* maxWidth = */ Optional.ofNullable(options.maxWidth).orElse((Integer) formattingOptionsClass.getMethod("getMaxWidth").invoke(formattingOptions)),
423+
/* blockIndent = */ Optional.ofNullable(options.blockIndent).orElse((Integer) formattingOptionsClass.getMethod("getBlockIndent").invoke(formattingOptions)),
424+
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
425+
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
426+
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions),
427+
/* trailingCommaManagementStrategy */ Optional.ofNullable(options.trailingCommaManagementStrategy).orElse((TrailingCommaManagementStrategy) formattingOptionsClass.getMethod("getTrailingCommaManagementStrategy").invoke(formattingOptions)));
441428
}
442429
}
443430

0 commit comments

Comments
 (0)