Skip to content

Commit

Permalink
Fixed Ktfmt options usage in Gradle Kotlin (kts) scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
DDeg committed Mar 29, 2022
1 parent f1281c9 commit 66e1a17
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
16 changes: 16 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ public KtfmtFormattingOptions(
this.continuationIndent = continuationIndent;
this.removeUnusedImport = removeUnusedImport;
}

public void setMaxWidth(int maxWidth) {
this.maxWidth = maxWidth;
}

public void setBlockIndent(int blockIndent) {
this.blockIndent = blockIndent;
}

public void setContinuationIndent(int continuationIndent) {
this.continuationIndent = continuationIndent;
}

public void setRemoveUnusedImport(boolean removeUnusedImport) {
this.removeUnusedImport = removeUnusedImport;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ private FormatterStep createStep() {
return KtfmtStep.create(version, provisioner(), style, options);
}

class ConfigurableStyle {
public class ConfigurableStyle {

void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
public void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
KtfmtFormattingOptions ktfmtFormattingOptions = new KtfmtFormattingOptions();
optionsConfiguration.accept(ktfmtFormattingOptions);
options = ktfmtFormattingOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ private FormatterStep createStep() {
return KtfmtStep.create(version, provisioner(), style, options);
}

class ConfigurableStyle {
public class ConfigurableStyle {

void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
public void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
KtfmtFormattingOptions ktfmtFormattingOptions = new KtfmtFormattingOptions();
optionsConfiguration.accept(ktfmtFormattingOptions);
options = ktfmtFormattingOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,28 @@ void testWithCustomMaxWidthDefaultStyleKtfmt() throws IOException {
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width.clean");
}

@Test
@EnabledForJreRange(min = JAVA_11) // ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
void testWithCustomMaxWidthDefaultStyleKtfmtGradleKts() throws IOException {
setFile("build.gradle.kts").toLines(
"plugins {",
" id(\"org.jetbrains.kotlin.jvm\") version \"1.5.31\"",
" id(\"com.diffplug.spotless\")",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktfmt().configure { options ->",
" options.setMaxWidth(120)",
" }",
" }",
"}");

setFile("src/main/kotlin/max-width.kt").toResource("kotlin/ktfmt/max-width.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width.clean");
}

@Test
@EnabledForJreRange(min = JAVA_11) // ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
void testWithCustomMaxWidthDropboxStyleKtfmt() throws IOException {
Expand All @@ -313,4 +335,26 @@ void testWithCustomMaxWidthDropboxStyleKtfmt() throws IOException {
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width-dropbox.clean");
}

@Test
@EnabledForJreRange(min = JAVA_11) // ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
void testWithCustomMaxWidthDropboxStyleKtfmtGradleKts() throws IOException {
setFile("build.gradle.kts").toLines(
"plugins {",
" id(\"org.jetbrains.kotlin.jvm\") version \"1.5.31\"",
" id(\"com.diffplug.spotless\")",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktfmt().dropboxStyle().configure { options ->",
" options.setMaxWidth(120)",
" }",
" }",
"}");

setFile("src/main/kotlin/max-width.kt").toResource("kotlin/ktfmt/max-width.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width-dropbox.clean");
}
}

0 comments on commit 66e1a17

Please sign in to comment.