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.
1818import static com .diffplug .spotless .kotlin .KtfmtStep .Style .DEFAULT ;
1919import static com .diffplug .spotless .kotlin .KtfmtStep .Style .DROPBOX ;
2020import static com .diffplug .spotless .kotlin .KtfmtStep .Style .META ;
21-
2221import static com .diffplug .spotless .kotlin .KtfmtStep .TrailingCommaManagementStrategy .ONLY_ADD ;
2322
2423import 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