From 5af82312fbdec54af41fb60daae6aa59b3ee7379 Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 14 Aug 2017 23:31:46 +0100 Subject: [PATCH] Set up one BufEnter event used for everything, and add tests for linting when the filetype changes --- autoload/ale/events.vim | 11 +++++ plugin/ale.vim | 17 ++++--- test/test_ale_init_au_groups.vader | 25 +++++----- test/test_lint_on_filetype_changed.vader | 47 +++++++++++++++++++ ...lts_not_cleared_when_opening_loclist.vader | 3 ++ ..._setting_loclist_from_another_buffer.vader | 8 +++- 6 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 test/test_lint_on_filetype_changed.vader diff --git a/autoload/ale/events.vim b/autoload/ale/events.vim index 3cfe5478..a3b74677 100644 --- a/autoload/ale/events.vim +++ b/autoload/ale/events.vim @@ -24,9 +24,20 @@ function! s:LintOnEnter(buffer) abort endfunction function! ale#events#EnterEvent(buffer) abort + let l:filetype = getbufvar(a:buffer, '&filetype') + call setbufvar(a:buffer, 'ale_original_filetype', l:filetype) + call s:LintOnEnter(a:buffer) endfunction +function! ale#events#FileTypeEvent(buffer, new_filetype) abort + let l:filetype = getbufvar(a:buffer, 'ale_original_filetype', '') + + if a:new_filetype isnot# l:filetype + call ale#Queue(300, 'lint_file', a:buffer) + endif +endfunction + function! ale#events#FileChangedEvent(buffer) abort call setbufvar(a:buffer, 'ale_file_changed', 1) diff --git a/plugin/ale.vim b/plugin/ale.vim index e2180129..584295c5 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -218,27 +218,26 @@ function! ALEInitAuGroups() abort augroup ALERunOnEnterGroup autocmd! + if g:ale_enabled + " Handle everything that needs to happen when buffers are entered. + autocmd BufEnter * call ale#events#EnterEvent(str2nr(expand(''))) + endif if g:ale_enabled && g:ale_lint_on_enter autocmd BufWinEnter,BufRead * call ale#Queue(0, 'lint_file', str2nr(expand(''))) " Track when the file is changed outside of Vim. autocmd FileChangedShellPost * call ale#events#FileChangedEvent(str2nr(expand(''))) - " If the file has been changed, then check it again on enter. - autocmd BufEnter * call ale#events#EnterEvent(str2nr(expand(''))) endif augroup END augroup ALERunOnFiletypeChangeGroup autocmd! if g:ale_enabled && g:ale_lint_on_filetype_changed - " Set the filetype after a buffer is opened or read. - autocmd BufEnter,BufRead * let b:ale_original_filetype = &filetype " Only start linting if the FileType actually changes after " opening a buffer. The FileType will fire when buffers are opened. - autocmd FileType * - \ if has_key(b:, 'ale_original_filetype') - \ && b:ale_original_filetype isnot# expand('') - \| call ale#Queue(300, 'lint_file') - \| endif + autocmd FileType * call ale#events#FileTypeEvent( + \ str2nr(expand('')), + \ expand('') + \) endif augroup END diff --git a/test/test_ale_init_au_groups.vader b/test/test_ale_init_au_groups.vader index c7f56469..da77ccee 100644 --- a/test/test_ale_init_au_groups.vader +++ b/test/test_ale_init_au_groups.vader @@ -107,10 +107,12 @@ Execute (g:ale_pattern_options_enabled = 1 should bind BufReadPost and BufEnter) \ 'BufReadPost * call ale#pattern_options#SetOptions()', \], CheckAutocmd('ALEPatternOptionsGroup') -Execute (g:ale_lint_on_enter = 0 should bind no events): +Execute (g:ale_lint_on_enter = 0 should bind only the BufEnter event): let g:ale_lint_on_enter = 0 - AssertEqual [], CheckAutocmd('ALERunOnEnterGroup') + AssertEqual + \ ['BufEnter * call ale#events#EnterEvent(str2nr(expand('''')))'], + \ CheckAutocmd('ALERunOnEnterGroup') Execute (g:ale_lint_on_enter = 1 should bind the required events): let g:ale_lint_on_enter = 1 @@ -127,18 +129,17 @@ Execute (g:ale_lint_on_filetype_changed = 0 should bind no events): AssertEqual [], CheckAutocmd('ALERunOnFiletypeChangeGroup') -Execute (g:ale_lint_on_filetype_changed = 1 should bind FileType, and required buffer events): +Execute (g:ale_lint_on_filetype_changed = 1 should bind the FileType event): let g:ale_lint_on_filetype_changed = 1 - AssertEqual [ - \ 'BufEnter * let b:ale_original_filetype = &filetype', - \ 'BufReadPost * let b:ale_original_filetype = &filetype', - \ 'FileType * ' - \ . 'if has_key(b:, ''ale_original_filetype'') ' - \ . '&& b:ale_original_filetype isnot# expand('''')' - \ . '| call ale#Queue(300, ''lint_file'')' - \ . '| endif', - \], CheckAutocmd('ALERunOnFiletypeChangeGroup') + AssertEqual + \ [ + \ 'FileType * call ale#events#FileTypeEvent( ' + \ . 'str2nr(expand('''')), ' + \ . 'expand('''')' + \ . ')', + \ ], + \ CheckAutocmd('ALERunOnFiletypeChangeGroup') Execute (g:ale_lint_on_save = 0 should bind no events): let g:ale_lint_on_save = 0 diff --git a/test/test_lint_on_filetype_changed.vader b/test/test_lint_on_filetype_changed.vader new file mode 100644 index 00000000..44446ef0 --- /dev/null +++ b/test/test_lint_on_filetype_changed.vader @@ -0,0 +1,47 @@ +Before: + Save &filetype + + let g:queue_calls = [] + + function! ale#Queue(...) + call add(g:queue_calls, a:000) + endfunction + +After: + Restore + + unlet! g:queue_calls + + " Reload the ALE code to load the real function again. + runtime autoload/ale.vim + + unlet! b:ale_original_filetype + +Execute(The original filetype should be set on BufEnter): + let &filetype = 'foobar' + + call ale#events#EnterEvent(bufnr('')) + + AssertEqual 'foobar', b:ale_original_filetype + + let &filetype = 'bazboz' + + call ale#events#EnterEvent(bufnr('')) + + AssertEqual 'bazboz', b:ale_original_filetype + +Execute(Linting should not be queued when the filetype is the same): + let b:ale_original_filetype = 'foobar' + let g:queue_calls = [] + + call ale#events#FileTypeEvent(bufnr(''), 'foobar') + + AssertEqual [], g:queue_calls + +Execute(Linting should be queued when the filetype changes): + let b:ale_original_filetype = 'foobar' + let g:queue_calls = [] + + call ale#events#FileTypeEvent(bufnr(''), 'bazboz') + + AssertEqual [[300, 'lint_file', bufnr('')]], g:queue_calls diff --git a/test/test_results_not_cleared_when_opening_loclist.vader b/test/test_results_not_cleared_when_opening_loclist.vader index 07d3d303..0c053b85 100644 --- a/test/test_results_not_cleared_when_opening_loclist.vader +++ b/test/test_results_not_cleared_when_opening_loclist.vader @@ -26,6 +26,9 @@ After: delfunction TestCallback let g:ale_buffer_info = {} call ale#linter#Reset() + call setloclist(0, []) + call clearmatches() + sign unplace * Given foobar (Some file): abc diff --git a/test/test_setting_loclist_from_another_buffer.vader b/test/test_setting_loclist_from_another_buffer.vader index 4b757c61..ae53de1f 100644 --- a/test/test_setting_loclist_from_another_buffer.vader +++ b/test/test_setting_loclist_from_another_buffer.vader @@ -1,8 +1,14 @@ Before: + Save g:ale_buffer_info + + let g:ale_buffer_info = {} + let g:original_buffer = bufnr('%') - new + noautocmd new After: + Restore + unlet! g:original_buffer Execute(Errors should be set in the loclist for the original buffer, not the new one):