diff --git a/CHANGES.md b/CHANGES.md index dbeb53a0a2..c0d6abde75 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * `FileSignature.Promised` and `JarState.Promised` to facilitate round-trip serialization for the Gradle configuration cache. ([#1945](https://github.com/diffplug/spotless/pull/1945)) * Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031)) ### Fixed +* Check if ktlint_code_style is set in .editorconfig before overriding it ([#2143](https://github.com/diffplug/spotless/issues/2143)) * Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) * Correctly provide EditorConfig property types for Ktlint ([#2052](https://github.com/diffplug/spotless/issues/2052)) * 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)) diff --git a/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java b/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java index 1790f9475c..a6e631718d 100644 --- a/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java +++ b/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java @@ -93,20 +93,21 @@ public String format( .flatMap(loader -> loader.get().getRuleProviders().stream()) .collect(Collectors.toUnmodifiableSet()); - EditorConfigOverride editorConfigOverride; - if (editorConfigOverrideMap.isEmpty()) { - editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE(); - } else { - editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map( - RuleProvider::createNewRuleInstance).collect(Collectors.toList()), - editorConfigOverrideMap); - } EditorConfigDefaults editorConfig; if (editorConfigPath == null || !Files.exists(editorConfigPath)) { editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(); } else { editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders)); } + EditorConfigOverride editorConfigOverride; + if (editorConfigOverrideMap.isEmpty()) { + editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE(); + } else { + editorConfigOverride = createEditorConfigOverride( + editorConfig, + allRuleProviders.stream().map(RuleProvider::createNewRuleInstance).collect(Collectors.toList()), + editorConfigOverrideMap); + } return new KtLintRuleEngine( allRuleProviders, @@ -120,7 +121,7 @@ public String format( /** * Create EditorConfigOverride from user provided parameters. */ - private static EditorConfigOverride createEditorConfigOverride(final List rules, Map editorConfigOverrideMap) { + private static EditorConfigOverride createEditorConfigOverride(final EditorConfigDefaults editorConfig, final List rules, Map editorConfigOverrideMap) { // Get properties from rules in the rule sets Stream> ruleProperties = rules.stream() .flatMap(rule -> rule.getUsesEditorConfigProperties().stream()); @@ -132,7 +133,9 @@ private static EditorConfigOverride createEditorConfigOverride(final List .collect(Collectors.toMap(EditorConfigProperty::getName, property -> property)); // The default style had been changed from intellij_idea to ktlint_official in version 1.0.0 - if (!editorConfigOverrideMap.containsKey("ktlint_code_style")) { + boolean isCodeStyleDefinedInEditorConfig = editorConfig.getValue().getSections().stream() + .anyMatch(section -> section.getProperties().containsKey("ktlint_code_style")); + if (!isCodeStyleDefinedInEditorConfig && !editorConfigOverrideMap.containsKey("ktlint_code_style")) { editorConfigOverrideMap.put("ktlint_code_style", "intellij_idea"); } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index bccb62dcb1..e8d0461798 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031)) * Add support for formatting and sorting Maven POMs ([#2082](https://github.com/diffplug/spotless/issues/2082)) ### Fixed +* Check if ktlint_code_style is set in .editorconfig before overriding it ([#2143](https://github.com/diffplug/spotless/issues/2143)) * 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)). * Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) * Correctly provide EditorConfig property types for Ktlint ([#2052](https://github.com/diffplug/spotless/issues/2052)) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index 1e96d34574..9720276e15 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -127,6 +127,25 @@ void testReadCodeStyleFromEditorConfigFile() throws IOException { checkKtlintOfficialStyle(); } + @Test + void testEditorConfigOverrideWithUnsetCodeStyleDoesNotOverrideEditorConfigCodeStyleWithDefault() throws IOException { + setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig"); + setFile("build.gradle").toLines( + "plugins {", + " id 'org.jetbrains.kotlin.jvm' version '1.6.21'", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlin {", + " ktlint().editorConfigOverride([", + " ktlint_test_key: true,", + " ])", + " }", + "}"); + checkKtlintOfficialStyle(); + } + @Test void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException { setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig"); diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 557e011ac4..1308c36768 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -7,6 +7,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031)) * Skip execution in M2E (incremental) builds by default ([#1814](https://github.com/diffplug/spotless/issues/1814), [#2037](https://github.com/diffplug/spotless/issues/2037)) ### Fixed +* Check if ktlint_code_style is set in .editorconfig before overriding it ([#2143](https://github.com/diffplug/spotless/issues/2143)) +* Default EditorConfig path to ".editorconfig" ([#2143](https://github.com/diffplug/spotless/issues/2143)) * Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) * Correctly provide EditorConfig property types for Ktlint ([#2052](https://github.com/diffplug/spotless/issues/2052)) * 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)) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java index 2459a52828..a54e0d9764 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.maven.kotlin; +import java.io.File; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -30,6 +31,8 @@ import com.diffplug.spotless.maven.FormatterStepFactory; public class Ktlint implements FormatterStepFactory { + private static final File defaultEditorConfig = new File(".editorconfig"); + @Parameter private String version; @Parameter @@ -43,6 +46,9 @@ public class Ktlint implements FormatterStepFactory { public FormatterStep newFormatterStep(final FormatterStepConfig stepConfig) { String ktlintVersion = version != null ? version : KtLintStep.defaultVersion(); FileSignature configPath = null; + if (editorConfigPath == null && defaultEditorConfig.exists()) { + editorConfigPath = defaultEditorConfig.getPath(); + } if (editorConfigPath != null) { configPath = ThrowingEx.get(() -> FileSignature.signAsList(stepConfig.getFileLocator().locateFile(editorConfigPath))); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java index fe5b0e1b57..5586335449 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,6 +55,17 @@ void testReadCodeStyleFromEditorConfigFile() throws Exception { checkKtlintOfficialStyle(); } + @Test + void testEditorConfigOverrideWithUnsetCodeStyleDoesNotOverrideEditorConfigCodeStyleWithDefault() throws Exception { + setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig"); + writePomWithKotlinSteps("\n" + + " \n" + + " true\n" + + " \n" + + ""); + checkKtlintOfficialStyle(); + } + @Test void testSetEditorConfigCanOverrideEditorConfigFile() throws Exception { setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");