detekt-intellij-plugin icon indicating copy to clipboard operation
detekt-intellij-plugin copied to clipboard

Feature request: ability to add intention to a custom rule

Open pinkasey opened this issue 4 years ago • 4 comments

Something like this: image

(Yes, this issue looks somewhat like Feature request: intention action to add @Suppress #12 )

pinkasey avatar Apr 15 '20 22:04 pinkasey

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.

arturbosch avatar Apr 16 '20 19:04 arturbosch

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)

pinkasey avatar Apr 19 '20 11:04 pinkasey

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 :).

arturbosch avatar Apr 19 '20 11:04 arturbosch

Wow thanks @arturbosch !

pinkasey avatar Apr 19 '20 11:04 pinkasey