File tree 3 files changed +53
-0
lines changed
main/java/com/diffplug/gradle/spotless
test/java/com/diffplug/gradle/spotless
3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 3
3
We adhere to the [ keepachangelog] ( https://keepachangelog.com/en/1.0.0/ ) format (starting after version ` 3.27.0 ` ).
4
4
5
5
## [ Unreleased]
6
+ * Support for ktfmt in KotlinGradleExtension ([ #583 ] ( https://github.com/diffplug/spotless/pull/583 ) )
6
7
7
8
## [ 4.0.1] - 2020-05-21
8
9
### Fixed
Original file line number Diff line number Diff line change 22
22
import com .diffplug .common .collect .ImmutableSortedMap ;
23
23
import com .diffplug .spotless .FormatterStep ;
24
24
import com .diffplug .spotless .kotlin .KtLintStep ;
25
+ import com .diffplug .spotless .kotlin .KtfmtStep ;
25
26
26
27
public class KotlinGradleExtension extends FormatExtension {
27
28
private static final String GRADLE_KOTLIN_DSL_FILE_EXTENSION = "*.gradle.kts" ;
@@ -65,6 +66,33 @@ private FormatterStep createStep() {
65
66
}
66
67
}
67
68
69
+ /** Uses the [ktfmt](https://github.com/facebookincubator/ktfmt) jar to format source code. */
70
+ public KtfmtConfig ktfmt () {
71
+ return ktfmt (KtfmtStep .defaultVersion ());
72
+ }
73
+
74
+ /**
75
+ * Uses the given version of [ktfmt](https://github.com/facebookincubator/ktfmt) to format source
76
+ * code.
77
+ */
78
+ public KtfmtConfig ktfmt (String version ) {
79
+ Objects .requireNonNull (version );
80
+ return new KtfmtConfig (version );
81
+ }
82
+
83
+ public class KtfmtConfig {
84
+ final String version ;
85
+
86
+ KtfmtConfig (String version ) {
87
+ this .version = Objects .requireNonNull (version );
88
+ addStep (createStep ());
89
+ }
90
+
91
+ private FormatterStep createStep () {
92
+ return KtfmtStep .create (version , GradleProvisioner .fromProject (getProject ()));
93
+ }
94
+ }
95
+
68
96
@ Override
69
97
protected void setupTask (SpotlessTask task ) {
70
98
if (target == null ) {
Original file line number Diff line number Diff line change 22
22
import org .gradle .testkit .runner .BuildResult ;
23
23
import org .junit .Test ;
24
24
25
+ import com .diffplug .spotless .JreVersion ;
26
+
25
27
public class KotlinGradleExtensionTest extends GradleIntegrationTest {
26
28
@ Test
27
29
public void integration () throws IOException {
@@ -109,6 +111,28 @@ public void indentStep() throws IOException {
109
111
assertThat (result .getOutput ()).contains ("Unexpected indentation (4) (it should be 6)" );
110
112
}
111
113
114
+ @ Test
115
+ public void integration_ktfmt () throws IOException {
116
+ if (JreVersion .thisVm () == JreVersion ._8 ) {
117
+ // ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
118
+ return ;
119
+ }
120
+ setFile ("build.gradle" ).toLines (
121
+ "plugins {" ,
122
+ " id 'nebula.kotlin' version '1.0.6'" ,
123
+ " id 'com.diffplug.gradle.spotless'" ,
124
+ "}" ,
125
+ "repositories { mavenCentral() }" ,
126
+ "spotless {" ,
127
+ " kotlinGradle {" ,
128
+ " ktfmt()" ,
129
+ " }" ,
130
+ "}" );
131
+ setFile ("configuration.gradle.kts" ).toResource ("kotlin/ktfmt/basic.dirty" );
132
+ gradleRunner ().withArguments ("spotlessApply" ).build ();
133
+ assertFile ("configuration.gradle.kts" ).sameAsResource ("kotlin/ktfmt/basic.clean" );
134
+ }
135
+
112
136
@ Test
113
137
public void integration_lint_script_files_without_top_level_declaration () throws IOException {
114
138
setFile ("build.gradle" ).toLines (
You can’t perform that action at this time.
0 commit comments