Code conventions
Our platform is coded using the Kotlin programming language.
To standardize code formatting, we introduce the ktlint formatter (Ktlint Github) that can be used standalone and is also available as a plugin for IntelliJ IDEA.
Moreover, during the CI/CD pipeline run, a check of changed files in the merge request is performed.
If the code does not adhere to the rules defined in the .editorconfig file, the pipeline fails.
To facilitate this process, we have created a pre-commit hook that prevents commits containing changes that do not adhere to the linting style defined in the .editorconfig file.
So how to make all of this work?
Installing Ktlint in IntelliJ IDEA
If you are using IntelliJ IDEA for development (which is the recommended way), just install the ktlint plugin for IntelliJ IDEA.
The plugin will take care of the formatting automatically for you — it uses the rules defined in the .editorconfig file and, with the recommended configuration, you won’t even notice it’s there.
To install:
-
Go to
File → Settings -
Click
Plugins, search for "Ktlint", and install it
Configure the plugin as follows:
This will make the plugin reformat your files based on the rules defined in the .editorconfig file in the root project folder every time the file is saved in IDEA.
Installing Pre-commit Hook
This is simply about copying the pre-commit file into the .git/hooks folder in the project directory.
You can do it manually or run the following Gradle task from the root project folder:
./gradlew ktlintCopyPreCommitHook
The pre-commit hook will run the ktlint format checker on all changed files in the commit before the commit is finalized.
If there is a violation, the commit will fail.
The reports will be stored in the build/reports folder.
Note that this hook uses Gradle to run ktlint, which might take a few seconds.
If you wish to speed this up, feel free to install ktlint as a system package and write your own pre-commit hook that uses it directly.
See (link to be added) for more information.
CI/CD Pipeline Error Reporting
When the CI/CD pipeline run of the ktlint plugin detects a coding style violation, all violations will be reported in the pipeline logs for stage validation and job gradle:checkFormatting
Does IDEA force you to use wildcard imports?
| Only if IDEA makes life complicated for you with wildcard imports. |
Our current configuration enables wildcard import of only couple of chosen packages resulting in single name imports being used most of the time. But IDEA can be set to use star imports after some number of imports from the package used. To disable it go to settings: