summaryrefslogtreecommitdiff
path: root/ale_linters/nix/nix.vim
blob: 7c2ce963c66cecd712cbde22483c157361437de4 (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
62
63
" Author: Alistair Bill <@alibabzo>
" Author: Maximilian Bosch <maximilian@mbosch.me>
" Description: nix-instantiate linter for nix files

function! ale_linters#nix#nix#Command(buffer, output, meta) abort
    let l:version = a:output[0][22:]

    if l:version =~# '^\(2.[4-9]\|3\).*'
        return 'nix-instantiate --log-format internal-json --parse -'
    else
        return 'nix-instantiate --parse -'
    endif
endfunction

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

    if empty(a:lines)
        return l:output
    endif

    if a:lines[0] =~# '^@nix .*'
        for l:line in a:lines
            if l:line =~# '^@nix .*'
                let l:result = json_decode(strpart(l:line, 4))

                if has_key(l:result, 'column')
                    call add(l:output, {
                    \     'type': 'E',
                    \     'lnum': l:result.line,
                    \     'col': l:result.column,
                    \     'text': l:result.raw_msg
                    \})
                endif
            endif
        endfor
    else
        let l:pattern = '^\(.\+\): \(.\+\) at .*:\(\d\+\):\(\d\+\)$'

        for l:match in ale#util#GetMatches(a:lines, l:pattern)
            call add(l:output, {
            \   'lnum': l:match[3] + 0,
            \   'col': l:match[4] + 0,
            \   'text': l:match[1] . ': ' . substitute(l:match[2], ',$', '', ''),
            \   'type': l:match[1] =~# '^error' ? 'E' : 'W',
            \})
        endfor
    endif

    return l:output
endfunction

call ale#linter#Define('nix', {
\   'name': 'nix',
\   'output_stream': 'stderr',
\   'executable': 'nix-instantiate',
\   'command': {buffer -> ale#command#Run(
\       buffer,
\       'nix-instantiate --version',
\       function('ale_linters#nix#nix#Command')
\   )},
\   'callback': 'ale_linters#nix#nix#Handle',
\})