dense-analysis / ale

Check syntax in Vim/Neovim asynchronously and fix files, with Language Server Protocol (LSP) support
BSD 2-Clause "Simplified" License
13.51k stars 1.43k forks source link

Possible fix for issue #4642: use ModeChanged events instead of InsertLeave emulation #4738

Open zlogic opened 6 months ago

zlogic commented 6 months ago

ALE is my favourite way of using LSPs and linters, the only thing that might need improvement is https://github.com/dense-analysis/ale/issues/4642. Any time I press Esc and start typing or moving around the document (muscle memory :) ), Vim beeps and Tried switching to NeoVim and Helix, only to return back to Vim+ALE :)

I've done some research/testing during this weekend, and it looks like Vim 9 has some issues (a timeout reset?) if a timer is started from an InsertEnterEvent. InsertEnter is called before switching into Insert mode, so mode() might potentially return "normal" mode if the timer is triggered too early. I was able improve the situation by specifying a shorter timeoutlen, but that doesn't seem like an ideal solution. When Esc or\<C-[\> is pressed, Vim waits some time to see if it's an escape code for something else (like an Arrow Up key).

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:

so the current tests are covering both InsertMode emulation and the new ModeChanged events.

With this PoC, the situation has improved significantly on my machine - no more beeping, and exiting Insert mode is instantaneous.

zlogic commented 6 months ago

Or it might make sense to just make the InsertLeave emulation optional (configurable), perhaps some developers might use Esc to run the linter and other event handlers, but use \<C-c\> to temporarily exit Insert mode without any possible side-effects/callbacks.