(!) See diKTat codestyle first.
(!) Also see the list of all supported rules.
(!) Have a look at maven and gradle examples.
(!) Check and try diktat/ktlint online demo
DiKTat is a collection of Kotlin code style rules implemented as AST visitors on top of KTlint. The full list of available supported rules and inspections is here.
-
Install KTlint manually: here
OR use curl:
curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.37.1/ktlint && chmod a+x ktlint # another option is "brew install ktlint"
-
Load diKTat manually: here
OR use curl:
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.0/diktat-0.1.0.jar
-
Finally, run KTlint (with diKTat injected) to check your
*.kt
files indir/your/dir
:$ ./ktlint -R diktat.jar "dir/your/dir/**/*.kt"
To autofix all code style violations use -F
option.
You can see how it is configured in our project for self-checks: pom.xml
Add this plugin to your pom.xml:
Maven plugin snippet
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>diktat</id>
<phase>none</phase>
<configuration>
<target name="ktlint">
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true"
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
<arg value="src/main/**/*.kt"/>
<arg value="src/test/kotlin/**/*.kt"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.pinterest</groupId>
<artifactId>ktlint</artifactId>
<version>0.37.1</version>
<exclusions>
<exclusion> <!-- without this exclusion both rulesets are enabled which we discourage -->
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-ruleset-standard</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-rules</artifactId>
<version>0.1.0</version> <!-- replace it with diktat latest version -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
In case you want to add autofixer with diKTat ruleset just extend
the snippet above with <arg value="-F"/>
.
To run diktat to check/fix code style - run $ mvn antrun:run@diktat
.
You can see how it is configured in our project for self-checks: build.gradle.kts.
Add the code below to your build.gradle.kts
:
Gradle plugin snippet
val ktlint by configurations.creating
dependencies {
ktlint("com.pinterest:ktlint:0.37.1") {
// need to exclude standard ruleset to use only diktat rules
exclude("com.pinterest.ktlint", "ktlint-ruleset-standard")
}
// diktat ruleset
ktlint("org.cqfn.diktat:diktat-rules:0.1.0") {
exclude("org.slf4j", "slf4j-log4j12")
}
}
val outputDir = "${project.buildDir}/reports/diktat/"
val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt"))
val diktatCheck by tasks.creating(JavaExec::class) {
inputs.files(inputFiles)
outputs.dir(outputDir)
description = "Check Kotlin code style."
classpath = ktlint
main = "com.pinterest.ktlint.Main"
// specify proper path to sources that should be checked here
args = listOf("src/main/kotlin/**/*.kt")
}
val diktatFormat by tasks.creating(JavaExec::class) {
inputs.files(inputFiles)
outputs.dir(outputDir)
description = "Fix Kotlin code style deviations."
classpath = ktlint
main = "com.pinterest.ktlint.Main"
// specify proper path to sources that should be checked here
args = listOf("-F", "src/main/kotlin/**/*.kt")
}
To run diktat to check/fix code style - run $ gradle diktatCheck
.
In KTlint, rules can be configured via .editorconfig
, but
this does not give a chance to customize or enable/disable
each and every rule independently.
That is why we have supported diktat-analysis.yml
that can be easily
changed and help in customization of your own rule set.
It has simple fields:
name
— name of the rule,
enabled
(true/false) — to enable or disable that rule (all rules are enabled by the default),
configuration
— a simple map of some extra unique configurations for this particular rule.
For example:
- name: HEADER_MISSING_OR_WRONG_COPYRIGHT
# all rules are enabled by the default. To disable add 'enabled: false' to the config.
enabled: true
configuration:
isCopyrightMandatory: true,
copyrightText: Copyright (c) Jeff Lebowski, 2012-2020. All rights reserved.
Note, that you can specify and put diktat-analysis.yml
that contains configuration of diktat in the parent directory of your project on the same level where build.gradle/pom.xml
is stored.
See default configuration in diktat-analysis.yml
Also see the list of all rules supported by diKTat.
In addition to enabling/disabling warning globally via config file (enable = false
), you can suppress warnings by adding @Suppress
annotation on individual code blocks
For example:
@Suppress("FUNCTION_NAME_INCORRECT_CASE")
class SomeClass {
fun methODTREE(): String {
}
}
Main components are:
- diktat-rules — number of rules that are supported by diKTat;
- diktat-test-framework — functional/unit test framework that can be used for running your code fixer on the initial code and compare it with the expected result;
- also see our demo: diktat-demo in a separate repository.
Mainly we wanted to create a common configurable mechanism that will give us a chance to enable/disable and customize all rules. That's why we added logic for:
- Parsing
.yml
file with configurations of rules and passing it to visitors; - Passing information about properties to visitors. This information is very useful, when you are trying to get, for example, a filename of file where the code is stored;
- We added a bunch of visitors, checkers and fixers that will extended KTlint functionaliity with code style rules;
- We have proposed a code style for Kotlin language.
Before you make a pull request, make sure the build is clean as we have lot of tests and other prechecks:
$ mvn clean install
Also see our Contributing Policy and Code of Conduct