Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: ability to add intention to a custom rule #82

Open
pinkasey opened this issue Apr 15, 2020 · 4 comments
Open

Feature request: ability to add intention to a custom rule #82

pinkasey opened this issue Apr 15, 2020 · 4 comments
Labels
feature New feature or request

Comments

@pinkasey
Copy link

Something like this:
image

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

@arturbosch
Copy link
Member

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 arturbosch added the feature New feature or request label Apr 16, 2020
@pinkasey
Copy link
Author

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)

@arturbosch
Copy link
Member

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

@pinkasey
Copy link
Author

Wow thanks @arturbosch !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants