fix(keys): properly re-create buffer-local mappings. Fixes #1448

This commit is contained in:
Folke Lemaitre 2024-05-20 21:15:03 +02:00
parent 0de782a6b0
commit 39de11a2fa
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 12 additions and 15 deletions

View File

@ -123,11 +123,6 @@ function M:_add(keys)
Util.track() Util.track()
end end
-- Create the real buffer-local mapping
if keys.ft then
self:_set(keys, buf)
end
if keys.mode:sub(-1) == "a" then if keys.mode:sub(-1) == "a" then
lhs = lhs .. "<C-]>" lhs = lhs .. "<C-]>"
end end
@ -162,19 +157,21 @@ function M:_add(keys)
end end
end end
-- Delete a mapping and create the real global -- Delete a mapping and create the real global/buffer-local
-- mapping when needed -- mapping when needed
---@param keys LazyKeys ---@param keys LazyKeys
function M:_del(keys) function M:_del(keys)
pcall(vim.keymap.del, keys.mode, keys.lhs, { -- bufs will be all buffers of the filetype for a buffer-local mapping
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer -- OR `false` for a global mapping
-- So the mapping could still exist in other buffers local bufs = keys.ft
buffer = keys.ft and true or nil, and vim.tbl_filter(function(buf)
}) return vim.bo[buf].filetype == keys.ft
-- make sure to create global mappings when needed end, vim.api.nvim_list_bufs())
-- buffer-local mappings are managed by lazy or { false }
if not keys.ft then
self:_set(keys) 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
end end