Implement a more efficient statusbar

The statusbar now keeps its state in a separate variable, in order to
avoid excess iterations. The engine now updates said variable on run,
and a new function is made available for external statusbars to call (to
avoid dependencies on internal implementation details of ale).

To keep things light, the status bar code is not loaded unless invoked
by the user or an external plugin. On the first load it will update
itself from the global loclist, after that, the engine will handle all
updates.

The external integration function, `ale#statusline#Count()`, will return
a tuple in the format [E, W] (where E is errors, W is warnings), unless
no data exists (ie, the plugin doesn't have a linter for a file or has
not run yet), in which case it returns 0/false.
This commit is contained in:
Bjorn Neergaard
2016-10-11 16:51:01 -05:00
parent f4159ac7ee
commit dc58db7640
5 changed files with 76 additions and 40 deletions

View File

@@ -1,20 +1,12 @@
" Author: KabbAmine <amine.kabb@gmail.com>
" Description: Statusline related function(s)
function! ale#statusline#Status() abort
" Returns a formatted string that can be integrated in the
" statusline
let l:buffer = bufnr('%')
let l:loclist = g:ale_buffer_loclist_map
if !has_key(l:loclist, l:buffer)
return ''
endif
" 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 l:loclist[l:buffer]
for l:entry in a:loclist
if l:entry.type ==# 'E'
let l:errors += 1
else
@@ -22,8 +14,41 @@ function! ale#statusline#Status() abort
endif
endfor
let l:errors = l:errors ? printf(g:ale_statusline_format[0], l:errors) : ''
let l:warnings = l:warnings ? printf(g:ale_statusline_format[1], l:warnings) : ''
let g:ale_buffer_count_map[a:buffer] = [l:errors, l:warnings]
endfunction
" Returns a tuple of errors and warnings (or false if no data exists)
" for use in third-party integrations.
function! ale#statusline#Count(buffer) abort
if !has_key(g:ale_buffer_count_map, a:buffer)
if has_key(g:ale_buffer_loclist_map, a:buffer)
call ale#statusline#Update(a:buffer, g:ale_buffer_loclist_map[a:buffer])
return ale#statusline#Count(a:buffer)
else
return 0
endif
endif
return g:ale_buffer_count_map[a:buffer]
endfunction
" Returns a formatted string that can be integrated in the statusline.
function! ale#statusline#Status() abort
let l:buffer = bufnr('%')
if !has_key(g:ale_buffer_count_map, l:buffer)
if has_key(g:ale_buffer_loclist_map, l:buffer)
call ale#statusline#Update(l:buffer, g:ale_buffer_loclist_map[l:buffer])
return ale#statusline#Status()
else
return ''
endif
endif
let l:errors = g:ale_buffer_count_map[l:buffer][0] ?
\ printf(g:ale_statusline_format[0], g:ale_buffer_count_map[l:buffer][0]) : ''
let l:warnings = g:ale_buffer_count_map[l:buffer][1] ?
\ printf(g:ale_statusline_format[1], g:ale_buffer_count_map[l:buffer][1]) : ''
let l:no_errors = g:ale_statusline_format[2]
" Different formats if no errors or no warnings