Skip to content

Commit

Permalink
Support ktfmt v0.19+ with dropboxStyle() (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jan 2, 2021
2 parents 4002aff + 2c92684 commit f3457a9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Added support for git-submodule with `ratchetFrom` ([#746](https://github.com/diffplug/spotless/issues/746))
* Update default ktfmt from 0.16 to 0.18 ([#748](https://github.com/diffplug/spotless/issues/748))
* fix typo in javadoc comment for SQL\_FORMATTER\_INDENT\_TYPE ([#753](https://github.com/diffplug/spotless/pull/753))
### Fixed
* ktfmt v0.19+ with dropbox-style works again.

## [2.10.2] - 2020-11-16
### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -118,6 +118,12 @@ FormatterFunc createFormat() throws Exception {
}

private Object getDropboxStyleFormattingOptions(ClassLoader classLoader) throws Exception {
try {
// ktfmt v0.19 and later
return classLoader.loadClass(pkg + ".ktfmt.FormatterKt").getField("DROPBOX_FORMAT").get(null);
} catch (NoSuchFieldException ignored) {}

// fallback to old, pre-0.19 ktfmt interface.
Class<?> formattingOptionsCompanionClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions$Companion");
Object companion = formattingOptionsCompanionClazz.getConstructors()[0].newInstance((Object) null);
Method formattingOptionsMethod = formattingOptionsCompanionClazz.getDeclaredMethod(DROPBOX_STYLE_METHOD);
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Updated default eclipse-wtp from 4.17.0 to 4.18.0.
### Fixed
* `ratchetFrom` now works with git-submodule ([#746](https://github.com/diffplug/spotless/issues/746))
* ktfmt v0.19+ with dropbox-style works again.

## [5.8.2] - 2020-11-16
### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,6 +66,46 @@ public void integrationKtfmt() throws IOException {
assertFile("src/main/kotlin/basic.kt").sameAsResource("kotlin/ktfmt/basic.clean");
}

@Test
public void integrationKtfmt_dropboxStyle_0_18() throws IOException {
// ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
JreVersion.assume11OrGreater();
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktfmt('0.18').dropboxStyle()",
" }",
"}");
setFile("src/main/kotlin/basic.kt").toResource("kotlin/ktfmt/basic.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/kotlin/basic.kt").sameAsResource("kotlin/ktfmt/basic-dropboxstyle.clean");
}

@Test
public void integrationKtfmt_dropboxStyle_0_19() throws IOException {
// ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
JreVersion.assume11OrGreater();
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktfmt('0.19').dropboxStyle()",
" }",
"}");
setFile("src/main/kotlin/basic.kt").toResource("kotlin/ktfmt/basic.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/kotlin/basic.kt").sameAsResource("kotlin/ktfmt/basic-dropboxstyle.clean");
}

@Test
public void testWithIndentation() throws IOException {
setFile("build.gradle").toLines(
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
### Fixed
* `ratchetFrom` now works with git-submodule ([#746](https://github.com/diffplug/spotless/issues/746))
* Fix broken test for spotlessFiles parameter on windows ([#737](https://github.com/diffplug/spotless/pull/737))
* ktfmt v0.19+ with dropbox-style works again.

## [2.6.1] - 2020-11-16
### Fixed
Expand Down
13 changes: 13 additions & 0 deletions testlib/src/main/resources/kotlin/ktfmt/basic-dropboxstyle.clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import a.*
import a.b
import a.b.c.*
import kotlinx.android.synthetic.main.layout_name.*

fun main() {
fun name() {
a()
return b
}
println(";")
println()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,22 @@ public void behavior() throws Exception {
StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic.clean");
}

@Test
public void dropboxStyle_0_18() throws Exception {
// ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
JreVersion.assume11OrGreater();
FormatterStep step = KtfmtStep.create("0.18", TestProvisioner.mavenCentral(), KtfmtStep.Style.DROPBOX);
StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic-dropboxstyle.clean");
}

@Test
public void dropboxStyle_0_19() throws Exception {
// ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
JreVersion.assume11OrGreater();
FormatterStep step = KtfmtStep.create("0.19", TestProvisioner.mavenCentral(), KtfmtStep.Style.DROPBOX);
StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic-dropboxstyle.clean");
}

@Test
public void equality() throws Exception {
new SerializableEqualityTester() {
Expand Down

0 comments on commit f3457a9

Please sign in to comment.