diff --git a/CHANGES.md b/CHANGES.md index 261c12d323..79d818512c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changed * Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+. +* Added support for ktlint's FilenameRule ([#974](https://github.com/diffplug/spotless/pull/974)). ### Fixed * Temporary workspace deletion for Eclipse based formatters on JVM shutdown ([#967](https://github.com/diffplug/spotless/issues/967)). Change is only applied for Eclipse versions using JVM 11+, no back-port to older versions is planned. diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 6c21fac890..2364a2d227 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -154,10 +154,10 @@ FormatterFunc createFormat() throws Exception { /* editorConfigPath, nullable */ String.class, /* debug */ boolean.class); Method formatterMethod = ktlintClass.getMethod("format", paramsClass); - formatterFunc = input -> { + FormatterFunc.NeedsFile needsFile = (input, file) -> { try { Object params = constructor.newInstance( - /* fileName, nullable */ null, + /* fileName, nullable */ file.getName(), /* text */ input, /* ruleSets */ ruleSets, /* userData */ userData, @@ -170,6 +170,7 @@ FormatterFunc createFormat() throws Exception { throw ThrowingEx.unwrapCause(e); } }; + formatterFunc = FormatterFunc.needsFile(needsFile); } else { // and its format method String formatterMethodName = isScript ? "formatScript" : "format"; diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index e26eb22ef1..1ff074f253 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changed * Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+. +* Added support for ktlint's FilenameRule ([#974](https://github.com/diffplug/spotless/pull/974)). ### Fixed * Temporary workspace deletion for Eclipse based formatters on JVM shutdown ([#967](https://github.com/diffplug/spotless/issues/967)). Change is only applied for Eclipse versions using JVM 11+, no back-port to older versions is planned. diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index de881182a6..24e3a40fcf 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -153,9 +153,9 @@ void testWithHeader() throws IOException { " ktlint()", " }", "}"); - setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); + setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test")); + assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test")); } @Test @@ -173,9 +173,9 @@ void testWithHeaderKtfmt() throws IOException { " ktfmt()", " }", "}"); - setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); + setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test")); + assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test")); } @Test @@ -192,9 +192,9 @@ void testWithCustomHeaderSeparator() throws IOException { " ktlint()", " }", "}"); - setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); + setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test")); + assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test")); } @Test @@ -212,9 +212,9 @@ void testWithCustomHeaderSeparatorKtfmt() throws IOException { " ktfmt()", " }", "}"); - setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); + setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test")); + assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test")); } @Test @@ -232,13 +232,13 @@ void testWithNonStandardYearSeparator() throws IOException { " }", "}"); - setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test"); - setFile("src/main/kotlin/test2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test"); + setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test"); + setFile("src/main/kotlin/AnObject2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("src/main/kotlin/test.kt").matches(matcher -> { + assertFile("src/main/kotlin/AnObject.kt").matches(matcher -> { matcher.startsWith("// License Header 2012, 2014"); }); - assertFile("src/main/kotlin/test2.kt").matches(matcher -> { + assertFile("src/main/kotlin/AnObject2.kt").matches(matcher -> { matcher.startsWith("// License Header 2012, 2014"); }); } @@ -259,13 +259,13 @@ void testWithNonStandardYearSeparatorKtfmt() throws IOException { " }", "}"); - setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test"); - setFile("src/main/kotlin/test2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test"); + setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test"); + setFile("src/main/kotlin/AnObject2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("src/main/kotlin/test.kt").matches(matcher -> { + assertFile("src/main/kotlin/AnObject.kt").matches(matcher -> { matcher.startsWith("// License Header 2012, 2014"); }); - assertFile("src/main/kotlin/test2.kt").matches(matcher -> { + assertFile("src/main/kotlin/AnObject2.kt").matches(matcher -> { matcher.startsWith("// License Header 2012, 2014"); }); } diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 21331b7980..4be4bdf8b6 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changed * Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+. +* Added support for ktlint's FilenameRule ([#974](https://github.com/diffplug/spotless/pull/974)). + ### Fixed * Revert change from 2.17.2 regarding [skip bug](https://github.com/diffplug/spotless/pull/969) because fixing the skip bug caused inconsistent behavior between `check.skip` and `apply.skip`. * [skip bug](https://github.com/diffplug/spotless/issues/968) if ratchetFrom is specified, the build will still fail in if no Git repository is found, even if `skip` is true (new fix). diff --git a/testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test b/testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test index 70707e6d6a..005124dec9 100644 --- a/testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test +++ b/testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test @@ -2,4 +2,4 @@ @file:JvmName("SomeFileName") package my.test -object AnObject +object AnObject2