Skip to content

Commit 878b67b

Browse files
authored
Implemented max lines width changing support for Kotlin ktfmt formatter for Gradle plugin. (#1145)
2 parents 1ce515b + fb62f36 commit 878b67b

File tree

17 files changed

+502
-44
lines changed

17 files changed

+502
-44
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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+
* Added support for setting custom parameters for Kotlin ktfmt in Gradle and Maven plugins. ([#1145](https://github.com/diffplug/spotless/pull/1145))
1314

1415
## [2.23.0] - 2022-02-15
1516
### Added

lib/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ apply from: rootProject.file('gradle/java-publish.gradle')
99
def NEEDS_GLUE = [
1010
'sortPom',
1111
'palantirJavaFormat',
12+
'ktfmt',
1213
'ktlint',
1314
'flexmark'
1415
]
@@ -34,6 +35,15 @@ dependencies {
3435

3536
palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0'
3637

38+
String VER_KTFMT = '0.34'
39+
ktfmtCompileOnly "com.facebook:ktfmt:$VER_KTFMT"
40+
String VER_KTLINT_GOOGLE_JAVA_FORMAT = '1.7' // for JDK 8 compatibility
41+
ktfmtCompileOnly("com.google.googlejavaformat:google-java-format") {
42+
version {
43+
strictly VER_KTLINT_GOOGLE_JAVA_FORMAT
44+
}
45+
}
46+
3747
String VER_KTLINT='0.43.2'
3848
ktlintCompileOnly "com.pinterest:ktlint:$VER_KTLINT"
3949
ktlintCompileOnly "com.pinterest.ktlint:ktlint-core:$VER_KTLINT"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktfmt;
17+
18+
import javax.annotation.Nonnull;
19+
import javax.annotation.Nullable;
20+
21+
import com.facebook.ktfmt.format.Formatter;
22+
import com.facebook.ktfmt.format.FormattingOptions;
23+
24+
import com.diffplug.spotless.FormatterFunc;
25+
26+
public final class KtfmtFormatterFunc implements FormatterFunc {
27+
28+
@Nonnull
29+
private final KtfmtStyle style;
30+
31+
@Nullable
32+
private final KtfmtFormattingOptions ktfmtFormattingOptions;
33+
34+
public KtfmtFormatterFunc() {
35+
this(KtfmtStyle.DEFAULT, null);
36+
}
37+
38+
public KtfmtFormatterFunc(@Nonnull KtfmtStyle style) {
39+
this(style, null);
40+
}
41+
42+
public KtfmtFormatterFunc(@Nullable KtfmtFormattingOptions ktfmtFormattingOptions) {
43+
this(KtfmtStyle.DEFAULT, ktfmtFormattingOptions);
44+
}
45+
46+
public KtfmtFormatterFunc(@Nonnull KtfmtStyle style, @Nullable KtfmtFormattingOptions ktfmtFormattingOptions) {
47+
this.style = style;
48+
this.ktfmtFormattingOptions = ktfmtFormattingOptions;
49+
}
50+
51+
@Nonnull
52+
@Override
53+
public String apply(@Nonnull String input) throws Exception {
54+
return Formatter.format(createFormattingOptions(), input);
55+
}
56+
57+
private FormattingOptions createFormattingOptions() {
58+
FormattingOptions formattingOptions;
59+
switch (style) {
60+
case DEFAULT:
61+
formattingOptions = new FormattingOptions();
62+
break;
63+
case DROPBOX:
64+
formattingOptions = Formatter.DROPBOX_FORMAT;
65+
break;
66+
case GOOGLE:
67+
formattingOptions = Formatter.GOOGLE_FORMAT;
68+
break;
69+
case KOTLIN_LANG:
70+
formattingOptions = Formatter.KOTLINLANG_FORMAT;
71+
break;
72+
default:
73+
throw new IllegalStateException("Unknown formatting option");
74+
}
75+
76+
if (ktfmtFormattingOptions != null) {
77+
formattingOptions = formattingOptions.copy(
78+
formattingOptions.getStyle(),
79+
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
80+
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
81+
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getBlockIndent()),
82+
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
83+
formattingOptions.getDebuggingPrintOpsAfterFormatting());
84+
}
85+
86+
return formattingOptions;
87+
}
88+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktfmt;
17+
18+
import java.util.Optional;
19+
20+
import javax.annotation.Nonnull;
21+
import javax.annotation.Nullable;
22+
23+
public final class KtfmtFormattingOptions {
24+
25+
@Nullable
26+
private Integer maxWidth;
27+
28+
@Nullable
29+
private Integer blockIndent;
30+
31+
@Nullable
32+
private Integer continuationIndent;
33+
34+
@Nullable
35+
private Boolean removeUnusedImport;
36+
37+
public KtfmtFormattingOptions(
38+
@Nullable Integer maxWidth,
39+
@Nullable Integer blockIndent,
40+
@Nullable Integer continuationIndent,
41+
@Nullable Boolean removeUnusedImport) {
42+
this.maxWidth = maxWidth;
43+
this.blockIndent = blockIndent;
44+
this.continuationIndent = continuationIndent;
45+
this.removeUnusedImport = removeUnusedImport;
46+
}
47+
48+
@Nonnull
49+
public Optional<Integer> getMaxWidth() {
50+
return Optional.ofNullable(maxWidth);
51+
}
52+
53+
@Nonnull
54+
public Optional<Integer> getBlockIndent() {
55+
return Optional.ofNullable(blockIndent);
56+
}
57+
58+
@Nonnull
59+
public Optional<Integer> getContinuationIndent() {
60+
return Optional.ofNullable(continuationIndent);
61+
}
62+
63+
@Nonnull
64+
public Optional<Boolean> getRemoveUnusedImport() {
65+
return Optional.ofNullable(removeUnusedImport);
66+
}
67+
68+
public void setMaxWidth(int maxWidth) {
69+
if (maxWidth <= 0) {
70+
throw new IllegalArgumentException("Max width cannot be negative value or 0");
71+
}
72+
this.maxWidth = maxWidth;
73+
}
74+
75+
public void setBlockIndent(int blockIndent) {
76+
if (blockIndent < 0) {
77+
throw new IllegalArgumentException("Block indent cannot be negative value");
78+
}
79+
this.blockIndent = blockIndent;
80+
}
81+
82+
public void setContinuationIndent(int continuationIndent) {
83+
if (continuationIndent < 0) {
84+
throw new IllegalArgumentException("Continuation indent cannot be negative value");
85+
}
86+
this.continuationIndent = continuationIndent;
87+
}
88+
89+
public void setRemoveUnusedImport(boolean removeUnusedImport) {
90+
this.removeUnusedImport = removeUnusedImport;
91+
}
92+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktfmt;
17+
18+
public enum KtfmtStyle {
19+
DEFAULT, DROPBOX, GOOGLE, KOTLIN_LANG
20+
}

0 commit comments

Comments
 (0)