summaryrefslogtreecommitdiff
path: root/ale_linters/python/vulture.vim
blob: 84ffd49a4248ae48608681c504825b4fdba0ef2d (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
" Author: Yauheni Kirylau <actionless.loveless@gmail.com>
" Description: vulture linting for python files

call ale#Set('python_vulture_executable', 'vulture')
call ale#Set('python_vulture_options', '')
call ale#Set('python_vulture_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('python_vulture_change_directory', 1)

" The directory to change to before running vulture
function! s:GetDir(buffer) abort
    let l:project_root = ale#python#FindProjectRoot(a:buffer)

    return !empty(l:project_root)
    \   ? l:project_root
    \   : expand('#' . a:buffer . ':p:h')
endfunction

function! ale_linters#python#vulture#GetExecutable(buffer) abort
    return ale#python#FindExecutable(a:buffer, 'python_vulture', ['vulture'])
endfunction

function! ale_linters#python#vulture#GetCwd(buffer) abort
    if !ale#Var(a:buffer, 'python_vulture_change_directory')
        return ''
    endif

    return s:GetDir(a:buffer)
endfunction

function! ale_linters#python#vulture#GetCommand(buffer) abort
    let l:executable = ale_linters#python#vulture#GetExecutable(a:buffer)
    let l:exec_args = l:executable =~? 'pipenv$'
    \   ? ' run vulture'
    \   : ''
    let l:lint_dest = ale#Var(a:buffer, 'python_vulture_change_directory')
    \   ? ' .'
    \   : ' %s'

    return ale#Escape(l:executable) . l:exec_args
    \   . ' '
    \   . ale#Var(a:buffer, 'python_vulture_options')
    \   . l:lint_dest
endfunction


function! ale_linters#python#vulture#Handle(buffer, lines) abort
    let l:output = ale#python#HandleTraceback(a:lines, 10)

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

    " Matches patterns line the following:
    let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+): (.*)$'
    let l:dir = s:GetDir(a:buffer)

    for l:match in ale#util#GetMatches(a:lines, l:pattern)
        let l:abspath = ale#path#GetAbsPath(l:dir, l:match[1])
        let l:item = {
        \   'filename': l:abspath,
        \   'lnum': l:match[2] + 0,
        \   'text': l:match[3],
        \   'type': 'W',
        \}
        call add(l:output, l:item)
    endfor

    return l:output
endfunction


call ale#linter#Define('python', {
\   'name': 'vulture',
\   'executable': function('ale_linters#python#vulture#GetExecutable'),
\   'cwd': function('ale_linters#python#vulture#GetCwd'),
\   'command': function('ale_linters#python#vulture#GetCommand'),
\   'callback': 'ale_linters#python#vulture#Handle',
\   'lint_file': 1,
\})