From 2c89a4c98a5a3435ed2b5b479a20f0e715553596 Mon Sep 17 00:00:00 2001 From: Francis Agyapong Date: Fri, 2 Jun 2017 12:41:46 -0600 Subject: [PATCH] Add ktlint support (without formatting) for kotlin filetype (#610) * Add ktlint support (without formatting) for kotlin filetype * Fix code style and refactor to use ALE utility functions (GetMatches) * Remove options for configuration file * Refactor: Rename exec variable and use ale#Set for variable configuration --- README.md | 2 +- ale_linters/kotlin/ktlint.vim | 54 +++++++++++++++++++++++++++++++++++ doc/ale-kotlin.txt | 25 ++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 ale_linters/kotlin/ktlint.vim diff --git a/README.md b/README.md index d5c3e076..44df3b58 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ name. That seems to be the fairest way to arrange this table. | Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html) | | JavaScript | [eslint](http://eslint.org/), [jscs](http://jscs.info/), [jshint](http://jshint.com/), [flow](https://flowtype.org/), [standard](http://standardjs.com/), [xo](https://github.com/sindresorhus/xo) | JSON | [jsonlint](http://zaa.ch/jsonlint/) | -| Kotlin | [kotlinc](https://kotlinlang.org) see `:help ale-integration-kotlin` for configuration instructions +| Kotlin | [kotlinc](https://kotlinlang.org), [ktlint](https://ktlint.github.io) see `:help ale-integration-kotlin` for configuration instructions | LaTeX | [chktex](http://www.nongnu.org/chktex/), [lacheck](https://www.ctan.org/pkg/lacheck), [proselint](http://proselint.com/) | | Lua | [luacheck](https://github.com/mpeterv/luacheck) | | Markdown | [mdl](https://github.com/mivok/markdownlint), [proselint](http://proselint.com/), [vale](https://github.com/ValeLint/vale) | diff --git a/ale_linters/kotlin/ktlint.vim b/ale_linters/kotlin/ktlint.vim new file mode 100644 index 00000000..f474e845 --- /dev/null +++ b/ale_linters/kotlin/ktlint.vim @@ -0,0 +1,54 @@ +" Author: Francis Agyapong +" Description: Lint kotlin files using ktlint + +call ale#Set('kotlin_ktlint_executable', 'ktlint') +call ale#Set('kotlin_ktlint_rulesets', []) +call ale#Set('kotlin_ktlint_format', 0) + + +function! ale_linters#kotlin#ktlint#GetCommand(buffer) abort + let l:executable = ale#Var(a:buffer, 'kotlin_ktlint_executable') + let l:file_path = expand('#' . a:buffer . ':p') + let l:options = '' + + " Formmatted content written to original file, not sure how to handle + " if ale#Var(a:buffer, 'kotlin_ktlint_format') + " let l:options = l:options . ' --format' + " endif + + for l:ruleset in ale#Var(a:buffer, 'kotlin_ktlint_rulesets') + let l:options = l:options . ' --ruleset ' . l:ruleset + endfor + + return l:executable . ' ' . l:options . ' ' . l:file_path +endfunction + +function! ale_linters#kotlin#ktlint#Handle(buffer, lines) abort + let l:message_pattern = '^\(.*\):\([0-9]\+\):\([0-9]\+\):\s\+\(.*\)' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:message_pattern) + let l:line = l:match[2] + 0 + let l:column = l:match[3] + 0 + let l:text = l:match[4] + + let l:type = l:text =~? 'not a valid kotlin file' ? 'E' : 'W' + + call add(l:output, { + \ 'lnum': l:line, + \ 'col': l:column, + \ 'text': l:text, + \ 'type': l:type + \}) + endfor + + return l:output +endfunction + +call ale#linter#Define('kotlin', { +\ 'name': 'ktlint', +\ 'executable': 'ktlint', +\ 'command_callback': 'ale_linters#kotlin#ktlint#GetCommand', +\ 'callback': 'ale_linters#kotlin#ktlint#Handle', +\ 'lint_file': 1 +\}) diff --git a/doc/ale-kotlin.txt b/doc/ale-kotlin.txt index 04efaeaf..07e26393 100644 --- a/doc/ale-kotlin.txt +++ b/doc/ale-kotlin.txt @@ -62,4 +62,29 @@ g:ale_kotlin_kotlinc_module_filename *g:ale_kotlin_kotlinc_module_filename* The filename of the module file that the linter should pass to the kotlin compiler. + +------------------------------------------------------------------------------- +ktlint *ale-kotlin-ktlint* + +g:ale_kotlin_ktlint_executable *g:ale_kotlin_ktlint_executable* + Type: |String| + Default: `''` + + The Ktlint executable. + + Posix-compliant shell scripts are the only executables that can be found on + Ktlint's github release page. If you are not on such a system, your best + bet will be to download the ktlint jar and set this option to something + similar to `'java -jar /path/to/ktlint.jar'` + +g:ale_kotlin_ktlint_rulesets *g:ale_kotlin_ktlint_rulesets* + Type: |List| of |String|s + Default: [] + + This list should contain paths to ruleset jars and/or strings of maven + artifact triples. Example: + > + let g:ale_kotlin_ktlint_rulesets = ['/path/to/custom-rulset.jar', + 'com.ktlint.rulesets:mycustomrule:1.0.0'] + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: