1818import java .io .Serial ;
1919import java .io .Serializable ;
2020import java .lang .reflect .Constructor ;
21+ import java .util .Locale ;
2122import java .util .Objects ;
2223
2324import com .diffplug .spotless .FormatterFunc ;
2728
2829/**
2930 * Formats {@code @TableTest} annotation tables in Java and Kotlin source files.
30- * Configuration is read from {@code .editorconfig} files.
31+ * <p>
32+ * Configuration is read from {@code .editorconfig} files. When no editorconfig is found
33+ * or the lookup fails, the configured fallback indent style and size are used.
34+ * If no fallback is configured, defaults matching {@code Config.SPACES_4} and
35+ * {@code Config.NO_INDENT} are applied.
3136 */
3237public final class TableTestFormatterStep implements Serializable {
3338 @ Serial
34- private static final long serialVersionUID = 1L ;
39+ private static final long serialVersionUID = 2L ;
3540 private static final String NAME = "tableTestFormatter" ;
3641 private static final String MAVEN_COORDINATE = "org.tabletest:tabletest-formatter-core:" ;
3742 private static final String DEFAULT_VERSION = "1.1.1" ;
3843
44+ /** Default fallback indent style ({@code "space"}) for Java/Kotlin files. */
45+ public static final String DEFAULT_INDENT_STYLE = "space" ;
46+ /** Default fallback indent size ({@code 4}) for Java/Kotlin files, matching {@code Config.SPACES_4}. */
47+ public static final int DEFAULT_INDENT_SIZE = 4 ;
48+
3949 private final JarState .Promised jarState ;
4050 private final String version ;
51+ private final String indentStyle ;
52+ private final int indentSize ;
4153
42- private TableTestFormatterStep (JarState .Promised jarState , String version ) {
54+ private TableTestFormatterStep (JarState .Promised jarState , String version , String indentStyle , int indentSize ) {
4355 this .jarState = jarState ;
4456 this .version = version ;
57+ this .indentStyle = validateIndentStyle (indentStyle );
58+ this .indentSize = validateIndentSize (indentSize );
4559 }
4660
47- /** Creates a step which formats {@code @TableTest} tables using the default version. */
61+ /** Creates a step which formats {@code @TableTest} tables using the default version and indent config . */
4862 public static FormatterStep create (Provisioner provisioner ) {
4963 return create (defaultVersion (), provisioner );
5064 }
5165
52- /** Creates a step which formats {@code @TableTest} tables using the given version. */
66+ /** Creates a step which formats {@code @TableTest} tables using the given version and default indent config . */
5367 public static FormatterStep create (String version , Provisioner provisioner ) {
68+ return create (version , provisioner , DEFAULT_INDENT_STYLE , DEFAULT_INDENT_SIZE );
69+ }
70+
71+ /**
72+ * Creates a step which formats {@code @TableTest} tables using the given version and fallback indent config.
73+ * <p>
74+ * The fallback config is used when no {@code .editorconfig} is found or the lookup fails.
75+ *
76+ * @param version the tabletest-formatter-core version
77+ * @param provisioner the jar provisioner
78+ * @param indentStyle fallback indent style: {@code "space"} or {@code "tab"} (case-insensitive)
79+ * @param indentSize fallback indent size (must be >= 0)
80+ */
81+ public static FormatterStep create (String version , Provisioner provisioner , String indentStyle , int indentSize ) {
5482 Objects .requireNonNull (version , "version" );
5583 Objects .requireNonNull (provisioner , "provisioner" );
5684 return FormatterStep .create (NAME ,
57- new TableTestFormatterStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + version , provisioner )), version ),
85+ new TableTestFormatterStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + version , provisioner )), version , indentStyle , indentSize ),
5886 TableTestFormatterStep ::equalityState ,
5987 State ::createFormat );
6088 }
@@ -65,26 +93,46 @@ public static String defaultVersion() {
6593 }
6694
6795 private State equalityState () {
68- return new State (jarState .get (), version );
96+ return new State (jarState .get (), version , indentStyle , indentSize );
97+ }
98+
99+ public static String validateIndentStyle (String indentStyle ) {
100+ Objects .requireNonNull (indentStyle , "indentStyle" );
101+ String lower = indentStyle .toLowerCase (Locale .ROOT );
102+ if (!lower .equals ("space" ) && !lower .equals ("tab" )) {
103+ throw new IllegalArgumentException ("indentStyle must be 'space' or 'tab', got: " + indentStyle );
104+ }
105+ return lower ;
106+ }
107+
108+ public static int validateIndentSize (int indentSize ) {
109+ if (indentSize < 0 ) {
110+ throw new IllegalArgumentException ("indentSize must be >= 0, got: " + indentSize );
111+ }
112+ return indentSize ;
69113 }
70114
71115 private static final class State implements Serializable {
72116 @ Serial
73- private static final long serialVersionUID = 1L ;
117+ private static final long serialVersionUID = 2L ;
74118
75119 private final JarState jarState ;
76120 private final String version ;
121+ private final String indentStyle ;
122+ private final int indentSize ;
77123
78- State (JarState jarState , String version ) {
124+ State (JarState jarState , String version , String indentStyle , int indentSize ) {
79125 this .jarState = jarState ;
80126 this .version = version ;
127+ this .indentStyle = indentStyle ;
128+ this .indentSize = indentSize ;
81129 }
82130
83131 FormatterFunc createFormat () throws Exception {
84132 ClassLoader classLoader = jarState .getClassLoader ();
85133 Class <?> formatterClazz = classLoader .loadClass ("com.diffplug.spotless.glue.java.TableTestFormatterFunc" );
86- Constructor <?> constructor = formatterClazz .getConstructor ();
87- return (FormatterFunc .NeedsFile ) constructor .newInstance ();
134+ Constructor <?> constructor = formatterClazz .getConstructor (String . class , int . class );
135+ return (FormatterFunc .NeedsFile ) constructor .newInstance (indentStyle , indentSize );
88136 }
89137 }
90138}
0 commit comments