Merge pull request #2228 from Nomad145/ktlint

Add ktlint Fixer Support
This commit is contained in:
w0rp 2019-01-27 15:55:13 +00:00 committed by GitHub
commit 08d3523962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 135 additions and 47 deletions

View File

@ -1,54 +1,10 @@
" Author: Francis Agyapong <francisagyapong2@gmail.com>
" 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',
\ 'command_callback': 'ale#handlers#ktlint#GetCommand',
\ 'callback': 'ale#handlers#ktlint#Handle',
\ 'lint_file': 1
\})

View File

@ -275,6 +275,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['hcl', 'terraform'],
\ 'description': 'Fix tf and hcl files with terraform fmt.',
\ },
\ 'ktlint': {
\ 'function': 'ale#fixers#ktlint#Fix',
\ 'suggested_filetypes': ['kt'],
\ 'description': 'Fix Kotlin files with ktlint.',
\ },
\}
" Reset the function registry to the default entries.

View File

@ -0,0 +1,9 @@
" Author: Michael Phillips <michaeljoelphillips@gmail.com>
" Description: Fix Kotlin files with ktlint.
function! ale#fixers#ktlint#Fix(buffer) abort
return {
\ 'command': ale#handlers#ktlint#GetCommand(a:buffer) . ' --format',
\ 'read_temporary_file': 1,
\}
endfunction

View File

@ -0,0 +1,45 @@
" Author: Michael Phillips <michaeljoelphillips@gmail.com>
" Description: Handler functions for ktlint.
call ale#Set('kotlin_ktlint_executable', 'ktlint')
call ale#Set('kotlin_ktlint_rulesets', [])
call ale#Set('kotlin_ktlint_options', '')
function! ale#handlers#ktlint#GetCommand(buffer) abort
let l:executable = ale#Var(a:buffer, 'kotlin_ktlint_executable')
let l:options = ale#Var(a:buffer, 'kotlin_ktlint_options')
let l:rulesets = ale#handlers#ktlint#GetRulesets(a:buffer)
return ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . (empty(l:rulesets) ? '' : ' ' . l:rulesets)
\ . ' %t'
endfunction
function! ale#handlers#ktlint#GetRulesets(buffer) abort
let l:rulesets = map(ale#Var(a:buffer, 'kotlin_ktlint_rulesets'), '''--ruleset '' . v:val')
return join(l:rulesets, ' ')
endfunction
function! ale#handlers#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

View File

@ -84,9 +84,17 @@ g:ale_kotlin_ktlint_rulesets *g:ale_kotlin_ktlint_rulesets*
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',
let g:ale_kotlin_ktlint_rulesets = ['/path/to/custom-ruleset.jar',
'com.ktlint.rulesets:mycustomrule:1.0.0']
g:ale_kotlin_ktlint_options *g:ale_kotlin_ktlint_options*
Type: |String|
Default: `''`
Additional options to pass to ktlint for both linting and fixing. Example:
>
let g:ale_kotlin_ktlint_options = '--android'
===============================================================================
languageserver *ale-kotlin-languageserver*

View File

@ -0,0 +1,44 @@
Before:
Save g:ale_kotlin_ktlint_executable
Save g:ale_kotlin_ktlint_options
Save g:ale_kotlin_ktlint_rulesets
" Use an invalid global executable, so we don't match it.
let g:ale_kotlin_ktlint_executable = 'xxxinvalid'
let g:ale_kotlin_ktlint_options = ''
let g:ale_kotlin_ktlint_rulesets = []
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
call ale#test#RestoreDirectory()
Execute(The ktlint callback should return the correct default values):
call ale#test#SetFilename('../kotlin_files/testfile.kt')
AssertEqual
\ {
\ 'command': ale#Escape('xxxinvalid')
\ . ' %t'
\ . ' --format',
\ 'read_temporary_file': 1,
\ },
\ ale#fixers#ktlint#Fix(bufnr(''))
Execute(The ktlint callback should include custom ktlint options):
let g:ale_kotlin_ktlint_options = "--android"
let g:ale_kotlin_ktlint_rulesets = ['/path/to/custom/ruleset.jar']
call ale#test#SetFilename('../kotlin_files/testfile.kt')
AssertEqual
\ {
\ 'command': ale#Escape('xxxinvalid')
\ . ' ' . g:ale_kotlin_ktlint_options
\ . ' --ruleset /path/to/custom/ruleset.jar'
\ . ' %t'
\ . ' --format',
\ 'read_temporary_file': 1,
\ },
\ ale#fixers#ktlint#Fix(bufnr(''))

View File

@ -0,0 +1,21 @@
Before:
Save g:ale_kotlin_ktlint_rulesets
let g:ale_kotlin_ktlint_rulesets = []
After:
Restore
Execute(The ktlint handler method GetRulesets should properly parse custom rulesets):
let g:ale_kotlin_ktlint_rulesets = ['/path/to/custom/ruleset.jar', '/path/to/other/ruleset.jar']
AssertEqual
\ '--ruleset /path/to/custom/ruleset.jar --ruleset /path/to/other/ruleset.jar',
\ ale#handlers#ktlint#GetRulesets(bufnr(''))
Execute(The ktlint handler method GetRulesets should return an empty string when no rulesets have been configured):
let g:ale_kotlin_ktlint_rulesets = []
AssertEqual
\ '',
\ ale#handlers#ktlint#GetRulesets(bufnr(''))

View File