From 39de11a2fa7f4b91556631c49a673bf3e48bcc16 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 20 May 2024 21:15:03 +0200 Subject: [PATCH] fix(keys): properly re-create buffer-local mappings. Fixes #1448 --- lua/lazy/core/handler/keys.lua | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lua/lazy/core/handler/keys.lua b/lua/lazy/core/handler/keys.lua index 382d732..1d4ada3 100644 --- a/lua/lazy/core/handler/keys.lua +++ b/lua/lazy/core/handler/keys.lua @@ -123,11 +123,6 @@ function M:_add(keys) Util.track() end - -- Create the real buffer-local mapping - if keys.ft then - self:_set(keys, buf) - end - if keys.mode:sub(-1) == "a" then lhs = lhs .. "" end @@ -162,19 +157,21 @@ function M:_add(keys) end end --- Delete a mapping and create the real global +-- Delete a mapping and create the real global/buffer-local -- mapping when needed ---@param keys LazyKeys function M:_del(keys) - pcall(vim.keymap.del, keys.mode, keys.lhs, { - -- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer - -- So the mapping could still exist in other buffers - buffer = keys.ft and true or nil, - }) - -- make sure to create global mappings when needed - -- buffer-local mappings are managed by lazy - if not keys.ft then - self:_set(keys) + -- bufs will be all buffers of the filetype for a buffer-local mapping + -- OR `false` for a global mapping + local bufs = keys.ft + and vim.tbl_filter(function(buf) + return vim.bo[buf].filetype == keys.ft + end, vim.api.nvim_list_bufs()) + or { false } + + for _, buf in ipairs(bufs) do + pcall(vim.keymap.del, keys.mode, keys.lhs, { buffer = buf or nil }) + self:_set(keys, buf or nil) end end