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
8 changed files with 135 additions and 47 deletions

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