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

Problem creating rulesets #300

Closed
chaneylc opened this issue Oct 23, 2018 · 6 comments
Closed

Problem creating rulesets #300

chaneylc opened this issue Oct 23, 2018 · 6 comments

Comments

@chaneylc
Copy link

chaneylc commented Oct 23, 2018

Could you expand on how to create your own rulesets? I looked into ServiceLoader javadocs and have the following understanding:

You should have a directory structure similar to:
(taken from the ktlint-ruleset-template)

Project/
    src/main/
        kotlin/ 
            CustomRuleSetProvider
            Rules
        resources/
            META-INF.services/
                com.github.shyiko.ktlint.core.RuleSetProvider

From the project directory you should be able to run:
jar cf ruleset.jar Project/

And then this can be used by ktlint as:
ktlint -R ruleset.jar ....

Although my rule is never reached! Could you please give more details on how exactly this jar should be created?

Here is what my resources look like:
//resources/META-INF.services
//com.github.shyiko.ktlint.core.RuleSetProvider
mypackage.CustomRuleSetProvider

Here is my CustomRuleSetProvider
//CustomRuleSetProvider

 package mypackage
 
 import com.github.shyiko.ktlint.core.RuleSet
 import com.github.shyiko.ktlint.core.RuleSetProvider
 
 class CustomRuleSetProvider : RuleSetProvider {
 
     override fun get(): RuleSet = RuleSet("custom", TestVisit())
 }

Here is my Rule, which naively prints a test message on each visit.
//TestVisit

package mypackage

import com.github.shyiko.ktlint.core.Rule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.KtStringTemplateEntry

class TestVisit : Rule("test-visit") {

    override fun visit(
        node: ASTNode,
        autoCorrect: Boolean,
        emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
    ) {
        emit(node.startOffset, "test", false)
    }
}
@shyiko
Copy link
Collaborator

shyiko commented Oct 23, 2018

Hi. src/main/resource/META-INF.services should be src/main/resource/META-INF/services. Take a look at https://stackoverflow.com/questions/51368558/ktlint-custom-reporter/51391651#51391651. Let me know if that doesn't help.

@chaneylc
Copy link
Author

My folder structure looks like that, src/main/resource/META-INF/services. Sorry for the confusion, the folder structure is exactly like ktlint/ktlint-ruleset-template. Could you give an example jar file that actually works? Maybe I could figure the problem out then.

Thanks again

@shyiko
Copy link
Collaborator

shyiko commented Oct 23, 2018

@chaneylc try the following

$ cd ktlint-ruleset-template/
$ ./gradlew build
$ echo 'var v = 0' > test.kt
$ ktlint -R build/libs/ktlint-ruleset-template.jar --debug --relative test.kt

[DEBUG] Resolving build/libs/ktlint-ruleset-template.jar
[DEBUG] Loading file:/tmp/ktlint/ktlint-ruleset-template/build/libs/ktlint-ruleset-template.jar
[DEBUG] Discovered ruleset "standard"
[DEBUG] Discovered ruleset "custom"
[DEBUG] Discovered reporter "plain"
[DEBUG] Discovered reporter "json"
[DEBUG] Discovered reporter "checkstyle"
[DEBUG] Initializing "plain" reporter with {verbose=false, color=false}
[DEBUG] Checking test.kt
[DEBUG] Discovered .editorconfig (..) {charset=utf-8, end_of_line=lf, indent_size=4, indent_style=space, insert_final_newline=true, trim_trailing_whitespace=true}
test.kt:1:1: Unexpected var, use val instead (cannot be auto-corrected)
[DEBUG] 828ms / 1 file(s) / 1 error(s)

@chaneylc
Copy link
Author

Thanks! I should have looked into gradle before trying to make my own jar. From the main readme I would suggest you change "Once packaged in a JAR you can load it with" with the above snippet you shared.

@shyiko
Copy link
Collaborator

shyiko commented Oct 24, 2018

No worries! I'll keep this ticket open until README is updated if you don't mind. 🥂

@shyiko shyiko reopened this Oct 24, 2018
@chaneylc
Copy link
Author

chaneylc commented Oct 24, 2018

Awesome! Thanks again for your hard work on this project, it's great!

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

No branches or pull requests

2 participants