I am using detekt with ktlint for formatting my code is like below.
detekt.gradle
ext{
toolVersion = "1.0.0-RC16"
}
detekt {
input = files(...)
filters = ".*/resources/.*,.*/build/.*"
baseline = file("${project.rootDir}/tools/detekt-baseline.xml")
reports{
html{
enabled = true
}
xml{
enabled = false
}
}
config = files(file("$project.rootDir/tools/detekt.yml"))
}
dependencies {
detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:$toolVersion"
}
detekt-baseline.yml
autoCorrect: true
build:
maxIssues: 10
weights:
# complexity: 2
# LongParameterList: 1
# style: 1
# comments: 1
Project level build.gradle
buildscript {
ext{...}
repositories {...}
dependencies {...}
}
plugins{
id "io.gitlab.arturbosch.detekt" version "1.0.0-RC16"
}
apply from: 'tools/detekt.gradle'
...
allprojects {
repositories {...}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
As you can see I have added detekt plugin for formatting in detekt.gradle. I have also enabled autoCorrect
in detekt-baseline.yml. But code is not formatted when I ran ./gradlew detekt
Generated html report shows no findings, but the metrics are shown.
When I ran ./gradlew detekt
after commenting out following line in detekt.gradle.
// config = files(file("$project.rootDir/tools/detekt.yml"))
It does shows me findings, including formatting issues (ex: needless blank lines)
How could I configure detekt to auto format the code according to the ktlint?
This is an android project.
lint, or a linter, is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs. In this blog post, I intend to present two of the most known linters in the Kotlin community: ktlint and detekt. The programming language itself has a built-in linter which has poor documentation.
By using ktlint you put the importance of code clarity and community conventions over personal preferences. This makes things easier for people reading your code as well as frees you from having to document & explain what style potential contributor (s) have to follow.
Klint ships with a formatting tool. It means it indent and struct your code avoiding minor errors for you. On the other hand, detekt also checks for code smell and has a lot of configurations and different setups. Setting up detekt may be more complicated, but maybe a good option if you need custom checking.
This is meant primarily as an escape latch for the rare cases when ktlint is not able to produce the correct result (please report any such instances using GitHub Issues ). To disable a specific rule you'll need to turn on the verbose mode (ktlint --verbose ...).
Since detekt 1.1.0-RC15, you have to enable the autoCorrect in gradle:
detekt {
autoCorrect = true
}
https://detekt.github.io/detekt/changelog-rc.html#rc15
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With