(!) See diKTat codestyle first.
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 (until this PR is merged you will need to use KTlint fork):
$ curl -sSLO https://central.artipie.com/akuleshov7/files/ktlint && chmod a+x ktlint
-
Load diKTat manually: here
OR use curl:
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v1.0.0/diktat.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 violations use -F
option.
First, add this to your pom.xml
file:
<project>
[...]
<repositories>
<repository>
<id>artipie</id>
<url>https://central.artipie.com/akuleshov7/diktat</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>artipie</id>
<url>https://central.artipie.com/akuleshov7/diktat</url>
</pluginRepository>
</pluginRepositories>
</project>
Then, add this plugin:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</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/**/*.kt"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.pinterest</groupId>
<artifactId>ktlint</artifactId>
<version>0.37.1-fork</version> <!-- use this fork to be compatible with diktat -->
</dependency>
<dependency>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-rules</artifactId>
<version>1.0.0</version> <!-- replace it with diktat latest version -->
</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
.
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 rules-config.json
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, and
configuration
— a simple map of some extra unique configurations for the rule.
For example:
"configuration": {
"isCopyrightMandatory": true,
"copyrightText": "Copyright (c) Jeff Lebowski, 2012-2020. All rights reserved."
}
See default configuration in rules-config.json
Main components are:
- diktat-ruleset — 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
.json
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 that will extended KTlint functionaliity.
Before you make a pull request, make sure the build is clean:
$ mvn clean install
Also see our Contributing Policy and Code of Conduct