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 |
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.
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.
Nice, can you add a test case like 87537c0 for maven as well?
There was a problem hiding this comment.
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.
Will load EMPTY_EDITOR_CONFIG_DEFAULTS before
But this should parse the root .editorconfig, I forget the exact position, let me check.
There was a problem hiding this comment.
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:
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.
| 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.
And add
private static final File defaultEditorConfig = new File(".editorconfig");
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.