From ae6708a90b89a686f85b51288f488f4186dff2d4 Mon Sep 17 00:00:00 2001 From: Sean Mackesey Date: Mon, 25 Dec 2023 16:01:16 -0500 Subject: [PATCH] fix(which_key): always close on telescope prompt exit * Add autocmd to make `which_key` window close on prompt exit Currently `actions.which_key` supports a `close_with_action` option (default true). When this is set, the `which_key` window will close after any Telescope action is triggered. This makes sense. However, when it is false, the `which_key` window remains open even after Telescope closes. This seems like a bug. This PR fixes this by setting an autocommand when `close_with_action` is false to close on prompt exit. * [docgen] Update doc/telescope.txt skip-checks: true * Add autocmd to make `which_key` window close on prompt exit Currently `actions.which_key` supports a `close_with_action` option (default true). When this is set, the `which_key` window will close after any Telescope action is triggered. This makes sense. However, when it is false, the `which_key` window remains open even after Telescope closes. This seems like a bug. This PR fixes this by setting an autocommand when `close_with_action` is false to close on prompt exit. --------- Co-authored-by: Github Actions --- lua/telescope/actions/init.lua | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index f9e24b6..5acc8da 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -1407,20 +1407,27 @@ actions.which_key = function(prompt_bufnr, opts) end end - -- only set up autocommand after showing preview completed + -- if close_with_action is true, close the which_key window when any action is triggered + -- otherwise close the window when the prompt buffer is closed + local close_event, close_pattern, close_buffer if opts.close_with_action then - vim.schedule(function() - vim.api.nvim_create_autocmd("User", { - pattern = "TelescopeKeymap", - once = true, - callback = function() - pcall(vim.api.nvim_win_close, km_win_id, true) - pcall(vim.api.nvim_win_close, km_opts.border.win_id, true) - require("telescope.utils").buf_delete(km_buf) - end, - }) - end) + close_event, close_pattern, close_buffer = "User", "TelescopeKeymap", nil + else + close_event, close_pattern, close_buffer = "BufWinLeave", nil, prompt_bufnr end + -- only set up autocommand after showing preview completed + vim.schedule(function() + vim.api.nvim_create_autocmd(close_event, { + pattern = close_pattern, + buffer = close_buffer, + once = true, + callback = function() + pcall(vim.api.nvim_win_close, km_win_id, true) + pcall(vim.api.nvim_win_close, km_opts.border.win_id, true) + require("telescope.utils").buf_delete(km_buf) + end, + }) + end) end --- Move from a none fuzzy search to a fuzzy one