Skip to content

Commit 4063e9f

Browse files
authored
Add support for KtLint 0.48.0 (#1432 fixes #1430)
2 parents 0b111f8 + b44d70d commit 4063e9f

File tree

8 files changed

+156
-5
lines changed

8 files changed

+156
-5
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Fixed
1414
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
15+
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))
16+
### Changes
17+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.0` ([#1432](https://github.com/diffplug/spotless/pull/1432))
1518

1619
## [2.31.0] - 2022-11-24
1720
### Added

lib/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ versionCompatibility {
3434
'0.45.2',
3535
'0.46.0',
3636
'0.47.0',
37+
'0.48.0',
3738
]
3839
targetSourceSetName = 'ktlint'
3940
}
@@ -90,6 +91,9 @@ dependencies {
9091
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.47.0'
9192
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.47.0'
9293
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.47.0'
94+
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.48.0'
95+
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0'
96+
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0'
9397

9498
String VER_SCALAFMT="3.6.1"
9599
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2023 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.ktlint.compat;
17+
18+
import java.util.LinkedHashSet;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Objects;
22+
import java.util.Set;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
25+
26+
import com.pinterest.ktlint.core.KtLint;
27+
import com.pinterest.ktlint.core.LintError;
28+
import com.pinterest.ktlint.core.Rule;
29+
import com.pinterest.ktlint.core.RuleProvider;
30+
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties;
31+
import com.pinterest.ktlint.core.api.EditorConfigDefaults;
32+
import com.pinterest.ktlint.core.api.EditorConfigOverride;
33+
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties;
34+
import com.pinterest.ktlint.core.api.editorconfig.EditorConfigProperty;
35+
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
36+
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;
37+
38+
import kotlin.Pair;
39+
import kotlin.Unit;
40+
import kotlin.jvm.functions.Function2;
41+
42+
public class KtLintCompat0Dot48Dot0Adapter implements KtLintCompatAdapter {
43+
44+
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
45+
@Override
46+
public Unit invoke(LintError lint, Boolean corrected) {
47+
if (!corrected) {
48+
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
49+
}
50+
return null;
51+
}
52+
}
53+
54+
@Override
55+
public String format(final String text, final String name, final boolean isScript,
56+
final boolean useExperimental,
57+
final Map<String, String> userData,
58+
final Map<String, Object> editorConfigOverrideMap) {
59+
final FormatterCallback formatterCallback = new FormatterCallback();
60+
61+
Set<RuleProvider> allRuleProviders = new LinkedHashSet<>(
62+
new StandardRuleSetProvider().getRuleProviders());
63+
if (useExperimental) {
64+
allRuleProviders.addAll(new ExperimentalRuleSetProvider().getRuleProviders());
65+
}
66+
67+
EditorConfigOverride editorConfigOverride;
68+
if (editorConfigOverrideMap.isEmpty()) {
69+
editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride();
70+
} else {
71+
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
72+
RuleProvider::createNewRuleInstance).collect(
73+
Collectors.toList()),
74+
editorConfigOverrideMap);
75+
}
76+
77+
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
78+
name,
79+
text,
80+
allRuleProviders,
81+
userData,
82+
formatterCallback,
83+
isScript,
84+
false,
85+
EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(),
86+
editorConfigOverride,
87+
false));
88+
}
89+
90+
/**
91+
* Create EditorConfigOverride from user provided parameters.
92+
*/
93+
private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) {
94+
// Get properties from rules in the rule sets
95+
Stream<EditorConfigProperty<?>> ruleProperties = rules.stream()
96+
.filter(rule -> rule instanceof UsesEditorConfigProperties)
97+
.flatMap(rule -> ((UsesEditorConfigProperties) rule).getEditorConfigProperties().stream());
98+
99+
// Create a mapping of properties to their names based on rule properties and default properties
100+
Map<String, EditorConfigProperty<?>> supportedProperties = Stream
101+
.concat(ruleProperties, DefaultEditorConfigProperties.INSTANCE.getEditorConfigProperties().stream())
102+
.distinct()
103+
.collect(Collectors.toMap(EditorConfigProperty::getName, property -> property));
104+
105+
// Create config properties based on provided property names and values
106+
@SuppressWarnings("unchecked")
107+
Pair<EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream()
108+
.map(entry -> {
109+
EditorConfigProperty<?> property = supportedProperties.get(entry.getKey());
110+
if (property != null) {
111+
return new Pair<>(property, entry.getValue());
112+
} else {
113+
return null;
114+
}
115+
})
116+
.filter(Objects::nonNull)
117+
.toArray(Pair[]::new);
118+
119+
return EditorConfigOverride.Companion.from(properties);
120+
}
121+
}

lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot45Dot2Adapter;
2828
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot46Dot0Adapter;
2929
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot47Dot0Adapter;
30+
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot48Dot0Adapter;
3031
import com.diffplug.spotless.glue.ktlint.compat.KtLintCompatAdapter;
3132

3233
public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
@@ -41,7 +42,10 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
4142
public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, Map<String, String> userData,
4243
Map<String, Object> editorConfigOverrideMap) {
4344
int minorVersion = Integer.parseInt(version.split("\\.")[1]);
44-
if (minorVersion >= 47) {
45+
if (minorVersion >= 48) {
46+
// ExperimentalParams lost two constructor arguments, EditorConfigProperty moved to its own class
47+
this.adapter = new KtLintCompat0Dot48Dot0Adapter();
48+
} else if (minorVersion == 47) {
4549
// rename RuleSet to RuleProvider
4650
this.adapter = new KtLintCompat0Dot47Dot0Adapter();
4751
} else if (minorVersion >= 46) {

lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ public class KtLintStep {
3333
// prevent direct instantiation
3434
private KtLintStep() {}
3535

36-
private static final String DEFAULT_VERSION = "0.47.1";
36+
private static final String DEFAULT_VERSION = "0.48.0";
3737
static final String NAME = "ktlint";
3838
static final String PACKAGE_PRE_0_32 = "com.github.shyiko";
3939
static final String PACKAGE = "com.pinterest";

plugin-gradle/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Fixed
77
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
8+
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))
9+
### Changes
10+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.0` ([#1432](https://github.com/diffplug/spotless/pull/1432))
811

912
## [6.12.0] - 2022-11-24
1013
### Added

plugin-maven/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
* Added support for M2E's incremental compilation ([#1414](https://github.com/diffplug/spotless/pull/1414) fixes [#1413](https://github.com/diffplug/spotless/issues/1413))
88
### Fixed
99
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
10+
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))
11+
### Changes
12+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.0` ([#1432](https://github.com/diffplug/spotless/pull/1432))
1013

1114
## [2.28.0] - 2022-11-24
1215
### Added

testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -144,6 +144,19 @@ void works0_47_1() throws Exception {
144144
});
145145
}
146146

147+
@Test
148+
void works0_48_0() throws Exception {
149+
FormatterStep step = KtLintStep.create("0.48.0", TestProvisioner.mavenCentral());
150+
StepHarness.forStep(step)
151+
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
152+
.testResourceException("kotlin/ktlint/unsolvable.dirty", assertion -> {
153+
assertion.isInstanceOf(AssertionError.class);
154+
assertion.hasMessage("Error on line: 1, column: 1\n" +
155+
"rule: no-wildcard-imports\n" +
156+
"Wildcard import");
157+
});
158+
}
159+
147160
@Test
148161
void equality() throws Exception {
149162
new SerializableEqualityTester() {

0 commit comments

Comments
 (0)