Skip to content

Commit

Permalink
fix(keys): fixed buffer-local mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 7, 2023
1 parent 5aaafcb commit 09e30f8
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lua/lazy/core/handler/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function M:_add(keys)
local plugins = self.active[keys.id]

-- always delete the mapping immediately to prevent recursive mappings
self:_del(keys, buf)
self:_del(keys)
self.active[keys.id] = nil

if plugins then
Expand All @@ -81,6 +81,11 @@ function M:_add(keys)
Util.track()
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)
-- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, "i", false)
Expand All @@ -93,6 +98,7 @@ function M:_add(keys)
})
end

-- buffer-local mappings
if keys.ft then
vim.api.nvim_create_autocmd("FileType", {
pattern = keys.ft,
Expand All @@ -102,9 +108,7 @@ function M:_add(keys)
else
-- Only create the mapping if its managed by lazy
-- otherwise the plugin is supposed to manage it
if keys[2] then
self:_del(keys, event.buf)
end
self:_set(keys, event.buf)
end
end,
})
Expand All @@ -113,10 +117,22 @@ function M:_add(keys)
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 buf number?
function M:_del(keys, buf)
pcall(vim.keymap.del, keys.mode, keys[1], { buffer = buf })
function M:_set(keys, buf)
if keys[2] then
local opts = M.opts(keys)
opts.buffer = buf
Expand Down

0 comments on commit 09e30f8

Please sign in to comment.