Set up one BufEnter event used for everything, and add tests for linting when the filetype changes

This commit is contained in:
w0rp 2017-08-14 23:31:46 +01:00
parent 1680f7af63
commit 5af82312fb
6 changed files with 89 additions and 22 deletions

View file

@ -24,9 +24,20 @@ function! s:LintOnEnter(buffer) abort
endfunction endfunction
function! ale#events#EnterEvent(buffer) abort 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) call s:LintOnEnter(a:buffer)
endfunction 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 function! ale#events#FileChangedEvent(buffer) abort
call setbufvar(a:buffer, 'ale_file_changed', 1) call setbufvar(a:buffer, 'ale_file_changed', 1)

View file

@ -218,27 +218,26 @@ function! ALEInitAuGroups() abort
augroup ALERunOnEnterGroup augroup ALERunOnEnterGroup
autocmd! autocmd!
if g:ale_enabled
" Handle everything that needs to happen when buffers are entered.
autocmd BufEnter * call ale#events#EnterEvent(str2nr(expand('<abuf>')))
endif
if g:ale_enabled && g:ale_lint_on_enter if g:ale_enabled && g:ale_lint_on_enter
autocmd BufWinEnter,BufRead * call ale#Queue(0, 'lint_file', str2nr(expand('<abuf>'))) autocmd BufWinEnter,BufRead * call ale#Queue(0, 'lint_file', str2nr(expand('<abuf>')))
" Track when the file is changed outside of Vim. " Track when the file is changed outside of Vim.
autocmd FileChangedShellPost * call ale#events#FileChangedEvent(str2nr(expand('<abuf>'))) autocmd FileChangedShellPost * call ale#events#FileChangedEvent(str2nr(expand('<abuf>')))
" If the file has been changed, then check it again on enter.
autocmd BufEnter * call ale#events#EnterEvent(str2nr(expand('<abuf>')))
endif endif
augroup END augroup END
augroup ALERunOnFiletypeChangeGroup augroup ALERunOnFiletypeChangeGroup
autocmd! autocmd!
if g:ale_enabled && g:ale_lint_on_filetype_changed 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 " Only start linting if the FileType actually changes after
" opening a buffer. The FileType will fire when buffers are opened. " opening a buffer. The FileType will fire when buffers are opened.
autocmd FileType * autocmd FileType * call ale#events#FileTypeEvent(
\ if has_key(b:, 'ale_original_filetype') \ str2nr(expand('<abuf>')),
\ && b:ale_original_filetype isnot# expand('<amatch>') \ expand('<amatch>')
\| call ale#Queue(300, 'lint_file') \)
\| endif
endif endif
augroup END augroup END

View file

@ -107,10 +107,12 @@ Execute (g:ale_pattern_options_enabled = 1 should bind BufReadPost and BufEnter)
\ 'BufReadPost * call ale#pattern_options#SetOptions()', \ 'BufReadPost * call ale#pattern_options#SetOptions()',
\], CheckAutocmd('ALEPatternOptionsGroup') \], 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 let g:ale_lint_on_enter = 0
AssertEqual [], CheckAutocmd('ALERunOnEnterGroup') AssertEqual
\ ['BufEnter * call ale#events#EnterEvent(str2nr(expand(''<abuf>'')))'],
\ CheckAutocmd('ALERunOnEnterGroup')
Execute (g:ale_lint_on_enter = 1 should bind the required events): Execute (g:ale_lint_on_enter = 1 should bind the required events):
let g:ale_lint_on_enter = 1 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') 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 let g:ale_lint_on_filetype_changed = 1
AssertEqual [ AssertEqual
\ 'BufEnter * let b:ale_original_filetype = &filetype', \ [
\ 'BufReadPost * let b:ale_original_filetype = &filetype', \ 'FileType * call ale#events#FileTypeEvent( '
\ 'FileType * ' \ . 'str2nr(expand(''<abuf>'')), '
\ . 'if has_key(b:, ''ale_original_filetype'') ' \ . 'expand(''<amatch>'')'
\ . '&& b:ale_original_filetype isnot# expand(''<amatch>'')' \ . ')',
\ . '| call ale#Queue(300, ''lint_file'')' \ ],
\ . '| endif', \ CheckAutocmd('ALERunOnFiletypeChangeGroup')
\], CheckAutocmd('ALERunOnFiletypeChangeGroup')
Execute (g:ale_lint_on_save = 0 should bind no events): Execute (g:ale_lint_on_save = 0 should bind no events):
let g:ale_lint_on_save = 0 let g:ale_lint_on_save = 0

View file

@ -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

View file

@ -26,6 +26,9 @@ After:
delfunction TestCallback delfunction TestCallback
let g:ale_buffer_info = {} let g:ale_buffer_info = {}
call ale#linter#Reset() call ale#linter#Reset()
call setloclist(0, [])
call clearmatches()
sign unplace *
Given foobar (Some file): Given foobar (Some file):
abc abc

View file

@ -1,8 +1,14 @@
Before: Before:
Save g:ale_buffer_info
let g:ale_buffer_info = {}
let g:original_buffer = bufnr('%') let g:original_buffer = bufnr('%')
new noautocmd new
After: After:
Restore
unlet! g:original_buffer unlet! g:original_buffer
Execute(Errors should be set in the loclist for the original buffer, not the new one): Execute(Errors should be set in the loclist for the original buffer, not the new one):