From f16d7586fcdd8b2e3850d0abb7e46f944125cc25 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 2 Jun 2023 11:30:25 -0700 Subject: [PATCH] fix: close input window when entering cmdline window (#99) --- lua/dressing/input.lua | 13 ++++++++++++- lua/dressing/util.lua | 3 +++ tests/minimal_init.lua | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lua/dressing/input.lua b/lua/dressing/input.lua index f039135..e3279bb 100644 --- a/lua/dressing/input.lua +++ b/lua/dressing/input.lua @@ -96,7 +96,18 @@ local function confirm(text) -- otherwise vim gets into a very weird and bad state. I was seeing text get -- deleted from the buffer after the input window closes. vim.defer_fn(function() - pcall(vim.api.nvim_win_close, ctx.winid, true) + local ok, err = pcall(vim.api.nvim_win_close, ctx.winid, true) + -- If we couldn't close the window because we're in the cmdline, + -- try again after WinLeave + if not ok and err and err:match("^E11:") then + local winid = ctx.winid + vim.api.nvim_create_autocmd("WinLeave", { + callback = vim.schedule_wrap(function() + pcall(vim.api.nvim_win_close, winid, true) + end), + once = true, + }) + end if text and history[#history] ~= text then table.insert(history, text) end diff --git a/lua/dressing/util.lua b/lua/dressing/util.lua index 130e4d3..c09ff0a 100644 --- a/lua/dressing/util.lua +++ b/lua/dressing/util.lua @@ -161,6 +161,9 @@ M._on_win_closed = function(winid) end M.schedule_wrap_before_vimenter = function(func) + if vim.g.is_test then + return func + end return function(...) if vim.v.vim_did_enter == 0 then return vim.schedule_wrap(func)(...) diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index d2e93e8..232d05a 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -1,2 +1,3 @@ vim.cmd([[set runtimepath+=.]]) vim.cmd([[runtime! plugin/plenary.vim]]) +vim.g.is_test = true