Set up most of the autocmd events in one group

This commit is contained in:
w0rp 2018-06-20 13:35:57 +01:00
parent 9674132933
commit 0e1528ec34
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
3 changed files with 123 additions and 171 deletions

View File

@ -83,13 +83,13 @@ function! ale#events#Init() abort
" This value used to be a Boolean as a Number, and is now a String. " This value used to be a Boolean as a Number, and is now a String.
let l:text_changed = '' . g:ale_lint_on_text_changed let l:text_changed = '' . g:ale_lint_on_text_changed
augroup ALEPatternOptionsGroup augroup ALEEvents
autocmd! autocmd!
autocmd BufEnter,BufRead * call ale#pattern_options#SetOptions(str2nr(expand('<abuf>')))
augroup END
augroup ALERunOnTextChangedGroup " These events always need to be set up.
autocmd! autocmd BufEnter,BufRead * call ale#pattern_options#SetOptions(str2nr(expand('<abuf>')))
autocmd BufWritePost * call ale#events#SaveEvent(str2nr(expand('<abuf>')))
if g:ale_enabled if g:ale_enabled
if l:text_changed is? 'always' || l:text_changed is# '1' if l:text_changed is? 'always' || l:text_changed is# '1'
autocmd TextChanged,TextChangedI * call ale#Queue(g:ale_lint_delay) autocmd TextChanged,TextChangedI * call ale#Queue(g:ale_lint_delay)
@ -98,61 +98,36 @@ function! ale#events#Init() abort
elseif l:text_changed is? 'insert' elseif l:text_changed is? 'insert'
autocmd TextChangedI * call ale#Queue(g:ale_lint_delay) autocmd TextChangedI * call ale#Queue(g:ale_lint_delay)
endif endif
endif
augroup END
augroup ALERunOnEnterGroup
autocmd!
if g:ale_enabled
" Handle everything that needs to happen when buffers are entered. " Handle everything that needs to happen when buffers are entered.
autocmd BufEnter * call ale#events#EnterEvent(str2nr(expand('<abuf>'))) autocmd BufEnter * call ale#events#EnterEvent(str2nr(expand('<abuf>')))
endif
if g:ale_enabled && g:ale_lint_on_enter if 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>')))
endif
if g:ale_lint_on_filetype_changed
" Only start linting if the FileType actually changes after
" opening a buffer. The FileType will fire when buffers are opened.
autocmd FileType * call ale#events#FileTypeEvent(
\ str2nr(expand('<abuf>')),
\ expand('<amatch>')
\)
endif
if g:ale_lint_on_insert_leave
autocmd InsertLeave * call ale#Queue(0)
endif
if g:ale_echo_cursor
autocmd CursorMoved,CursorHold * call ale#cursor#EchoCursorWarningWithDelay()
" Look for a warning to echo as soon as we leave Insert mode.
" The script's position variable used when moving the cursor will
" not be changed here.
autocmd InsertLeave * call ale#cursor#EchoCursorWarning()
endif
endif endif
augroup END augroup END
augroup ALERunOnFiletypeChangeGroup
autocmd!
if g:ale_enabled && g:ale_lint_on_filetype_changed
" Only start linting if the FileType actually changes after
" opening a buffer. The FileType will fire when buffers are opened.
autocmd FileType * call ale#events#FileTypeEvent(
\ str2nr(expand('<abuf>')),
\ expand('<amatch>')
\)
endif
augroup END
augroup ALERunOnSaveGroup
autocmd!
autocmd BufWritePost * call ale#events#SaveEvent(str2nr(expand('<abuf>')))
augroup END
augroup ALERunOnInsertLeave
autocmd!
if g:ale_enabled && g:ale_lint_on_insert_leave
autocmd InsertLeave * call ale#Queue(0)
endif
augroup END
augroup ALECursorGroup
autocmd!
if g:ale_enabled && g:ale_echo_cursor
autocmd CursorMoved,CursorHold * call ale#cursor#EchoCursorWarningWithDelay()
" Look for a warning to echo as soon as we leave Insert mode.
" The script's position variable used when moving the cursor will
" not be changed here.
autocmd InsertLeave * call ale#cursor#EchoCursorWarning()
endif
augroup END
if !g:ale_enabled
augroup! ALERunOnTextChangedGroup
augroup! ALERunOnEnterGroup
augroup! ALERunOnInsertLeave
augroup! ALECursorGroup
endif
endfunction endfunction

View File

@ -32,12 +32,8 @@ Before:
\}] \}]
let g:expected_groups = [ let g:expected_groups = [
\ 'ALECleanupGroup', \ 'ALECleanupGroup',
\ 'ALECursorGroup', \ 'ALEEvents',
\ 'ALEHighlightBufferGroup', \ 'ALEHighlightBufferGroup',
\ 'ALERunOnEnterGroup',
\ 'ALERunOnFiletypeChangeGroup',
\ 'ALERunOnSaveGroup',
\ 'ALERunOnTextChangedGroup',
\] \]
function! ToggleTestCallback(buffer, output) function! ToggleTestCallback(buffer, output)
@ -60,7 +56,7 @@ Before:
let l:results = [] let l:results = []
for l:line in split(l:output, "\n") for l:line in split(l:output, "\n")
let l:match = matchlist(l:line, '^ALE[a-zA-Z]\+Group') let l:match = matchlist(l:line, '^ALE[a-zA-Z]\+')
" We don't care about some groups here. " We don't care about some groups here.
if !empty(l:match) if !empty(l:match)
@ -139,13 +135,7 @@ Execute(ALEToggle should reset everything and then run again):
AssertEqual [], getloclist(0), 'The loclist was not cleared' AssertEqual [], getloclist(0), 'The loclist was not cleared'
AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared' AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
AssertEqual [], getmatches(), 'The highlights were not cleared' AssertEqual [], getmatches(), 'The highlights were not cleared'
AssertEqual AssertEqual g:expected_groups, ParseAuGroups()
\ [
\ 'ALECleanupGroup',
\ 'ALEHighlightBufferGroup',
\ 'ALERunOnSaveGroup',
\ ],
\ ParseAuGroups()
" Toggle ALE on, everything should be set up and run again. " Toggle ALE on, everything should be set up and run again.
ALEToggle ALEToggle

View File

@ -26,8 +26,9 @@ Before:
" for the one matching the current buffer. " for the one matching the current buffer.
if l:line =~# '<buffer=' . bufnr('') . '>' if l:line =~# '<buffer=' . bufnr('') . '>'
let l:header .= ' <buffer>' let l:header .= ' <buffer>'
else elseif l:line[:0] is# ' '
call add(l:matches, join(split(l:header . l:line))) call add(l:matches, join(split(l:header . l:line)))
else
let l:header = '' let l:header = ''
endif endif
endif endif
@ -38,16 +39,28 @@ Before:
return l:matches return l:matches
endfunction endfunction
Save g:ale_completion_enabled
Save g:ale_echo_cursor
Save g:ale_enabled Save g:ale_enabled
Save g:ale_lint_on_text_changed Save g:ale_fix_on_save
Save g:ale_lint_on_insert_leave
Save g:ale_pattern_options_enabled
Save g:ale_lint_on_enter Save g:ale_lint_on_enter
Save g:ale_lint_on_filetype_changed Save g:ale_lint_on_filetype_changed
Save g:ale_lint_on_insert_leave
Save g:ale_lint_on_save Save g:ale_lint_on_save
Save g:ale_echo_cursor Save g:ale_lint_on_text_changed
Save g:ale_fix_on_save Save g:ale_pattern_options_enabled
Save g:ale_completion_enabled
" Turn everything on by defaul for these tests.
let g:ale_completion_enabled = 1
let g:ale_echo_cursor = 1
let g:ale_enabled = 1
let g:ale_fix_on_save = 1
let g:ale_lint_on_enter = 1
let g:ale_lint_on_filetype_changed = 1
let g:ale_lint_on_insert_leave = 1
let g:ale_lint_on_save = 1
let g:ale_lint_on_text_changed = 1
let g:ale_pattern_options_enabled = 1
After: After:
delfunction CheckAutocmd delfunction CheckAutocmd
@ -61,98 +74,95 @@ After:
call ale#events#Init() call ale#events#Init()
Execute (g:ale_lint_on_text_changed = 0 should bind no events): Execute (All events should be set up when everything is on):
let g:ale_lint_on_text_changed = 0 let g:ale_echo_cursor = 1
AssertEqual [], CheckAutocmd('ALERunOnTextChangedGroup') AssertEqual
\ [
\ 'BufEnter * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\ 'BufEnter call ale#events#EnterEvent(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost call ale#Queue(0, ''lint_file'', str2nr(expand(''<abuf>'')))',
\ 'BufWinEnter * call ale#Queue(0, ''lint_file'', str2nr(expand(''<abuf>'')))',
\ 'BufWritePost * call ale#events#SaveEvent(str2nr(expand(''<abuf>'')))',
\ 'CursorHold * call ale#cursor#EchoCursorWarningWithDelay()',
\ 'CursorMoved * call ale#cursor#EchoCursorWarningWithDelay()',
\ 'FileChangedShellPost * call ale#events#FileChangedEvent(str2nr(expand(''<abuf>'')))',
\ 'FileType * call ale#events#FileTypeEvent( str2nr(expand(''<abuf>'')), expand(''<amatch>''))',
\ 'InsertLeave * call ale#Queue(0)',
\ 'InsertLeave call ale#cursor#EchoCursorWarning()',
\ 'TextChanged * call ale#Queue(g:ale_lint_delay)',
\ 'TextChangedI * call ale#Queue(g:ale_lint_delay)',
\ ],
\ CheckAutocmd('ALEEvents')
Execute (Only the required events should be bound even if various settings are off):
let g:ale_completion_enabled = 0
let g:ale_echo_cursor = 0
let g:ale_enabled = 0
let g:ale_fix_on_save = 0
let g:ale_lint_on_enter = 0
let g:ale_lint_on_filetype_changed = 0
let g:ale_lint_on_insert_leave = 0
let g:ale_lint_on_save = 0
let g:ale_lint_on_text_changed = 0
let g:ale_pattern_options_enabled = 0
AssertEqual
\ [
\ 'BufEnter * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\ 'BufWritePost * call ale#events#SaveEvent(str2nr(expand(''<abuf>'')))',
\ ],
\ CheckAutocmd('ALEEvents')
Execute (g:ale_lint_on_text_changed = 1 bind both events): Execute (g:ale_lint_on_text_changed = 1 bind both events):
let g:ale_lint_on_text_changed = 1 let g:ale_lint_on_text_changed = 1
AssertEqual [ AssertEqual
\ 'TextChanged * call ale#Queue(g:ale_lint_delay)', \ [
\ 'TextChangedI * call ale#Queue(g:ale_lint_delay)' \ 'TextChanged * call ale#Queue(g:ale_lint_delay)',
\], CheckAutocmd('ALERunOnTextChangedGroup') \ 'TextChangedI * call ale#Queue(g:ale_lint_delay)',
\ ],
\ filter(CheckAutocmd('ALEEvents'), 'v:val =~ ''^TextChanged''')
Execute (g:ale_lint_on_text_changed = 'always' should bind both events): Execute (g:ale_lint_on_text_changed = 'always' should bind both events):
let g:ale_lint_on_text_changed = 'always' let g:ale_lint_on_text_changed = 'always'
AssertEqual [ AssertEqual
\ 'TextChanged * call ale#Queue(g:ale_lint_delay)', \ [
\ 'TextChangedI * call ale#Queue(g:ale_lint_delay)' \ 'TextChanged * call ale#Queue(g:ale_lint_delay)',
\], CheckAutocmd('ALERunOnTextChangedGroup') \ 'TextChangedI * call ale#Queue(g:ale_lint_delay)',
\ ],
\ filter(CheckAutocmd('ALEEvents'), 'v:val =~ ''^TextChanged''')
Execute (g:ale_lint_on_text_changed = 'normal' should bind only TextChanged): Execute (g:ale_lint_on_text_changed = 'normal' should bind only TextChanged):
let g:ale_lint_on_text_changed = 'normal' let g:ale_lint_on_text_changed = 'normal'
AssertEqual [ AssertEqual
\ 'TextChanged * call ale#Queue(g:ale_lint_delay)', \ [
\], CheckAutocmd('ALERunOnTextChangedGroup') \ 'TextChanged * call ale#Queue(g:ale_lint_delay)',
\ ],
\ filter(CheckAutocmd('ALEEvents'), 'v:val =~ ''^TextChanged''')
Execute (g:ale_lint_on_text_changed = 'insert' should bind only TextChangedI): Execute (g:ale_lint_on_text_changed = 'insert' should bind only TextChangedI):
let g:ale_lint_on_text_changed = 'insert' let g:ale_lint_on_text_changed = 'insert'
AssertEqual [ AssertEqual
\ 'TextChangedI * call ale#Queue(g:ale_lint_delay)', \ [
\], CheckAutocmd('ALERunOnTextChangedGroup') \ 'TextChangedI * call ale#Queue(g:ale_lint_delay)',
\ ],
\ filter(CheckAutocmd('ALEEvents'), 'v:val =~ ''^TextChanged''')
Execute (g:ale_lint_on_insert_leave = 1 should bind InsertLeave): Execute (g:ale_lint_on_insert_leave = 1 should bind InsertLeave):
let g:ale_lint_on_insert_leave = 1 let g:ale_lint_on_insert_leave = 1
let g:ale_echo_cursor = 0
AssertEqual [
\ 'InsertLeave * call ale#Queue(0)',
\], CheckAutocmd('ALERunOnInsertLeave')
Execute (g:ale_lint_on_insert_leave = 0 should bind no events):
let g:ale_lint_on_insert_leave = 0
AssertEqual [], CheckAutocmd('ALERunOnInsertLeave')
Execute (g:ale_pattern_options_enabled = 1 should bind BufReadPost and BufEnter):
let g:ale_pattern_options_enabled = 1
AssertEqual [
\ 'BufEnter * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALEPatternOptionsGroup')
Execute (g:ale_pattern_options_enabled = 0 should still bind events):
let g:ale_pattern_options_enabled = 0
AssertEqual [
\ 'BufEnter * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALEPatternOptionsGroup')
Execute (g:ale_enabled = 0 should still bind pattern events):
let g:ale_enabled = 0
AssertEqual [
\ 'BufEnter * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost * call ale#pattern_options#SetOptions(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALEPatternOptionsGroup')
Execute (g:ale_lint_on_enter = 0 should bind only the BufEnter event):
let g:ale_lint_on_enter = 0
AssertEqual AssertEqual
\ ['BufEnter * call ale#events#EnterEvent(str2nr(expand(''<abuf>'')))'], \ [
\ CheckAutocmd('ALERunOnEnterGroup') \ 'InsertLeave * call ale#Queue(0)',
\ ],
Execute (g:ale_lint_on_enter = 1 should bind the required events): \ filter(CheckAutocmd('ALEEvents'), 'v:val =~ ''^InsertLeave''')
let g:ale_lint_on_enter = 1
AssertEqual [
\ 'BufEnter * call ale#events#EnterEvent(str2nr(expand(''<abuf>'')))',
\ 'BufReadPost * call ale#Queue(0, ''lint_file'', str2nr(expand(''<abuf>'')))',
\ 'BufWinEnter * call ale#Queue(0, ''lint_file'', str2nr(expand(''<abuf>'')))',
\ 'FileChangedShellPost * call ale#events#FileChangedEvent(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALERunOnEnterGroup')
Execute (g:ale_lint_on_filetype_changed = 0 should bind no events):
let g:ale_lint_on_filetype_changed = 0
AssertEqual [], CheckAutocmd('ALERunOnFiletypeChangeGroup')
Execute (g:ale_lint_on_filetype_changed = 1 should bind the FileType event): 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
@ -164,30 +174,7 @@ Execute (g:ale_lint_on_filetype_changed = 1 should bind the FileType event):
\ . 'expand(''<amatch>'')' \ . 'expand(''<amatch>'')'
\ . ')', \ . ')',
\ ], \ ],
\ CheckAutocmd('ALERunOnFiletypeChangeGroup') \ filter(CheckAutocmd('ALEEvents'), 'v:val =~ ''\v^FileType''')
Execute (The SaveEvent should always be bound):
let g:ale_enabled = 0
let g:ale_lint_on_save = 0
let g:ale_fix_on_save = 0
AssertEqual [
\ 'BufWritePost * call ale#events#SaveEvent(str2nr(expand(''<abuf>'')))',
\], CheckAutocmd('ALERunOnSaveGroup')
Execute (g:ale_echo_cursor = 0 should bind no events):
let g:ale_echo_cursor = 0
AssertEqual [], CheckAutocmd('ALECursorGroup')
Execute (g:ale_echo_cursor = 1 should bind cursor events):
let g:ale_echo_cursor = 1
AssertEqual [
\ 'CursorHold * call ale#cursor#EchoCursorWarningWithDelay()',
\ 'CursorMoved * call ale#cursor#EchoCursorWarningWithDelay()',
\ 'InsertLeave * call ale#cursor#EchoCursorWarning()',
\], CheckAutocmd('ALECursorGroup')
Execute (ALECleanupGroup should include the right commands): Execute (ALECleanupGroup should include the right commands):
AssertEqual [ AssertEqual [