#149 - Support info and style problem types for status lines

This commit is contained in:
w0rp 2017-05-21 15:37:45 +01:00
parent 3532257a1a
commit ab44d05508
3 changed files with 140 additions and 85 deletions

View File

@ -1,77 +1,81 @@
" Author: KabbAmine <amine.kabb@gmail.com> " Author: KabbAmine <amine.kabb@gmail.com>
" Description: Statusline related function(s) " Description: Statusline related function(s)
function! s:CreateCountDict() abort
" Keys 0 and 1 are for backwards compatibility.
" The count object used to be a List of [error_count, warning_count].
return {
\ '0': 0,
\ '1': 0,
\ 'error': 0,
\ 'warning': 0,
\ 'info': 0,
\ 'style_error': 0,
\ 'style_warning': 0,
\ 'total': 0,
\}
endfunction
" Update the buffer error/warning count with data from loclist. " Update the buffer error/warning count with data from loclist.
function! ale#statusline#Update(buffer, loclist) abort function! ale#statusline#Update(buffer, loclist) abort
if !exists('g:ale_buffer_info') if !exists('g:ale_buffer_info') || !has_key(g:ale_buffer_info, a:buffer)
return return
endif endif
if !has_key(g:ale_buffer_info, a:buffer) let l:count = s:CreateCountDict()
return let l:count.total = len(a:loclist)
endif
let l:errors = 0
let l:warnings = 0
for l:entry in a:loclist for l:entry in a:loclist
if l:entry.type ==# 'E' if l:entry.type ==# 'W'
let l:errors += 1 if get(l:entry, 'sub_type', '') ==# 'style'
let l:count.warning += 1
else
let l:count.style_warning += 1
endif
elseif l:entry.type ==# 'I'
let l:count.info += 1
elseif get(l:entry, 'sub_type', '') ==# 'style'
let l:count.style_error += 1
else else
let l:warnings += 1 let l:count.error += 1
endif endif
endfor endfor
let g:ale_buffer_info[a:buffer].count = [l:errors, l:warnings] " Set keys for backwards compatibility.
let l:count[0] = l:count.error + l:count.style_error
let l:count[1] = l:count.total - l:count[0]
let g:ale_buffer_info[a:buffer].count = l:count
endfunction endfunction
" Set the error and warning counts, calling for an update only if needed. " Get the counts for the buffer, and update the counts if needed.
" If counts cannot be set, return 0. function! s:GetCounts(buffer) abort
function! s:SetupCount(buffer) abort if !exists('g:ale_buffer_info') || !has_key(g:ale_buffer_info, a:buffer)
if !has_key(g:ale_buffer_info, a:buffer) return s:CreateCountDict()
" Linters have not been run for the buffer yet, so stop here. endif
return 0
endif
" Cache is cold, so manually ask for an update. " Cache is cold, so manually ask for an update.
if !has_key(g:ale_buffer_info[a:buffer], 'count') if !has_key(g:ale_buffer_info[a:buffer], 'count')
call ale#statusline#Update(a:buffer, g:ale_buffer_info[a:buffer].loclist) call ale#statusline#Update(a:buffer, g:ale_buffer_info[a:buffer].loclist)
endif endif
return 1
endfunction
" Returns a tuple of errors and warnings for use in third-party integrations.
function! ale#statusline#Count(buffer) abort
if !exists('g:ale_buffer_info')
return [0, 0]
endif
if !s:SetupCount(a:buffer)
return [0, 0]
endif
return g:ale_buffer_info[a:buffer].count return g:ale_buffer_info[a:buffer].count
endfunction endfunction
" Returns a formatted string that can be integrated in the statusline. " Returns a Dictionary with counts for use in third party integrations.
function! ale#statusline#Status() abort function! ale#statusline#Count(buffer) abort
if !exists('g:ale_buffer_info') " The Dictionary is copied here before exposing it to other plugins.
return 'OK' return copy(s:GetCounts(a:buffer))
endif endfunction
" This is the historical format setting which could be configured before.
function! s:StatusForListFormat() abort
let [l:error_format, l:warning_format, l:no_errors] = g:ale_statusline_format let [l:error_format, l:warning_format, l:no_errors] = g:ale_statusline_format
let l:buffer = bufnr('%') let l:counts = s:GetCounts(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. " Build strings based on user formatting preferences.
let l:errors = l:error_count ? printf(l:error_format, l:error_count) : '' let l:errors = l:counts[0] ? printf(l:error_format, l:counts[0]) : ''
let l:warnings = l:warning_count ? printf(l:warning_format, l:warning_count) : '' let l:warnings = l:counts[1] ? printf(l:warning_format, l:counts[1]) : ''
" Different formats based on the combination of errors and warnings. " Different formats based on the combination of errors and warnings.
if empty(l:errors) && empty(l:warnings) if empty(l:errors) && empty(l:warnings)
@ -84,3 +88,16 @@ function! ale#statusline#Status() abort
return l:res return l:res
endfunction endfunction
" Returns a formatted string that can be integrated in the statusline.
function! ale#statusline#Status() abort
if !exists('g:ale_statusline_format')
return 'OK'
endif
if type(g:ale_statusline_format) == type([])
return s:StatusForListFormat()
endif
return ''
endfunction

View File

@ -1,21 +1,46 @@
Before: Before:
let g:ale_statusline_format = ['%sE', '%sW', 'OKIE'] Save g:ale_statusline_format, g:ale_buffer_info
let g:ale_buffer_info = {}
" A function for conveniently creating expected count objects.
function Counts(data) abort
let l:res = {
\ '0': 0,
\ '1': 0,
\ 'error': 0,
\ 'warning': 0,
\ 'info': 0,
\ 'style_error': 0,
\ 'style_warning': 0,
\ 'total': 0,
\}
for l:key in keys(a:data)
let l:res[l:key] = a:data[l:key]
endfor
let l:res[0] = l:res.error + l:res.style_error
let l:res[1] = l:res.warning + l:res.style_warning + l:res.info
let l:res.total = l:res[0] + l:res[1]
return l:res
endfunction
After: After:
let g:ale_buffer_info = {} Restore
delfunction Counts
Execute (Count should be 0 when data is empty): Execute (Count should be 0 when data is empty):
let g:ale_buffer_info = {} AssertEqual Counts({}), ale#statusline#Count(bufnr('%'))
AssertEqual [0, 0], ale#statusline#Count(bufnr('%'))
Execute (Count should read data from the cache): Execute (Count should read data from the cache):
let g:ale_buffer_info = {'44': {'count': [1, 2]}} let g:ale_buffer_info = {'44': {'count': Counts({'error': 1, 'warning': 2})}}
AssertEqual [1, 2], ale#statusline#Count(44) AssertEqual Counts({'error': 1, 'warning': 2}), ale#statusline#Count(44)
Execute (The count should be correct after an update): Execute (The count should be correct after an update):
let g:ale_buffer_info = {'44': {}} let g:ale_buffer_info = {'44': {}}
call ale#statusline#Update(44, []) call ale#statusline#Update(44, [])
AssertEqual [0, 0], ale#statusline#Count(44) AssertEqual Counts({}), ale#statusline#Count(44)
Execute (Count should be match the loclist): Execute (Count should be match the loclist):
let g:ale_buffer_info = { let g:ale_buffer_info = {
@ -34,27 +59,59 @@ Execute (Count should be match the loclist):
\ ], \ ],
\ }, \ },
\} \}
AssertEqual [1, 0], ale#statusline#Count(bufnr('%')) AssertEqual Counts({'error': 1}), ale#statusline#Count(bufnr('%'))
Execute (Output should be empty for non-existant buffer): Execute (Output should be empty for non-existant buffer):
AssertEqual [0, 0], ale#statusline#Count(9001) AssertEqual Counts({}), ale#statusline#Count(9001)
Execute (Statusline is formatted to the users preference for just errors): Execute (Status() should return just errors for the old format):
let g:ale_statusline_format = ['%sE', '%sW', 'OKIE']
let g:ale_buffer_info = {bufnr('%'): {}} let g:ale_buffer_info = {bufnr('%'): {}}
call ale#statusline#Update(bufnr('%'), [{'type': 'E'}, {'type': 'E'}]) call ale#statusline#Update(bufnr('%'), [
\ {'type': 'E'},
\ {'type': 'E', 'sub_type': 'style'},
\])
AssertEqual '2E', ale#statusline#Status() AssertEqual '2E', ale#statusline#Status()
Execute (Statusline is formatted to the users preference for just warnings): Execute (Status() should return just warnings for the old format):
let g:ale_statusline_format = ['%sE', '%sW', 'OKIE']
let g:ale_buffer_info = {bufnr('%'): {}} let g:ale_buffer_info = {bufnr('%'): {}}
call ale#statusline#Update(bufnr('%'), [{'type': 'W'}, {'type': 'W'}, {'type': 'W'}]) call ale#statusline#Update(bufnr('%'), [
\ {'type': 'W'},
\ {'type': 'W', 'sub_type': 'style'},
\ {'type': 'I'},
\])
AssertEqual '3W', ale#statusline#Status() AssertEqual '3W', ale#statusline#Status()
Execute (Statusline is formatted to the users preference for errors and warnings): Execute (Status() should return errors and warnings for the old format):
let g:ale_statusline_format = ['%sE', '%sW', 'OKIE']
let g:ale_buffer_info = {bufnr('%'): {}} let g:ale_buffer_info = {bufnr('%'): {}}
call ale#statusline#Update(bufnr('%'), [{'type': 'E'}, {'type': 'W'}, {'type': 'W'}]) call ale#statusline#Update(bufnr('%'), [
AssertEqual '1E 2W', ale#statusline#Status() \ {'type': 'E'},
\ {'type': 'E', 'sub_type': 'style'},
\ {'type': 'W'},
\ {'type': 'W', 'sub_type': 'style'},
\ {'type': 'I'},
\])
AssertEqual '2E 3W', ale#statusline#Status()
Execute (Statusline is formatted to the users preference for no errors or warnings): Execute (Status() should return the custom 'OK' string with the old format):
let g:ale_statusline_format = ['%sE', '%sW', 'OKIE']
let g:ale_buffer_info = {bufnr('%'): {}} let g:ale_buffer_info = {bufnr('%'): {}}
call ale#statusline#Update(bufnr('%'), []) call ale#statusline#Update(bufnr('%'), [])
AssertEqual 'OKIE', ale#statusline#Status() AssertEqual 'OKIE', ale#statusline#Status()
Execute(ale#statusline#Update shouldn't blow up when globals are undefined):
unlet! g:ale_buffer_info
unlet! g:ale_statusline_format
call ale#statusline#Update(1, [])
Execute(ale#statusline#Count should return 0 counts when globals are undefined):
unlet! g:ale_buffer_info
unlet! g:ale_statusline_format
AssertEqual Counts({}), ale#statusline#Count(1)
Execute(ale#statusline#Status should return 'OK' when globals are undefined):
unlet! g:ale_buffer_info
unlet! g:ale_statusline_format
AssertEqual 'OK', ale#statusline#Status()

View File

@ -1,19 +0,0 @@
" This file tests that statusline functions return meaningful output even
" when most of ALE itself has not been loaded.
"
" This is important for plugins which integrate with ALE like airline.
Before:
unlet! g:ale_buffer_info
After:
let g:ale_buffer_info = {}
Execute(ale#statusline#Update shouldn't blow up when globals are undefined):
call ale#statusline#Update(1, [])
Execute(ale#statusline#Count should return 0 counts when globals are undefined):
AssertEqual [0, 0], ale#statusline#Count(1)
Execute(ale#statusline#Status should return 'OK' when globals are undefined):
AssertEqual 'OK', ale#statusline#Status()