fix(snippet): cancel session on <Esc> in Select mode #39238

Problem: <Esc> in a Select-mode tabstop leaves the session and highlight active.
CursorMoved isn’t triggered since the cursor doesn’t move.

Solution: use ModeChanged (s:n) instead. Defer with vim.schedule() to avoid transient
s:n from jump().
This commit is contained in:
glepnir
2026-05-10 21:19:12 +08:00
committed by GitHub
parent 5d1910e1e0
commit 2dc0efccb0
3 changed files with 50 additions and 0 deletions

View File

@@ -1,3 +1,15 @@
--- @brief
--- Snippet expansion and navigation.
---
--- Internal autocmds live in the `nvim.snippet` augroup. To disable them (e.g.
--- to manage sessions yourself), clear it:
---
--- ```lua
--- vim.api.nvim_clear_autocmds({ group = 'nvim.snippet', buffer = 0 })
--- ```
---
--- Heads-up: |vim.snippet.jump()| re-registers them on every jump.
local G = vim.lsp._snippet_grammar
local snippet_group = vim.api.nvim_create_augroup('nvim.snippet', {})
local snippet_ns = vim.api.nvim_create_namespace('nvim.snippet')
@@ -468,6 +480,22 @@ local function setup_autocmds(bufnr)
M.stop()
end,
})
vim.api.nvim_create_autocmd('ModeChanged', {
group = snippet_group,
desc = 'Stop the snippet session when leaving select mode',
buf = bufnr,
callback = function(args)
if args.match ~= 's:n' then
return
end
vim.schedule(function()
if not M.active() or vim.api.nvim_get_mode().mode:match('^[siRS\19]') then
return
end
M.stop()
end)
end,
})
end
--- Expands the given snippet text.