From 504a4d245098f17b4f056f12c370e59c5c758218 Mon Sep 17 00:00:00 2001 From: Stephen Panaro Date: Tue, 26 Oct 2021 13:24:22 -0400 Subject: [PATCH 1/3] Add support for ktlint FilenameRule --- .../main/java/com/diffplug/spotless/kotlin/KtLintStep.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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"; From 0d2731afe35110316b72bf534edba5107e1cb13a Mon Sep 17 00:00:00 2001 From: Stephen Panaro Date: Tue, 26 Oct 2021 15:53:17 -0400 Subject: [PATCH 2/3] Update changelogs for ktlint filenamerule --- CHANGES.md | 1 + plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 2 ++ 3 files changed, 4 insertions(+) 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/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-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). From e0b2131f2d3ee84f723b6619eff7014b7a552ef1 Mon Sep 17 00:00:00 2001 From: Stephen Panaro Date: Tue, 26 Oct 2021 16:06:10 -0400 Subject: [PATCH 3/3] Resolve ktlint errors in tests --- .../gradle/spotless/KotlinExtensionTest.java | 32 +++++++++---------- .../KotlinCodeWithMultiYearHeader2.test | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) 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/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