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 the ktlint plugin 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 the step-by-step instructions for connecting to the project useful. 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`
, .. , / - , .
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. gradle `ktlint`
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" ]
}
#4. `ktlint`
compileKotlin.dependsOn ktlint
. , , . .
, , , .
.
#5. Idea
File -> Settings -> Code Style -> Kotlin.
#6.
.
Ctrl+Alt+L, Idea . , Idea, .
.
Idea ― ktlintFormat — .
#7.
- , , .editorconfig
.
, . , , , , Ctrl+Alt+L
ktlintFormat
, .
[*.{kt,kts}]
disabled_rules = import-ordering
build.gradle . 2- , .
, , / code style Kotlin , — , : , , .
, , Open Source Brain-up, , .
Sonar Cloud Kotlin , , .
!