summaryrefslogtreecommitdiff
path: root/ale_linters/java/checkstyle.vim
blob: ee3ca7a33dbb55dc8e2e760d0ded181919ffd588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
" Author: Devon Meunier <devon.meunier@gmail.com>
" Description: checkstyle for Java files

call ale#Set('java_checkstyle_executable', 'checkstyle')
call ale#Set('java_checkstyle_config', 'google_checks.xml')
call ale#Set('java_checkstyle_options', '')

function! ale_linters#java#checkstyle#Handle(buffer, lines) abort
    let l:output = []

    " modern checkstyle versions
    let l:pattern = '\v\[(WARN|ERROR)\] [a-zA-Z]?:?[^:]+:(\d+):(\d+)?:? (.*) \[(.+)\]$'

    for l:match in ale#util#GetMatches(a:lines, l:pattern)
        call add(l:output, {
        \   'type': l:match[1] is? 'WARN' ? 'W' : 'E',
        \   'lnum': l:match[2] + 0,
        \   'col': l:match[3] + 0,
        \   'text': l:match[4],
        \   'code': l:match[5],
        \})
    endfor

    if !empty(l:output)
        return l:output
    endif

    " 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',
        \   'lnum': l:match[2] + 0,
        \   'text': l:match[4],
        \})
    endfor

    return l:output
endfunction

function! ale_linters#java#checkstyle#GetCommand(buffer) abort
    let l:options = ale#Var(a:buffer, 'java_checkstyle_options')
    let l:config_option = ale#Var(a:buffer, 'java_checkstyle_config')
    let l:config = l:options !~# '\v(^| )-c' && !empty(l:config_option)
    \   ? ale#path#FindNearestFile(a:buffer, l:config_option)
    \   : ''

    return '%e'
    \ . ale#Pad(l:options)
    \ . (!empty(l:config) ? ' -c ' . ale#Escape(l:config) : '')
    \ . ' %s'
endfunction

call ale#linter#Define('java', {
\   'name': 'checkstyle',
\   'executable': {b -> ale#Var(b, 'java_checkstyle_executable')},
\   'command': function('ale_linters#java#checkstyle#GetCommand'),
\   'callback': 'ale_linters#java#checkstyle#Handle',
\   'lint_file': 1,
\})