From 3bb43d8be92de4dc2d365c8f4ad413cf2185cbf9 Mon Sep 17 00:00:00 2001 From: Filip Joska Date: Fri, 12 Dec 2025 21:24:26 +0100 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20timer=20callback=20be?= =?UTF-8?q?ing=20executed=20after=20timer:stop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/showkeys/init.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lua/showkeys/init.lua b/lua/showkeys/init.lua index 1272705..0653050 100644 --- a/lua/showkeys/init.lua +++ b/lua/showkeys/init.lua @@ -16,7 +16,9 @@ M.open = function() utils.gen_winconfig() vim.bo[state.buf].ft = "Showkeys" - state.timer = vim.loop.new_timer() + state.timer = vim.uv.new_timer() + state.timer_id = 0 + state.on_key = vim.on_key(function(_, char) if not state.win then state.win = api.nvim_open_win(state.buf, false, state.config.winopts) @@ -25,8 +27,17 @@ M.open = function() utils.parse_key(char) + state.timer_id = state.timer_id + 1 + local current_id = state.timer_id + state.timer:stop() - state.timer:start(state.config.timeout * 1000, 0, vim.schedule_wrap(utils.clear_and_close)) + state.timer:start(state.config.timeout * 1000, 0, function() + vim.schedule(function() + if state.timer_id ~= current_id then return end + state.timer_id = 0 + utils.clear_and_close() + end) + end) end) api.nvim_set_hl(0, "SkInactive", { default = true, link = "Visual" }) From 199c0a4455a80d1854b77368ccbb9ca56cbb672f Mon Sep 17 00:00:00 2001 From: Filip Joska Date: Fri, 12 Dec 2025 21:46:09 +0100 Subject: [PATCH 2/2] simplify the callback --- lua/showkeys/init.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lua/showkeys/init.lua b/lua/showkeys/init.lua index 0653050..c0d6b44 100644 --- a/lua/showkeys/init.lua +++ b/lua/showkeys/init.lua @@ -31,13 +31,11 @@ M.open = function() local current_id = state.timer_id state.timer:stop() - state.timer:start(state.config.timeout * 1000, 0, function() - vim.schedule(function() - if state.timer_id ~= current_id then return end - state.timer_id = 0 - utils.clear_and_close() - end) - end) + state.timer:start(state.config.timeout * 1000, 0, vim.schedule_wrap(function () + if state.timer_id ~= current_id then return end + state.timer_id = 0 + utils.clear_and_close() + end)) end) api.nvim_set_hl(0, "SkInactive", { default = true, link = "Visual" })