-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow editorconfig overrides in ktlint 0.49+ (fixes #707) using pinterest/ktlint#2194
- Loading branch information
1 parent
e5e6038
commit ca03b66
Showing
17 changed files
with
355 additions
and
43 deletions.
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
plugin/src/adapter100/kotlin/org/jlleitschuh/gradle/ktlint/worker/KtLintInvocation100.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,69 @@ | ||
package org.jlleitschuh.gradle.ktlint.worker | ||
|
||
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 | ||
import com.pinterest.ktlint.rule.engine.api.Code | ||
import com.pinterest.ktlint.rule.engine.api.EditorConfigOverride | ||
import com.pinterest.ktlint.rule.engine.api.EditorConfigPropertyRegistry | ||
import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine | ||
import com.pinterest.ktlint.rule.engine.api.LintError | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider | ||
import java.io.File | ||
import java.util.ServiceLoader | ||
|
||
class KtLintInvocation100( | ||
private val engine: KtLintRuleEngine | ||
) : KtLintInvocation { | ||
companion object Factory : KtLintInvocationFactory { | ||
fun initialize(editorConfigOverrides: Map<String, String>): KtLintInvocation { | ||
val ruleProviders = loadRuleSetsFromClasspathWithRuleSetProviderV3() | ||
val editorConfigPropertyRegistry = EditorConfigPropertyRegistry(ruleProviders) | ||
val engine = if (editorConfigOverrides.isEmpty()) { | ||
KtLintRuleEngine(ruleProviders = ruleProviders) | ||
} else { | ||
KtLintRuleEngine( | ||
ruleProviders = ruleProviders, | ||
editorConfigOverride = EditorConfigOverride.from( | ||
*editorConfigOverrides | ||
.mapKeys { editorConfigPropertyRegistry.find(it.key) } | ||
.entries | ||
.map { it.key to it.value } | ||
.toTypedArray() | ||
) | ||
) | ||
} | ||
return KtLintInvocation100(engine) | ||
} | ||
|
||
private fun loadRuleSetsFromClasspathWithRuleSetProviderV3(): Set<RuleProvider> { | ||
return ServiceLoader | ||
.load(RuleSetProviderV3::class.java) | ||
.flatMap { it.getRuleProviders() } | ||
.toSet() | ||
} | ||
} | ||
|
||
override fun invokeLint(file: File): LintErrorResult { | ||
val errors = mutableListOf<Pair<SerializableLintError, Boolean>>() | ||
engine.lint(Code.fromFile(file)) { le: LintError -> | ||
errors.add(le.toSerializable() to false) | ||
} | ||
return LintErrorResult(file, errors) | ||
} | ||
|
||
override fun invokeFormat(file: File): Pair<String, LintErrorResult> { | ||
val errors = mutableListOf<Pair<SerializableLintError, Boolean>>() | ||
val newCode = | ||
engine.format(Code.fromFile(file)) { le, boolean -> | ||
errors.add(le.toSerializable() to boolean) | ||
} | ||
return newCode to LintErrorResult(file, errors) | ||
} | ||
|
||
override fun trimMemory() { | ||
engine.trimMemory() | ||
} | ||
} | ||
|
||
internal fun LintError.toSerializable(): SerializableLintError { | ||
return SerializableLintError(line, col, ruleId.value, detail, canBeAutoCorrected) | ||
} |
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
Oops, something went wrong.