Skip to content

Commit f8fa941

Browse files
committed
fix: esc key mapping issues
Fixes #570
1 parent 9720f81 commit f8fa941

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

lua/copilot/keymaps/init.lua

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ local logger = require("copilot.logger")
22
local config = require("copilot.config")
33

44
local M = {}
5-
65
local previous_keymaps = {}
76

87
---@param mode string
98
---@param key string
109
---@param action function
1110
---@param desc string
1211
function M.register_keymap(mode, key, action, desc)
13-
if not mode or not key or not action then
12+
if not key then
13+
return
14+
end
15+
16+
if not mode or not action then
17+
logger.error("Invalid parameters to register_keymap" .. vim.inspect({ mode, key, action, desc }))
1418
return
1519
end
1620

@@ -27,7 +31,12 @@ end
2731
---@param action function: boolean
2832
---@param desc string
2933
function M.register_keymap_with_passthrough(mode, key, action, desc)
30-
if not mode or not key or not action then
34+
if not key then
35+
return
36+
end
37+
38+
if not mode or not action then
39+
logger.error("Invalid parameters to register_keymap_with_passthrough" .. vim.inspect({ mode, key, action, desc }))
3140
return
3241
end
3342

@@ -41,16 +50,21 @@ function M.register_keymap_with_passthrough(mode, key, action, desc)
4150
end
4251

4352
vim.keymap.set(mode, key, function()
44-
local action_ran = action()
45-
if not action_ran then
46-
-- If there was a previous mapping, execute it
53+
if action() then
54+
return "<Ignore>"
55+
else
4756
local prev = previous_keymaps[keymap_key]
57+
4858
if prev then
4959
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(prev, true, false, true), mode, true)
60+
return "<Ignore>"
5061
end
62+
63+
return key
5164
end
5265
end, {
5366
desc = desc,
67+
expr = true,
5468
silent = true,
5569
})
5670
end

lua/copilot/nes/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local M = {
1212
local function accept_suggestion(goto_end)
1313
local result = nes_api.nes_apply_pending_nes()
1414

15-
if goto_end then
15+
if goto_end and result then
1616
nes_api.nes_walk_cursor_end_edit()
1717
end
1818

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--|---------|-----
2+
01|1 2 3
3+
02|4 5 6
4+
03|7 8
5+
04|~
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|<e] [+] 3,5 All
11+
10|-- INSERT --
12+
13+
--|---------|-----
14+
01|000000000000000
15+
02|000000000000000
16+
03|000000000000000
17+
04|111111111111111
18+
05|111111111111111
19+
06|111111111111111
20+
07|111111111111111
21+
08|111111111111111
22+
09|222222222222222
23+
10|333333333333444

tests/test_suggestion.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ T["suggestion()"]["accept_word, 1 word, then dismiss"] = function()
115115
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
116116
end
117117

118+
T["suggestion()"]["accept_word, 1 word, then dismiss with Esc"] = function()
119+
child.o.lines, child.o.columns = 10, 15
120+
child.config.suggestion = child.config.suggestion
121+
.. "auto_trigger = true,"
122+
.. "keymap = { accept_word = '<C-e>', dismiss = '<Esc>' },"
123+
child.cmd("e numbers_with_spaces.txt")
124+
child.configure_copilot()
125+
child.type_keys("i1 2 3", "<Esc>", "o4 5 6", "<Esc>", "o7 ")
126+
child.wait_for_suggestion()
127+
child.type_keys("<C-e>", "<Esc>")
128+
129+
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
130+
end
131+
118132
T["suggestion()"]["accept_word, 1 word, then accept"] = function()
119133
child.o.lines, child.o.columns = 10, 15
120134
child.config.suggestion = child.config.suggestion

0 commit comments

Comments
 (0)