Skip to content

Commit

Permalink
Check if ktlint_code_style is set in .editorconfig before overriding it
Browse files Browse the repository at this point in the history
ktlint_code_style gets unconditionally overridden to its default value
(intellij_idea) when the editorConfigOverride map is non-empty but does
not define it. As a result, it gets overridden even if it's actually
defined in the .editorconfig file.

This change checks if ktlint_code_style is already defined in
.editorconfig before overriding it to its default value.

Fixes #2142.
  • Loading branch information
OlivierGenez committed May 28, 2024
1 parent 1e8fc34 commit cf6c76f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
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")) {
editorConfigOverrideMap.put("ktlint_code_style", "intellij_idea");
}

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,",
" ])",
" }",
"}");
checkKtlintOfficialStyle();
}

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

0 comments on commit cf6c76f

Please sign in to comment.