ale/plugin/ale/sign.vim

132 lines
3.7 KiB
VimL
Raw Normal View History

" Author: w0rp <devw0rp@gmail.com>
" Description: Draws error and warning signs into signcolumn
if exists('g:loaded_ale_sign')
finish
endif
let g:loaded_ale_sign = 1
if !hlexists('ALEErrorSign')
2016-09-29 21:41:55 +00:00
highlight link ALEErrorSign error
endif
if !hlexists('ALEWarningSign')
highlight link ALEWarningSign todo
endif
if !hlexists('ALEError')
highlight link ALEError SpellBad
endif
if !hlexists('ALEWarning')
highlight link ALEWarning SpellCap
endif
" Global variables for signs
let g:ale_sign_error = get(g:, 'ale_sign_error', '>>')
let g:ale_sign_warning = get(g:, 'ale_sign_warning', '--')
" 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.
execute 'sign define ALEErrorSign text=' . g:ale_sign_error
\ . ' texthl=ALEErrorSign'
execute 'sign define ALEWarningSign text=' . g:ale_sign_warning
\ . ' texthl=ALEWarningSign'
function! ale#sign#FindCurrentSigns(buffer)
" Matches output like :
" 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 = []
for obj in a:loclist
let should_append = 1
if len(signlist) > 0 && signlist[-1].lnum == obj.lnum
" We can't add the same line twice, because signs must be
" unique per line.
let should_append = 0
if signlist[-1].type ==# 'W' && obj.type ==# 'E'
" If we had a warning previously, but now have an error,
" we replace the object to set an error instead.
let signlist[-1] = obj
endif
endif
if should_append
call add(signlist, obj)
endif
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.
2016-09-26 14:55:26 +00:00
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)
let obj = signlist[i]
let name = obj['type'] ==# 'W' ? 'ALEWarningSign' : 'ALEErrorSign'
let sign_line = 'sign place ' . (i + g:ale_sign_offset)
\. ' line=' . obj['lnum']
\. ' name=' . name
\. ' buffer=' . a:buffer
exec sign_line
endfor
endfunction
2016-09-26 14:55:26 +00:00
" Show sign gutter if there are no signs and g:ale_sign_column_always is set to 1
2016-09-26 14:55:26 +00:00
function! ale#sign#InsertDummy(no_signs)
if g:ale_sign_column_always == 1 && a:no_signs == 0
sign define ALEDummySign
execute 'sign place '
\ . g:ale_sign_dummy_id
\ . ' line=1 name=ALEDummySign buffer='
\ . bufnr('')
2016-09-26 14:55:26 +00:00
endif
endfunction