Skip to content

Commit

Permalink
Switch to new ktlint 0.37.0 reloadEditorConfigFile api
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Sep 8, 2022
1 parent 56f0069 commit dcf068b
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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.ConfigurableFileCollection
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 {
Expand All @@ -19,12 +19,13 @@ internal fun editorConfigOverride(ktLintParams: KtLintParams): EditorConfigOverr
}

internal fun resetEditorconfigCacheIfNeeded(
wasEditorConfigChanged: Property<Boolean>,
changedEditorconfigFiles: ConfigurableFileCollection,
logger: Logger,
) {
if (wasEditorConfigChanged.get()) {
val changedFiles = changedEditorconfigFiles.files
if (changedFiles.any()) {
logger.info("Editorconfig changed, resetting KtLint caches")
KtLint.trimMemory() // Calling trimMemory() will also reset internal loaded `.editorconfig` cache
changedFiles.map(File::toPath).forEach(KtLint::reloadEditorConfigFile)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SourceTask
import org.gradle.internal.exceptions.MultiCauseException
import org.gradle.work.FileChange
import org.gradle.work.Incremental
import org.gradle.work.InputChanges
import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_DISABLED_RULES
Expand Down Expand Up @@ -44,8 +45,12 @@ abstract class ConfigurableKtLintTask(
disabledRules = disabledRules.get(),
)

protected fun wasEditorconfigChanged(inputChanges: InputChanges) =
inputChanges.isIncremental && inputChanges.getFileChanges(editorconfigFiles).any()
protected fun getChangedEditorconfigFiles(inputChanges: InputChanges) =
if (inputChanges.isIncremental) {
inputChanges.getFileChanges(editorconfigFiles).map(FileChange::getFile)
} else {
emptyList()
}
}

internal inline fun <reified T> ObjectFactory.property(default: T? = null): Property<T> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ open class FormatTask @Inject constructor(
p.projectDirectory.set(projectLayout.projectDirectory.asFile)
p.ktLintParams.set(getKtLintParams())
p.output.set(report)
p.wasEditorConfigChanged.set(wasEditorconfigChanged(inputChanges))
p.changedEditorConfigFiles.from(getChangedEditorconfigFiles(inputChanges))
}
runCatching { await() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ open class LintTask @Inject constructor(
p.projectDirectory.set(projectLayout.projectDirectory.asFile)
p.reporters.putAll(reports)
p.ktLintParams.set(getKtLintParams())
p.wasEditorConfigChanged.set(wasEditorconfigChanged(inputChanges))
p.changedEditorconfigFiles.from(getChangedEditorconfigFiles(inputChanges))
}
runCatching { await() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class FormatWorkerAction : WorkAction<FormatWorkerParameters> {

override fun execute() {
resetEditorconfigCacheIfNeeded(
wasEditorConfigChanged = parameters.wasEditorConfigChanged,
changedEditorconfigFiles = parameters.changedEditorConfigFiles,
logger = logger,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.jmailen.gradle.kotlinter.support.KtLintParams

interface FormatWorkerParameters : WorkParameters {
val name: Property<String>
val wasEditorConfigChanged: Property<Boolean>
val changedEditorConfigFiles: ConfigurableFileCollection
val files: ConfigurableFileCollection
val projectDirectory: RegularFileProperty
val ktLintParams: Property<KtLintParams>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class LintWorkerAction : WorkAction<LintWorkerParameters> {

override fun execute() {
resetEditorconfigCacheIfNeeded(
wasEditorConfigChanged = parameters.wasEditorConfigChanged,
changedEditorconfigFiles = parameters.changedEditorconfigFiles,
logger = logger,
)
var hasError = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.io.File

interface LintWorkerParameters : WorkParameters {
val name: Property<String>
val wasEditorConfigChanged: Property<Boolean>
val changedEditorconfigFiles: ConfigurableFileCollection
val files: ConfigurableFileCollection
val projectDirectory: RegularFileProperty
val reporters: MapProperty<String, File>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() {
fun `plugin respects disabled_rules set in editorconfig`() {
projectRoot.resolve(".editorconfig") {
appendText(
// language=editorconfig
"""
[*.{kt,kts}]
disabled_rules=filename
ktlint_disabled_rules = filename
""".trimIndent(),
)
}
Expand All @@ -76,6 +77,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() {
fun `plugin respects 'indent_size' set in editorconfig`() {
projectRoot.resolve(".editorconfig") {
appendText(
// language=editorconfig
"""
[*.{kt,kts}]
indent_size = 6
Expand Down Expand Up @@ -106,9 +108,10 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() {
fun `editorconfig changes are taken into account on lint task re-runs`() {
projectRoot.resolve(".editorconfig") {
writeText(
// language=editorconfig
"""
[*.{kt,kts}]
disabled_rules=filename
ktlint_disabled_rules = filename
""".trimIndent(),
)
}
Expand Down Expand Up @@ -158,9 +161,10 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() {

projectRoot.resolve(".editorconfig") {
writeText(
// language=editorconfig
"""
[*.{kt,kts}]
disabled_rules=filename
ktlint_disabled_rules = filename
""".trimIndent(),
)
}
Expand Down

0 comments on commit dcf068b

Please sign in to comment.