2727
2828/**
2929 * Formats {@code @TableTest} annotation tables in Java and Kotlin source files.
30- * Configuration is read from {@code .editorconfig} files.
30+ * <p>
31+ * Configuration is read from {@code .editorconfig} files. When no editorconfig is found
32+ * or the lookup fails, the configured fallback indent style and size are used.
33+ * If no fallback is configured, defaults matching {@code Config.SPACES_4} and
34+ * {@code Config.NO_INDENT} are applied.
3135 */
3236public final class TableTestFormatterStep implements Serializable {
3337 @ Serial
34- private static final long serialVersionUID = 1L ;
38+ private static final long serialVersionUID = 2L ;
3539 private static final String NAME = "tableTestFormatter" ;
3640 private static final String MAVEN_COORDINATE = "org.tabletest:tabletest-formatter-core:" ;
3741 private static final String DEFAULT_VERSION = "1.1.1" ;
3842
43+ /** Default fallback indent style ({@code "space"}) for Java/Kotlin files. */
44+ public static final String DEFAULT_INDENT_STYLE = "space" ;
45+ /** Default fallback indent size ({@code 4}) for Java/Kotlin files, matching {@code Config.SPACES_4}. */
46+ public static final int DEFAULT_INDENT_SIZE = 4 ;
47+
3948 private final JarState .Promised jarState ;
4049 private final String version ;
50+ private final String indentStyle ;
51+ private final int indentSize ;
4152
42- private TableTestFormatterStep (JarState .Promised jarState , String version ) {
53+ private TableTestFormatterStep (JarState .Promised jarState , String version , String indentStyle , int indentSize ) {
4354 this .jarState = jarState ;
4455 this .version = version ;
56+ this .indentStyle = validateIndentStyle (indentStyle );
57+ this .indentSize = validateIndentSize (indentSize );
4558 }
4659
47- /** Creates a step which formats {@code @TableTest} tables using the default version. */
60+ /** Creates a step which formats {@code @TableTest} tables using the default version and indent config . */
4861 public static FormatterStep create (Provisioner provisioner ) {
4962 return create (defaultVersion (), provisioner );
5063 }
5164
52- /** Creates a step which formats {@code @TableTest} tables using the given version. */
65+ /** Creates a step which formats {@code @TableTest} tables using the given version and default indent config . */
5366 public static FormatterStep create (String version , Provisioner provisioner ) {
67+ return create (version , provisioner , DEFAULT_INDENT_STYLE , DEFAULT_INDENT_SIZE );
68+ }
69+
70+ /**
71+ * Creates a step which formats {@code @TableTest} tables using the given version and fallback indent config.
72+ * <p>
73+ * The fallback config is used when no {@code .editorconfig} is found or the lookup fails.
74+ *
75+ * @param version the tabletest-formatter-core version
76+ * @param provisioner the jar provisioner
77+ * @param indentStyle fallback indent style: {@code "space"} or {@code "tab"} (case-insensitive)
78+ * @param indentSize fallback indent size (must be >= 0)
79+ */
80+ public static FormatterStep create (String version , Provisioner provisioner , String indentStyle , int indentSize ) {
5481 Objects .requireNonNull (version , "version" );
5582 Objects .requireNonNull (provisioner , "provisioner" );
5683 return FormatterStep .create (NAME ,
57- new TableTestFormatterStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + version , provisioner )), version ),
84+ new TableTestFormatterStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + version , provisioner )), version , indentStyle , indentSize ),
5885 TableTestFormatterStep ::equalityState ,
5986 State ::createFormat );
6087 }
@@ -65,26 +92,46 @@ public static String defaultVersion() {
6592 }
6693
6794 private State equalityState () {
68- return new State (jarState .get (), version );
95+ return new State (jarState .get (), version , indentStyle , indentSize );
96+ }
97+
98+ public static String validateIndentStyle (String indentStyle ) {
99+ Objects .requireNonNull (indentStyle , "indentStyle" );
100+ String lower = indentStyle .toLowerCase ();
101+ if (!lower .equals ("space" ) && !lower .equals ("tab" )) {
102+ throw new IllegalArgumentException ("indentStyle must be 'space' or 'tab', got: " + indentStyle );
103+ }
104+ return lower ;
105+ }
106+
107+ public static int validateIndentSize (int indentSize ) {
108+ if (indentSize < 0 ) {
109+ throw new IllegalArgumentException ("indentSize must be >= 0, got: " + indentSize );
110+ }
111+ return indentSize ;
69112 }
70113
71114 private static final class State implements Serializable {
72115 @ Serial
73- private static final long serialVersionUID = 1L ;
116+ private static final long serialVersionUID = 2L ;
74117
75118 private final JarState jarState ;
76119 private final String version ;
120+ private final String indentStyle ;
121+ private final int indentSize ;
77122
78- State (JarState jarState , String version ) {
123+ State (JarState jarState , String version , String indentStyle , int indentSize ) {
79124 this .jarState = jarState ;
80125 this .version = version ;
126+ this .indentStyle = indentStyle ;
127+ this .indentSize = indentSize ;
81128 }
82129
83130 FormatterFunc createFormat () throws Exception {
84131 ClassLoader classLoader = jarState .getClassLoader ();
85132 Class <?> formatterClazz = classLoader .loadClass ("com.diffplug.spotless.glue.java.TableTestFormatterFunc" );
86- Constructor <?> constructor = formatterClazz .getConstructor ();
87- return (FormatterFunc .NeedsFile ) constructor .newInstance ();
133+ Constructor <?> constructor = formatterClazz .getConstructor (String . class , int . class );
134+ return (FormatterFunc .NeedsFile ) constructor .newInstance (indentStyle , indentSize );
88135 }
89136 }
90137}
0 commit comments