Fix #271 - Add the ability to open the quickfix or loclist windows only after saving a file

This commit is contained in:
w0rp 2017-08-08 00:46:42 +01:00
parent 2edea15358
commit 16cfedf04a
7 changed files with 84 additions and 13 deletions

View File

@ -50,10 +50,11 @@ function! ale#cursor#TruncatedEcho(message) abort
endfunction endfunction
function! s:FindItemAtCursor() abort function! s:FindItemAtCursor() abort
let l:info = get(g:ale_buffer_info, bufnr('%'), {'loclist': []}) let l:info = get(g:ale_buffer_info, bufnr(''), {})
let l:loclist = get(l:info, 'loclist', [])
let l:pos = getcurpos() let l:pos = getcurpos()
let l:index = ale#util#BinarySearch(l:info.loclist, l:pos[1], l:pos[2]) let l:index = ale#util#BinarySearch(l:loclist, l:pos[1], l:pos[2])
let l:loc = l:index >= 0 ? l:info.loclist[l:index] : {} let l:loc = l:index >= 0 ? l:loclist[l:index] : {}
return [l:info, l:loc] return [l:info, l:loc]
endfunction endfunction

View File

@ -83,7 +83,7 @@ function! ale#engine#CreateDirectory(buffer) abort
endfunction endfunction
function! ale#engine#RemoveManagedFiles(buffer) abort function! ale#engine#RemoveManagedFiles(buffer) abort
let l:info = get(g:ale_buffer_info, a:buffer) let l:info = get(g:ale_buffer_info, a:buffer, {})
" We can't delete anything in a sandbox, so wait until we escape from " We can't delete anything in a sandbox, so wait until we escape from
" it to delete temporary files and directories. " it to delete temporary files and directories.
@ -298,6 +298,9 @@ function! ale#engine#SetResults(buffer, loclist) abort
endif endif
if l:linting_is_done if l:linting_is_done
" Reset the save event marker, used for opening windows, etc.
call setbufvar(a:buffer, 'ale_save_event_fired', 0)
" Automatically remove all managed temporary files and directories " Automatically remove all managed temporary files and directories
" now that all jobs have completed. " now that all jobs have completed.
call ale#engine#RemoveManagedFiles(a:buffer) call ale#engine#RemoveManagedFiles(a:buffer)

View File

@ -1,6 +1,7 @@
" Author: w0rp <devw0rp@gmail.com> " Author: w0rp <devw0rp@gmail.com>
function! ale#events#SaveEvent(buffer) abort function! ale#events#SaveEvent(buffer) abort
call setbufvar(a:buffer, 'ale_save_event_fired', 1)
let l:should_lint = ale#Var(a:buffer, '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

View File

@ -11,6 +11,16 @@ function! ale#list#IsQuickfixOpen() abort
return 0 return 0
endfunction endfunction
" Check if we should open the list, based on the save event being fired, and
" that setting being on, or the setting just being set to `1`.
function! s:ShouldOpen(buffer) abort
let l:val = ale#Var(a:buffer, 'open_list')
let l:saved = getbufvar(a:buffer, 'ale_save_event_fired', 0)
return (type(l:val) == type(1) && l:val == 1)
\ || (l:val ==# 'on_save' && l:saved)
endfunction
function! ale#list#SetLists(buffer, loclist) abort function! ale#list#SetLists(buffer, loclist) abort
let l:title = expand('#' . a:buffer . ':p') let l:title = expand('#' . a:buffer . ':p')
@ -35,8 +45,10 @@ function! ale#list#SetLists(buffer, loclist) abort
endif endif
endif endif
" If we have errors in our list, open the list. Only if it isn't already open let l:keep_open = ale#Var(a:buffer, 'keep_list_window_open')
if (g:ale_open_list && !empty(a:loclist)) || g:ale_keep_list_window_open
" Open a window to show the problems if we need to.
if s:ShouldOpen(a:buffer) && (l:keep_open || !empty(a:loclist))
let l:winnr = winnr() let l:winnr = winnr()
let l:mode = mode() let l:mode = mode()
let l:reset_visual_selection = l:mode ==? 'v' || l:mode ==# "\<c-v>" let l:reset_visual_selection = l:mode ==? 'v' || l:mode ==# "\<c-v>"
@ -68,7 +80,7 @@ function! ale#list#SetLists(buffer, loclist) abort
endfunction endfunction
function! ale#list#CloseWindowIfNeeded(buffer) abort function! ale#list#CloseWindowIfNeeded(buffer) abort
if g:ale_keep_list_window_open || !g:ale_open_list if ale#Var(a:buffer, 'keep_list_window_open') || !s:ShouldOpen(a:buffer)
return return
endif endif

View File

@ -398,7 +398,7 @@ g:ale_history_log_output *g:ale_history_log_output*
g:ale_keep_list_window_open *g:ale_keep_list_window_open* g:ale_keep_list_window_open *g:ale_keep_list_window_open*
*b:ale_keep_list_window_open*
Type: |Number| Type: |Number|
Default: `0` Default: `0`
@ -600,13 +600,17 @@ g:ale_maximum_file_size *g:ale_maximum_file_size*
g:ale_open_list *g:ale_open_list* g:ale_open_list *g:ale_open_list*
*b:ale_open_list*
Type: |Number| Type: |Number| or |String|
Default: `0` Default: `0`
When set to `1`, this will cause ALE to automatically open a window for When set to `1`, this will cause ALE to automatically open a window for the
the loclist (|lopen|) or for the quickfix list instead if loclist (|lopen|) or for the quickfix list instead if |g:ale_set_quickfix|
|g:ale_set_quickfix| is `1`. (|copen|) is `1`. (|copen|)
When set to `'on_save'`, ALE will only open the loclist after buffers have
been saved. The list will be opened some time after buffers are saved and
any linter for a buffer returns results.
The window will be kept open until all warnings or errors are cleared, The window will be kept open until all warnings or errors are cleared,
including those not set by ALE, unless |g:ale_keep_list_window_open| is set including those not set by ALE, unless |g:ale_keep_list_window_open| is set

View File

@ -81,6 +81,8 @@ Before:
After: After:
Restore Restore
unlet! b:ale_save_event_fired
unlet! b:ale_enabled
unlet g:buffer_result unlet g:buffer_result
let g:ale_buffer_info = {} let g:ale_buffer_info = {}
call ale#linter#Reset() call ale#linter#Reset()
@ -251,3 +253,17 @@ Execute(The Save event should respect the buffer number):
\ 'type': 'E', \ 'type': 'E',
\ }, \ },
\], GetSimplerLoclist() \], GetSimplerLoclist()
Execute(The Save event should set b:ale_save_event_fired to 1):
let b:ale_enabled = 0
call ale#events#SaveEvent(bufnr(''))
" This flag needs to be set so windows can be opened, etc.
AssertEqual 1, b:ale_save_event_fired
Execute(b:ale_save_event_fired should be set to 0 when results are set):
let b:ale_save_event_fired = 1
call ale#engine#SetResults(bufnr(''), [])
AssertEqual 0, b:ale_save_event_fired

View File

@ -34,6 +34,10 @@ After:
unlet! g:loclist unlet! g:loclist
unlet! b:ale_list_window_size unlet! b:ale_list_window_size
unlet! b:ale_open_list
unlet! b:ale_keep_list_window_open
unlet! b:ale_save_event_fired
delfunction GetQuickfixHeight delfunction GetQuickfixHeight
" Close quickfix window after every execute block " Close quickfix window after every execute block
@ -163,3 +167,33 @@ Execute(The quickfix window height should be correct for the quickfix list with
call ale#list#CloseWindowIfNeeded(bufnr('')) call ale#list#CloseWindowIfNeeded(bufnr(''))
AssertEqual 8, GetQuickfixHeight() AssertEqual 8, GetQuickfixHeight()
Execute(The buffer ale_open_list option should be respected):
let b:ale_open_list = 1
call ale#list#SetLists(bufnr('%'), g:loclist)
Assert ale#list#IsQuickfixOpen()
Execute(The buffer ale_keep_list_window_open option should be respected):
let b:ale_open_list = 1
let b:ale_keep_list_window_open = 1
call ale#list#SetLists(bufnr('%'), g:loclist)
call ale#list#CloseWindowIfNeeded(bufnr(''))
call ale#list#SetLists(bufnr('%'), [])
call ale#list#CloseWindowIfNeeded(bufnr(''))
Assert ale#list#IsQuickfixOpen()
Execute(The ale_open_list='on_save' option should work):
let b:ale_open_list = 'on_save'
call ale#list#SetLists(bufnr('%'), g:loclist)
" The list shouldn't open yet, the event wasn't fired.
Assert !ale#list#IsQuickfixOpen()
let b:ale_save_event_fired = 1
call ale#list#SetLists(bufnr('%'), g:loclist)
" Now the list should have opened.
Assert ale#list#IsQuickfixOpen()