Skip to content

Commit 9204088

Browse files
committed
fix: Tab passthrough
Fixes #670
1 parent 0552b44 commit 9204088

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lua/copilot/keymaps/init.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ local function save_existing_keymap(mode, key, keymap_key)
5959
logger.trace("Saved existing keymap for " .. keymap_key .. ": " .. existing.rhs)
6060
return
6161
elseif existing.callback then
62-
previous_keymaps[keymap_key] = { type = "callback", value = existing.callback }
62+
previous_keymaps[keymap_key] = { type = "callback", value = existing.callback, expr = existing.expr == 1 }
6363
logger.trace("Saved existing keymap callback for " .. keymap_key)
6464
return
6565
end
@@ -110,13 +110,16 @@ function M.register_keymap_with_passthrough(mode, key, action, desc, bufnr)
110110
return "<Ignore>"
111111
elseif prev.type == "callback" then
112112
logger.trace("Passing through to previous keymap callback for " .. keymap_key)
113+
if prev.expr then
114+
return prev.value()
115+
end
113116
prev.value()
114117
return "<Ignore>"
115118
end
116119
end
117120

118121
logger.trace("No previous keymap to pass through for " .. keymap_key)
119-
return key
122+
return vim.api.nvim_replace_termcodes(key, true, false, true)
120123
end, {
121124
desc = desc,
122125
expr = true,

tests/test_keymaps.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,16 @@ T["keymaps()"]["passthrough Esc - return true, will not remove hl"] = function()
7676
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
7777
end
7878

79+
T["keymaps()"]["passthrough Tab - return false inserts tab character"] = function()
80+
child.lua([[
81+
require("copilot.keymaps").register_keymap_with_passthrough("i", "<Tab>", function()
82+
return false
83+
end, "Passthrough Tab", vim.api.nvim_get_current_buf())
84+
]])
85+
child.type_keys("i", "hello", "<Tab>", "world", "<Esc>")
86+
87+
local line = child.lua("return vim.api.nvim_get_current_line()")
88+
MiniTest.expect.equality(line, "hello\tworld")
89+
end
90+
7991
return T

0 commit comments

Comments
 (0)