-
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.
- Loading branch information
1 parent
059252a
commit 888644f
Showing
5 changed files
with
133 additions
and
6 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
67 changes: 67 additions & 0 deletions
67
plugin/src/adapter1/kotlin/org/jlleitschuh/gradle/ktlint/worker/KtLintInvocation1.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,67 @@ | ||
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 KtLintInvocation1( | ||
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 KtLintInvocation1(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