-
Notifications
You must be signed in to change notification settings - Fork 460
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
Check if ktlint_code_style is set in .editorconfig before overriding it #2143
Conversation
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 diffplug#2142.
This comment was marked as outdated.
This comment was marked as outdated.
I see it! Could you add a test case for this? Or just update spotless/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java Lines 130 to 147 in 5f840aa
|
Thanks for the quick reply! The PR already has this test: https://github.com/diffplug/spotless/pull/2143/files#diff-7bbe43d03149d8aad5b95564d57dc5bf59cba4ce539a44e01bbe51215dcdae71R131-R147. Is it sufficient, or is there a specific/additional scenario I am missing that you're after? |
Ooooops, will take a look later. |
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")) { |
There was a problem hiding this comment.
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.
"spotless {", | ||
" kotlin {", | ||
" ktlint().editorConfigOverride([", | ||
" ktlint_test_key: true,", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
The default path was never set, which means that .editorconfig was never picked up if not explicitly set.
This verifies that the previous fix which checks if ktlint_code_style is already defined in .editorconfig before overriding it works as expected in plugin-maven.
if (editorConfigPath == null) { | ||
File defaultEditorConfig = new File(".editorconfig"); | ||
if (defaultEditorConfig.exists()) { | ||
editorConfigPath = defaultEditorConfig.getPath(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will load EMPTY_EDITOR_CONFIG_DEFAULTS
before
Lines 96 to 101 in cf6c76f
EditorConfigDefaults editorConfig; | |
if (editorConfigPath == null || !Files.exists(editorConfigPath)) { | |
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(); | |
} else { | |
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders)); | |
} |
But this should parse the root .editorconfig, I forget the exact position, let me check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right! KtLintRuleEngine
will load the .editorconfig
.
However I believe we still need to load it ourselves as well in order to check if it sets ktlint_code_style
in KtLintCompat1Dot0Dot0Adapter
. Otherwise we will run into the original issue: ktlint_code_style
will be overridden to its default value (intellij_idea
) when the editorConfigOverride
map is non-empty but does not define it. This is also what plugin-gradle
does:
spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/BaseKotlinExtension.java
Lines 159 to 160 in add4c1e
File defaultEditorConfig = getProject().getRootProject().file(".editorconfig"); | |
FileSignature editorConfigPath = defaultEditorConfig.exists() ? FileSignature.signAsList(defaultEditorConfig) : null; |
Co-authored-by: Zongle Wang <wangzongler@gmail.com>
if (editorConfigPath == null) { | ||
File defaultEditorConfig = new File(".editorconfig"); | ||
if (defaultEditorConfig.exists()) { | ||
editorConfigPath = defaultEditorConfig.getPath(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (editorConfigPath == null) { | |
File defaultEditorConfig = new File(".editorconfig"); | |
if (defaultEditorConfig.exists()) { | |
editorConfigPath = defaultEditorConfig.getPath(); | |
} | |
} | |
if (editorConfigPath == null && defaultEditorConfig.exists()) { | |
editorConfigPath = defaultEditorConfig.getPath(); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And add
private static final File defaultEditorConfig = new File(".editorconfig");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick shot!
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.