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

View File

@ -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('<abuf>')))
endif
if g:ale_enabled && g:ale_lint_on_enter
autocmd BufWinEnter,BufRead * call ale#Queue(0, 'lint_file', str2nr(expand('<abuf>')))
" Track when the file is changed outside of Vim.
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
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('<amatch>')
\| call ale#Queue(300, 'lint_file')
\| endif
autocmd FileType * call ale#events#FileTypeEvent(
\ str2nr(expand('<abuf>')),
\ expand('<amatch>')
\)
endif
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()',
\], 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(''<abuf>'')))'],
\ 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(''<amatch>'')'
\ . '| call ale#Queue(300, ''lint_file'')'
\ . '| endif',
\], CheckAutocmd('ALERunOnFiletypeChangeGroup')
AssertEqual
\ [
\ 'FileType * call ale#events#FileTypeEvent( '
\ . 'str2nr(expand(''<abuf>'')), '
\ . 'expand(''<amatch>'')'
\ . ')',
\ ],
\ CheckAutocmd('ALERunOnFiletypeChangeGroup')
Execute (g:ale_lint_on_save = 0 should bind no events):
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
let g:ale_buffer_info = {}
call ale#linter#Reset()
call setloclist(0, [])
call clearmatches()
sign unplace *
Given foobar (Some file):
abc

View File

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