Skip to content

Commit 4016c91

Browse files
authored
Add support for ktlint FilenameRule (#974)
2 parents 2e61129 + e0b2131 commit 4016c91

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Changed
1414
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+.
15+
* Added support for ktlint's FilenameRule ([#974](https://github.com/diffplug/spotless/pull/974)).
1516

1617
### Fixed
1718
* 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.

lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ FormatterFunc createFormat() throws Exception {
154154
/* editorConfigPath, nullable */ String.class,
155155
/* debug */ boolean.class);
156156
Method formatterMethod = ktlintClass.getMethod("format", paramsClass);
157-
formatterFunc = input -> {
157+
FormatterFunc.NeedsFile needsFile = (input, file) -> {
158158
try {
159159
Object params = constructor.newInstance(
160-
/* fileName, nullable */ null,
160+
/* fileName, nullable */ file.getName(),
161161
/* text */ input,
162162
/* ruleSets */ ruleSets,
163163
/* userData */ userData,
@@ -170,6 +170,7 @@ FormatterFunc createFormat() throws Exception {
170170
throw ThrowingEx.unwrapCause(e);
171171
}
172172
};
173+
formatterFunc = FormatterFunc.needsFile(needsFile);
173174
} else {
174175
// and its format method
175176
String formatterMethodName = isScript ? "formatScript" : "format";

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changed
77
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+.
8+
* Added support for ktlint's FilenameRule ([#974](https://github.com/diffplug/spotless/pull/974)).
89

910
### Fixed
1011
* 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.

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ void testWithHeader() throws IOException {
153153
" ktlint()",
154154
" }",
155155
"}");
156-
setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
156+
setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
157157
gradleRunner().withArguments("spotlessApply").build();
158-
assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"));
158+
assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"));
159159
}
160160

161161
@Test
@@ -173,9 +173,9 @@ void testWithHeaderKtfmt() throws IOException {
173173
" ktfmt()",
174174
" }",
175175
"}");
176-
setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
176+
setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
177177
gradleRunner().withArguments("spotlessApply").build();
178-
assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test"));
178+
assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test"));
179179
}
180180

181181
@Test
@@ -192,9 +192,9 @@ void testWithCustomHeaderSeparator() throws IOException {
192192
" ktlint()",
193193
" }",
194194
"}");
195-
setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
195+
setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
196196
gradleRunner().withArguments("spotlessApply").build();
197-
assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"));
197+
assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test"));
198198
}
199199

200200
@Test
@@ -212,9 +212,9 @@ void testWithCustomHeaderSeparatorKtfmt() throws IOException {
212212
" ktfmt()",
213213
" }",
214214
"}");
215-
setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
215+
setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test");
216216
gradleRunner().withArguments("spotlessApply").build();
217-
assertFile("src/main/kotlin/test.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test"));
217+
assertFile("src/main/kotlin/AnObject.kt").hasContent(HEADER + "\n" + getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeaderKtfmt.test"));
218218
}
219219

220220
@Test
@@ -232,13 +232,13 @@ void testWithNonStandardYearSeparator() throws IOException {
232232
" }",
233233
"}");
234234

235-
setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test");
236-
setFile("src/main/kotlin/test2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test");
235+
setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test");
236+
setFile("src/main/kotlin/AnObject2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test");
237237
gradleRunner().withArguments("spotlessApply").build();
238-
assertFile("src/main/kotlin/test.kt").matches(matcher -> {
238+
assertFile("src/main/kotlin/AnObject.kt").matches(matcher -> {
239239
matcher.startsWith("// License Header 2012, 2014");
240240
});
241-
assertFile("src/main/kotlin/test2.kt").matches(matcher -> {
241+
assertFile("src/main/kotlin/AnObject2.kt").matches(matcher -> {
242242
matcher.startsWith("// License Header 2012, 2014");
243243
});
244244
}
@@ -259,13 +259,13 @@ void testWithNonStandardYearSeparatorKtfmt() throws IOException {
259259
" }",
260260
"}");
261261

262-
setFile("src/main/kotlin/test.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test");
263-
setFile("src/main/kotlin/test2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test");
262+
setFile("src/main/kotlin/AnObject.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test");
263+
setFile("src/main/kotlin/AnObject2.kt").toResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test");
264264
gradleRunner().withArguments("spotlessApply").build();
265-
assertFile("src/main/kotlin/test.kt").matches(matcher -> {
265+
assertFile("src/main/kotlin/AnObject.kt").matches(matcher -> {
266266
matcher.startsWith("// License Header 2012, 2014");
267267
});
268-
assertFile("src/main/kotlin/test2.kt").matches(matcher -> {
268+
assertFile("src/main/kotlin/AnObject2.kt").matches(matcher -> {
269269
matcher.startsWith("// License Header 2012, 2014");
270270
});
271271
}

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changed
77
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+.
8+
* Added support for ktlint's FilenameRule ([#974](https://github.com/diffplug/spotless/pull/974)).
9+
810
### Fixed
911
* 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`.
1012
* [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).

testlib/src/main/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
@file:JvmName("SomeFileName")
33
package my.test
44

5-
object AnObject
5+
object AnObject2

0 commit comments

Comments
 (0)