@@ -13,7 +13,6 @@ local copilot = {
1313 ns_id = vim .api .nvim_create_namespace (" copilot.suggestion" ),
1414 extmark_id = 1 ,
1515
16- uuid = nil ,
1716 _copilot_timer = nil ,
1817 _copilot = {
1918 first = nil ,
@@ -143,12 +142,14 @@ local function stop_timer()
143142 end
144143end
145144
146- local function reject_current ()
147- if copilot .uuid then
145+ --- @param bufnr integer
146+ local function reject (bufnr )
147+ local uuid = vim .fn .getbufvar (bufnr , " _copilot_uuid" , " " )
148+ if uuid ~= " " then
148149 with_client (function (client )
149- api .notify_rejected (client , { uuids = { copilot . uuid } }, function () end )
150+ api .notify_rejected (client , { uuids = { uuid } }, function () end )
150151 end )
151- copilot . uuid = nil
152+ vim . api . nvim_buf_set_var ( bufnr , " _copilot_uuid " , " " )
152153 end
153154end
154155
@@ -249,9 +250,9 @@ local function update_preview()
249250
250251 vim .api .nvim_buf_set_extmark (0 , copilot .ns_id , vim .fn .line (" ." ) - 1 , cursor_col - 1 , extmark )
251252
252- if suggestion .uuid ~= copilot . uuid then
253- reject_current ( )
254- copilot . uuid = suggestion .uuid
253+ if suggestion .uuid ~= vim . fn . getbufvar ( 0 , " _copilot_uuid " , " " ) then
254+ reject ( 0 )
255+ vim . api . nvim_buf_set_var ( 0 , " _copilot_uuid " , suggestion .uuid )
255256 with_client (function (client )
256257 api .notify_shown (client , { uuid = suggestion .uuid }, function () end )
257258 end )
@@ -414,6 +415,7 @@ function mod.accept(modifier)
414415 cancel_inflight_requests ()
415416 reset_state ()
416417
418+ vim .api .nvim_buf_set_var (0 , " _copilot_uuid" , " " )
417419 with_client (function (client )
418420 if modifier then
419421 -- do not notify_accepted for partial accept.
@@ -423,7 +425,6 @@ function mod.accept(modifier)
423425
424426 api .notify_accepted (client , { uuid = suggestion .uuid }, function () end )
425427 end )
426- copilot .uuid = nil
427428 clear_preview ()
428429
429430 if type (modifier ) == " function" then
@@ -476,7 +477,7 @@ function mod.accept_line()
476477end
477478
478479function mod .dismiss ()
479- reject_current ( )
480+ reject ( 0 )
480481 clear ()
481482 update_preview ()
482483end
@@ -522,8 +523,13 @@ local function on_complete_changed()
522523 clear ()
523524end
524525
526+ --- @param info { buf : integer }
527+ local function on_buf_unload (info )
528+ reject (info .buf )
529+ end
530+
525531local function on_vim_leave_pre ()
526- reject_current ( )
532+ reject ( 0 )
527533end
528534
529535local function create_autocmds ()
@@ -565,6 +571,12 @@ local function create_autocmds()
565571 desc = " [copilot] (suggestion) complete changed" ,
566572 })
567573
574+ vim .api .nvim_create_autocmd (" BufUnload" , {
575+ group = copilot .augroup ,
576+ callback = on_buf_unload ,
577+ desc = " [copilot] (suggestion) buf unload" ,
578+ })
579+
568580 vim .api .nvim_create_autocmd (" VimLeavePre" , {
569581 group = copilot .augroup ,
570582 callback = on_vim_leave_pre ,
0 commit comments