Using the code-style ktlint plugin in a Kotlin project. A quick guide for a backend developer

I am a Java / Kotlin developer at EPAM.

In the first article, I talked about my project – Brain-Up

In this article I want to share my experience in setting up a plugin. ktlint for a Kotlin project.

This plugin helps to ensure a consistent code style across the project. It builds on the official Kotlin code formatting guidelines from JetBrains. Using this tool, you can not only check the code, but also format it.

While I was setting up ktlint, I was looking for information, a description, and it seemed to me that little attention was paid to the configuration topic of this plugin in the reviews, and the answers to the questions that appeared were not obvious.

Therefore, I decided to share my experience, I hope someone will find useful step-by-step instructions for connecting to the project. This example is relevant for a project on Kotlin 1.4, gradle 6.0.

#one. Add dependency to build.gradle per plugin

dependencies {    
    ktlint "com.pinterest:ktlint:0.38.0"
}

# 2. Adding a gradle task `ktlintFormat`

With its help, you can format the entire project at once, i.e. most of the recommendations will be applied automatically, all that remains is to add / remove empty lines manually if necessary, or something that this task could not fix.

task ktlintFormat(type: JavaExec, group: "formatting") 
{
    description = "Fix Kotlin code style deviations."    
    classpath = configurations.ktlint    
    main = "com.pinterest.ktlint.Main"    
    args "-F", "src/*/.kt"
}

# 3. Configuring the `ktlint` gradle task for your project

project.task("ktlint", type: JavaExec) {    
    group = "verification"    
    description = "Runs ktlint."    
    main = "com.pinterest.ktlint.Main"    
    classpath = project.configurations.ktlint    
    args = [            
        "--reporter=plain",            
        "--reporter=checkstyle,output=${project.buildDir}/reports/ktlint/ktlint-checkstyle-report.xml",            
        "src/*/.kt"    ]
}

#four. Embedding the `ktlint` task in the application build process

compileKotlin.dependsOn ktlint

In this case, the code formatting check will run before compilation. The sooner we learn about the discrepancy, that is, this check falls, the better. In this case, you can quickly fix everything.

If a formatting error occurs, then we will see something like this, from which it is clear what is wrong and where.

We correct the formatting.

#five. Configuring Idea Options for Correct Formatting

This can be configured here File -> Settings -> Code Style -> Kotlin.

# 6. Formatting the current class

The first way.

When working with a class, it is convenient for me to press Ctrl + Alt + L right after the end, and the class will be automatically formatted according to the formatting properties set in Idea. If you select a folder in the project with the mouse and click on it this combination, then all classes in this folder will be formatted according to the Idea settings that we specified above.

Second way.

If you do not want to customize Idea and use its formatting capabilities, you can run the ktlintFormat task before assembly – it will work for the entire project.

# 7. Turn off rules

If you don’t really like something and really want to turn off the rule that interferes, you can do this by adding a special file .editorconfig and turn off the rules there.

For example, in the project we abandoned the rule that imports must be sorted in lexicographic order. That is, the rule may not be bad, by the way, it recently appeared in the plugin, only now  Ctrl+Alt+L  imports do not group and task them  ktlintFormat  too, but doing it every time with your hands is boring.

[*.{kt,kts}]
disabled_rules = import-ordering

Summary

This is how it looks like adding a plugin in build.gradle as a result of our project. We have been working with him for 2 years, everything is stable so far.

If you have your own ideas, experience on how to improve / solve the problem of a unified code style on a Kotlin project, write in the comments, it will be interesting to know: what plugins you use, how convenient they are, are stable to language updates and quickly customizable.

A complete example, if desired, can be viewed in our open source repository of the social project Brain-upthat anyone can join, gain experience in a professional team and bring real benefits to society.

In the next article I will try to tell you about connecting Sonar Cloud to a Kotlin project, which also had a lot of questions when it was configured.

Good luck to all!

Similar Posts

Leave a Reply