summaryrefslogtreecommitdiff
path: root/ale_linters/crystal/ameba.vim
blob: d50d9fd399f0b62ff4c3733dbe9d6c51c834d4c2 (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: Harrison Bachrach - https://github.com/HarrisonB
" Description: Ameba, a linter for crystal files

call ale#Set('crystal_ameba_executable', 'bin/ameba')

function! ale_linters#crystal#ameba#GetCommand(buffer) abort
    let l:executable = ale#Var(a:buffer, 'crystal_ameba_executable')

    return ale#Escape(l:executable)
    \   . ' --format json '
    \   .  ale#Escape(expand('#' . a:buffer . ':p'))
endfunction

call ale#linter#Define('crystal', {
\   'name': 'ameba',
\   'executable_callback': ale#VarFunc('crystal_ameba_executable'),
\   'command_callback': 'ale_linters#crystal#ameba#GetCommand',
\   'callback': 'ale_linters#crystal#ameba#HandleAmebaOutput',
\})

" Handle output from ameba
function! ale_linters#crystal#ameba#HandleAmebaOutput(buffer, lines) abort
    if len(a:lines) == 0
      return []
    endif

    let l:errors = ale#util#FuzzyJSONDecode(a:lines[0], {})

    if !has_key(l:errors, 'summary')
    \|| l:errors['summary']['issues_count'] == 0
    \|| empty(l:errors['sources'])
        return []
    endif

    let l:output = []

    for l:error in l:errors['sources'][0]['issues']
        let l:start_col = str2nr(l:error['location']['column'])
        let l:end_col = str2nr(l:error['end_location']['column'])

        if !l:end_col
          let l:end_col = l:start_col + 1
        endif

        call add(l:output, {
        \   'lnum': str2nr(l:error['location']['line']),
        \   'col': l:start_col,
        \   'end_col': l:end_col,
        \   'code': l:error['rule_name'],
        \   'text': l:error['message'],
        \   'type': 'W',
        \})
    endfor

    return l:output
endfunction