summaryrefslogtreecommitdiff
path: root/autoload/ale/statusline.vim
blob: c01dd34dd1faaf3fe2ba76f64124f9e40b7ec468 (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
" Author: KabbAmine <amine.kabb@gmail.com>
" Description: Statusline related function(s)

" Update the buffer error/warning count with data from loclist.
function! ale#statusline#Update(buffer, loclist) abort
    let l:errors = 0
    let l:warnings = 0

    for l:entry in a:loclist
        if l:entry.type ==# 'E'
            let l:errors += 1
        else
            let l:warnings += 1
        endif
    endfor

    let g:ale_buffer_info[a:buffer].count = [l:errors, l:warnings]
endfunction

" Set the error and warning counts, calling for an update only if needed.
" If counts cannot be set, return 0.
function! s:SetupCount(buffer) abort
    if !has_key(g:ale_buffer_info, a:buffer)
        " Linters have not been run for the buffer yet, so stop here.
        return 0
    endif

    " Cache is cold, so manually ask for an update.
    if !has_key(g:ale_buffer_info[a:buffer], 'count')
        call ale#statusline#Update(a:buffer, g:ale_buffer_info[a:buffer].loclist)
    endif

    return 1
endfunction

" Returns a tuple of errors and warnings for use in third-party integrations.
function! ale#statusline#Count(buffer) abort
    if !s:SetupCount(a:buffer)
        return [0, 0]
    endif

    return g:ale_buffer_info[a:buffer].count
endfunction

" Returns a formatted string that can be integrated in the statusline.
function! ale#statusline#Status() abort
    let [l:error_format, l:warning_format, l:no_errors] = g:ale_statusline_format
    let l:buffer = bufnr('%')

    if !s:SetupCount(l:buffer)
        return l:no_errors
    endif

    let [l:error_count, l:warning_count] = g:ale_buffer_info[l:buffer].count

    " Build strings based on user formatting preferences.
    let l:errors = l:error_count ? printf(l:error_format, l:error_count) : ''
    let l:warnings = l:warning_count ? printf(l:warning_format, l:warning_count) : ''

    " Different formats based on the combination of errors and warnings.
    if empty(l:errors) && empty(l:warnings)
        let l:res = l:no_errors
    elseif !empty(l:errors) && !empty(l:warnings)
        let l:res = printf('%s %s', l:errors, l:warnings)
    else
        let l:res = empty(l:errors) ? l:warnings : l:errors
    endif

    return l:res
endfunction