fix(keys): fixed buffer-local mappings

This commit is contained in:
Folke Lemaitre 2023-10-07 20:41:06 +02:00
parent 5aaafcb301
commit 09e30f88cd
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 22 additions and 6 deletions

View File

@ -72,7 +72,7 @@ function M:_add(keys)
local plugins = self.active[keys.id] local plugins = self.active[keys.id]
-- always delete the mapping immediately to prevent recursive mappings -- always delete the mapping immediately to prevent recursive mappings
self:_del(keys, buf) self:_del(keys)
self.active[keys.id] = nil self.active[keys.id] = nil
if plugins then if plugins then
@ -81,6 +81,11 @@ 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
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true) local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
-- insert instead of append the lhs -- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, "i", false) vim.api.nvim_feedkeys(feed, "i", false)
@ -93,6 +98,7 @@ function M:_add(keys)
}) })
end end
-- buffer-local mappings
if keys.ft then if keys.ft then
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
pattern = keys.ft, pattern = keys.ft,
@ -102,9 +108,7 @@ function M:_add(keys)
else else
-- Only create the mapping if its managed by lazy -- Only create the mapping if its managed by lazy
-- otherwise the plugin is supposed to manage it -- otherwise the plugin is supposed to manage it
if keys[2] then self:_set(keys, event.buf)
self:_del(keys, event.buf)
end
end end
end, end,
}) })
@ -113,10 +117,22 @@ function M:_add(keys)
end end
end end
-- Delete a mapping and create the real global
-- mapping when needed
---@param keys LazyKeys
function M:_del(keys)
pcall(vim.keymap.del, keys.mode, keys[1])
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lazy
if not keys.ft then
self:_set(keys)
end
end
-- Create a mapping if it is managed by lazy
---@param keys LazyKeys ---@param keys LazyKeys
---@param buf number? ---@param buf number?
function M:_del(keys, buf) function M:_set(keys, buf)
pcall(vim.keymap.del, keys.mode, keys[1], { buffer = buf })
if keys[2] then if keys[2] then
local opts = M.opts(keys) local opts = M.opts(keys)
opts.buffer = buf opts.buffer = buf