From 33b0852c84452afbaf0f41c2abc954008be7ef77 Mon Sep 17 00:00:00 2001 From: Drew Neil Date: Sat, 3 Jun 2017 12:45:52 +0100 Subject: Add :ALEFirst and :ALELast commands (#616) * Add :ALEFirst and :ALELast commands * Add documentation for ALEFirst and ALELast commands * Add tests for ale#loclist_jumping#JumpToIndex() * Fix the loclist jumping tests --- autoload/ale/loclist_jumping.vim | 13 ++++++ doc/ale.txt | 7 +++ plugin/ale.vim | 4 ++ test/test_loclist_jumping.vader | 76 +++++++++++++++++++++++++++++++++ test/test_loclist_jumping_loading.vader | 55 ------------------------ 5 files changed, 100 insertions(+), 55 deletions(-) create mode 100644 test/test_loclist_jumping.vader delete mode 100644 test/test_loclist_jumping_loading.vader diff --git a/autoload/ale/loclist_jumping.vim b/autoload/ale/loclist_jumping.vim index 58fb8638..88ed4c97 100644 --- a/autoload/ale/loclist_jumping.vim +++ b/autoload/ale/loclist_jumping.vim @@ -64,3 +64,16 @@ function! ale#loclist_jumping#Jump(direction, wrap) abort call cursor(l:nearest) endif endfunction + +function! ale#loclist_jumping#JumpToIndex(index) abort + let l:info = get(g:ale_buffer_info, bufnr('%'), {'loclist': []}) + let l:loclist = l:info.loclist + if empty(l:loclist) + return + endif + + let l:item = l:loclist[a:index] + if !empty(l:item) + call cursor([l:item.lnum, l:item.col]) + endif +endfunction diff --git a/doc/ale.txt b/doc/ale.txt index 45fe4902..9949d150 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -857,6 +857,8 @@ ALEPrevious *ALEPrevious* ALEPreviousWrap *ALEPreviousWrap* ALENext *ALENext* ALENextWrap *ALENextWrap* +ALEFirst *ALEFirst* +ALELast *ALELast* *ale-navigation-commands* Move between warnings or errors in a buffer. ALE will only navigate between @@ -867,11 +869,16 @@ ALENextWrap *ALENextWrap* `ALEPreviousWrap` and `ALENextWrap` will wrap around the file to find the last or first warning or error in the file, respectively. + `ALEFirst` goes the the first error or warning in the buffer, while `ALELast` + goes to the last one. + The following || mappings are defined for the commands: > (ale_previous) - ALEPrevious (ale_previous_wrap) - ALEPreviousWrap (ale_next) - ALENext (ale_next_wrap) - ALENextWrap + (ale_first) - ALEFirst + (ale_last) - ALELast < For example, these commands could be bound to the keys Ctrl + j and Ctrl + k: > diff --git a/plugin/ale.vim b/plugin/ale.vim index 25622318..40e1a363 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -311,6 +311,8 @@ command! -bar ALEPrevious :call ale#loclist_jumping#Jump('before', 0) command! -bar ALEPreviousWrap :call ale#loclist_jumping#Jump('before', 1) command! -bar ALENext :call ale#loclist_jumping#Jump('after', 0) command! -bar ALENextWrap :call ale#loclist_jumping#Jump('after', 1) +command! -bar ALEFirst :call ale#loclist_jumping#JumpToIndex(0) +command! -bar ALELast :call ale#loclist_jumping#JumpToIndex(-1) " A command for showing error details. command! -bar ALEDetail :call ale#cursor#ShowCursorDetail() @@ -338,6 +340,8 @@ nnoremap (ale_previous) :ALEPrevious nnoremap (ale_previous_wrap) :ALEPreviousWrap nnoremap (ale_next) :ALENext nnoremap (ale_next_wrap) :ALENextWrap +nnoremap (ale_first) :ALEFirst +nnoremap (ale_last) :ALELast nnoremap (ale_toggle) :ALEToggle nnoremap (ale_lint) :ALELint nnoremap (ale_detail) :ALEDetail diff --git a/test/test_loclist_jumping.vader b/test/test_loclist_jumping.vader new file mode 100644 index 00000000..13eac5ce --- /dev/null +++ b/test/test_loclist_jumping.vader @@ -0,0 +1,76 @@ +Before: + 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! TestJump(position, wrap, pos) + call cursor(a:pos) + + if type(a:position) == type(0) + call ale#loclist_jumping#JumpToIndex(a:position) + else + call ale#loclist_jumping#Jump(a:position, a:wrap) + endif + + return getcurpos()[1:2] + endfunction + +After: + let g:ale_buffer_info = {} + delfunction TestJump + +Given foobar (Some imaginary filetype): + 12345678 + 12345678 + +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]) + +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]) + + AssertEqual [1, 2], TestJump('after', 1, [2, 8]) + AssertEqual [2, 8], TestJump('before', 1, [1, 2]) + +Execute(loclist jumping not jump when the loclist is empty): + let g:ale_buffer_info[bufnr('%')].loclist = [] + + AssertEqual [1, 6], TestJump('before', 0, [1, 6]) + AssertEqual [1, 6], TestJump('before', 1, [1, 6]) + AssertEqual [1, 6], TestJump('after', 0, [1, 6]) + AssertEqual [1, 6], TestJump('after', 1, [1, 6]) + +Execute(We should be able to jump to the last item): + AssertEqual [2, 8], TestJump(-1, 0, [1, 6]) + +Execute(We shouldn't move when jumping to the last item where there are none): + let g:ale_buffer_info[bufnr('%')].loclist = [] + + AssertEqual [1, 6], TestJump(-1, 0, [1, 6]) + +Execute(We should be able to jump to the first item): + AssertEqual [1, 2], TestJump(0, 0, [1, 6]) + +Execute(We shouldn't move when jumping to the first item where there are none): + let g:ale_buffer_info[bufnr('%')].loclist = [] + + AssertEqual [1, 6], TestJump(0, 0, [1, 6]) diff --git a/test/test_loclist_jumping_loading.vader b/test/test_loclist_jumping_loading.vader deleted file mode 100644 index 9da5bd5f..00000000 --- a/test/test_loclist_jumping_loading.vader +++ /dev/null @@ -1,55 +0,0 @@ -Before: - 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! TestJump(direction, wrap, pos) - call cursor(a:pos) - call ale#loclist_jumping#Jump(a:direction, a:wrap) - - return getcurpos()[1:2] - endfunction - -After: - let g:ale_buffer_info = {} - delfunction TestJump - -Given foobar (Some imaginary filetype): - 12345678 - 12345678 - -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]) - -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]) - - AssertEqual [1, 2], TestJump('after', 1, [2, 8]) - AssertEqual [2, 8], TestJump('before', 1, [1, 2]) - -Execute(loclist jumping not jump when the loclist is empty): - let g:ale_buffer_info[bufnr('%')].loclist = [] - - AssertEqual [1, 6], TestJump('before', 0, [1, 6]) - AssertEqual [1, 6], TestJump('before', 1, [1, 6]) - AssertEqual [1, 6], TestJump('after', 0, [1, 6]) - AssertEqual [1, 6], TestJump('after', 1, [1, 6]) -- cgit v1.2.3