Use ModeChanged events instead of InsertLeave emulation #4919 #4738
Some checks are pending
CI / build_image (push) Waiting to run
CI / test_ale (--linters-only) (push) Blocked by required conditions
CI / test_ale (--neovim-06-only) (push) Blocked by required conditions
CI / test_ale (--neovim-08-only) (push) Blocked by required conditions
CI / test_ale (--vim-80-only) (push) Blocked by required conditions
CI / test_ale (--vim-90-only) (push) Blocked by required conditions

`ModeChanged` looks like a more reliable way to detect an "exit insert mode" event and is a lot simpler (doesn't need a timer). Also, it can detect some other transitions like `\<C-o\>` in insert mode.
The `ModeChanged` event is available in:

* [Vim 8.2.3430](f1e8876fa2)
* [NeoVim 0.7.0](69bd1e4e36)

---------

Co-authored-by: Dmitry Zolotukhin <zlogic@gmail.com>
This commit is contained in:
w0rp
2025-03-10 21:28:20 +00:00
committed by GitHub
parent 3611c32d60
commit 0f71d4c494
3 changed files with 61 additions and 17 deletions

View File

@@ -100,6 +100,10 @@ if !exists('s:insert_leave_timer')
let s:insert_leave_timer = -1
endif
" True if the ModeChanged event exists.
" In this case, ModeChanged will be used instead of InsertLeave emulation.
let s:mode_changed_exists = exists('##ModeChanged')
function! ale#events#EmulateInsertLeave(buffer) abort
if mode() is# 'n'
call timer_stop(s:insert_leave_timer)
@@ -114,8 +118,12 @@ function! ale#events#InsertEnterEvent(buffer) abort
" Start a repeating timer if the use might not trigger InsertLeave, so we
" can emulate its behavior.
" If the ModeChanged autocmd exists, it will be used instead of this
" timer; as ModeChanged will be sent regardless of how the insert mode is
" exited, including <Esc>, <C-c> and <C-]>.
if ale#Var(a:buffer, 'lint_on_insert_leave')
\&& maparg("\<C-c>", 'i') isnot# '<Esc>'
\&& !s:mode_changed_exists
call timer_stop(s:insert_leave_timer)
let s:insert_leave_timer = timer_start(
\ 100,
@@ -126,10 +134,15 @@ function! ale#events#InsertEnterEvent(buffer) abort
endfunction
function! ale#events#InsertLeaveEvent(buffer) abort
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.
" If the ModeChanged event is available, it will be used instead of
" a timer.
if !s:mode_changed_exists
call timer_stop(s:insert_leave_timer)
call ale#Queue(0)
endif
if ale#Var(a:buffer, 'lint_on_insert_leave')
call ale#Queue(0, '', a:buffer)
endif
" Look for a warning to echo as soon as we leave Insert mode.
@@ -189,8 +202,11 @@ function! ale#events#Init() abort
"
" We will emulate leaving insert mode for users that might not
" trigger InsertLeave.
"
" If the ModeChanged event is available, this timer will not
" be used.
if g:ale_close_preview_on_insert
\|| (g:ale_lint_on_insert_leave && maparg("\<C-c>", 'i') isnot# '<Esc>')
\|| (g:ale_lint_on_insert_leave && maparg("\<C-c>", 'i') isnot# '<Esc>' && !s:mode_changed_exists)
autocmd InsertEnter * call ale#events#InsertEnterEvent(str2nr(expand('<abuf>')))
endif
@@ -211,7 +227,14 @@ function! ale#events#Init() abort
endif
if l:add_insert_leave_event
autocmd InsertLeave * call ale#events#InsertLeaveEvent(str2nr(expand('<abuf>')))
if s:mode_changed_exists
" If the ModeChanged event is available, handle any
" transition from the Insert mode to any other mode.
autocmd ModeChanged i*:* call ale#events#InsertLeaveEvent(str2nr(expand('<abuf>')))
else
" If ModeChanged is not available, handle InsertLeave events.
autocmd InsertLeave * call ale#events#InsertLeaveEvent(str2nr(expand('<abuf>')))
endif
endif
if g:ale_hover_cursor