Skip to content

Commit 75fe839

Browse files
committed
fix: warn the user if unregistering a keymap fails
Fixes #533
1 parent ef3fc4a commit 75fe839

File tree

5 files changed

+113
-28
lines changed

5 files changed

+113
-28
lines changed

lua/copilot/panel/init.lua

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ function panel:close()
243243
end
244244
end
245245

246-
local function set_keymap(bufnr)
246+
function M.set_keymap(bufnr)
247247
if panel.keymap.accept then
248248
vim.keymap.set("n", panel.keymap.accept, M.accept, {
249249
buffer = bufnr,
@@ -275,6 +275,8 @@ local function set_keymap(bufnr)
275275
silent = true,
276276
})
277277
end
278+
279+
M.keymaps_set = true
278280
end
279281

280282
function panel:ensure_bufnr()
@@ -293,7 +295,7 @@ function panel:ensure_bufnr()
293295
vim.api.nvim_set_option_value(name, value, { buf = self.bufnr })
294296
end
295297

296-
set_keymap(self.bufnr)
298+
M.set_keymap(self.bufnr)
297299
end
298300

299301
vim.api.nvim_buf_set_name(self.bufnr, self.panel_uri)
@@ -581,12 +583,16 @@ function M.teardown()
581583
return
582584
end
583585

584-
if panel.keymap.open then
585-
vim.keymap.del("i", panel.keymap.open)
586+
util.unset_keymap_if_exists("i", panel.keymap.open)
587+
588+
if M.keymaps_set then
589+
M.unset_keymap_if_exists("n", panel.keymap.accept)
590+
M.unset_keymap_if_exists("n", panel.keymap.jump_prev)
591+
M.unset_keymap_if_exists("n", panel.keymap.jump_next)
592+
M.unset_keymap_if_exists("n", panel.keymap.refresh)
586593
end
587594

588595
panel:close()
589-
590596
panel.setup_done = false
591597
end
592598

lua/copilot/suggestion/init.lua

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,29 +179,12 @@ local function set_keymap(keymap)
179179
end
180180

181181
local function unset_keymap(keymap)
182-
if keymap.accept then
183-
vim.keymap.del("i", keymap.accept)
184-
end
185-
186-
if keymap.accept_word then
187-
vim.keymap.del("i", keymap.accept_word)
188-
end
189-
190-
if keymap.accept_line then
191-
vim.keymap.del("i", keymap.accept_line)
192-
end
193-
194-
if keymap.next then
195-
vim.keymap.del("i", keymap.next)
196-
end
197-
198-
if keymap.prev then
199-
vim.keymap.del("i", keymap.prev)
200-
end
201-
202-
if keymap.dismiss then
203-
vim.keymap.del("i", keymap.dismiss)
204-
end
182+
util.unset_keymap_if_exists("i", keymap.accept)
183+
util.unset_keymap_if_exists("i", keymap.accept_word)
184+
util.unset_keymap_if_exists("i", keymap.accept_line)
185+
util.unset_keymap_if_exists("i", keymap.next)
186+
util.unset_keymap_if_exists("i", keymap.prev)
187+
util.unset_keymap_if_exists("i", keymap.dismiss)
205188
end
206189

207190
local function stop_timer()

lua/copilot/util.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,35 @@ function M.strutf16len(str)
113113
end
114114
end
115115

116+
---@param mode string
117+
---@param key string|false
118+
function M.unset_keymap_if_exists(mode, key)
119+
if not key then
120+
return
121+
end
122+
123+
local ok, err = pcall(vim.api.nvim_del_keymap, mode, key)
124+
125+
if not ok then
126+
local suggestion_keymaps = config.suggestion.keymap or {}
127+
local panel_keymaps = config.panel.keymap or {}
128+
local found = false
129+
130+
for _, tbl in ipairs({ suggestion_keymaps, panel_keymaps }) do
131+
for _, v in pairs(tbl) do
132+
if v == key then
133+
if found then
134+
logger.error("Keymap " .. key .. " is used for two different actions, please review your configuration.")
135+
return
136+
else
137+
found = true
138+
end
139+
end
140+
end
141+
end
142+
143+
logger.error("Could not unset keymap for " .. mode .. " " .. key .. ": " .. err)
144+
end
145+
end
146+
116147
return M
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--|---------|---------|---------|---------|---------|---------|---------|---------|
2+
01|1, 2, 3,
3+
02|4, 5, 6,
4+
03|7, 8, 9,
5+
04|~
6+
05|~
7+
06|~
8+
07|~
9+
08|~
10+
09|~
11+
10|~
12+
11|~
13+
12|~
14+
13|~
15+
14|~
16+
15|~
17+
16|~
18+
17|~
19+
18|~
20+
19|~
21+
20|~
22+
21|~
23+
22|~
24+
23|[No Name] [+] 3,10-17 All
25+
24|-- INSERT --
26+
27+
--|---------|---------|---------|---------|---------|---------|---------|---------|
28+
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
29+
02|00000000000000000000000000000000000000000000000000000000000000000000000000000000
30+
03|00000000000000000000000000000000000000000000000000000000000000000000000000000000
31+
04|11111111111111111111111111111111111111111111111111111111111111111111111111111111
32+
05|11111111111111111111111111111111111111111111111111111111111111111111111111111111
33+
06|11111111111111111111111111111111111111111111111111111111111111111111111111111111
34+
07|11111111111111111111111111111111111111111111111111111111111111111111111111111111
35+
08|11111111111111111111111111111111111111111111111111111111111111111111111111111111
36+
09|11111111111111111111111111111111111111111111111111111111111111111111111111111111
37+
10|11111111111111111111111111111111111111111111111111111111111111111111111111111111
38+
11|11111111111111111111111111111111111111111111111111111111111111111111111111111111
39+
12|11111111111111111111111111111111111111111111111111111111111111111111111111111111
40+
13|11111111111111111111111111111111111111111111111111111111111111111111111111111111
41+
14|11111111111111111111111111111111111111111111111111111111111111111111111111111111
42+
15|11111111111111111111111111111111111111111111111111111111111111111111111111111111
43+
16|11111111111111111111111111111111111111111111111111111111111111111111111111111111
44+
17|11111111111111111111111111111111111111111111111111111111111111111111111111111111
45+
18|11111111111111111111111111111111111111111111111111111111111111111111111111111111
46+
19|11111111111111111111111111111111111111111111111111111111111111111111111111111111
47+
20|11111111111111111111111111111111111111111111111111111111111111111111111111111111
48+
21|11111111111111111111111111111111111111111111111111111111111111111111111111111111
49+
22|11111111111111111111111111111111111111111111111111111111111111111111111111111111
50+
23|22222222222222222222222222222222222222222222222222222222222222222222222222222222
51+
24|33333333333344444444444444444444444444444444444444444444444444444444444444444444

tests/test_suggestion.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,18 @@ T["suggestion()"]["accept_line, 1 line, then accept"] = function()
167167
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 49, 50 }, ignore_attr = { 49, 50 } })
168168
end
169169

170+
T["suggestion()"]["duplicated keymap yields correct error message"] = function()
171+
child.config.suggestion = child.config.suggestion .. "auto_trigger = true," .. "keymap = { accept = '<M-CR>' },"
172+
child.configure_copilot()
173+
child.type_keys("i1, 2, 3,", "<Esc>", "o4, 5, 6,", "<Esc>", "o7, ")
174+
child.wait_for_suggestion()
175+
child.type_keys("<M-CR>", "<Tab>")
176+
child.cmd_capture("Copilot disable")
177+
local mess = child.cmd_capture("messages")
178+
assert(mess:match("E31") == nil, "Error E31 should have been handled")
179+
assert(mess:match("please review your configuration") ~= nil, "Should have logged a message about keymap conflict")
180+
181+
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 49, 50 }, ignore_attr = { 49, 50 } })
182+
end
183+
170184
return T

0 commit comments

Comments
 (0)