diff options
author | w0rp <devw0rp@gmail.com> | 2017-10-28 15:41:14 +0100 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2017-10-28 15:41:14 +0100 |
commit | ea3a8e3c62d3051fee9342ece2e04bcd04b392b6 (patch) | |
tree | 36507761c15f0264877d9979b0f5a10f91aaa3c3 | |
parent | 6e681d9066bde1f2a6b7583fa91e9c94cfffb11e (diff) | |
download | ale-ea3a8e3c62d3051fee9342ece2e04bcd04b392b6.zip |
#817 Move code for toggling ALE to its own file
-rw-r--r-- | autoload/ale/toggle.vim | 140 | ||||
-rw-r--r-- | plugin/ale.vim | 140 | ||||
-rw-r--r-- | test/test_ale_toggle.vader | 29 | ||||
-rw-r--r-- | test/test_autocmd_commands.vader | 5 |
4 files changed, 178 insertions, 136 deletions
diff --git a/autoload/ale/toggle.vim b/autoload/ale/toggle.vim new file mode 100644 index 00000000..acddea4b --- /dev/null +++ b/autoload/ale/toggle.vim @@ -0,0 +1,140 @@ +function! ale#toggle#InitAuGroups() abort + " 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 + + augroup ALEPatternOptionsGroup + autocmd! + if g:ale_enabled && g:ale_pattern_options_enabled + autocmd BufEnter,BufRead * call ale#pattern_options#SetOptions() + endif + augroup END + + augroup ALERunOnTextChangedGroup + autocmd! + if g:ale_enabled + if l:text_changed is? 'always' || l:text_changed is# '1' + autocmd TextChanged,TextChangedI * call ale#Queue(g:ale_lint_delay) + elseif l:text_changed is? 'normal' + autocmd TextChanged * call ale#Queue(g:ale_lint_delay) + elseif l:text_changed is? 'insert' + autocmd TextChangedI * call ale#Queue(g:ale_lint_delay) + endif + endif + augroup END + + 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>'))) + endif + 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! + if (g:ale_enabled && g:ale_lint_on_save) || g:ale_fix_on_save + autocmd BufWritePost * call ale#events#SaveEvent(str2nr(expand('<abuf>'))) + endif + 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 + if !g:ale_fix_on_save + augroup! ALERunOnSaveGroup + endif + + augroup! ALEPatternOptionsGroup + augroup! ALERunOnTextChangedGroup + augroup! ALERunOnEnterGroup + augroup! ALERunOnInsertLeave + augroup! ALECursorGroup + endif +endfunction + +function! ale#toggle#Toggle() abort + let g:ale_enabled = !get(g:, 'ale_enabled') + + if g:ale_enabled + " Set pattern options again, if enabled. + if g:ale_pattern_options_enabled + call ale#pattern_options#SetOptions() + endif + + " Lint immediately, including running linters against the file. + call ale#Queue(0, 'lint_file') + + if g:ale_set_balloons + call ale#balloon#Enable() + endif + else + for l:key in keys(g:ale_buffer_info) + " The key could be a filename or a buffer number, so try and + " convert it to a number. We need a number for the other + " functions. + let l:buffer = str2nr(l:key) + + if l:buffer > 0 + " Stop all jobs and clear the results for everything, and delete + " all of the data we stored for the buffer. + call ale#engine#Cleanup(l:buffer) + endif + endfor + + " Remove highlights for the current buffer now. + if g:ale_set_highlights + call ale#highlight#UpdateHighlights() + endif + + if g:ale_set_balloons + call ale#balloon#Disable() + endif + endif + + call ale#toggle#InitAuGroups() +endfunction + +function! ale#toggle#Enable() abort + if !g:ale_enabled + call ale#toggle#Toggle() + endif +endfunction + +function! ale#toggle#Disable() abort + if g:ale_enabled + call ale#toggle#Toggle() + endif +endfunction diff --git a/plugin/ale.vim b/plugin/ale.vim index 23e8572a..83ca2dd0 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -201,137 +201,6 @@ call ale#Set('completion_enabled', 0) call ale#Set('completion_delay', 100) call ale#Set('completion_max_suggestions', 50) -function! ALEInitAuGroups() abort - " 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 - - augroup ALEPatternOptionsGroup - autocmd! - if g:ale_enabled && g:ale_pattern_options_enabled - autocmd BufEnter,BufRead * call ale#pattern_options#SetOptions() - endif - augroup END - - augroup ALERunOnTextChangedGroup - autocmd! - if g:ale_enabled - if l:text_changed is? 'always' || l:text_changed is# '1' - autocmd TextChanged,TextChangedI * call ale#Queue(g:ale_lint_delay) - elseif l:text_changed is? 'normal' - autocmd TextChanged * call ale#Queue(g:ale_lint_delay) - elseif l:text_changed is? 'insert' - autocmd TextChangedI * call ale#Queue(g:ale_lint_delay) - endif - endif - augroup END - - 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>'))) - endif - 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! - if (g:ale_enabled && g:ale_lint_on_save) || g:ale_fix_on_save - autocmd BufWritePost * call ale#events#SaveEvent(str2nr(expand('<abuf>'))) - endif - 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 - if !g:ale_fix_on_save - augroup! ALERunOnSaveGroup - endif - - augroup! ALEPatternOptionsGroup - augroup! ALERunOnTextChangedGroup - augroup! ALERunOnEnterGroup - augroup! ALERunOnInsertLeave - augroup! ALECursorGroup - endif -endfunction - -function! s:ALEToggle() abort - let g:ale_enabled = !get(g:, 'ale_enabled') - - if g:ale_enabled - " Set pattern options again, if enabled. - if g:ale_pattern_options_enabled - call ale#pattern_options#SetOptions() - endif - - " Lint immediately, including running linters against the file. - call ale#Queue(0, 'lint_file') - - if g:ale_set_balloons - call ale#balloon#Enable() - endif - else - for l:key in keys(g:ale_buffer_info) - " The key could be a filename or a buffer number, so try and - " convert it to a number. We need a number for the other - " functions. - let l:buffer = str2nr(l:key) - - if l:buffer > 0 - " Stop all jobs and clear the results for everything, and delete - " all of the data we stored for the buffer. - call ale#engine#Cleanup(l:buffer) - endif - endfor - - " Remove highlights for the current buffer now. - if g:ale_set_highlights - call ale#highlight#UpdateHighlights() - endif - - if g:ale_set_balloons - call ale#balloon#Disable() - endif - endif - - call ALEInitAuGroups() -endfunction - -call ALEInitAuGroups() - if g:ale_set_balloons call ale#balloon#Enable() endif @@ -352,9 +221,9 @@ command! -bar ALELast :call ale#loclist_jumping#JumpToIndex(-1) command! -bar ALEDetail :call ale#cursor#ShowCursorDetail() " Define commands for turning ALE on or off. -command! -bar ALEToggle :call s:ALEToggle() -command! -bar ALEEnable :if !g:ale_enabled | ALEToggle | endif -command! -bar ALEDisable :if g:ale_enabled | ALEToggle | endif +command! -bar ALEToggle :call ale#toggle#Toggle() +command! -bar ALEEnable :call ale#toggle#Enable() +command! -bar ALEDisable :call ale#toggle#Disable() " A command for linting manually. command! -bar ALELint :call ale#Queue(0, 'lint_file') @@ -381,6 +250,9 @@ nnoremap <silent> <Plug>(ale_lint) :ALELint<Return> nnoremap <silent> <Plug>(ale_detail) :ALEDetail<Return> nnoremap <silent> <Plug>(ale_fix) :ALEFix<Return> +" Set up autocmd groups now. +call ale#toggle#InitAuGroups() + " Housekeeping augroup ALECleanupGroup diff --git a/test/test_ale_toggle.vader b/test/test_ale_toggle.vader index f3dbf102..733ae35d 100644 --- a/test/test_ale_toggle.vader +++ b/test/test_ale_toggle.vader @@ -2,6 +2,7 @@ Before: Save g:ale_buffer_info Save g:ale_set_signs Save g:ale_set_lists_synchronously + Save g:ale_run_synchronously let g:ale_set_signs = 1 let g:ale_set_lists_synchronously = 1 @@ -175,3 +176,31 @@ Execute(ALEToggle should skip filename keys and preserve them): \ 'history': [], \ }, \ get(g:ale_buffer_info, '/foo/bar/baz.txt', {}) + +Execute(ALEDisable should reset everything and stay disabled): + " We can just lint sychronously for these tests. + let g:ale_run_synchronously = 1 + + call ale#Lint() + + AssertEqual g:expected_loclist, getloclist(0) + + ALEDisable + + AssertEqual [], getloclist(0) + AssertEqual 0, g:ale_enabled + + ALEDisable + + AssertEqual [], getloclist(0) + AssertEqual 0, g:ale_enabled + +Execute(ALEEnable should enable ALE and lint again): + " We can just lint sychronously for these tests. + let g:ale_enabled = 0 + let g:ale_run_synchronously = 1 + + ALEEnable + + AssertEqual g:expected_loclist, getloclist(0) + AssertEqual 1, g:ale_enabled diff --git a/test/test_autocmd_commands.vader b/test/test_autocmd_commands.vader index 17e3b16d..6bc3f5cd 100644 --- a/test/test_autocmd_commands.vader +++ b/test/test_autocmd_commands.vader @@ -1,6 +1,7 @@ Before: function! CheckAutocmd(group) - call ALEInitAuGroups() + call ale#toggle#InitAuGroups() + redir => l:output execute 'silent! autocmd ' . a:group redir END @@ -58,7 +59,7 @@ After: call ale#completion#Disable() endif - call ALEInitAuGroups() + call ale#toggle#InitAuGroups() Execute (g:ale_lint_on_text_changed = 0 should bind no events): let g:ale_lint_on_text_changed = 0 |