Improve the signs feature so it can work with vim-gitgutter and possibly other plugins.

This commit is contained in:
w0rp 2016-10-04 21:10:36 +01:00
parent c6dc324add
commit 6754b9f1f8
3 changed files with 58 additions and 12 deletions

View File

@ -33,7 +33,8 @@ endif
" This flag can be set to 0 to disable setting signs. " This flag can be set to 0 to disable setting signs.
if !exists('g:ale_set_signs') if !exists('g:ale_set_signs')
let g:ale_set_signs = 1 " Enable the flag by default if the 'signs' feature exists.
let g:ale_set_signs = has('signs')
endif endif
" This flag can be set to 0 to disable echoing when the cursor moves. " This flag can be set to 0 to disable echoing when the cursor moves.

View File

@ -26,17 +26,43 @@ endif
" Global variables for signs " Global variables for signs
let g:ale_sign_error = get(g:, 'ale_sign_error', '>>') let g:ale_sign_error = get(g:, 'ale_sign_error', '>>')
let g:ale_sign_warning = get(g:, 'ale_sign_error', '--') let g:ale_sign_warning = get(g:, 'ale_sign_error', '--')
" An offset which can be set for sign IDs.
" This ID can be changed depending on what IDs are set for other plugins.
let g:ale_sign_offset = get(g:, 'ale_sign_offset', 1000000)
let g:ale_sign_dummy_id = get(g:, 'ale_sign_dummy_id', 10000000)
" Signs show up on the left for error markers. " Signs show up on the left for error markers.
execute 'sign define ALEErrorSign text=' . g:ale_sign_error execute 'sign define ALEErrorSign text=' . g:ale_sign_error
\ . ' texthl=ALEErrorSign' \ . ' texthl=ALEErrorSign'
execute 'sign define ALEWarningSign text=' . g:ale_sign_warning execute 'sign define ALEWarningSign text=' . g:ale_sign_warning
\ . ' texthl=ALEWarningSign' \ . ' texthl=ALEWarningSign'
" This function will set the signs which show up on the left. function! ale#sign#FindCurrentSigns(buffer)
function! ale#sign#SetSigns(buffer, loclist) " Matches output like :
exec 'sign unplace * buffer=' . a:buffer " line=4 id=1 name=ALEErrorSign
let pattern = 'id=\(\d\+\) \+name=ALE\(Warning\|Error\)Sign'
redir => output
silent exec 'sign place buffer=' . a:buffer
redir END
let id_list = []
for line in split(output, "\n")
let match = matchlist(line, pattern)
if len(match) > 0
call add(id_list, match[1] + 0)
endif
endfor
return id_list
endfunction
" Given a loclist, combine the loclist into a list of signs such that only
" one sign appears per line. Error lines will take precedence.
" The loclist will have been previously sorted.
function! ale#sign#CombineSigns(loclist)
let signlist = [] let signlist = []
for obj in a:loclist for obj in a:loclist
@ -59,13 +85,31 @@ function! ale#sign#SetSigns(buffer, loclist)
endif endif
endfor endfor
return signlist
endfunction
" This function will set the signs which show up on the left.
function! ale#sign#SetSigns(buffer, loclist)
let signlist = ale#sign#CombineSigns(a:loclist)
" Insert a dummy sign if one is missing.
" We will only insert the dummy sign at most once.
call ale#sign#InsertDummy(len(signlist)) call ale#sign#InsertDummy(len(signlist))
" Find the current signs with the markers we use.
let current_id_list = ale#sign#FindCurrentSigns(a:buffer)
" Remove those markers.
for current_id in current_id_list
exec 'sign unplace ' . current_id . ' buffer=' . a:buffer
endfor
" Now set all of the signs.
for i in range(0, len(signlist) - 1) for i in range(0, len(signlist) - 1)
let obj = signlist[i] let obj = signlist[i]
let name = obj['type'] ==# 'W' ? 'ALEWarningSign' : 'ALEErrorSign' let name = obj['type'] ==# 'W' ? 'ALEWarningSign' : 'ALEErrorSign'
let sign_line = 'sign place ' . (i + 1) let sign_line = 'sign place ' . (i + g:ale_sign_offset)
\. ' line=' . obj['lnum'] \. ' line=' . obj['lnum']
\. ' name=' . name \. ' name=' . name
\. ' buffer=' . a:buffer \. ' buffer=' . a:buffer
@ -74,11 +118,14 @@ function! ale#sign#SetSigns(buffer, loclist)
endfor endfor
endfunction endfunction
" Show signd gutter if there is no signs and g:ale_sign_column_alwas is set to 1 " Show sign gutter if there are no signs and g:ale_sign_column_always is set to 1
function! ale#sign#InsertDummy(no_signs) function! ale#sign#InsertDummy(no_signs)
if g:ale_sign_column_always == 1 && a:no_signs == 0 if g:ale_sign_column_always == 1 && a:no_signs == 0
sign define ale_keep_open_dummy sign define ALEDummySign
execute 'sign place 9999 line=1 name=ale_keep_open_dummy buffer=' . bufnr('') execute 'sign place '
\ . g:ale_sign_dummy_id
\ . ' line=1 name=ALEDummySign buffer='
\ . bufnr('')
endif endif
endfunction endfunction

View File

@ -327,8 +327,6 @@ function! ALELint(delay)
if a:delay > 0 if a:delay > 0
let s:lint_timer = timer_start(a:delay, function('s:TimerHandler')) let s:lint_timer = timer_start(a:delay, function('s:TimerHandler'))
else else
" Show empty gutter if g:ale_sign_column_always = 1
call ale#sign#InsertDummy(0)
call s:TimerHandler() call s:TimerHandler()
endif endif
endfunction endfunction