fix(picker.input): better handling of stopinsert with prompt buffers. Closes #723

This commit is contained in:
Folke Lemaitre 2025-01-23 23:02:26 +01:00
parent 38649556ee
commit c2916cb526
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 22 additions and 1 deletions

View file

@ -24,6 +24,14 @@ function M.new(picker)
text = picker.opts.live and self.filter.search or self.filter.pattern, text = picker.opts.live and self.filter.search or self.filter.pattern,
ft = "regex", ft = "regex",
on_win = function(win) on_win = function(win)
-- HACK: set all other picker input prompt buffers to nofile.
-- Otherwise when the prompt buffer is closed,
-- Neovim always stops insert mode.
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if buf ~= win.buf and vim.bo[buf].filetype == "snacks_picker_input" then
vim.bo[buf].buftype = "nofile"
end
end
vim.fn.prompt_setprompt(win.buf, "") vim.fn.prompt_setprompt(win.buf, "")
win:focus() win:focus()
vim.cmd("startinsert!") vim.cmd("startinsert!")
@ -75,6 +83,19 @@ function M:close()
self.picker = nil -- needed for garbage collection of the picker self.picker = nil -- needed for garbage collection of the picker
end end
function M:stopinsert()
-- only stop insert mode if needed
if not vim.fn.mode():find("^i") then
return
end
local buf = vim.api.nvim_get_current_buf()
-- if the other buffer is a prompt, then don't stop insert mode
if buf ~= self.win.buf and vim.bo[buf].buftype == "prompt" then
return
end
vim.cmd("stopinsert")
end
function M:statuscolumn() function M:statuscolumn()
local parts = {} ---@type string[] local parts = {} ---@type string[]
local function add(str, hl) local function add(str, hl)

View file

@ -464,10 +464,10 @@ end
--- Close the picker --- Close the picker
function M:close() function M:close()
vim.cmd.stopinsert()
if self.closed then if self.closed then
return return
end end
self.input:stopinsert()
self:hist_record(true) self:hist_record(true)
self.closed = true self.closed = true