From daecbad543c68de1c0a2d8c368e4edde2f90461e Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 29 Oct 2017 17:03:29 +0000 Subject: [PATCH] Fix #719 - Add ALEReset and ALEResetBuffer for removing problems for all buffers or one buffer --- autoload/ale/toggle.vim | 40 ++++++++++++++++++++----------- doc/ale.txt | 23 ++++++++++++++++-- plugin/ale.vim | 4 ++++ test/test_ale_toggle.vader | 49 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 16 deletions(-) diff --git a/autoload/ale/toggle.vim b/autoload/ale/toggle.vim index 8e54dc2..e8cb83a 100644 --- a/autoload/ale/toggle.vim +++ b/autoload/ale/toggle.vim @@ -110,25 +110,28 @@ function! s:DisablePostamble() abort endif endfunction +function! s:CleanupEveryBuffer() abort + for l:key in keys(g:ale_buffer_info) + " The key could be a filename or a buffer number, so try and + " convert it to a number. We need a number for the other + " functions. + let l:buffer = str2nr(l:key) + + if l:buffer > 0 + " Stop all jobs and clear the results for everything, and delete + " all of the data we stored for the buffer. + call ale#engine#Cleanup(l:buffer) + endif + endfor +endfunction + function! ale#toggle#Toggle() abort let g:ale_enabled = !get(g:, 'ale_enabled') if g:ale_enabled call s:EnablePreamble() else - for l:key in keys(g:ale_buffer_info) - " The key could be a filename or a buffer number, so try and - " convert it to a number. We need a number for the other - " functions. - let l:buffer = str2nr(l:key) - - if l:buffer > 0 - " Stop all jobs and clear the results for everything, and delete - " all of the data we stored for the buffer. - call ale#engine#Cleanup(l:buffer) - endif - endfor - + call s:CleanupEveryBuffer() call s:DisablePostamble() endif @@ -152,6 +155,11 @@ function! ale#toggle#Disable() abort endif endfunction +function! ale#toggle#Reset() abort + call s:CleanupEveryBuffer() + call ale#highlight#UpdateHighlights() +endfunction + function! ale#toggle#ToggleBuffer(buffer) abort " Get the new value for the toggle. let l:enabled = !getbufvar(a:buffer, 'ale_enabled', 1) @@ -171,7 +179,6 @@ function! ale#toggle#ToggleBuffer(buffer) abort " Stop all jobs and clear the results for everything, and delete " all of the data we stored for the buffer. call ale#engine#Cleanup(a:buffer) - call s:DisablePostamble() endif endfunction @@ -188,3 +195,8 @@ function! ale#toggle#DisableBuffer(buffer) abort call ale#toggle#ToggleBuffer(a:buffer) endif endfunction + +function! ale#toggle#ResetBuffer(buffer) abort + call ale#engine#Cleanup(a:buffer) + call ale#highlight#UpdateHighlights() +endfunction diff --git a/doc/ale.txt b/doc/ale.txt index 2026978..340d7fa 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -1468,8 +1468,7 @@ ALEDisableBuffer *ALEDisableBuffer* globally, as disabling ALE globally removes the autocmd events needed to perform linting with. - The following plug mappings are defined, for conveniently defining - keybinds: + The following plug mappings are defined, for conveniently defining keybinds: |ALEToggle| - `(ale_toggle)` |ALEEnable| - `(ale_enable)` @@ -1478,6 +1477,8 @@ ALEDisableBuffer *ALEDisableBuffer* |ALEEnableBuffer| - `(ale_enable_buffer)` |ALEDisableBuffer| - `(ale_disable_buffer)` + For removing problems reported by ALE, but leaving ALE enabled, see + |ALEReset| and |ALEResetBuffer|. ALEDetail *ALEDetail* @@ -1505,6 +1506,24 @@ ALEInfoToClipboard *ALEInfoToClipboard* your clipboard. This might not work on every machine. +ALEReset *ALEReset* +ALEResetBuffer *ALEResetBuffer* + + `ALEReset` will remove all problems reported by ALE for all buffers. + `ALEResetBuffer` will remove all problems reported for a single buffer. + + Either command will leave ALE linting enabled, so ALE will report problems + when linting is performed again. See |ale-lint| for more information. + + The following plug mappings are defined, for conveniently defining keybinds: + + |ALEReset| - `(ale_reset)` + |ALEResetBuffer| - `(ale_reset_buffer)` + + ALE can be disabled globally or for a buffer with |ALEDisable| or + |ALEDisableBuffer|. + + =============================================================================== 9. API *ale-api* diff --git a/plugin/ale.vim b/plugin/ale.vim index f700aff..0b5ac78 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -224,10 +224,12 @@ command! -bar ALEDetail :call ale#cursor#ShowCursorDetail() command! -bar ALEToggle :call ale#toggle#Toggle() command! -bar ALEEnable :call ale#toggle#Enable() command! -bar ALEDisable :call ale#toggle#Disable() +command! -bar ALEReset :call ale#toggle#Reset() " Commands for turning ALE on or off for a buffer. command! -bar ALEToggleBuffer :call ale#toggle#ToggleBuffer(bufnr('')) command! -bar ALEEnableBuffer :call ale#toggle#EnableBuffer(bufnr('')) command! -bar ALEDisableBuffer :call ale#toggle#DisableBuffer(bufnr('')) +command! -bar ALEResetBuffer :call ale#toggle#ResetBuffer(bufnr('')) " A command for linting manually. command! -bar ALELint :call ale#Queue(0, 'lint_file') @@ -252,9 +254,11 @@ nnoremap (ale_last) :ALELast nnoremap (ale_toggle) :ALEToggle nnoremap (ale_enable) :ALEEnable nnoremap (ale_disable) :ALEDisable +nnoremap (ale_reset) :ALEReset nnoremap (ale_toggle_buffer) :ALEToggleBuffer nnoremap (ale_enable_buffer) :ALEEnableBuffer nnoremap (ale_disable_buffer) :ALEDisableBuffer +nnoremap (ale_reset_buffer) :ALEResetBuffer nnoremap (ale_lint) :ALELint nnoremap (ale_detail) :ALEDetail nnoremap (ale_fix) :ALEFix diff --git a/test/test_ale_toggle.vader b/test/test_ale_toggle.vader index d8de398..c3bd2f5 100644 --- a/test/test_ale_toggle.vader +++ b/test/test_ale_toggle.vader @@ -215,6 +215,30 @@ Execute(ALEEnable should enable ALE and lint again): AssertEqual g:expected_loclist, getloclist(0) AssertEqual 1, g:ale_enabled +Execute(ALEReset should reset everything for a buffer): + AssertEqual 'foobar', &filetype + + call ale#Lint() + + " First check that everything is there... + AssertEqual g:expected_loclist, getloclist(0) + AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%')) + AssertEqual + \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}], + \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}') + AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist + + " Now Toggle ALE off. + ALEReset + + " Everything should be cleared. + Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed' + AssertEqual [], getloclist(0), 'The loclist was not cleared' + AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared' + AssertEqual [], getmatches(), 'The highlights were not cleared' + + AssertEqual 1, g:ale_enabled + Execute(ALEToggleBuffer should reset everything and then run again): " Run this test asynchrously. let g:ale_run_synchronously = 0 @@ -285,3 +309,28 @@ Execute(ALEEnableBuffer should complain when ALE is disabled globally): AssertEqual \ 'ALE cannot be enabled locally when disabled globally', \ join(split(g:output)) + +Execute(ALEResetBuffer should reset everything for a buffer): + AssertEqual 'foobar', &filetype + + call ale#Lint() + + " First check that everything is there... + AssertEqual g:expected_loclist, getloclist(0) + AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%')) + AssertEqual + \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}], + \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}') + AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist + + " Now Toggle ALE off. + ALEResetBuffer + + " Everything should be cleared. + Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed' + AssertEqual [], getloclist(0), 'The loclist was not cleared' + AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared' + AssertEqual [], getmatches(), 'The highlights were not cleared' + + AssertEqual 1, g:ale_enabled + AssertEqual 1, get(b:, 'ale_enabled', 1)