Skip to content

Commit 9e7b390

Browse files
committed
Merge branch 'main' into feat/ktlint-editor-config
2 parents 0c79674 + 308c7cc commit 9e7b390

File tree

11 files changed

+151
-14
lines changed

11 files changed

+151
-14
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142)
15+
### Fixed
16+
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
1517
### Changes
1618
* Bump the dev version of Gradle from `7.5.1` to `7.6` ([#1409](https://github.com/diffplug/spotless/pull/1409))
1719
* We also removed the no-longer-required dependency `org.codehaus.groovy:groovy-xml`
@@ -20,6 +22,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
2022
* `StepHarness` now operates on `Formatter` rather than a `FormatterStep`
2123
* `StepHarnessWithFile` now takes a `ResourceHarness` in its constructor to handle the file manipulation parts
2224
* Standardized that we test exception *messages*, not types, which will ease the transition to linting later on
25+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.1` ([#1456](https://github.com/diffplug/spotless/pull/1456))
2326

2427
## [2.31.1] - 2023-01-02
2528
### Fixed

lib/build.gradle

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ dependencies {
4545
compileOnly 'org.slf4j:slf4j-api:2.0.0'
4646
// zero runtime reqs is a hard requirements for spotless-lib
4747
// if you need a dep, put it in lib-extra
48-
testImplementation "org.junit.jupiter:junit-jupiter:$VER_JUNIT"
49-
testImplementation "org.assertj:assertj-core:$VER_ASSERTJ"
50-
testImplementation "com.diffplug.durian:durian-testlib:$VER_DURIAN"
48+
testCommonImplementation "org.junit.jupiter:junit-jupiter:$VER_JUNIT"
49+
testCommonImplementation "org.assertj:assertj-core:$VER_ASSERTJ"
50+
testCommonImplementation "com.diffplug.durian:durian-testlib:$VER_DURIAN"
5151

5252
// used for pom sorting
5353
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0'
@@ -91,9 +91,9 @@ dependencies {
9191
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.47.0'
9292
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.47.0'
9393
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'
94+
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-core:0.48.0'
95+
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0'
96+
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0'
9797

9898
String VER_SCALAFMT="3.6.1"
9999
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT"
@@ -108,7 +108,9 @@ dependencies {
108108
// we'll hold the core lib to a high standard
109109
spotbugs { reportLevel = 'low' } // low|medium|high (low = sensitive to even minor mistakes)
110110

111-
test { useJUnitPlatform() }
111+
tasks.withType(Test).configureEach {
112+
useJUnitPlatform()
113+
}
112114

113115
jar {
114116
for (glue in NEEDS_GLUE) {

lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.util.ArrayList;
19+
import java.util.Collections;
1820
import java.nio.file.Path;
1921
import java.util.LinkedHashSet;
2022
import java.util.List;
@@ -28,11 +30,17 @@
2830
import com.pinterest.ktlint.core.LintError;
2931
import com.pinterest.ktlint.core.Rule;
3032
import com.pinterest.ktlint.core.RuleProvider;
31-
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties;
3233
import com.pinterest.ktlint.core.api.EditorConfigDefaults;
3334
import com.pinterest.ktlint.core.api.EditorConfigOverride;
3435
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties;
36+
import com.pinterest.ktlint.core.api.editorconfig.CodeStyleEditorConfigPropertyKt;
37+
import com.pinterest.ktlint.core.api.editorconfig.DisabledRulesEditorConfigPropertyKt;
3538
import com.pinterest.ktlint.core.api.editorconfig.EditorConfigProperty;
39+
import com.pinterest.ktlint.core.api.editorconfig.IndentSizeEditorConfigPropertyKt;
40+
import com.pinterest.ktlint.core.api.editorconfig.IndentStyleEditorConfigPropertyKt;
41+
import com.pinterest.ktlint.core.api.editorconfig.InsertFinalNewLineEditorConfigPropertyKt;
42+
import com.pinterest.ktlint.core.api.editorconfig.MaxLineLengthEditorConfigPropertyKt;
43+
import com.pinterest.ktlint.core.api.editorconfig.RuleExecutionEditorConfigPropertyKt;
3644
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
3745
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;
3846

@@ -42,13 +50,29 @@
4250

4351
public class KtLintCompat0Dot48Dot0Adapter implements KtLintCompatAdapter {
4452

53+
private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES;
54+
55+
static {
56+
List<EditorConfigProperty<?>> list = new ArrayList<>();
57+
list.add(CodeStyleEditorConfigPropertyKt.getCODE_STYLE_PROPERTY());
58+
//noinspection deprecation
59+
list.add(DisabledRulesEditorConfigPropertyKt.getDISABLED_RULES_PROPERTY());
60+
//noinspection KotlinInternalInJava,deprecation
61+
list.add(DisabledRulesEditorConfigPropertyKt.getKTLINT_DISABLED_RULES_PROPERTY());
62+
list.add(IndentStyleEditorConfigPropertyKt.getINDENT_STYLE_PROPERTY());
63+
list.add(IndentSizeEditorConfigPropertyKt.getINDENT_SIZE_PROPERTY());
64+
list.add(InsertFinalNewLineEditorConfigPropertyKt.getINSERT_FINAL_NEWLINE_PROPERTY());
65+
list.add(MaxLineLengthEditorConfigPropertyKt.getMAX_LINE_LENGTH_PROPERTY());
66+
DEFAULT_EDITOR_CONFIG_PROPERTIES = Collections.unmodifiableList(list);
67+
}
68+
4569
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
4670
@Override
4771
public Unit invoke(LintError lint, Boolean corrected) {
4872
if (!corrected) {
4973
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
5074
}
51-
return null;
75+
return Unit.INSTANCE;
5276
}
5377
}
5478

@@ -67,17 +91,23 @@ public String format(final String text, Path path, final boolean isScript,
6791

6892
EditorConfigOverride editorConfigOverride;
6993
if (editorConfigOverrideMap.isEmpty()) {
70-
editorConfigOverride = new EditorConfigOverride();
94+
editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE();
7195
} else {
7296
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
7397
RuleProvider::createNewRuleInstance).collect(
7498
Collectors.toList()),
7599
editorConfigOverrideMap);
76100
}
101+
EditorConfigDefaults editorConfig;
102+
if (editorConfigPath == null || !editorConfigPath.toFile().exists()) {
103+
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS();
104+
} else {
105+
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath);
106+
}
77107

78108
return new KtLintRuleEngine(
79109
allRuleProviders,
80-
EditorConfigDefaults.Companion.load(editorConfigPath),
110+
editorConfig,
81111
editorConfigOverride,
82112
false)
83113
.format(path, formatterCallback);
@@ -94,7 +124,7 @@ private static EditorConfigOverride createEditorConfigOverride(final List<Rule>
94124

95125
// Create a mapping of properties to their names based on rule properties and default properties
96126
Map<String, EditorConfigProperty<?>> supportedProperties = Stream
97-
.concat(ruleProperties, DefaultEditorConfigProperties.INSTANCE.getEditorConfigProperties().stream())
127+
.concat(ruleProperties, DEFAULT_EDITOR_CONFIG_PROPERTIES.stream())
98128
.distinct()
99129
.collect(Collectors.toMap(EditorConfigProperty::getName, property -> property));
100130

@@ -105,6 +135,18 @@ private static EditorConfigOverride createEditorConfigOverride(final List<Rule>
105135
EditorConfigProperty<?> property = supportedProperties.get(entry.getKey());
106136
if (property != null) {
107137
return new Pair<>(property, entry.getValue());
138+
} else if (entry.getKey().startsWith("ktlint_")) {
139+
String[] parts = entry.getKey().substring(7).split("_", 2);
140+
if (parts.length == 1) {
141+
// convert ktlint_{ruleset} to {ruleset}
142+
String qualifiedRuleId = parts[0];
143+
property = RuleExecutionEditorConfigPropertyKt.createRuleSetExecutionEditorConfigProperty(qualifiedRuleId);
144+
} else {
145+
// convert ktlint_{ruleset}_{rulename} to {ruleset}:{rulename}
146+
String qualifiedRuleId = parts[0] + ":" + parts[1];
147+
property = RuleExecutionEditorConfigPropertyKt.createRuleExecutionEditorConfigProperty(qualifiedRuleId);
148+
}
149+
return new Pair<>(property, entry.getValue());
108150
} else {
109151
return null;
110152
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class KtLintStep {
3636
// prevent direct instantiation
3737
private KtLintStep() {}
3838

39-
private static final String DEFAULT_VERSION = "0.48.0";
39+
private static final String DEFAULT_VERSION = "0.48.1";
4040
static final String NAME = "ktlint";
4141
static final String PACKAGE_PRE_0_32 = "com.github.shyiko";
4242
static final String PACKAGE = "com.pinterest";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.nio.charset.StandardCharsets;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.io.TempDir;
31+
32+
public class KtLintCompat0Dot48Dot0AdapterTest {
33+
@Test
34+
public void testDefaults(@TempDir Path path) throws IOException {
35+
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
36+
String text = loadAndWriteText(path, "empty_class_body.kt");
37+
38+
Map<String, String> userData = new HashMap<>();
39+
40+
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
41+
42+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, path, false, false, null, userData, editorConfigOverrideMap);
43+
assertEquals("class empty_class_body\n", formatted);
44+
}
45+
46+
@Test
47+
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
48+
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
49+
String text = loadAndWriteText(path, "fails_no_semicolons.kt");
50+
51+
Map<String, String> userData = new HashMap<>();
52+
53+
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
54+
editorConfigOverrideMap.put("indent_style", "tab");
55+
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");
56+
57+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, path, false, false, null, userData, editorConfigOverrideMap);
58+
assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted);
59+
}
60+
61+
private static String loadAndWriteText(Path path, String name) throws IOException {
62+
try (InputStream is = KtLintCompat0Dot48Dot0AdapterTest.class.getResourceAsStream("/" + name)) {
63+
Files.copy(is, path.resolve(name));
64+
}
65+
return new String(Files.readAllBytes(path.resolve(name)), StandardCharsets.UTF_8);
66+
}
67+
68+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class empty_class_body {
2+
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class fails_no_semicolons {
2+
val i = 0;
3+
}

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
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
* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142)
88
### Fixed
99
* Prevent tool configurations from being resolved outside project ([#1447](https://github.com/diffplug/spotless/pull/1447) fixes [#1215](https://github.com/diffplug/spotless/issues/1215))
10+
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
11+
### Changes
12+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.1` ([#1456](https://github.com/diffplug/spotless/pull/1456))
1013

1114
## [6.12.1] - 2023-01-02
1215
### Fixed

plugin-maven/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
66
### Added
77
* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142)
88
### Fixed
9+
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
10+
### Changes
11+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.1` ([#1456](https://github.com/diffplug/spotless/pull/1456))
912
* Reduce spurious invalidations of the up-to-date index file ([#1461](https://github.com/diffplug/spotless/pull/1461))
1013

1114
## [2.29.0] - 2023-01-02

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pluginManagement {
1616
// https://github.com/radarsh/gradle-test-logger-plugin/blob/develop/CHANGELOG.md
1717
id 'com.adarshr.test-logger' version '3.2.0'
1818
// https://github.com/davidburstrom/version-compatibility-gradle-plugin/tags
19-
id 'io.github.davidburstrom.version-compatibility' version '0.3.0'
19+
id 'io.github.davidburstrom.version-compatibility' version '0.4.0'
2020
}
2121
}
2222
plugins {

0 commit comments

Comments
 (0)