Skip to content

Commit cb8b6e5

Browse files
authored
Ktfmt custom options fix for Gradle Kotlin scripts (kts) (#1155)
2 parents b610a46 + 07c9ac8 commit cb8b6e5

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* Fixed access modifiers for setters in KtfmtStep configuration
1315

1416
## [2.24.0] - 2022-03-28
1517
### Added

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ public KtfmtFormattingOptions(
9797
this.continuationIndent = continuationIndent;
9898
this.removeUnusedImport = removeUnusedImport;
9999
}
100+
101+
public void setMaxWidth(int maxWidth) {
102+
this.maxWidth = maxWidth;
103+
}
104+
105+
public void setBlockIndent(int blockIndent) {
106+
this.blockIndent = blockIndent;
107+
}
108+
109+
public void setContinuationIndent(int continuationIndent) {
110+
this.continuationIndent = continuationIndent;
111+
}
112+
113+
public void setRemoveUnusedImport(boolean removeUnusedImport) {
114+
this.removeUnusedImport = removeUnusedImport;
115+
}
100116
}
101117

102118
/**

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Fixed ktfmt options configuration in Gradle plugin for Gradle Kotlin scripts (kts).
68

79
## [6.4.0] - 2022-03-28
810
### Added

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ private FormatterStep createStep() {
141141
return KtfmtStep.create(version, provisioner(), style, options);
142142
}
143143

144-
class ConfigurableStyle {
144+
public class ConfigurableStyle {
145145

146-
void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
146+
public void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
147147
KtfmtFormattingOptions ktfmtFormattingOptions = new KtfmtFormattingOptions();
148148
optionsConfiguration.accept(ktfmtFormattingOptions);
149149
options = ktfmtFormattingOptions;

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ private FormatterStep createStep() {
128128
return KtfmtStep.create(version, provisioner(), style, options);
129129
}
130130

131-
class ConfigurableStyle {
131+
public class ConfigurableStyle {
132132

133-
void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
133+
public void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
134134
KtfmtFormattingOptions ktfmtFormattingOptions = new KtfmtFormattingOptions();
135135
optionsConfiguration.accept(ktfmtFormattingOptions);
136136
options = ktfmtFormattingOptions;

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,28 @@ void testWithCustomMaxWidthDefaultStyleKtfmt() throws IOException {
292292
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width.clean");
293293
}
294294

295+
@Test
296+
@EnabledForJreRange(min = JAVA_11) // ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
297+
void testWithCustomMaxWidthDefaultStyleKtfmtGradleKts() throws IOException {
298+
setFile("build.gradle.kts").toLines(
299+
"plugins {",
300+
" id(\"org.jetbrains.kotlin.jvm\") version \"1.5.31\"",
301+
" id(\"com.diffplug.spotless\")",
302+
"}",
303+
"repositories { mavenCentral() }",
304+
"spotless {",
305+
" kotlin {",
306+
" ktfmt().configure { options ->",
307+
" options.setMaxWidth(120)",
308+
" }",
309+
" }",
310+
"}");
311+
312+
setFile("src/main/kotlin/max-width.kt").toResource("kotlin/ktfmt/max-width.dirty");
313+
gradleRunner().withArguments("spotlessApply").build();
314+
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width.clean");
315+
}
316+
295317
@Test
296318
@EnabledForJreRange(min = JAVA_11) // ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
297319
void testWithCustomMaxWidthDropboxStyleKtfmt() throws IOException {
@@ -313,4 +335,26 @@ void testWithCustomMaxWidthDropboxStyleKtfmt() throws IOException {
313335
gradleRunner().withArguments("spotlessApply").build();
314336
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width-dropbox.clean");
315337
}
338+
339+
@Test
340+
@EnabledForJreRange(min = JAVA_11) // ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
341+
void testWithCustomMaxWidthDropboxStyleKtfmtGradleKts() throws IOException {
342+
setFile("build.gradle.kts").toLines(
343+
"plugins {",
344+
" id(\"org.jetbrains.kotlin.jvm\") version \"1.5.31\"",
345+
" id(\"com.diffplug.spotless\")",
346+
"}",
347+
"repositories { mavenCentral() }",
348+
"spotless {",
349+
" kotlin {",
350+
" ktfmt().dropboxStyle().configure { options ->",
351+
" options.setMaxWidth(120)",
352+
" }",
353+
" }",
354+
"}");
355+
356+
setFile("src/main/kotlin/max-width.kt").toResource("kotlin/ktfmt/max-width.dirty");
357+
gradleRunner().withArguments("spotlessApply").build();
358+
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width-dropbox.clean");
359+
}
316360
}

0 commit comments

Comments
 (0)