-
Notifications
You must be signed in to change notification settings - Fork 39
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
Showing
18 changed files
with
238 additions
and
191 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
22 changes: 22 additions & 0 deletions
22
diktat-rules/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintRuleSetProviderWrapper.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,22 @@ | ||
package org.cqfn.diktat.ktlint | ||
|
||
import org.cqfn.diktat.ktlint.KtLintRuleSetWrapper.Companion.toKtLint | ||
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider | ||
import com.pinterest.ktlint.core.RuleSet | ||
import com.pinterest.ktlint.core.RuleSetProvider | ||
|
||
/** | ||
* This is a wrapper around __KtLint__'s [RuleSetProvider]. | ||
*/ | ||
class KtLintRuleSetProviderWrapper private constructor( | ||
private val diktatRuleSetFactory: DiktatRuleSetProvider, | ||
) : RuleSetProvider { | ||
override fun get(): RuleSet = diktatRuleSetFactory().toKtLint() | ||
|
||
companion object { | ||
/** | ||
* @return __KtLint__'s [RuleSetProvider] created from [DiktatRuleSetProvider] | ||
*/ | ||
fun DiktatRuleSetProvider.toKtLint(): RuleSetProvider = KtLintRuleSetProviderWrapper(this) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
diktat-rules/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintRuleSetWrapper.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,35 @@ | ||
package org.cqfn.diktat.ktlint | ||
|
||
import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.ruleset.rules.DiktatRule | ||
import org.cqfn.diktat.ruleset.rules.DiktatRuleSet | ||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.RuleSet | ||
|
||
/** | ||
* This is a wrapper around __KtLint__'s [RuleSet] which adjusts visitorModifiers for all rules to keep order with prevRule | ||
* Added as a workaround after introducing a new logic for sorting KtLint Rules: https://github.com/pinterest/ktlint/issues/1478 | ||
* | ||
* @param diktatRuleSet the rules which belong to the current [DiktatRuleSet]. | ||
*/ | ||
class KtLintRuleSetWrapper private constructor( | ||
diktatRuleSet: DiktatRuleSet, | ||
) : RuleSet(DIKTAT_RULE_SET_ID, rules = wrapRules(diktatRuleSet.rules)) { | ||
companion object { | ||
/** | ||
* @return __KtLint__'s [RuleSet] created from [DiktatRuleSet] | ||
*/ | ||
fun DiktatRuleSet.toKtLint(): RuleSet = KtLintRuleSetWrapper(this) | ||
|
||
private fun wrapRules(rules: List<DiktatRule>): Array<Rule> { | ||
if (rules.isEmpty()) { | ||
return emptyArray() | ||
} | ||
return rules.runningFold(null as KtLintRuleWrapper?) { prevRule, diktatRule -> | ||
KtLintRuleWrapper(diktatRule, prevRule) | ||
} | ||
.filterNotNull() | ||
.toTypedArray() | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
diktat-rules/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintRuleWrapper.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.cqfn.diktat.ktlint | ||
|
||
import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.common.config.rules.qualifiedWithRuleSetId | ||
import org.cqfn.diktat.ruleset.constants.EmitType | ||
import org.cqfn.diktat.ruleset.rules.DiktatRule | ||
import com.pinterest.ktlint.core.Rule | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
/** | ||
* This is a wrapper around __KtLint__'s [Rule] which adjusts visitorModifiers to keep order with prevRule. | ||
* @property rule | ||
*/ | ||
class KtLintRuleWrapper( | ||
val rule: DiktatRule, | ||
prevRule: KtLintRuleWrapper? = null, | ||
) : Rule( | ||
id = rule.id.qualifiedWithRuleSetId(DIKTAT_RULE_SET_ID), | ||
visitorModifiers = createVisitorModifiers(rule, prevRule), | ||
) { | ||
@Deprecated( | ||
"Marked for deletion in ktlint 0.48.0", | ||
replaceWith = ReplaceWith("beforeVisitChildNodes(node, autoCorrect, emit)"), | ||
) | ||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: EmitType, | ||
) = rule.visit(node, autoCorrect, emit) | ||
|
||
companion object { | ||
private fun createVisitorModifiers( | ||
rule: DiktatRule, | ||
prevRule: KtLintRuleWrapper?, | ||
): Set<VisitorModifier> = prevRule?.id?.qualifiedWithRuleSetId(DIKTAT_RULE_SET_ID) | ||
?.let { previousRuleId -> | ||
val ruleId = rule.id.qualifiedWithRuleSetId(DIKTAT_RULE_SET_ID) | ||
require(ruleId != previousRuleId) { | ||
"PrevRule has same ID as rule: $ruleId" | ||
} | ||
setOf( | ||
VisitorModifier.RunAfterRule( | ||
ruleId = previousRuleId, | ||
loadOnlyWhenOtherRuleIsLoaded = false, | ||
runOnlyWhenOtherRuleIsEnabled = false | ||
) | ||
) | ||
} ?: emptySet() | ||
|
||
/** | ||
* @return a rule to which a logic is delegated | ||
*/ | ||
internal fun Rule.delegatee(): DiktatRule = (this as? KtLintRuleWrapper)?.rule ?: error("Provided rule ${javaClass.simpleName} is not wrapped by diktat") | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSet.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,10 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
/** | ||
* A group of [DiktatRule]'s as a single set. | ||
* | ||
* @property rules diktat rules. | ||
*/ | ||
data class DiktatRuleSet( | ||
val rules: List<DiktatRule> | ||
) |
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
17 changes: 17 additions & 0 deletions
17
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/DiktatRuleSetProviderSpi.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,17 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import org.cqfn.diktat.ktlint.KtLintRuleSetProviderWrapper.Companion.toKtLint | ||
import com.pinterest.ktlint.core.RuleSet | ||
import com.pinterest.ktlint.core.RuleSetProvider | ||
|
||
/** | ||
* [RuleSetProvider] that provides diKTat ruleset. | ||
* | ||
* By default, it is expected to have `diktat-analysis.yml` configuration in the root folder where 'ktlint' is run | ||
* otherwise it will use default configuration where some rules are disabled. | ||
* | ||
* This class is registered in [resources/META-INF/services/com.pinterest.ktlint.core.RuleSetProvider] | ||
*/ | ||
class DiktatRuleSetProviderSpi : RuleSetProvider { | ||
override fun get(): RuleSet = DiktatRuleSetProvider().toKtLint().get() | ||
} |
Oops, something went wrong.