forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let API Consumer decide whether a LintError has to be autocorrected, …
…or not (pinterest#2671) When formatting code, the API Consumer should be able to decide whether a `LintError` has to be autocorrected, or not. In most cases the API Consumer wants to autocorrect all errors that have an autocorrect fix when formatting the code. The `ktlint-intellij-plugin` has two use cases in which not all `LintError` having an autocorrect should be fixed when invoking the `format` functionality. * In `manual` mode the plugin shows all `LintError`s. Users want to be able to choose to autocorrect a specific `LintError`, while at the same time ignoring other `LintErrors`. * When selecting a block of code in a file, the user want to be able to format only the `LintError`s in the selected text. To avoid breaking changes in Ktlint 1.x, a new `RuleAutocorrectApproveHandler` interface is added. This interfaces defines the new signatures for functions `beforeVisitChildNodes` and `afterVisitChildNodes`. Rules that implement this interface will request the API Consumer to approve to autocorrect a `LintError` before continuing with formatting the code. Closes pinterest#2658
- Loading branch information
1 parent
40e95fe
commit 32fd86f
Showing
134 changed files
with
3,130 additions
and
2,586 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
12 changes: 9 additions & 3 deletions
12
ktlint-api-consumer/src/main/kotlin/com/example/ktlint/api/consumer/rules/NoVarRule.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 |
---|---|---|
@@ -1,22 +1,28 @@ | ||
package com.example.ktlint.api.consumer.rules | ||
|
||
import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType | ||
import com.pinterest.ktlint.rule.engine.core.api.Rule | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleAutocorrectApproveHandler | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleId | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
public class NoVarRule : | ||
Rule( | ||
ruleId = RuleId("$CUSTOM_RULE_SET_ID:no-var"), | ||
about = About(), | ||
) { | ||
), | ||
RuleAutocorrectApproveHandler { | ||
override fun beforeVisitChildNodes( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision, | ||
) { | ||
if (node.elementType == ElementType.VAR_KEYWORD) { | ||
emit(node.startOffset, "Unexpected var, use val instead", false) | ||
// In case that LintError can be autocorrected, use syntax below | ||
// .ifAutocorrectAllowed { | ||
// // Fix | ||
// } | ||
} | ||
} | ||
} |
59 changes: 0 additions & 59 deletions
59
ktlint-api-consumer/src/test/kotlin/com/pinterest/ktlint/api/consumer/ApiTestRunner.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.