Skip to content

Commit 934eba3

Browse files
committed
feat(panel): add teardown method
1 parent faa40d7 commit 934eba3

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

lua/copilot/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local default_config = {
44
panel = {
55
enabled = true,
66
auto_refresh = false,
7-
---@type table<'accept'|'next'|'prev'|'dismiss', false|string>
7+
---@type table<'jump_prev'|'jump_next'|'accept'|'refresh'|'open', false|string>
88
keymap = {
99
jump_prev = "[[",
1010
jump_next = "]]",

lua/copilot/panel.lua

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local api = require("copilot.api")
22
local c = require("copilot.client")
3+
local config = require("copilot.config")
34
local hl_group = require("copilot.highlight").group
45
local util = require("copilot.util")
56

@@ -237,9 +238,17 @@ function panel:accept()
237238
-- Put cursor at the end of current line.
238239
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<End>", true, false, true), "n", false)
239240

240-
self:unlock():clear():lock()
241+
self:close()
242+
end
243+
244+
function panel:close()
245+
if self.bufnr and vim.api.nvim_win_is_valid(self.bufnr) then
246+
self:unlock():clear():lock()
247+
end
241248

242-
vim.api.nvim_win_close(self.winid, true)
249+
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
250+
vim.api.nvim_win_close(self.winid, true)
251+
end
243252
end
244253

245254
local function set_keymap(bufnr)
@@ -533,17 +542,20 @@ function mod.open(layout)
533542
panel:init()
534543
end
535544

536-
function mod.setup(config)
537-
if panel.setup_done then
545+
function mod.setup()
546+
local opts = config.get("panel") --[[@as copilot_config_panel]]
547+
if not opts.enabled then
538548
return
539549
end
540550

541-
config = config or {}
551+
if panel.setup_done then
552+
return
553+
end
542554

543-
panel.auto_refresh = config.auto_refresh or false
555+
panel.auto_refresh = opts.auto_refresh or false
544556

545-
panel.keymap = config.keymap or {}
546-
panel.layout = vim.tbl_deep_extend("force", panel.layout, config.layout or {})
557+
panel.keymap = opts.keymap or {}
558+
panel.layout = vim.tbl_deep_extend("force", panel.layout, opts.layout or {})
547559

548560
if panel.keymap.open then
549561
vim.keymap.set("i", panel.keymap.open, mod.open, {
@@ -555,4 +567,23 @@ function mod.setup(config)
555567
panel.setup_done = true
556568
end
557569

570+
function mod.teardown()
571+
local opts = config.get("panel") --[[@as copilot_config_panel]]
572+
if not opts.enabled then
573+
return
574+
end
575+
576+
if not panel.setup_done then
577+
return
578+
end
579+
580+
if panel.keymap.open then
581+
vim.keymap.del("i", panel.keymap.open)
582+
end
583+
584+
panel:close()
585+
586+
panel.setup_done = false
587+
end
588+
558589
return mod

0 commit comments

Comments
 (0)