]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/statusline.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / statusline.vim
1 " Author: KabbAmine <amine.kabb@gmail.com>
2 " Additions by: petpetpetpet <chris@freelanceninjas.com>
3 " Description: Statusline related function(s)
4
5 function! s:CreateCountDict() abort
6     " Keys 0 and 1 are for backwards compatibility.
7     " The count object used to be a List of [error_count, warning_count].
8     return {
9     \   '0': 0,
10     \   '1': 0,
11     \   'error': 0,
12     \   'warning': 0,
13     \   'info': 0,
14     \   'style_error': 0,
15     \   'style_warning': 0,
16     \   'total': 0,
17     \}
18 endfunction
19
20 " Update the buffer error/warning count with data from loclist.
21 function! ale#statusline#Update(buffer, loclist) abort
22     if !exists('g:ale_buffer_info') || !has_key(g:ale_buffer_info, a:buffer)
23         return
24     endif
25
26     let l:loclist = filter(copy(a:loclist), 'v:val.bufnr == a:buffer')
27     let l:count = s:CreateCountDict()
28     let l:count.total = len(l:loclist)
29
30     " Allows easy access to the first instance of each problem type.
31     let l:first_problems = {}
32
33     for l:entry in l:loclist
34         if l:entry.type is# 'W'
35             if get(l:entry, 'sub_type', '') is# 'style'
36                 let l:count.style_warning += 1
37
38                 if l:count.style_warning == 1
39                     let l:first_problems.style_warning = l:entry
40                 endif
41             else
42                 let l:count.warning += 1
43
44                 if l:count.warning == 1
45                     let l:first_problems.warning = l:entry
46                 endif
47             endif
48         elseif l:entry.type is# 'I'
49             let l:count.info += 1
50
51             if l:count.info == 1
52                 let l:first_problems.info = l:entry
53             endif
54         elseif get(l:entry, 'sub_type', '') is# 'style'
55             let l:count.style_error += 1
56
57             if l:count.style_error == 1
58                 let l:first_problems.style_error = l:entry
59             endif
60         else
61             let l:count.error += 1
62
63             if l:count.error == 1
64                 let l:first_problems.error = l:entry
65             endif
66         endif
67     endfor
68
69     " Set keys for backwards compatibility.
70     let l:count[0] = l:count.error + l:count.style_error
71     let l:count[1] = l:count.total - l:count[0]
72
73     let g:ale_buffer_info[a:buffer].count = l:count
74     let g:ale_buffer_info[a:buffer].first_problems = l:first_problems
75 endfunction
76
77 " Get the counts for the buffer, and update the counts if needed.
78 function! s:UpdateCacheIfNecessary(buffer) abort
79     " Cache is cold, so manually ask for an update.
80     if !has_key(g:ale_buffer_info[a:buffer], 'count')
81         call ale#statusline#Update(
82         \   a:buffer,
83         \   g:ale_buffer_info[a:buffer].loclist
84         \)
85     endif
86 endfunction
87
88 function! s:BufferCacheExists(buffer) abort
89     if !exists('g:ale_buffer_info') || !has_key(g:ale_buffer_info, a:buffer)
90         return 0
91     endif
92
93     return 1
94 endfunction
95
96 " Get the counts for the buffer, and update the counts if needed.
97 function! s:GetCounts(buffer) abort
98     if !s:BufferCacheExists(a:buffer)
99         return s:CreateCountDict()
100     endif
101
102     call s:UpdateCacheIfNecessary(a:buffer)
103
104     return g:ale_buffer_info[a:buffer].count
105 endfunction
106
107 " Get the dict of first_problems, update the buffer info cache if necessary.
108 function! s:GetFirstProblems(buffer) abort
109     if !s:BufferCacheExists(a:buffer)
110         return {}
111     endif
112
113     call s:UpdateCacheIfNecessary(a:buffer)
114
115     return g:ale_buffer_info[a:buffer].first_problems
116 endfunction
117
118 " Returns a Dictionary with counts for use in third party integrations.
119 function! ale#statusline#Count(buffer) abort
120     " The Dictionary is copied here before exposing it to other plugins.
121     return copy(s:GetCounts(a:buffer))
122 endfunction
123
124 " Returns a copy of the *first* locline instance of the specified problem
125 " type. (so this would allow an external integration to know all the info
126 " about the first style warning in the file, for example.)
127 function! ale#statusline#FirstProblem(buffer, type) abort
128     let l:first_problems = s:GetFirstProblems(a:buffer)
129
130     if !empty(l:first_problems) && has_key(l:first_problems, a:type)
131         return copy(l:first_problems[a:type])
132     endif
133
134     return {}
135 endfunction