Skip to content

Commit 46e9895

Browse files
committed
fix: support for keymap passthrough to function callbacks
1 parent 63be137 commit 46e9895

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

lua/copilot/keymaps/init.lua

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@ function M.register_keymap_with_passthrough(mode, key, action, desc)
4343
local keymap_key = mode .. ":" .. key
4444
-- Save any existing mapping for this key
4545
local existing = vim.fn.maparg(key, mode, false, true)
46-
if existing and existing.rhs and existing.rhs ~= "" then
47-
previous_keymaps[keymap_key] = existing.rhs
48-
logger.trace("Saved existing keymap for " .. keymap_key .. ": " .. existing.rhs)
46+
if existing then
47+
if existing.rhs and existing.rhs ~= "" then
48+
previous_keymaps[keymap_key] = { type = "rhs", value = existing.rhs }
49+
logger.trace("Saved existing keymap for " .. keymap_key .. ": " .. existing.rhs)
50+
elseif existing.callback then
51+
previous_keymaps[keymap_key] = { type = "callback", value = existing.callback }
52+
logger.trace("Saved existing keymap callback for " .. keymap_key)
53+
else
54+
previous_keymaps[keymap_key] = nil
55+
logger.trace("No existing keymap for " .. keymap_key)
56+
end
4957
else
5058
previous_keymaps[keymap_key] = nil
5159
logger.trace("No existing keymap for " .. keymap_key)
@@ -61,8 +69,13 @@ function M.register_keymap_with_passthrough(mode, key, action, desc)
6169
local prev = previous_keymaps[keymap_key]
6270

6371
if prev then
64-
logger.trace("Passing through to previous keymap for " .. keymap_key .. ": " .. prev)
65-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(prev, true, false, true), mode, true)
72+
if prev.type == "rhs" then
73+
logger.trace("Passing through to previous keymap for " .. keymap_key .. ": " .. prev.value)
74+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(prev.value, true, false, true), mode, true)
75+
elseif prev.type == "callback" then
76+
logger.trace("Passing through to previous keymap callback for " .. keymap_key)
77+
prev.value()
78+
end
6679
return "<Ignore>"
6780
end
6881
logger.trace("No previous keymap to pass through for " .. keymap_key)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--|---------|-----
2+
01|123
3+
02|456
4+
03|~
5+
04|~
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|<e] [+] 1,1 All
11+
10|W [
12+
13+
--|---------|-----
14+
01|000000000000000
15+
02|000000000000000
16+
03|111111111111111
17+
04|111111111111111
18+
05|111111111111111
19+
06|111111111111111
20+
07|111111111111111
21+
08|111111111111111
22+
09|222222222222222
23+
10|333333333333333

tests/test_keymaps.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ T["keymaps()"]["passthrough Esc - base test setting highlight"] = function()
2929
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
3030
end
3131

32+
T["keymaps()"]["passthrough Esc with func - return false, will remove hl"] = function()
33+
child.o.lines, child.o.columns = 10, 15
34+
child.lua(
35+
[[vim.keymap.set("n", "<Esc>", function() vim.cmd.nohlsearch() end, { desc = "general clear highlights" })]]
36+
)
37+
child.lua([[
38+
require("copilot.keymaps").register_keymap_with_passthrough("n", "<esc>", function()
39+
return false
40+
end, "Passthrough Esc")
41+
]])
42+
child.type_keys("i123", "<Esc>", "o456", "<Esc>")
43+
child.type_keys("/123", "<CR>")
44+
child.type_keys("<Esc>")
45+
46+
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
47+
end
48+
3249
T["keymaps()"]["passthrough Esc - return false, will remove hl"] = function()
3350
child.o.lines, child.o.columns = 10, 15
3451
child.lua([[vim.keymap.set("n", "<Esc>", "<cmd>noh<CR>", { desc = "general clear highlights" })]])

0 commit comments

Comments
 (0)