ktlint
ktlint documentation
- Version in MegaLinter: 0.46.1
- Visit Official Web Site
- See Index of problems detected by ktlint
Configuration in MegaLinter
- Enable ktlint by adding
KOTLIN_KTLINT
in ENABLE_LINTERS variable - Disable ktlint by adding
KOTLIN_KTLINT
in DISABLE_LINTERS variable
- Enable auto-fixes by adding
KOTLIN_KTLINT
in APPLY_FIXES variable
Variable | Description | Default value |
---|---|---|
KOTLIN_KTLINT_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
KOTLIN_KTLINT_FILTER_REGEX_INCLUDE | Custom regex including filter Ex: (src\|lib) |
Include every file |
KOTLIN_KTLINT_FILTER_REGEX_EXCLUDE | Custom regex excluding filter Ex: (test\|examples) |
Exclude no file |
KOTLIN_KTLINT_CLI_LINT_MODE | Override default CLI lint mode - file : Calls the linter for each file- list_of_files : Call the linter with the list of files as argument- project : Call the linter from the root of the project |
file |
KOTLIN_KTLINT_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".kt", ".kts"] |
KOTLIN_KTLINT_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
KOTLIN_KTLINT_PRE_COMMANDS | List of bash commands to run before the linter | None |
KOTLIN_KTLINT_POST_COMMANDS | List of bash commands to run after the linter | None |
KOTLIN_KTLINT_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
KOTLIN_KTLINT_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
IDE Integration
Use ktlint in your favorite IDE to catch errors before MegaLinter !
IDE | Extension Name | Install | |
---|---|---|---|
Emacs | flycheck-kotlin | Visit Web Site | |
vim | ale | Visit Web Site |
MegaLinter Flavours
This linter is available in the following flavours
Flavor | Description | Embedded linters | Info | |
---|---|---|---|---|
all | Default MegaLinter Flavor | 101 | ||
documentation | MegaLinter for documentation projects | 42 | ||
dotnet | Optimized for C, C++, C# or VB based projects | 50 | ||
go | Optimized for GO based projects | 44 | ||
java | Optimized for JAVA based projects | 44 | ||
javascript | Optimized for JAVASCRIPT or TYPESCRIPT based projects | 51 | ||
php | Optimized for PHP based projects | 46 | ||
python | Optimized for PYTHON based projects | 50 | ||
ruby | Optimized for RUBY based projects | 43 | ||
rust | Optimized for RUST based projects | 43 | ||
salesforce | Optimized for Salesforce based projects | 45 | ||
swift | Optimized for SWIFT based projects | 43 | ||
terraform | Optimized for TERRAFORM based projects | 48 |
Behind the scenes
How are identified applicable files
- File extensions:
.kt
,.kts
How the linting is performed
- ktlint is called one time by identified file
Example calls
ktlint myfile.kt
ktlint --format myfile.kt
Help content
An anti-bikeshedding Kotlin linter with built-in formatter.
(https://github.com/pinterest/ktlint).
Usage:
ktlint <flags> [patterns]
java -jar ktlint.jar <flags> [patterns]
Examples:
# Check the style of all Kotlin files (ending with '.kt' or '.kts') inside
the current dir (recursively).
#
# Hidden folders will be skipped.
ktlint
# Check only certain locations starting from the current directory.
#
# Prepend ! to negate the pattern, KtLint uses .gitignore pattern style
syntax.
# Globs are applied starting from the last one.
#
# Hidden folders will be skipped.
# Check all '.kt' files in 'src/' directory, but ignore files ending with
'Test.kt':
ktlint "src/**/*.kt" "!src/**/*Test.kt"
# Check all '.kt' files in 'src/' directory, but ignore 'generated' directory
and its subdirectories:
ktlint "src/**/*.kt" "!src/**/generated/**"
# Auto-correct style violations.
ktlint -F "src/**/*.kt"
# Using custom reporter jar and overriding report location
ktlint --reporter=csv,artifact=/path/to/reporter/csv.jar,
output=my-custom-report.csv
Flags:
-a, --android Turn on Android Kotlin Style Guide compatibility
--color Make output colorful
--color-name=<colorName>
Customize the output color
--debug Turn on debug output
--trace Turn on trace output
--disabled_rules=<disabledRules>
Comma-separated list of rules to globally disable. To
disable standard ktlint rule-set use
--disabled_rules=standard
-F, --format Fix any deviations from the code style
--limit=<limit> Maximum number of errors to show (default: show all)
--relative Print files relative to the working directory (e.g.
dir/file.kt instead of /home/user/project/dir/file.kt)
--reporter=<reporters>
A reporter to use (built-in: plain (default), plain?
group_by_file, json, sarif, checkstyle, html). To use
a third-party reporter specify a path to a JAR file
on the filesystem via ',artifact=' option. To
override reporter output, use ',output=' option.
-R, --ruleset=<rulesetJarFiles>
A path to a JAR file containing additional ruleset(s)
--stdin Read file from stdin
-v, --verbose Show error codes
--editorconfig=<editorConfigPath>
Path to .editorconfig
--experimental Enabled experimental rules (ktlint-ruleset-experimental)
--baseline=<baseline>
Defines a baseline file to check against
-h, --help Show this help message and exit.
-V, --version Print version information and exit.
Commands:
installGitPreCommitHook, --install-git-pre-commit-hook Install git hook to
automatically check
files for style
violations on commit
installGitPrePushHook, --install-git-pre-push-hook Install git hook to
automatically check
files for style
violations before
push
printAST, --print-ast Print AST (useful
when
writing/debugging
rules)
applyToIDEA, --apply-to-idea Update Intellij IDEA
Kotlin codestyle
settings (global)
applyToIDEAProject, --apply-to-idea-project Update Intellij IDEA
project settings
generateEditorConfig EXPERIMENTAL!!!
Generate kotlin
style section for '.
editorconfig' file.
Installation on mega-linter Docker image
- Dockerfile commands :
RUN curl --retry 5 --retry-delay 5 -sSLO https://github.com/pinterest/ktlint/releases/latest/download/ktlint && \
chmod a+x ktlint && \
mv "ktlint" /usr/bin/