summaryrefslogtreecommitdiff
path: root/ale_linters/go/gometalinter.vim
blob: 71d60c54f06e947afaaf9671e92f3bb7470e0b22 (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
" Author: Ben Reedy <https://github.com/breed808>
" Description: Adds support for the gometalinter suite for Go files

if !exists('g:ale_go_gometalinter_options')
    let g:ale_go_gometalinter_options = ''
endif

function! ale_linters#go#gometalinter#GetCommand(buffer) abort
    return 'gometalinter '
    \   . ale#Var(a:buffer, 'go_gometalinter_options')
    \   . ' ' . fnameescape(fnamemodify(bufname(a:buffer), ':p:h'))
endfunction

function! ale_linters#go#gometalinter#Handler(buffer, lines) abort
    " Matches patterns line the following:
    "
    " file.go:27: missing argument for Printf("%s"): format reads arg 2, have only 1 args
    " file.go:53:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
    " file.go:5:2: expected declaration, found 'STRING' "log"

    " gometalinter returns relative paths so use tail of filename as part of pattern matcher
    let l:filename = fnamemodify(bufname(a:buffer), ':t')
    let l:path_pattern = '[a-zA-Z]\?\\\?:\?[[:alnum:]/\.\-_]\+'
    let l:pattern = '^' . l:path_pattern . ':\(\d\+\):\?\(\d\+\)\?:\?:\?\(warning\|error\):\?\s\*\?\(.\+\)$'
    let l:output = []

    for l:line in a:lines
        let l:match = matchlist(l:line, l:pattern)

        " Omit errors from files other than the one currently open
        if len(l:match) == 0 || l:line !~ l:filename
            continue
        endif

        " vcol is Needed to indicate that the column is a character.
        call add(l:output, {
        \   'bufnr': a:buffer,
        \   'lnum': l:match[1] + 0,
        \   'vcol': 0,
        \   'col': l:match[2] + 0,
        \   'text': l:match[4],
        \   'type': tolower(l:match[3]) ==# 'warning' ? 'W' : 'E',
        \   'nr': -1,
        \})
    endfor

    return l:output
endfunction

call ale#linter#Define('go', {
\   'name': 'gometalinter',
\   'executable': 'gometalinter',
\   'command_callback': 'ale_linters#go#gometalinter#GetCommand',
\   'callback': 'ale_linters#go#gometalinter#Handler',
\   'lint_file': 1,
\})