#697 - Remove highlights more thoroughly

This commit is contained in:
w0rp 2017-07-13 23:27:02 +01:00
parent b50ae96413
commit 4c6c5bf84f
4 changed files with 69 additions and 93 deletions

View File

@ -667,11 +667,6 @@ endfunction
function! ale#engine#Cleanup(buffer) abort function! ale#engine#Cleanup(buffer) abort
call ale#engine#RunLinters(a:buffer, [], 1) call ale#engine#RunLinters(a:buffer, [], 1)
if g:ale_set_highlights
call ale#highlight#UnqueueHighlights(a:buffer)
call ale#highlight#RemoveHighlights()
endif
call remove(g:ale_buffer_info, a:buffer) call remove(g:ale_buffer_info, a:buffer)
endfunction endfunction

View File

@ -22,12 +22,6 @@ if !hlexists('ALEInfo')
highlight link ALEInfo ALEWarning highlight link ALEInfo ALEWarning
endif endif
" This map holds highlights to be set when buffers are opened.
" We can only set highlights for whatever the current buffer is, so we will
" wait until the buffer is entered again to show the highlights, unless
" the buffer is in focus when linting completes.
let s:buffer_highlights = {}
let s:buffer_restore_map = {}
" The maximum number of items for the second argument of matchaddpos() " The maximum number of items for the second argument of matchaddpos()
let s:MAX_POS_VALUES = 8 let s:MAX_POS_VALUES = 8
let s:MAX_COL_SIZE = 1073741824 " pow(2, 30) let s:MAX_COL_SIZE = 1073741824 " pow(2, 30)
@ -53,86 +47,56 @@ function! ale#highlight#CreatePositions(line, col, end_line, end_col) abort
\) \)
endfunction endfunction
function! ale#highlight#UnqueueHighlights(buffer) abort
if has_key(s:buffer_highlights, a:buffer)
call remove(s:buffer_highlights, a:buffer)
endif
if has_key(s:buffer_restore_map, a:buffer)
call remove(s:buffer_restore_map, a:buffer)
endif
endfunction
function! s:GetALEMatches() abort
return filter(getmatches(), 'v:val.group =~# ''^ALE''')
endfunction
" Given a loclist for current items to highlight, remove all highlights " Given a loclist for current items to highlight, remove all highlights
" except these which have matching loclist item entries. " except these which have matching loclist item entries.
function! ale#highlight#RemoveHighlights() abort function! ale#highlight#RemoveHighlights() abort
for l:match in s:GetALEMatches() for l:match in getmatches()
call matchdelete(l:match.id) if l:match.group =~# '^ALE'
call matchdelete(l:match.id)
endif
endfor endfor
endfunction endfunction
function! ale#highlight#UpdateHighlights() abort function! ale#highlight#UpdateHighlights() abort
let l:buffer = bufnr('%') let l:item_list = g:ale_enabled
let l:has_new_items = has_key(s:buffer_highlights, l:buffer) \ ? get(b:, 'ale_highlight_items', [])
let l:loclist = l:has_new_items ? remove(s:buffer_highlights, l:buffer) : [] \ : []
if l:has_new_items || !g:ale_enabled call ale#highlight#RemoveHighlights()
call ale#highlight#RemoveHighlights()
endif
" Restore items from the map of hidden items, for l:item in l:item_list
" if we don't have some new items to set already. if l:item.type ==# 'W'
if empty(l:loclist) && has_key(s:buffer_restore_map, l:buffer) if get(l:item, 'sub_type', '') ==# 'style'
let l:loclist = s:buffer_restore_map[l:buffer] let l:group = 'ALEStyleWarning'
endif
if g:ale_enabled
for l:item in l:loclist
if l:item.type ==# 'W'
if get(l:item, 'sub_type', '') ==# 'style'
let l:group = 'ALEStyleWarning'
else
let l:group = 'ALEWarning'
endif
elseif l:item.type ==# 'I'
let l:group = 'ALEInfo'
elseif get(l:item, 'sub_type', '') ==# 'style'
let l:group = 'ALEStyleError'
else else
let l:group = 'ALEError' let l:group = 'ALEWarning'
endif endif
elseif l:item.type ==# 'I'
let l:group = 'ALEInfo'
elseif get(l:item, 'sub_type', '') ==# 'style'
let l:group = 'ALEStyleError'
else
let l:group = 'ALEError'
endif
let l:line = l:item.lnum let l:line = l:item.lnum
let l:col = l:item.col let l:col = l:item.col
let l:end_line = get(l:item, 'end_lnum', l:line) let l:end_line = get(l:item, 'end_lnum', l:line)
let l:end_col = get(l:item, 'end_col', l:col) let l:end_col = get(l:item, 'end_col', l:col)
" Set all of the positions, which are chunked into Lists which " Set all of the positions, which are chunked into Lists which
" are as large as will be accepted by matchaddpos. " are as large as will be accepted by matchaddpos.
call map( call map(
\ ale#highlight#CreatePositions(l:line, l:col, l:end_line, l:end_col), \ ale#highlight#CreatePositions(l:line, l:col, l:end_line, l:end_col),
\ 'matchaddpos(l:group, v:val)' \ 'matchaddpos(l:group, v:val)'
\) \)
endfor endfor
endif
endfunction endfunction
function! ale#highlight#BufferHidden(buffer) abort function! ale#highlight#BufferHidden(buffer) abort
let l:loclist = get(g:ale_buffer_info, a:buffer, {'loclist': []}).loclist " Remove highlights right away when buffers are hidden.
" They will be restored later when buffers are entered.
" Remember loclist items, so they can be restored later. call ale#highlight#RemoveHighlights()
if !empty(l:loclist)
let s:buffer_restore_map[a:buffer] = filter(
\ copy(l:loclist),
\ 'v:val.bufnr == a:buffer && v:val.col > 0'
\)
call ale#highlight#RemoveHighlights()
endif
endfunction endfunction
augroup ALEHighlightBufferGroup augroup ALEHighlightBufferGroup
@ -142,19 +106,14 @@ augroup ALEHighlightBufferGroup
augroup END augroup END
function! ale#highlight#SetHighlights(buffer, loclist) abort function! ale#highlight#SetHighlights(buffer, loclist) abort
" Only set set items for the buffer if ALE is enabled. let l:new_list = g:ale_enabled
if g:ale_enabled \ ? filter(copy(a:loclist), 'v:val.bufnr == a:buffer && v:val.col > 0')
" Set a list of items to be set as highlights for a buffer when \ : []
" we next open it.
"
" We'll filter the loclist down to items we can set now.
let s:buffer_highlights[a:buffer] = filter(
\ copy(a:loclist),
\ 'v:val.bufnr == a:buffer && v:val.col > 0'
\)
" Update highlights for the current buffer, which may or may not " Set the list in the buffer variable.
" be the buffer we just set highlights for. call setbufvar(str2nr(a:buffer), 'ale_highlight_items', l:new_list)
call ale#highlight#UpdateHighlights()
endif " Update highlights for the current buffer, which may or may not
" be the buffer we just set highlights for.
call ale#highlight#UpdateHighlights()
endfunction endfunction

View File

@ -1,4 +0,0 @@
Execute(ALE should be able to queue highlights and clear them for some other buffer):
" We'll just make sure that this doesn't blow up.
call ale#highlight#SetHighlights(bufnr('%') + 1, [])
call ale#highlight#UnqueueHighlights(bufnr('%') + 1)

View File

@ -204,3 +204,29 @@ Execute(Highlighting should support errors spanning many lines):
\ }, \ },
\ ], \ ],
\ GetMatchesWithoutIDs() \ GetMatchesWithoutIDs()
\
Execute(Highlights should always be cleared when the buffer highlight list is empty):
" Add our highlights and something else.
call matchaddpos('ALEError', [[1, 1, 1]])
call matchaddpos('SomeOtherGroup', [[1, 1, 1]])
AssertEqual
\ [
\ {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]},
\ {'group': 'SomeOtherGroup', 'priority': 10, 'pos1': [1, 1, 1]},
\ ],
\ GetMatchesWithoutIDs()
" Set the List we use for holding highlights for buffers.
let b:ale_highlight_items = []
" Call the function for updating the highlights called when buffers
" are entered, or when problems are presented.
call ale#highlight#UpdateHighlights()
" Check that we remove our highlights.
AssertEqual
\ [
\ {'group': 'SomeOtherGroup', 'priority': 10, 'pos1': [1, 1, 1]},
\ ],
\ GetMatchesWithoutIDs()