Make the navigation commands only work with ALE's pre-sorted list

This commit is contained in:
w0rp 2017-03-07 23:31:25 +00:00
parent b487c62130
commit 21caf54543
3 changed files with 56 additions and 120 deletions

View file

@ -1,38 +1,6 @@
" Author: w0rp <devw0rp@gmail.com> " Author: w0rp <devw0rp@gmail.com>
" Description: This file implements functions for jumping around in a file " Description: This file implements functions for jumping around in a file
" based on errors and warnings in the loclist or quickfix list. " based on ALE's internal loclist.
function! s:GetCurrentList() abort
let l:buffer = bufnr('%')
let l:list = []
if g:ale_set_quickfix
let l:list = getqflist()
elseif g:ale_set_loclist
let l:list = getloclist(winnr())
endif
return filter(l:list, 'get(v:val, ''bufnr'', -1) == ' . l:buffer)
endfunction
function! ale#loclist_jumping#GetSortedList() abort
let l:loclist = []
for l:item in s:GetCurrentList()
if l:item.lnum < 1
" Remove items we can't even jump to.
continue
endif
call add(l:loclist, l:item)
endfor
" We must sort the list again, as the loclist could contain items set
" by other plugins.
call sort(l:loclist, 'ale#util#LocItemCompare')
return l:loclist
endfunction
" Search for the nearest line either before or after the current position " Search for the nearest line either before or after the current position
" in the loclist. The argument 'wrap' can be passed to enable wrapping " in the loclist. The argument 'wrap' can be passed to enable wrapping
@ -42,18 +10,15 @@ endfunction
" List will be returned, otherwise a pair of [line_number, column_number] will " List will be returned, otherwise a pair of [line_number, column_number] will
" be returned. " be returned.
function! ale#loclist_jumping#FindNearest(direction, wrap) abort function! ale#loclist_jumping#FindNearest(direction, wrap) abort
let l:loclist = ale#loclist_jumping#GetSortedList() let l:pos = getcurpos()
let l:info = get(g:ale_buffer_info, bufnr('%'), {'loclist': []})
if empty(l:loclist) " This list will have already been sorted.
" We couldn't find anything, so stop here. let l:loclist = l:info.loclist
return [] let l:search_item = {'lnum': l:pos[1], 'col': l:pos[2]}
endif
let l:search_item = {'lnum': getcurpos()[1], 'col': getcurpos()[2]}
" When searching backwards, so we can find the next smallest match. " When searching backwards, so we can find the next smallest match.
if a:direction ==# 'before' if a:direction ==# 'before'
call reverse(l:loclist) let l:loclist = reverse(copy(l:loclist))
endif endif
" Look for items before or after the current position. " Look for items before or after the current position.
@ -82,8 +47,8 @@ function! ale#loclist_jumping#FindNearest(direction, wrap) abort
" If we found nothing, and the wrap option is set to 1, then we should " If we found nothing, and the wrap option is set to 1, then we should
" wrap around the list of warnings/errors " wrap around the list of warnings/errors
if a:wrap if a:wrap && !empty(l:loclist)
let l:item = get(l:loclist, 0) let l:item = l:loclist[0]
return [l:item.lnum, l:item.col] return [l:item.lnum, l:item.col]
endif endif

View file

@ -1155,7 +1155,9 @@ ALENext *ALENext*
ALENextWrap *ALENextWrap* ALENextWrap *ALENextWrap*
*ale-navigation-commands* *ale-navigation-commands*
Move between warnings or errors in a buffer. Move between warnings or errors in a buffer. ALE will only navigate between
the errors or warnings it generated, even if both |g:ale_set_quickfix|
and |g:ale_set_loclist| are set to `0`.
`ALEPrevious` and `ALENext` will stop at the top and bottom of a file, while `ALEPrevious` and `ALENext` will stop at the top and bottom of a file, while
`ALEPreviousWrap` and `ALENextWrap` will wrap around the file to find `ALEPreviousWrap` and `ALENextWrap` will wrap around the file to find

View file

@ -1,86 +1,55 @@
Before: Before:
let g:buffer = bufnr('%') let g:ale_buffer_info = {
\ bufnr('%'): {
\ 'loclist': [
\ {'lnum': 1, 'col': 2},
\ {'lnum': 1, 'col': 3},
\ {'lnum': 2, 'col': 1},
\ {'lnum': 2, 'col': 2},
\ {'lnum': 2, 'col': 3},
\ {'lnum': 2, 'col': 6},
\ {'lnum': 2, 'col': 700},
\ ],
\ },
\}
function! GetList() abort function! TestJump(direction, wrap, pos)
return map( call cursor(a:pos)
\ ale#loclist_jumping#GetSortedList(), call ale#loclist_jumping#Jump(a:direction, a:wrap)
\ '{''lnum'': v:val.lnum, ''col'': v:val.col, ''text'': v:val.text}'
\) return getcurpos()[1:2]
endfunction endfunction
After: After:
unlet! g:buffer let g:ale_buffer_info = {}
unlet! g:new_buffer delfunction TestJump
let g:ale_set_loclist = 1
let g:ale_set_quickfix = 0
call setloclist(winnr(), [])
call setqflist([])
delfunction GetList
Execute(The loclist should be filtered and sorted appropriately for jumping): Given foobar (Some imaginary filetype):
:new 12345678
12345678
let g:new_buffer = bufnr('%') Execute(loclist jumping should jump correctly when not wrapping):
AssertEqual [2, 1], TestJump('before', 0, [2, 2])
AssertEqual [1, 3], TestJump('before', 0, [2, 1])
AssertEqual [2, 3], TestJump('after', 0, [2, 2])
AssertEqual [2, 1], TestJump('after', 0, [1, 3])
AssertEqual [2, 6], TestJump('after', 0, [2, 4])
AssertEqual [2, 8], TestJump('after', 0, [2, 6])
AssertNotEqual g:new_buffer, g:buffer Execute(loclist jumping should jump correctly when wrapping):
AssertEqual [2, 1], TestJump('before', 1, [2, 2])
AssertEqual [1, 3], TestJump('before', 1, [2, 1])
AssertEqual [2, 3], TestJump('after', 1, [2, 2])
AssertEqual [2, 1], TestJump('after', 1, [1, 3])
AssertEqual [2, 6], TestJump('after', 1, [2, 4])
call setloclist(winnr(), [ AssertEqual [1, 2], TestJump('after', 1, [2, 8])
\ {'lnum': 1, 'col': 1, 'text': 'ignore this', 'bufnr': g:buffer}, AssertEqual [2, 8], TestJump('before', 1, [1, 2])
\ {'lnum': 20, 'col': 5, 'text': 'baz', 'bufnr': g:new_buffer},
\ {'lnum': 10, 'col': 6, 'text': 'bar', 'bufnr': g:new_buffer},
\ {'lnum': 10, 'col': 5, 'text': 'foo', 'bufnr': g:new_buffer},
\])
AssertEqual Execute(loclist jumping not jump when the loclist is empty):
\ [ let g:ale_buffer_info[bufnr('%')].loclist = []
\ {'lnum': 10, 'col': 5, 'text': 'foo'},
\ {'lnum': 10, 'col': 6, 'text': 'bar'},
\ {'lnum': 20, 'col': 5, 'text': 'baz'},
\ ],
\ GetList()
Execute(quickfix should be filtered and sorted appropriately for jumping): AssertEqual [1, 6], TestJump('before', 0, [1, 6])
let g:ale_set_loclist = 0 AssertEqual [1, 6], TestJump('before', 1, [1, 6])
let g:ale_set_quickfix = 1 AssertEqual [1, 6], TestJump('after', 0, [1, 6])
AssertEqual [1, 6], TestJump('after', 1, [1, 6])
:new
let g:new_buffer = bufnr('%')
AssertNotEqual g:new_buffer, g:buffer
call setqflist([
\ {'lnum': 1, 'col': 1, 'text': 'ignore this', 'bufnr': g:buffer},
\ {'lnum': 20, 'col': 5, 'text': 'baz', 'bufnr': g:new_buffer},
\ {'lnum': 10, 'col': 6, 'text': 'bar', 'bufnr': g:new_buffer},
\ {'lnum': 10, 'col': 5, 'text': 'foo', 'bufnr': g:new_buffer},
\])
AssertEqual
\ [
\ {'lnum': 10, 'col': 5, 'text': 'foo'},
\ {'lnum': 10, 'col': 6, 'text': 'bar'},
\ {'lnum': 20, 'col': 5, 'text': 'baz'},
\ ],
\ GetList()
Execute(An empty List should be returned when both lists are turned off):
let g:ale_set_loclist = 0
let g:ale_set_quickfix = 0
call setqflist([{'lnum': 1, 'col': 1, 'text': 'foo', 'bufnr': bufnr('%')}])
call setloclist(winnr(), [{'lnum': 1, 'col': 1, 'text': 'foo', 'bufnr': bufnr('%')}])
AssertEqual [], GetList()
Execute(quickfix should take precedence over loclist when on):
let g:ale_set_quickfix = 1
call setloclist(winnr(), [
\ {'lnum': 1, 'col': 1, 'text': 'ignore this', 'bufnr': g:buffer}
\])
call setqflist([
\ {'lnum': 1, 'col': 1, 'text': 'foo', 'bufnr': g:buffer},
\])
AssertEqual [{'lnum': 1, 'col': 1, 'text': 'foo'}], GetList()