getFormatterFactories() {
- return Stream.of(format, java, scala)
+ return Stream.of(format, java, scala, kotlin)
.filter(Objects::nonNull)
.collect(toList());
}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java
index 42b04e33f9..dd0cbd4959 100644
--- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java
@@ -32,7 +32,7 @@
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
-import com.diffplug.spotless.maven.generic.LicenseHeader;
+import com.diffplug.spotless.maven.generic.*;
public abstract class FormatterFactory {
@Parameter
@@ -87,7 +87,27 @@ public final void addLicenseHeader(LicenseHeader licenseHeader) {
addStepFactory(licenseHeader);
}
- protected void addStepFactory(FormatterStepFactory stepFactory) {
+ public final void addEndWithNewline(EndWithNewline endWithNewline) {
+ addStepFactory(endWithNewline);
+ }
+
+ public final void addIndent(Indent indent) {
+ addStepFactory(indent);
+ }
+
+ public final void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
+ addStepFactory(trimTrailingWhitespace);
+ }
+
+ public final void addReplace(Replace replace) {
+ addStepFactory(replace);
+ }
+
+ public final void addReplaceRegex(ReplaceRegex replaceRegex) {
+ addStepFactory(replaceRegex);
+ }
+
+ protected final void addStepFactory(FormatterStepFactory stepFactory) {
Objects.requireNonNull(stepFactory);
stepFactories.add(stepFactory);
}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java
index 10f1ba505c..465381fb52 100644
--- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java
@@ -20,6 +20,12 @@
import com.diffplug.spotless.maven.FormatterFactory;
+/**
+ * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element.
+ *
+ * It defines a formatter for custom includes/excludes that executes list of generic, language agnostic steps,
+ * like {@link LicenseHeader}.
+ */
public class Format extends FormatterFactory {
@Override
@@ -32,24 +38,4 @@ public String licenseHeaderDelimiter() {
// do not specify a default delimiter
return null;
}
-
- public void addEndWithNewline(EndWithNewline endWithNewline) {
- addStepFactory(endWithNewline);
- }
-
- public void addIndent(Indent indent) {
- addStepFactory(indent);
- }
-
- public void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
- addStepFactory(trimTrailingWhitespace);
- }
-
- public void addReplace(Replace replace) {
- addStepFactory(replace);
- }
-
- public void addReplaceRegex(ReplaceRegex replaceRegex) {
- addStepFactory(replaceRegex);
- }
}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java
index a8c0078fb9..f48f2b742c 100644
--- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java
@@ -15,16 +15,21 @@
*/
package com.diffplug.spotless.maven.java;
-import static com.diffplug.common.collect.Sets.newHashSet;
-import static java.util.Collections.unmodifiableSet;
-
import java.util.Set;
-import com.diffplug.spotless.maven.generic.Format;
+import com.diffplug.common.collect.ImmutableSet;
+import com.diffplug.spotless.maven.FormatterFactory;
+import com.diffplug.spotless.maven.generic.LicenseHeader;
+
+/**
+ * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element.
+ *
+ * It defines a formatter for java source files that can execute both language agnostic (e.g. {@link LicenseHeader})
+ * and java-specific (e.g. {@link Eclipse}) steps.
+ */
+public class Java extends FormatterFactory {
-public class Java extends Format {
- private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/java/**/*.java",
- "src/test/java/**/*.java"));
+ private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/main/java/**/*.java", "src/test/java/**/*.java");
private static final String LICENSE_HEADER_DELIMITER = "package ";
@Override
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Kotlin.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Kotlin.java
new file mode 100644
index 0000000000..51618613a4
--- /dev/null
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Kotlin.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016 DiffPlug
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.diffplug.spotless.maven.kotlin;
+
+import static com.diffplug.spotless.kotlin.KotlinConstants.LICENSE_HEADER_DELIMITER;
+
+import java.util.Set;
+
+import com.diffplug.common.collect.ImmutableSet;
+import com.diffplug.spotless.maven.FormatterFactory;
+
+public class Kotlin extends FormatterFactory {
+
+ private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/main/kotlin/**/*.kt", "src/test/kotlin/**/*.kt");
+
+ @Override
+ public Set defaultIncludes() {
+ return DEFAULT_INCLUDES;
+ }
+
+ @Override
+ public String licenseHeaderDelimiter() {
+ return LICENSE_HEADER_DELIMITER;
+ }
+
+ public void addKtlint(Ktlint ktlint) {
+ addStepFactory(ktlint);
+ }
+}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java
new file mode 100644
index 0000000000..796523d315
--- /dev/null
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2016 DiffPlug
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.diffplug.spotless.maven.kotlin;
+
+import org.apache.maven.plugins.annotations.Parameter;
+
+import com.diffplug.spotless.FormatterStep;
+import com.diffplug.spotless.kotlin.KtLintStep;
+import com.diffplug.spotless.maven.FormatterStepConfig;
+import com.diffplug.spotless.maven.FormatterStepFactory;
+
+public class Ktlint implements FormatterStepFactory {
+
+ @Parameter
+ private String version;
+
+ @Override
+ public FormatterStep newFormatterStep(FormatterStepConfig config) {
+ String ktlintVersion = version != null ? version : KtLintStep.defaultVersion();
+ return KtLintStep.create(ktlintVersion, config.getProvisioner());
+ }
+}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java
index dbaada1cad..423ca71930 100644
--- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java
@@ -15,16 +15,22 @@
*/
package com.diffplug.spotless.maven.scala;
-import static com.diffplug.common.collect.Sets.newHashSet;
-import static java.util.Collections.unmodifiableSet;
-
import java.util.Set;
-import com.diffplug.spotless.maven.generic.Format;
+import com.diffplug.common.collect.ImmutableSet;
+import com.diffplug.spotless.maven.FormatterFactory;
+import com.diffplug.spotless.maven.generic.LicenseHeader;
+
+/**
+ * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element.
+ *
+ * It defines a formatter for scala source files that can execute both language agnostic (e.g. {@link LicenseHeader})
+ * and scala-specific (e.g. {@link Scalafmt}) steps.
+ */
+public class Scala extends FormatterFactory {
-public class Scala extends Format {
- private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/scala/**/*.scala",
- "src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc"));
+ private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/main/scala/**/*.scala",
+ "src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc");
private static final String LICENSE_HEADER_DELIMITER = "package ";
@Override
diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java
index c5d8e75620..d5fcfcb4f8 100644
--- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java
+++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java
@@ -98,6 +98,10 @@ protected void writePomWithScalaSteps(String... steps) throws IOException {
writePom(groupWithSteps("scala", steps));
}
+ protected void writePomWithKotlinSteps(String... steps) throws IOException {
+ writePom(groupWithSteps("kotlin", steps));
+ }
+
protected void writePom(String... configuration) throws IOException {
writePom(null, configuration);
}
diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java
index 957bbd2e5c..5110f84dc6 100644
--- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java
+++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java
@@ -21,6 +21,7 @@
public class LicenseHeaderTest extends MavenIntegrationTest {
private static final String KEY_LICENSE = "license/TestLicense";
+ private static final String KOTLIN_LICENSE_HEADER = "// Hello, I'm Kotlin license header";
@Test
public void fromFileJava() throws Exception {
@@ -79,6 +80,23 @@ public void fromContentFormat() throws Exception {
runTest();
}
+ @Test
+ public void fromContentKotlin() throws Exception {
+ writePomWithKotlinSteps(
+ "",
+ " ",
+ KOTLIN_LICENSE_HEADER,
+ " ",
+ "");
+
+ String path = "src/main/kotlin/test.kt";
+ String noLicenseHeader = getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
+
+ setFile(path).toContent(noLicenseHeader);
+ mavenRunner().withArguments("spotless:apply").runNoError();
+ assertFile(path).hasContent(KOTLIN_LICENSE_HEADER + '\n' + noLicenseHeader);
+ }
+
private void runTest() throws Exception {
String path = "src/main/java/test.java";
setFile(path).toResource("license/MissingLicense.test");
diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java
new file mode 100644
index 0000000000..eac462f71e
--- /dev/null
+++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2016 DiffPlug
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.diffplug.spotless.maven.kotlin;
+
+import org.junit.Test;
+
+import com.diffplug.spotless.maven.MavenIntegrationTest;
+
+public class KtlintTest extends MavenIntegrationTest {
+
+ @Test
+ public void testKtlint() throws Exception {
+ writePomWithKotlinSteps("");
+
+ String path1 = "src/main/kotlin/main1.kt";
+ String path2 = "src/main/kotlin/main2.kt";
+
+ setFile(path1).toResource("kotlin/ktlint/basic.dirty");
+ setFile(path2).toResource("kotlin/ktlint/basic.dirty");
+
+ mavenRunner().withArguments("spotless:apply").runNoError();
+
+ assertFile(path1).sameAsResource("kotlin/ktlint/basic.clean");
+ assertFile(path2).sameAsResource("kotlin/ktlint/basic.clean");
+ }
+}
diff --git a/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test b/testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test
similarity index 100%
rename from plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test
rename to testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test
diff --git a/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test b/testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test
similarity index 100%
rename from plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test
rename to testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test
diff --git a/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithoutHeader.test b/testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithoutHeader.test
similarity index 100%
rename from plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithoutHeader.test
rename to testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithoutHeader.test