summaryrefslogtreecommitdiff
path: root/ale_linters/nim/nimcheck.vim
blob: 0b8cfc8f7e05302ee4b13038e79bfee8f39e6148 (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
64
65
" Author: Baabelfish
" Description: Typechecking for nim files


function! ale_linters#nim#nimcheck#Handle(buffer, lines) abort
    let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p:t')
    let l:pattern = '^\(.\+\.nim\)(\(\d\+\), \(\d\+\)) \(.\+\)'
    let l:output = [] 

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

        if len(l:match) == 0
            continue
        endif

        " Only show errors of the current buffer
        " NOTE: Checking filename only is OK because nim enforces unique
        "       module names.

        let l:temp_buffer_filename = fnamemodify(l:match[1], ':p:t')
        if l:buffer_filename !=# '' && l:temp_buffer_filename !=# l:buffer_filename
            continue
        endif

        let l:line = l:match[2] + 0
        let l:column = l:match[3] + 0
        let l:text = l:match[4]
        let l:type = 'W'

        " Extract error type from message of type 'Error: Some error message'
        let l:textmatch = matchlist(l:match[4], '^\(.\{-}\): .\+$')

        if len(l:textmatch) > 0
            let l:errortype = l:textmatch[1]
            if l:errortype ==# 'Error'
                let l:type = 'E'
            endif
        endif

        call add(l:output, {
        \   'bufnr': a:buffer,
        \   'lnum': l:line,
        \   'col': l:column,
        \   'text': l:text,
        \   'type': l:type,
        \})
    endfor

    return l:output
endfunction


function! ale_linters#nim#nimcheck#GetCommand(buffer)
    return 'nim check --path:' . fnameescape(fnamemodify(bufname(a:buffer), ':p:h')) . ' --threads:on --verbosity:0 --colors:off --listFullPaths %t'
endfunction


call ale#linter#Define('nim', {
\    'name': 'nimcheck',
\    'executable': 'nim',
\    'output_stream': 'both',
\    'command_callback': 'ale_linters#nim#nimcheck#GetCommand',
\    'callback': 'ale_linters#nim#nimcheck#Handle'
\})