2017-05-12 13:42:32 +00:00
|
|
|
" Author: Devon Meunier <devon.meunier@gmail.com>
|
|
|
|
" Description: checkstyle for Java files
|
|
|
|
|
2019-06-03 22:40:22 +00:00
|
|
|
call ale#Set('java_checkstyle_executable', 'checkstyle')
|
2019-06-10 01:18:06 +00:00
|
|
|
call ale#Set('java_checkstyle_config', '/google_checks.xml')
|
2019-06-03 22:40:22 +00:00
|
|
|
call ale#Set('java_checkstyle_options', '')
|
|
|
|
|
2017-05-12 13:42:32 +00:00
|
|
|
function! ale_linters#java#checkstyle#Handle(buffer, lines) abort
|
|
|
|
let l:output = []
|
|
|
|
|
2018-11-07 12:25:48 +00:00
|
|
|
" modern checkstyle versions
|
2020-11-26 21:29:23 +00:00
|
|
|
let l:pattern = '\v\[(WARN|ERROR)\] [a-zA-Z]?:?[^:]+:(\d+):(\d+)?:? (.*) \[(.+)\]'
|
2018-11-07 12:25:48 +00:00
|
|
|
|
2017-11-18 18:29:12 +00:00
|
|
|
for l:match in ale#util#GetMatches(a:lines, l:pattern)
|
|
|
|
call add(l:output, {
|
|
|
|
\ 'type': l:match[1] is? 'WARN' ? 'W' : 'E',
|
2021-07-04 12:31:00 +00:00
|
|
|
\ 'sub_type': 'style',
|
2017-05-12 13:42:32 +00:00
|
|
|
\ 'lnum': l:match[2] + 0,
|
2017-11-18 18:29:12 +00:00
|
|
|
\ 'col': l:match[3] + 0,
|
|
|
|
\ 'text': l:match[4],
|
|
|
|
\ 'code': l:match[5],
|
|
|
|
\})
|
2017-05-12 13:42:32 +00:00
|
|
|
endfor
|
|
|
|
|
2019-05-28 08:48:27 +00:00
|
|
|
if !empty(l:output)
|
|
|
|
return l:output
|
|
|
|
endif
|
|
|
|
|
2018-11-07 12:25:48 +00:00
|
|
|
" old checkstyle versions
|
|
|
|
let l:pattern = '\v(.+):(\d+): ([^:]+): (.+)$'
|
|
|
|
|
|
|
|
for l:match in ale#util#GetMatches(a:lines, l:pattern)
|
|
|
|
call add(l:output, {
|
|
|
|
\ 'type': l:match[3] is? 'warning' ? 'W' : 'E',
|
2021-07-04 12:31:00 +00:00
|
|
|
\ 'sub_type': 'style',
|
2018-11-07 12:25:48 +00:00
|
|
|
\ 'lnum': l:match[2] + 0,
|
|
|
|
\ 'text': l:match[4],
|
|
|
|
\})
|
|
|
|
endfor
|
|
|
|
|
2017-05-12 13:42:32 +00:00
|
|
|
return l:output
|
|
|
|
endfunction
|
|
|
|
|
2019-06-10 01:18:06 +00:00
|
|
|
function! s:GetConfig(buffer, config) abort
|
|
|
|
if ale#path#IsAbsolute(a:config)
|
|
|
|
return a:config
|
|
|
|
endif
|
|
|
|
|
|
|
|
let s:file = ale#path#FindNearestFile(a:buffer, a:config)
|
|
|
|
|
|
|
|
return !empty(s:file) ? s:file : a:config
|
|
|
|
endfunction
|
|
|
|
|
2017-05-12 13:42:32 +00:00
|
|
|
function! ale_linters#java#checkstyle#GetCommand(buffer) abort
|
2019-06-03 22:40:22 +00:00
|
|
|
let l:options = ale#Var(a:buffer, 'java_checkstyle_options')
|
|
|
|
let l:config_option = ale#Var(a:buffer, 'java_checkstyle_config')
|
2019-11-27 14:19:44 +00:00
|
|
|
let l:config = l:options !~# '\v(^| )-c ' && !empty(l:config_option)
|
2019-06-10 01:18:06 +00:00
|
|
|
\ ? s:GetConfig(a:buffer, l:config_option)
|
2019-06-03 22:40:22 +00:00
|
|
|
\ : ''
|
|
|
|
|
|
|
|
return '%e'
|
|
|
|
\ . ale#Pad(l:options)
|
|
|
|
\ . (!empty(l:config) ? ' -c ' . ale#Escape(l:config) : '')
|
2018-11-07 12:26:54 +00:00
|
|
|
\ . ' %s'
|
2017-05-12 13:42:32 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
call ale#linter#Define('java', {
|
|
|
|
\ 'name': 'checkstyle',
|
2019-06-03 22:40:22 +00:00
|
|
|
\ 'executable': {b -> ale#Var(b, 'java_checkstyle_executable')},
|
2019-02-22 18:05:04 +00:00
|
|
|
\ 'command': function('ale_linters#java#checkstyle#GetCommand'),
|
2017-05-12 13:42:32 +00:00
|
|
|
\ 'callback': 'ale_linters#java#checkstyle#Handle',
|
2018-11-07 12:26:54 +00:00
|
|
|
\ 'lint_file': 1,
|
2017-05-12 13:42:32 +00:00
|
|
|
\})
|