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()
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 .. "<C-]>"
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