11local api = require (" copilot.api" )
22local c = require (" copilot.client" )
3+ local config = require (" copilot.config" )
34local hl_group = require (" copilot.highlight" ).group
45local 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
243252end
244253
245254local function set_keymap (bufnr )
@@ -533,17 +542,20 @@ function mod.open(layout)
533542 panel :init ()
534543end
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
556568end
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+
558589return mod
0 commit comments