#4605 Use a single time for InsertLeave emulation

Use a single timer for InsertLeave emulation to optimise it for many
buffers, and specifically lint the buffer we entered insert mode on.
This commit is contained in:
w0rp
2023-09-08 18:51:04 +01:00
parent dd3abf1ad9
commit dadc778451

View File

@@ -92,10 +92,18 @@ function! ale#events#FileChangedEvent(buffer) abort
endif endif
endfunction endfunction
function! ale#events#EmulateInsertLeave(timer) abort " A timer for emulating InsertLeave.
"
" We only need a single timer, and we'll lint the last buffer we entered
" insert mode on.
if !exists('s:insert_leave_timer')
let s:insert_leave_timer = -1
endif
function! ale#events#EmulateInsertLeave(buffer) abort
if mode() is# 'n' if mode() is# 'n'
call timer_stop(a:timer) call timer_stop(s:insert_leave_timer)
call ale#Queue(0) call ale#Queue(0, '', a:buffer)
endif endif
endfunction endfunction
@@ -108,20 +116,19 @@ function! ale#events#InsertEnterEvent(buffer) abort
" can emulate its behavior. " can emulate its behavior.
if ale#Var(a:buffer, 'lint_on_insert_leave') if ale#Var(a:buffer, 'lint_on_insert_leave')
\&& maparg("\<C-c>", 'i') isnot# '<Esc>' \&& maparg("\<C-c>", 'i') isnot# '<Esc>'
call timer_stop(getbufvar(a:buffer, 'ale_insert_leave_timer', -1)) call timer_stop(s:insert_leave_timer)
let l:timer = timer_start( let s:insert_leave_timer = timer_start(
\ 100, \ 100,
\ function('ale#events#EmulateInsertLeave'), \ {-> ale#events#EmulateInsertLeave(a:buffer) },
\ {'repeat': -1} \ {'repeat': -1}
\) \)
call setbufvar(a:buffer, 'ale_insert_leave_timer', l:timer)
endif endif
endfunction endfunction
function! ale#events#InsertLeaveEvent(buffer) abort function! ale#events#InsertLeaveEvent(buffer) abort
if ale#Var(a:buffer, 'lint_on_insert_leave') if ale#Var(a:buffer, 'lint_on_insert_leave')
" Kill the InsertLeave emulation if the event fired. " Kill the InsertLeave emulation if the event fired.
call timer_stop(getbufvar(a:buffer, 'ale_insert_leave_timer', -1)) call timer_stop(s:insert_leave_timer)
call ale#Queue(0) call ale#Queue(0)
endif endif