diff --git a/autoload/ale/statusline.vim b/autoload/ale/statusline.vim index efb7e9e..337db22 100644 --- a/autoload/ale/statusline.vim +++ b/autoload/ale/statusline.vim @@ -1,77 +1,81 @@ " Author: KabbAmine " 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. 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 endif - if !has_key(g:ale_buffer_info, a:buffer) - return - endif - - let l:errors = 0 - let l:warnings = 0 + let l:count = s:CreateCountDict() + let l:count.total = len(a:loclist) for l:entry in a:loclist - if l:entry.type ==# 'E' - let l:errors += 1 + if l:entry.type ==# 'W' + 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 - let l:warnings += 1 + let l:count.error += 1 endif 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 -" 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 +" Get the counts for the buffer, and update the counts if needed. +function! s:GetCounts(buffer) abort +if !exists('g:ale_buffer_info') || !has_key(g:ale_buffer_info, a:buffer) + return s:CreateCountDict() +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 !exists('g:ale_buffer_info') - return [0, 0] - endif - - if !s:SetupCount(a:buffer) - return [0, 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 g:ale_buffer_info[a:buffer].count endfunction -" Returns a formatted string that can be integrated in the statusline. -function! ale#statusline#Status() abort - if !exists('g:ale_buffer_info') - return 'OK' - endif +" Returns a Dictionary with counts for use in third party integrations. +function! ale#statusline#Count(buffer) abort + " The Dictionary is copied here before exposing it to other plugins. + return copy(s:GetCounts(a:buffer)) +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: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 + let l:counts = s:GetCounts(bufnr('')) " 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) : '' + let l:errors = l:counts[0] ? printf(l:error_format, l:counts[0]) : '' + let l:warnings = l:counts[1] ? printf(l:warning_format, l:counts[1]) : '' " Different formats based on the combination of errors and warnings. if empty(l:errors) && empty(l:warnings) @@ -84,3 +88,16 @@ function! ale#statusline#Status() abort return l:res 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 diff --git a/test/test_statusline.vader b/test/test_statusline.vader index 05e6047..d7c6b15 100644 --- a/test/test_statusline.vader +++ b/test/test_statusline.vader @@ -1,21 +1,46 @@ 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: - let g:ale_buffer_info = {} + Restore + delfunction Counts Execute (Count should be 0 when data is empty): - let g:ale_buffer_info = {} - AssertEqual [0, 0], ale#statusline#Count(bufnr('%')) + AssertEqual Counts({}), ale#statusline#Count(bufnr('%')) Execute (Count should read data from the cache): - let g:ale_buffer_info = {'44': {'count': [1, 2]}} - AssertEqual [1, 2], ale#statusline#Count(44) + let g:ale_buffer_info = {'44': {'count': Counts({'error': 1, 'warning': 2})}} + AssertEqual Counts({'error': 1, 'warning': 2}), ale#statusline#Count(44) Execute (The count should be correct after an update): let g:ale_buffer_info = {'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): 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): - 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('%'): {}} - 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() -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('%'): {}} - 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() -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('%'): {}} - call ale#statusline#Update(bufnr('%'), [{'type': 'E'}, {'type': 'W'}, {'type': 'W'}]) - AssertEqual '1E 2W', ale#statusline#Status() + call ale#statusline#Update(bufnr('%'), [ + \ {'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('%'): {}} call ale#statusline#Update(bufnr('%'), []) 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() diff --git a/test/test_statusline_api_without_globals.vader b/test/test_statusline_api_without_globals.vader deleted file mode 100644 index 29677f3..0000000 --- a/test/test_statusline_api_without_globals.vader +++ /dev/null @@ -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()