-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear ktlint caches when
.editorconfig
changes
- Loading branch information
1 parent
08fdfac
commit a88d650
Showing
11 changed files
with
131 additions
and
53 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.jmailen.gradle.kotlinter.support | ||
|
||
import com.pinterest.ktlint.core.KtLint | ||
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties | ||
import com.pinterest.ktlint.core.api.EditorConfigOverride | ||
import org.gradle.api.file.ProjectLayout | ||
import org.gradle.api.logging.Logger | ||
import org.gradle.api.provider.Property | ||
import java.io.File | ||
|
||
internal fun editorConfigOverride(ktLintParams: KtLintParams): EditorConfigOverride { | ||
val rules = ktLintParams.disabledRules | ||
|
||
return if (rules.isEmpty()) { | ||
EditorConfigOverride.emptyEditorConfigOverride | ||
} else { | ||
EditorConfigOverride.from(DefaultEditorConfigProperties.disabledRulesProperty to rules.joinToString(separator = ",")) | ||
} | ||
} | ||
|
||
internal fun resetEditorconfigCacheIfNeeded( | ||
wasEditorConfigChanged: Property<Boolean>, | ||
logger: Logger, | ||
) { | ||
if (wasEditorConfigChanged.get()) { | ||
logger.info("Editorconfig changed, resetting KtLint caches") | ||
KtLint.trimMemory() // Calling trimMemory() will also reset internal loaded `.editorconfig` cache | ||
} | ||
} | ||
|
||
internal fun ProjectLayout.findApplicableEditorConfigFiles(): Sequence<File> { | ||
val projectEditorConfig = projectDirectory.file(".editorconfig").asFile | ||
|
||
return generateSequence(seed = projectEditorConfig) { editorconfig -> | ||
if (editorconfig.isRootEditorConfig) { | ||
null | ||
} else { | ||
editorconfig.parentFile?.parentFile?.resolve(".editorconfig") | ||
} | ||
} | ||
} | ||
|
||
private val File.isRootEditorConfig: Boolean | ||
get() { | ||
if (!isFile || !canRead()) return false | ||
|
||
return useLines { lines -> | ||
lines.any { line -> line.matches(editorConfigRootRegex) } | ||
} | ||
} | ||
|
||
/** | ||
* According to https://editorconfig.org/ root-most EditorConfig file contains line with `root=true` | ||
*/ | ||
private val editorConfigRootRegex = "^root\\s?=\\s?true".toRegex() |
15 changes: 0 additions & 15 deletions
15
src/main/kotlin/org/jmailen/gradle/kotlinter/support/KtLintEditoConfigOverrides.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/EditorConfigUtils.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters