detekt-intellij-plugin
detekt-intellij-plugin copied to clipboard
Feature request: ability to add intention to a custom rule
Something like this:
(Yes, this issue looks somewhat like Feature request: intention action to add @Suppress #12 )
What you can do now is to modify the AST in your custom rule inside a withAutoCorrect { ... }
closure the same way KtLint does it.
This plugin ships an AutoCorrect by detekt
action which will than trigger your rule's auto correct feature.
Thanks! I didn't know that.
I'm not sure I understand - withAutoCorrect { ... }
is a workaround until this feature is done?
Or is it the way to go?
Also, If you have an example, that would be great. (But don't bother creating one just for me - when I get to it, I'll dig, and If I succeed I can contribute documentation)
You can look up examples how to manipulate the AST in the KtLint rules of the FormattingRule.wrapper
e.g.: detekt/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/FinalNewline.kt
if (autoCorrect) {
node.addChild(PsiWhiteSpaceImpl("\n"), null)
}
if (autoCorrect) {
lastNode.node.treeParent.removeChild(lastNode.node)
}
withAutoCorrect { ... }
is just the same as if (autoCorect) { }
- a helper to only edit code when the user wants it.
It's a feature we ship with detekt from like milestone 4 and there is no plan to remove it as long as we have wrappers for KtLint. In the standard detekt rule set we just do not want to mess around with users code :).
Wow thanks @arturbosch !