@@ -236,7 +236,7 @@ local function clear_preview()
236
236
end
237
237
238
238
--- @param ctx ? copilot_suggestion_context
239
- --- @return copilot_get_completions_data_completion | nil
239
+ --- @return copilot_get_completions_data_completion ?
240
240
local function get_current_suggestion (ctx )
241
241
logger .trace (" suggestion get current suggestion" , ctx )
242
242
ctx = ctx or get_ctx ()
@@ -454,10 +454,11 @@ local function get_suggestions_cycling(callback, ctx)
454
454
end
455
455
end
456
456
457
- local function schedule (ctx )
457
+ --- @param bufnr ? integer
458
+ local function schedule (bufnr )
458
459
local function is_authenticated ()
459
460
return auth .is_authenticated (function ()
460
- schedule (ctx )
461
+ schedule (bufnr )
461
462
end )
462
463
end
463
464
@@ -467,36 +468,35 @@ local function schedule(ctx)
467
468
return
468
469
end
469
470
470
- logger .trace (" suggestion schedule" , ctx )
471
+ logger .trace (" suggestion schedule" )
471
472
472
473
if copilot ._copilot_timer then
473
- cancel_inflight_requests (ctx )
474
+ cancel_inflight_requests ()
474
475
stop_timer ()
475
476
end
476
477
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 ()
479
480
copilot ._copilot_timer = vim .fn .timer_start (copilot .debounce , function (timer )
480
481
logger .trace (" suggestion schedule timer" , bufnr )
481
482
trigger (bufnr , timer )
482
483
end )
483
484
end
484
485
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 )
489
491
end
490
492
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 )
495
495
if not should_auto_trigger () then
496
496
return
497
497
end
498
498
499
- request_suggestion (context )
499
+ request_suggestion (bufnr )
500
500
end
501
501
502
502
function M .has_next ()
@@ -524,12 +524,11 @@ local function advance(count, ctx)
524
524
end
525
525
526
526
--- @param ctx copilot_suggestion_context
527
- --- @param caller_context string
528
527
--- @return boolean
529
- function M .first_request_scheduled (ctx , caller_context )
528
+ function M .first_request_scheduled (ctx )
530
529
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 ()
533
532
return true
534
533
end
535
534
@@ -544,7 +543,7 @@ function M.next()
544
543
reset_ctx (ctx )
545
544
end
546
545
547
- if M .first_request_scheduled (ctx , " next " ) then
546
+ if M .first_request_scheduled (ctx ) then
548
547
return
549
548
end
550
549
@@ -561,7 +560,7 @@ function M.prev()
561
560
reset_ctx (ctx )
562
561
end
563
562
564
- if M .first_request_scheduled (ctx , " prev " ) then
563
+ if M .first_request_scheduled (ctx ) then
565
564
return
566
565
end
567
566
@@ -575,7 +574,7 @@ function M.accept(modifier)
575
574
local ctx = get_ctx ()
576
575
logger .trace (" suggestion accept" , ctx )
577
576
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
579
578
return
580
579
end
581
580
@@ -736,33 +735,41 @@ local function on_buf_leave()
736
735
end
737
736
end
738
737
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 )
741
742
end
742
743
743
- local function on_buf_enter ()
744
+ local function on_buf_enter (args )
744
745
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 )
746
749
end
747
750
end
748
751
749
- local function on_cursor_moved_i ()
752
+ local function on_cursor_moved_i (args )
750
753
if ignore_next_cursor_moved then
751
754
ignore_next_cursor_moved = false
752
755
return
753
756
end
754
757
755
758
local ctx = get_ctx ()
756
759
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 )
758
763
end
759
764
end
760
765
761
- local function on_text_changed_p ()
766
+ local function on_text_changed_p (args )
762
767
local ctx = get_ctx ()
763
768
764
769
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 )
766
773
end
767
774
end
768
775
@@ -797,25 +804,33 @@ local function create_autocmds()
797
804
798
805
vim .api .nvim_create_autocmd (" InsertEnter" , {
799
806
group = copilot .augroup ,
800
- callback = on_insert_enter ,
807
+ callback = function (args )
808
+ on_insert_enter (args )
809
+ end ,
801
810
desc = " [copilot] (suggestion) insert enter" ,
802
811
})
803
812
804
813
vim .api .nvim_create_autocmd (" BufEnter" , {
805
814
group = copilot .augroup ,
806
- callback = on_buf_enter ,
815
+ callback = function (args )
816
+ on_buf_enter (args )
817
+ end ,
807
818
desc = " [copilot] (suggestion) buf enter" ,
808
819
})
809
820
810
821
vim .api .nvim_create_autocmd (" CursorMovedI" , {
811
822
group = copilot .augroup ,
812
- callback = on_cursor_moved_i ,
823
+ callback = function (args )
824
+ on_cursor_moved_i (args )
825
+ end ,
813
826
desc = " [copilot] (suggestion) cursor moved insert" ,
814
827
})
815
828
816
829
vim .api .nvim_create_autocmd (" TextChangedP" , {
817
830
group = copilot .augroup ,
818
- callback = on_text_changed_p ,
831
+ callback = function (args )
832
+ on_text_changed_p (args )
833
+ end ,
819
834
desc = " [copilot] (suggestion) text changed pum" ,
820
835
})
821
836
@@ -827,7 +842,9 @@ local function create_autocmds()
827
842
828
843
vim .api .nvim_create_autocmd (" BufUnload" , {
829
844
group = copilot .augroup ,
830
- callback = on_buf_unload ,
845
+ callback = function (args )
846
+ on_buf_unload (args )
847
+ end ,
831
848
desc = " [copilot] (suggestion) buf unload" ,
832
849
})
833
850
0 commit comments