Skip to content

Commit 3308d33

Browse files
authored
Check if ktlint_code_style is set in .editorconfig before overriding it (#2143)
2 parents d95c2eb + 397a7fd commit 3308d33

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* `FileSignature.Promised` and `JarState.Promised` to facilitate round-trip serialization for the Gradle configuration cache. ([#1945](https://github.com/diffplug/spotless/pull/1945))
1515
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
1616
### Fixed
17+
* Check if ktlint_code_style is set in .editorconfig before overriding it ([#2143](https://github.com/diffplug/spotless/issues/2143))
1718
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
1819
* Correctly provide EditorConfig property types for Ktlint ([#2052](https://github.com/diffplug/spotless/issues/2052))
1920
* Made ShadowCopy (`npmInstallCache`) more robust by re-creating the cache dir if it goes missing ([#1984](https://github.com/diffplug/spotless/issues/1984),[2096](https://github.com/diffplug/spotless/pull/2096))

lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,21 @@ public String format(
9393
.flatMap(loader -> loader.get().getRuleProviders().stream())
9494
.collect(Collectors.toUnmodifiableSet());
9595

96-
EditorConfigOverride editorConfigOverride;
97-
if (editorConfigOverrideMap.isEmpty()) {
98-
editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE();
99-
} else {
100-
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
101-
RuleProvider::createNewRuleInstance).collect(Collectors.toList()),
102-
editorConfigOverrideMap);
103-
}
10496
EditorConfigDefaults editorConfig;
10597
if (editorConfigPath == null || !Files.exists(editorConfigPath)) {
10698
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS();
10799
} else {
108100
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
109101
}
102+
EditorConfigOverride editorConfigOverride;
103+
if (editorConfigOverrideMap.isEmpty()) {
104+
editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE();
105+
} else {
106+
editorConfigOverride = createEditorConfigOverride(
107+
editorConfig,
108+
allRuleProviders.stream().map(RuleProvider::createNewRuleInstance).collect(Collectors.toList()),
109+
editorConfigOverrideMap);
110+
}
110111

111112
return new KtLintRuleEngine(
112113
allRuleProviders,
@@ -120,7 +121,7 @@ public String format(
120121
/**
121122
* Create EditorConfigOverride from user provided parameters.
122123
*/
123-
private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) {
124+
private static EditorConfigOverride createEditorConfigOverride(final EditorConfigDefaults editorConfig, final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) {
124125
// Get properties from rules in the rule sets
125126
Stream<EditorConfigProperty<?>> ruleProperties = rules.stream()
126127
.flatMap(rule -> rule.getUsesEditorConfigProperties().stream());
@@ -132,7 +133,9 @@ private static EditorConfigOverride createEditorConfigOverride(final List<Rule>
132133
.collect(Collectors.toMap(EditorConfigProperty::getName, property -> property));
133134

134135
// The default style had been changed from intellij_idea to ktlint_official in version 1.0.0
135-
if (!editorConfigOverrideMap.containsKey("ktlint_code_style")) {
136+
boolean isCodeStyleDefinedInEditorConfig = editorConfig.getValue().getSections().stream()
137+
.anyMatch(section -> section.getProperties().containsKey("ktlint_code_style"));
138+
if (!isCodeStyleDefinedInEditorConfig && !editorConfigOverrideMap.containsKey("ktlint_code_style")) {
136139
editorConfigOverrideMap.put("ktlint_code_style", "intellij_idea");
137140
}
138141

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
99
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
1010
* Add support for formatting and sorting Maven POMs ([#2082](https://github.com/diffplug/spotless/issues/2082))
1111
### Fixed
12+
* Check if ktlint_code_style is set in .editorconfig before overriding it ([#2143](https://github.com/diffplug/spotless/issues/2143))
1213
* Full no-asterisk support for configuration cache ([#2088](https://github.com/diffplug/spotless/pull/2088) closes [#1274](https://github.com/diffplug/spotless/issues/1274) and [#987](https://github.com/diffplug/spotless/issues/987)).
1314
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
1415
* Correctly provide EditorConfig property types for Ktlint ([#2052](https://github.com/diffplug/spotless/issues/2052))

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 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.
@@ -127,6 +127,25 @@ void testReadCodeStyleFromEditorConfigFile() throws IOException {
127127
checkKtlintOfficialStyle();
128128
}
129129

130+
@Test
131+
void testEditorConfigOverrideWithUnsetCodeStyleDoesNotOverrideEditorConfigCodeStyleWithDefault() throws IOException {
132+
setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig");
133+
setFile("build.gradle").toLines(
134+
"plugins {",
135+
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
136+
" id 'com.diffplug.spotless'",
137+
"}",
138+
"repositories { mavenCentral() }",
139+
"spotless {",
140+
" kotlin {",
141+
" ktlint().editorConfigOverride([",
142+
" ktlint_test_key: true,",
143+
" ])",
144+
" }",
145+
"}");
146+
checkKtlintOfficialStyle();
147+
}
148+
130149
@Test
131150
void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException {
132151
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
88
* Skip execution in M2E (incremental) builds by default ([#1814](https://github.com/diffplug/spotless/issues/1814), [#2037](https://github.com/diffplug/spotless/issues/2037))
99
### Fixed
10+
* Check if ktlint_code_style is set in .editorconfig before overriding it ([#2143](https://github.com/diffplug/spotless/issues/2143))
11+
* Default EditorConfig path to ".editorconfig" ([#2143](https://github.com/diffplug/spotless/issues/2143))
1012
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
1113
* Correctly provide EditorConfig property types for Ktlint ([#2052](https://github.com/diffplug/spotless/issues/2052))
1214
* Made ShadowCopy (`npmInstallCache`) more robust by re-creating the cache dir if it goes missing ([#1984](https://github.com/diffplug/spotless/issues/1984),[2096](https://github.com/diffplug/spotless/pull/2096))

plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 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.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.maven.kotlin;
1717

18+
import java.io.File;
1819
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.List;
@@ -30,6 +31,8 @@
3031
import com.diffplug.spotless.maven.FormatterStepFactory;
3132

3233
public class Ktlint implements FormatterStepFactory {
34+
private static final File defaultEditorConfig = new File(".editorconfig");
35+
3336
@Parameter
3437
private String version;
3538
@Parameter
@@ -43,6 +46,9 @@ public class Ktlint implements FormatterStepFactory {
4346
public FormatterStep newFormatterStep(final FormatterStepConfig stepConfig) {
4447
String ktlintVersion = version != null ? version : KtLintStep.defaultVersion();
4548
FileSignature configPath = null;
49+
if (editorConfigPath == null && defaultEditorConfig.exists()) {
50+
editorConfigPath = defaultEditorConfig.getPath();
51+
}
4652
if (editorConfigPath != null) {
4753
configPath = ThrowingEx.get(() -> FileSignature.signAsList(stepConfig.getFileLocator().locateFile(editorConfigPath)));
4854
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 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.
@@ -55,6 +55,17 @@ void testReadCodeStyleFromEditorConfigFile() throws Exception {
5555
checkKtlintOfficialStyle();
5656
}
5757

58+
@Test
59+
void testEditorConfigOverrideWithUnsetCodeStyleDoesNotOverrideEditorConfigCodeStyleWithDefault() throws Exception {
60+
setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig");
61+
writePomWithKotlinSteps("<ktlint>\n" +
62+
" <editorConfigOverride>\n" +
63+
" <ktlint_test_key>true</ktlint_test_key>\n" +
64+
" </editorConfigOverride>\n" +
65+
"</ktlint>");
66+
checkKtlintOfficialStyle();
67+
}
68+
5869
@Test
5970
void testSetEditorConfigCanOverrideEditorConfigFile() throws Exception {
6071
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");

0 commit comments

Comments
 (0)