@@ -236,7 +236,7 @@ local function clear_preview()
236236end
237237
238238--- @param ctx ? copilot_suggestion_context
239- --- @return copilot_get_completions_data_completion | nil
239+ --- @return copilot_get_completions_data_completion ?
240240local function get_current_suggestion (ctx )
241241 logger .trace (" suggestion get current suggestion" , ctx )
242242 ctx = ctx or get_ctx ()
@@ -454,10 +454,11 @@ local function get_suggestions_cycling(callback, ctx)
454454 end
455455end
456456
457- local function schedule (ctx )
457+ --- @param bufnr ? integer
458+ local function schedule (bufnr )
458459 local function is_authenticated ()
459460 return auth .is_authenticated (function ()
460- schedule (ctx )
461+ schedule (bufnr )
461462 end )
462463 end
463464
@@ -467,36 +468,35 @@ local function schedule(ctx)
467468 return
468469 end
469470
470- logger .trace (" suggestion schedule" , ctx )
471+ logger .trace (" suggestion schedule" )
471472
472473 if copilot ._copilot_timer then
473- cancel_inflight_requests (ctx )
474+ cancel_inflight_requests ()
474475 stop_timer ()
475476 end
476477
477- update_preview (ctx )
478- local bufnr = vim .api .nvim_get_current_buf ()
478+ update_preview ()
479+ bufnr = bufnr or vim .api .nvim_get_current_buf ()
479480 copilot ._copilot_timer = vim .fn .timer_start (copilot .debounce , function (timer )
480481 logger .trace (" suggestion schedule timer" , bufnr )
481482 trigger (bufnr , timer )
482483 end )
483484end
484485
485- --- @param context string
486- local function request_suggestion (context )
487- logger .trace (" suggestion on " .. context )
488- schedule ()
486+ --- @param bufnr ? integer
487+ local function request_suggestion (bufnr )
488+ logger .trace (" suggestion request" )
489+ c .buf_attach (false , bufnr )
490+ schedule (bufnr )
489491end
490492
491- --- @param context string
492- local function request_suggestion_when_auto_trigger (context )
493- c .buf_attach ()
494-
493+ --- @param bufnr ? integer
494+ local function request_suggestion_when_auto_trigger (bufnr )
495495 if not should_auto_trigger () then
496496 return
497497 end
498498
499- request_suggestion (context )
499+ request_suggestion (bufnr )
500500end
501501
502502function M .has_next ()
@@ -524,12 +524,11 @@ local function advance(count, ctx)
524524end
525525
526526--- @param ctx copilot_suggestion_context
527- --- @param caller_context string
528527--- @return boolean
529- function M .first_request_scheduled (ctx , caller_context )
528+ function M .first_request_scheduled (ctx )
530529 if not ctx .first then
531- logger .trace (" suggestion " .. caller_context .. " , no first request" )
532- request_suggestion (caller_context )
530+ logger .trace (" suggestion, no first request" )
531+ request_suggestion ()
533532 return true
534533 end
535534
@@ -544,7 +543,7 @@ function M.next()
544543 reset_ctx (ctx )
545544 end
546545
547- if M .first_request_scheduled (ctx , " next " ) then
546+ if M .first_request_scheduled (ctx ) then
548547 return
549548 end
550549
@@ -561,7 +560,7 @@ function M.prev()
561560 reset_ctx (ctx )
562561 end
563562
564- if M .first_request_scheduled (ctx , " prev " ) then
563+ if M .first_request_scheduled (ctx ) then
565564 return
566565 end
567566
@@ -575,7 +574,7 @@ function M.accept(modifier)
575574 local ctx = get_ctx ()
576575 logger .trace (" suggestion accept" , ctx )
577576
578- if config .suggestion .trigger_on_accept and M .first_request_scheduled (ctx , " suggestion accept " ) then
577+ if config .suggestion .trigger_on_accept and M .first_request_scheduled (ctx ) then
579578 return
580579 end
581580
@@ -736,33 +735,41 @@ local function on_buf_leave()
736735 end
737736end
738737
739- local function on_insert_enter ()
740- request_suggestion_when_auto_trigger (" insert enter" )
738+ local function on_insert_enter (args )
739+ logger .trace (" insert enter" )
740+ local bufnr = (args and args .buf ) or nil
741+ request_suggestion_when_auto_trigger (bufnr )
741742end
742743
743- local function on_buf_enter ()
744+ local function on_buf_enter (args )
744745 if vim .fn .mode ():match (" ^[iR]" ) then
745- request_suggestion_when_auto_trigger (" buf enter" )
746+ logger .trace (" buf enter" )
747+ local bufnr = (args and args .buf ) or nil
748+ request_suggestion_when_auto_trigger (bufnr )
746749 end
747750end
748751
749- local function on_cursor_moved_i ()
752+ local function on_cursor_moved_i (args )
750753 if ignore_next_cursor_moved then
751754 ignore_next_cursor_moved = false
752755 return
753756 end
754757
755758 local ctx = get_ctx ()
756759 if copilot ._copilot_timer or ctx .params or should_auto_trigger () then
757- request_suggestion (" cursor moved insert" )
760+ logger .trace (" cursor moved insert" )
761+ local bufnr = (args and args .buf ) or nil
762+ request_suggestion (bufnr )
758763 end
759764end
760765
761- local function on_text_changed_p ()
766+ local function on_text_changed_p (args )
762767 local ctx = get_ctx ()
763768
764769 if not copilot .hide_during_completion and (copilot ._copilot_timer or ctx .params or should_auto_trigger ()) then
765- request_suggestion (" text changed pum" )
770+ logger .trace (" text changed pum" )
771+ local bufnr = (args and args .buf ) or nil
772+ request_suggestion (bufnr )
766773 end
767774end
768775
@@ -797,25 +804,33 @@ local function create_autocmds()
797804
798805 vim .api .nvim_create_autocmd (" InsertEnter" , {
799806 group = copilot .augroup ,
800- callback = on_insert_enter ,
807+ callback = function (args )
808+ on_insert_enter (args )
809+ end ,
801810 desc = " [copilot] (suggestion) insert enter" ,
802811 })
803812
804813 vim .api .nvim_create_autocmd (" BufEnter" , {
805814 group = copilot .augroup ,
806- callback = on_buf_enter ,
815+ callback = function (args )
816+ on_buf_enter (args )
817+ end ,
807818 desc = " [copilot] (suggestion) buf enter" ,
808819 })
809820
810821 vim .api .nvim_create_autocmd (" CursorMovedI" , {
811822 group = copilot .augroup ,
812- callback = on_cursor_moved_i ,
823+ callback = function (args )
824+ on_cursor_moved_i (args )
825+ end ,
813826 desc = " [copilot] (suggestion) cursor moved insert" ,
814827 })
815828
816829 vim .api .nvim_create_autocmd (" TextChangedP" , {
817830 group = copilot .augroup ,
818- callback = on_text_changed_p ,
831+ callback = function (args )
832+ on_text_changed_p (args )
833+ end ,
819834 desc = " [copilot] (suggestion) text changed pum" ,
820835 })
821836
@@ -827,7 +842,9 @@ local function create_autocmds()
827842
828843 vim .api .nvim_create_autocmd (" BufUnload" , {
829844 group = copilot .augroup ,
830- callback = on_buf_unload ,
845+ callback = function (args )
846+ on_buf_unload (args )
847+ end ,
831848 desc = " [copilot] (suggestion) buf unload" ,
832849 })
833850
0 commit comments