Skip to content

Commit 09e30f8

Browse files
committed
fix(keys): fixed buffer-local mappings
1 parent 5aaafcb commit 09e30f8

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

lua/lazy/core/handler/keys.lua

+22-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function M:_add(keys)
7272
local plugins = self.active[keys.id]
7373

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

7878
if plugins then
@@ -81,6 +81,11 @@ function M:_add(keys)
8181
Util.track()
8282
end
8383

84+
-- Create the real buffer-local mapping
85+
if keys.ft then
86+
self:_set(keys, buf)
87+
end
88+
8489
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
8590
-- insert instead of append the lhs
8691
vim.api.nvim_feedkeys(feed, "i", false)
@@ -93,6 +98,7 @@ function M:_add(keys)
9398
})
9499
end
95100

101+
-- buffer-local mappings
96102
if keys.ft then
97103
vim.api.nvim_create_autocmd("FileType", {
98104
pattern = keys.ft,
@@ -102,9 +108,7 @@ function M:_add(keys)
102108
else
103109
-- Only create the mapping if its managed by lazy
104110
-- otherwise the plugin is supposed to manage it
105-
if keys[2] then
106-
self:_del(keys, event.buf)
107-
end
111+
self:_set(keys, event.buf)
108112
end
109113
end,
110114
})
@@ -113,10 +117,22 @@ function M:_add(keys)
113117
end
114118
end
115119

120+
-- Delete a mapping and create the real global
121+
-- mapping when needed
122+
---@param keys LazyKeys
123+
function M:_del(keys)
124+
pcall(vim.keymap.del, keys.mode, keys[1])
125+
-- make sure to create global mappings when needed
126+
-- buffer-local mappings are managed by lazy
127+
if not keys.ft then
128+
self:_set(keys)
129+
end
130+
end
131+
132+
-- Create a mapping if it is managed by lazy
116133
---@param keys LazyKeys
117134
---@param buf number?
118-
function M:_del(keys, buf)
119-
pcall(vim.keymap.del, keys.mode, keys[1], { buffer = buf })
135+
function M:_set(keys, buf)
120136
if keys[2] then
121137
local opts = M.opts(keys)
122138
opts.buffer = buf

0 commit comments

Comments
 (0)