Skip to content

Commit 8f4bd1c

Browse files
committed
Implemented configuration options for Kotlin ktfmt formatter.
1 parent 1ce515b commit 8f4bd1c

File tree

14 files changed

+498
-44
lines changed

14 files changed

+498
-44
lines changed

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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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(@Nullable Integer maxWidth,
38+
@Nullable Integer blockIndent,
39+
@Nullable Integer continuationIndent,
40+
@Nullable Boolean removeUnusedImport) {
41+
this.maxWidth = maxWidth;
42+
this.blockIndent = blockIndent;
43+
this.continuationIndent = continuationIndent;
44+
this.removeUnusedImport = removeUnusedImport;
45+
}
46+
47+
@Nonnull
48+
public Optional<Integer> getMaxWidth() {
49+
return Optional.ofNullable(maxWidth);
50+
}
51+
52+
@Nonnull
53+
public Optional<Integer> getBlockIndent() {
54+
return Optional.ofNullable(blockIndent);
55+
}
56+
57+
@Nonnull
58+
public Optional<Integer> getContinuationIndent() {
59+
return Optional.ofNullable(continuationIndent);
60+
}
61+
62+
@Nonnull
63+
public Optional<Boolean> getRemoveUnusedImport() {
64+
return Optional.ofNullable(removeUnusedImport);
65+
}
66+
67+
public void setMaxWidth(int maxWidth) {
68+
if (maxWidth <= 0) {
69+
throw new IllegalArgumentException("Max width cannot be negative value or 0");
70+
}
71+
this.maxWidth = maxWidth;
72+
}
73+
74+
public void setBlockIndent(int blockIndent) {
75+
if (blockIndent < 0) {
76+
throw new IllegalArgumentException("Block indent cannot be negative value");
77+
}
78+
this.blockIndent = blockIndent;
79+
}
80+
81+
public void setContinuationIndent(int continuationIndent) {
82+
if (continuationIndent < 0) {
83+
throw new IllegalArgumentException("Continuation indent cannot be negative value");
84+
}
85+
this.continuationIndent = continuationIndent;
86+
}
87+
88+
public void setRemoveUnusedImport(boolean removeUnusedImport) {
89+
this.removeUnusedImport = removeUnusedImport;
90+
}
91+
}
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)