From 57c061dcb003b6022ee20cbfc331afa0e5811af2 Mon Sep 17 00:00:00 2001 From: petpetpet Date: Sun, 27 Jan 2019 13:57:18 +0000 Subject: [PATCH] Added a hook to the new ALE api to grab line numbers. Should be more efficient. --- autoload/airline/extensions/ale.vim | 43 ++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/autoload/airline/extensions/ale.vim b/autoload/airline/extensions/ale.vim index 71f63173..90b392ba 100644 --- a/autoload/airline/extensions/ale.vim +++ b/autoload/airline/extensions/ale.vim @@ -7,7 +7,11 @@ function! s:airline_ale_count(cnt, symbol) return a:cnt ? a:symbol. a:cnt : '' endfunction -function! s:airline_ale_get_line_number(cnt, type) abort +function! s:legacy_airline_ale_get_line_number(cnt, type) abort + " Before ALE introduced the FirstProblem API function, this is how + " airline would get the line numbers: + " 1. Get the whole loclist; 2. Filter it for the desired problem type. + " 3. Return the line number of the first element in the filtered list. if a:cnt == 0 return '' endif @@ -28,6 +32,43 @@ function! s:airline_ale_get_line_number(cnt, type) abort return open_lnum_symbol . problems[0].lnum . close_lnum_symbol endfunction +function! s:new_airline_ale_get_line_number(cnt, type) abort + " The FirstProblem call in ALE is a far more efficient way + " of obtaining line number data. If the installed ALE supports + " it, we should use this method of getting line data. + if a:cnt == 0 + return '' + endif + let l:buffer = bufnr('') + + " Try to get the first error from ALE. + let l:result = ale#statusline#FirstProblem(l:buffer, a:type) + if empty(l:result) + " If there are no errors then try and check for style errors. + let l:result = ale#statusline#FirstProblem(l:buffer, 'style_' . a:type) + endif + + if empty(l:result) + return '' + endif + + let l:open_lnum_symbol = + \ get(g:, 'airline#extensions#ale#open_lnum_symbol', '(L') + let l:close_lnum_symbol = + \ get(g:, 'airline#extensions#ale#close_lnum_symbol', ')') + + return open_lnum_symbol . l:result.lnum . close_lnum_symbol +endfunction + +function! s:airline_ale_get_line_number(cnt, type) abort + " Use the new ALE statusline API function if it is available. + if exists("*ale#statusline#FirstProblem") + return s:new_airline_ale_get_line_number(a:cnt, a:type) + endif + + return s:legacy_airline_ale_get_line_number(a:cnt, a:type) +endfunction + function! airline#extensions#ale#get(type) if !exists(':ALELint') return ''