Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if ktlint_code_style is set in .editorconfig before overriding it #2143

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -120,7 +121,7 @@ public String format(
/**
* Create EditorConfigOverride from user provided parameters.
*/
private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) {
private static EditorConfigOverride createEditorConfigOverride(final EditorConfigDefaults editorConfig, final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) {
// Get properties from rules in the rule sets
Stream<EditorConfigProperty<?>> ruleProperties = rules.stream()
.flatMap(rule -> rule.getUsesEditorConfigProperties().stream());
Expand All @@ -132,7 +133,9 @@ private static EditorConfigOverride createEditorConfigOverride(final List<Rule>
.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")) {
Comment on lines -135 to +138
Copy link
Member

@Goooler Goooler May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key point is here, I missed checking EditorConfig file properties in #1808.

editorConfigOverrideMap.put("ktlint_code_style", "intellij_idea");
}

Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, can you add a test case like 87537c0 for maven as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

While adding the test I realised that plugin-maven never defaulted EditorConfig's path to .editorconfig, so I fixed that too.

" ])",
" }",
"}");
checkKtlintOfficialStyle();
}

@Test
void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException {
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -55,6 +55,17 @@ void testReadCodeStyleFromEditorConfigFile() throws Exception {
checkKtlintOfficialStyle();
}

@Test
void testEditorConfigOverrideWithUnsetCodeStyleDoesNotOverrideEditorConfigCodeStyleWithDefault() throws Exception {
setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig");
writePomWithKotlinSteps("<ktlint>\n" +
" <editorConfigOverride>\n" +
" <ktlint_test_key>true</ktlint_test_key>\n" +
" </editorConfigOverride>\n" +
"</ktlint>");
checkKtlintOfficialStyle();
}

@Test
void testSetEditorConfigCanOverrideEditorConfigFile() throws Exception {
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");
Expand Down
Loading