-
Notifications
You must be signed in to change notification settings - Fork 455
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
Changes from 6 commits
cf6c76f
0be12f5
ccf141e
1453214
2effcbd
f33f8be
397a7fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
@@ -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,", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done! While adding the test I realised that |
||
" ])", | ||
" }", | ||
"}"); | ||
checkKtlintOfficialStyle(); | ||
} | ||
|
||
@Test | ||
void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException { | ||
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig"); | ||
|
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. | ||||||||||||||||||||||||||||||||||||
|
@@ -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; | ||||||||||||||||||||||||||||||||||||
|
@@ -43,6 +44,12 @@ public class Ktlint implements FormatterStepFactory { | |||||||||||||||||||||||||||||||||||
public FormatterStep newFormatterStep(final FormatterStepConfig stepConfig) { | ||||||||||||||||||||||||||||||||||||
String ktlintVersion = version != null ? version : KtLintStep.defaultVersion(); | ||||||||||||||||||||||||||||||||||||
FileSignature configPath = null; | ||||||||||||||||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Will load Lines 96 to 101 in cf6c76f
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 commentThe reason will be displayed to describe this comment to others. Learn more. You are right! However I believe we still need to load it ourselves as well in order to check if it sets spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/BaseKotlinExtension.java Lines 159 to 160 in add4c1e
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Quick shot! |
||||||||||||||||||||||||||||||||||||
if (editorConfigPath != null) { | ||||||||||||||||||||||||||||||||||||
configPath = ThrowingEx.get(() -> FileSignature.signAsList(stepConfig.getFileLocator().locateFile(editorConfigPath))); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
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.