#734 - Use the buffer number from the events for entering buffers and saving buffers for checking buffers

This commit is contained in:
w0rp 2017-08-01 00:03:24 +01:00
parent ec82530247
commit a4ffd2f37c
8 changed files with 52 additions and 40 deletions

View File

@ -15,19 +15,19 @@ endfunction
" A function for checking various conditions whereby ALE just shouldn't " A function for checking various conditions whereby ALE just shouldn't
" attempt to do anything, say if particular buffer types are open in Vim. " attempt to do anything, say if particular buffer types are open in Vim.
function! ale#ShouldDoNothing() abort function! ale#ShouldDoNothing(buffer) abort
" Do nothing for blacklisted files " Do nothing for blacklisted files
" OR if ALE is running in the sandbox " OR if ALE is running in the sandbox
return index(g:ale_filetype_blacklist, &filetype) >= 0 return index(g:ale_filetype_blacklist, &filetype) >= 0
\ || (exists('*getcmdwintype') && !empty(getcmdwintype())) \ || (exists('*getcmdwintype') && !empty(getcmdwintype()))
\ || ale#util#InSandbox() \ || ale#util#InSandbox()
\ || !ale#Var(bufnr(''), 'enabled') \ || !ale#Var(a:buffer, 'enabled')
\ || ale#FileTooLarge() \ || ale#FileTooLarge()
endfunction endfunction
" (delay, [linting_flag]) " (delay, [linting_flag, buffer_number])
function! ale#Queue(delay, ...) abort function! ale#Queue(delay, ...) abort
if len(a:0) > 1 if a:0 > 2
throw 'too many arguments!' throw 'too many arguments!'
endif endif
@ -38,7 +38,13 @@ function! ale#Queue(delay, ...) abort
throw "linting_flag must be either '' or 'lint_file'" throw "linting_flag must be either '' or 'lint_file'"
endif endif
if ale#ShouldDoNothing() let l:buffer = get(a:000, 1, bufnr(''))
if type(l:buffer) != type(0)
throw 'buffer_number must be a Number'
endif
if ale#ShouldDoNothing(l:buffer)
return return
endif endif
@ -53,7 +59,6 @@ function! ale#Queue(delay, ...) abort
let s:lint_timer = -1 let s:lint_timer = -1
endif endif
let l:buffer = bufnr('')
let l:linters = ale#linter#Get(getbufvar(l:buffer, '&filetype')) let l:linters = ale#linter#Get(getbufvar(l:buffer, '&filetype'))
" Don't set up buffer data and so on if there are no linters to run. " Don't set up buffer data and so on if there are no linters to run.
@ -68,21 +73,26 @@ function! ale#Queue(delay, ...) abort
endif endif
if a:delay > 0 if a:delay > 0
let s:queued_buffer_number = bufnr('%') let s:queued_buffer_number = l:buffer
let s:lint_timer = timer_start(a:delay, function('ale#Lint')) let s:lint_timer = timer_start(a:delay, function('ale#Lint'))
else else
call ale#Lint() call ale#Lint(-1, l:buffer)
endif endif
endfunction endfunction
function! ale#Lint(...) abort function! ale#Lint(...) abort
" Get the buffer number linting was queued for. if a:0 > 1
" or else take the current one. " Use the buffer number given as the optional second argument.
let l:buffer = len(a:0) > 1 && a:1 == s:lint_timer let l:buffer = a:2
\ ? s:queued_buffer_number elseif a:0 > 0 && a:1 == s:lint_timer
\ : bufnr('%') " Use the buffer number for the buffer linting was queued for.
let l:buffer = s:queued_buffer_number
else
" Use the current buffer number.
let l:buffer = bufnr('')
endif
if ale#ShouldDoNothing() if ale#ShouldDoNothing(l:buffer)
return return
endif endif

View File

@ -66,7 +66,7 @@ function! s:StopCursorTimer() abort
endfunction endfunction
function! ale#cursor#EchoCursorWarning(...) abort function! ale#cursor#EchoCursorWarning(...) abort
if ale#ShouldDoNothing() if ale#ShouldDoNothing(bufnr(''))
return return
endif endif
@ -93,7 +93,7 @@ let s:cursor_timer = -1
let s:last_pos = [0, 0, 0] let s:last_pos = [0, 0, 0]
function! ale#cursor#EchoCursorWarningWithDelay() abort function! ale#cursor#EchoCursorWarningWithDelay() abort
if ale#ShouldDoNothing() if ale#ShouldDoNothing(bufnr(''))
return return
endif endif
@ -112,7 +112,7 @@ function! ale#cursor#EchoCursorWarningWithDelay() abort
endfunction endfunction
function! ale#cursor#ShowCursorDetail() abort function! ale#cursor#ShowCursorDetail() abort
if ale#ShouldDoNothing() if ale#ShouldDoNothing(bufnr(''))
return return
endif endif

View File

@ -139,7 +139,7 @@ function! s:HandleLoclist(linter_name, buffer, loclist) abort
" for efficient lookup of the messages in the cursor handler. " for efficient lookup of the messages in the cursor handler.
call sort(g:ale_buffer_info[a:buffer].loclist, 'ale#util#LocItemCompare') call sort(g:ale_buffer_info[a:buffer].loclist, 'ale#util#LocItemCompare')
if ale#ShouldDoNothing() if ale#ShouldDoNothing(a:buffer)
return return
endif endif

View File

@ -1,7 +1,7 @@
" Author: w0rp <devw0rp@gmail.com> " Author: w0rp <devw0rp@gmail.com>
function! ale#events#SaveEvent() abort function! ale#events#SaveEvent(buffer) abort
let l:should_lint = g:ale_enabled && g:ale_lint_on_save let l:should_lint = ale#Var(a:buffer, 'enabled') && g:ale_lint_on_save
if g:ale_fix_on_save if g:ale_fix_on_save
let l:will_fix = ale#fix#Fix('save_file') let l:will_fix = ale#fix#Fix('save_file')
@ -9,25 +9,27 @@ function! ale#events#SaveEvent() abort
endif endif
if l:should_lint if l:should_lint
call ale#Queue(0, 'lint_file') call ale#Queue(0, 'lint_file', a:buffer)
endif endif
endfunction endfunction
function! s:LintOnEnter() abort function! s:LintOnEnter(buffer) abort
if g:ale_enabled && g:ale_lint_on_enter && has_key(b:, 'ale_file_changed') if ale#Var(a:buffer, 'enabled')
\&& g:ale_lint_on_enter
\&& has_key(b:, 'ale_file_changed')
call remove(b:, 'ale_file_changed') call remove(b:, 'ale_file_changed')
call ale#Queue(0, 'lint_file') call ale#Queue(0, 'lint_file', a:buffer)
endif endif
endfunction endfunction
function! ale#events#EnterEvent() abort function! ale#events#EnterEvent(buffer) abort
call s:LintOnEnter() call s:LintOnEnter(a:buffer)
endfunction 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)
if bufnr('') == a:buffer if bufnr('') == a:buffer
call s:LintOnEnter() call s:LintOnEnter(a:buffer)
endif endif
endfunction endfunction

View File

@ -219,11 +219,11 @@ function! ALEInitAuGroups() abort
augroup ALERunOnEnterGroup augroup ALERunOnEnterGroup
autocmd! autocmd!
if g:ale_enabled && g:ale_lint_on_enter if g:ale_enabled && g:ale_lint_on_enter
autocmd BufWinEnter,BufRead * call ale#Queue(300, 'lint_file') 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. " If the file has been changed, then check it again on enter.
autocmd BufEnter * call ale#events#EnterEvent() autocmd BufEnter * call ale#events#EnterEvent(str2nr(expand('<abuf>')))
endif endif
augroup END augroup END
@ -245,7 +245,7 @@ function! ALEInitAuGroups() abort
augroup ALERunOnSaveGroup augroup ALERunOnSaveGroup
autocmd! autocmd!
if (g:ale_enabled && g:ale_lint_on_save) || g:ale_fix_on_save if (g:ale_enabled && g:ale_lint_on_save) || g:ale_fix_on_save
autocmd BufWritePost * call ale#events#SaveEvent() autocmd BufWritePost * call ale#events#SaveEvent(str2nr(expand('<abuf>')))
endif endif
augroup END augroup END

View File

@ -246,7 +246,7 @@ Execute(ALEFix should save files on the save event):
let g:ale_fixers.testft = ['AddDollars'] let g:ale_fixers.testft = ['AddDollars']
call SetUpLinters() call SetUpLinters()
call ale#events#SaveEvent() call ale#events#SaveEvent(bufnr(''))
" We should save the file. " We should save the file.
AssertEqual ['$a', '$b', '$c'], readfile('fix_test_file') AssertEqual ['$a', '$b', '$c'], readfile('fix_test_file')
@ -285,7 +285,7 @@ Execute(ALEFix should still lint with no linters to be applied):
let g:ale_fixers.testft = [] let g:ale_fixers.testft = []
call SetUpLinters() call SetUpLinters()
call ale#events#SaveEvent() call ale#events#SaveEvent(bufnr(''))
Assert !filereadable('fix_test_file'), 'The file should not have been saved' Assert !filereadable('fix_test_file'), 'The file should not have been saved'
@ -317,7 +317,7 @@ Execute(ALEFix should still lint when nothing was fixed on save):
let g:ale_fixers.testft = ['DoNothing'] let g:ale_fixers.testft = ['DoNothing']
call SetUpLinters() call SetUpLinters()
call ale#events#SaveEvent() call ale#events#SaveEvent(bufnr(''))
Assert !filereadable('fix_test_file'), 'The file should not have been saved' Assert !filereadable('fix_test_file'), 'The file should not have been saved'

View File

@ -116,9 +116,9 @@ 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
AssertEqual [ AssertEqual [
\ 'BufEnter * call ale#events#EnterEvent()', \ 'BufEnter * call ale#events#EnterEvent(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost * call ale#Queue(300, ''lint_file'')', \ 'BufReadPost * call ale#Queue(0, ''lint_file'', str2nr(expand(''<abuf>'')))',
\ 'BufWinEnter * call ale#Queue(300, ''lint_file'')', \ 'BufWinEnter * call ale#Queue(0, ''lint_file'', str2nr(expand(''<abuf>'')))',
\ 'FileChangedShellPost * call ale#events#FileChangedEvent(str2nr(expand(''<abuf>'')))', \ 'FileChangedShellPost * call ale#events#FileChangedEvent(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALERunOnEnterGroup') \], CheckAutocmd('ALERunOnEnterGroup')
@ -151,7 +151,7 @@ Execute (g:ale_lint_on_save = 1 should bind no events):
let g:ale_fix_on_save = 0 let g:ale_fix_on_save = 0
AssertEqual [ AssertEqual [
\ 'BufWritePost * call ale#events#SaveEvent()', \ 'BufWritePost * call ale#events#SaveEvent(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALERunOnSaveGroup') \], CheckAutocmd('ALERunOnSaveGroup')
Execute (g:ale_lint_on_save = 0 and g:ale_fix_on_save = 1 should bind events): Execute (g:ale_lint_on_save = 0 and g:ale_fix_on_save = 1 should bind events):
@ -159,7 +159,7 @@ Execute (g:ale_lint_on_save = 0 and g:ale_fix_on_save = 1 should bind events):
let g:ale_fix_on_save = 1 let g:ale_fix_on_save = 1
AssertEqual [ AssertEqual [
\ 'BufWritePost * call ale#events#SaveEvent()', \ 'BufWritePost * call ale#events#SaveEvent(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALERunOnSaveGroup') \], CheckAutocmd('ALERunOnSaveGroup')
Execute (g:ale_fix_on_save = 1 should bind events even when ALE is disabled): Execute (g:ale_fix_on_save = 1 should bind events even when ALE is disabled):
@ -168,7 +168,7 @@ Execute (g:ale_fix_on_save = 1 should bind events even when ALE is disabled):
let g:ale_fix_on_save = 1 let g:ale_fix_on_save = 1
AssertEqual [ AssertEqual [
\ 'BufWritePost * call ale#events#SaveEvent()', \ 'BufWritePost * call ale#events#SaveEvent(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALERunOnSaveGroup') \], CheckAutocmd('ALERunOnSaveGroup')
Execute (g:ale_echo_cursor = 0 should bind no events): Execute (g:ale_echo_cursor = 0 should bind no events):

View File

@ -64,7 +64,7 @@ Execute(The buffer should be checked after entering it after the file has change
let b:ale_file_changed = 1 let b:ale_file_changed = 1
set filetype=foobar set filetype=foobar
call ale#events#EnterEvent() call ale#events#EnterEvent(bufnr(''))
AssertEqual [{ AssertEqual [{
\ 'bufnr': bufnr(''), \ 'bufnr': bufnr(''),