@@ -453,30 +453,6 @@ local function get_suggestions_cycling(callback, ctx)
453
453
end
454
454
end
455
455
456
- function M .has_next ()
457
- local ctx = get_ctx ()
458
-
459
- -- no completions at all
460
- if not ctx .suggestions or # ctx .suggestions == 0 then
461
- return false
462
- end
463
-
464
- return (ctx .choice < # ctx .suggestions or not ctx .cycling )
465
- end
466
-
467
- local function advance (count , ctx )
468
- if ctx ~= get_ctx () then
469
- return
470
- end
471
-
472
- ctx .choice = (ctx .choice + count ) % # ctx .suggestions
473
- if ctx .choice < 1 then
474
- ctx .choice = # ctx .suggestions
475
- end
476
-
477
- update_preview (ctx )
478
- end
479
-
480
456
local function schedule (ctx )
481
457
local function is_authenticated ()
482
458
return auth .is_authenticated (function ()
@@ -505,14 +481,53 @@ local function schedule(ctx)
505
481
end )
506
482
end
507
483
484
+ --- @param context string
485
+ local function request_suggestion (context )
486
+ logger .trace (" suggestion on " .. context )
487
+ c .buf_attach ()
488
+ schedule ()
489
+ end
490
+
491
+ --- @param context string
492
+ local function request_suggestion_when_auto_trigger (context )
493
+ if not should_auto_trigger () then
494
+ return
495
+ end
496
+
497
+ request_suggestion (context )
498
+ end
499
+
500
+ function M .has_next ()
501
+ local ctx = get_ctx ()
502
+
503
+ -- no completions at all
504
+ if not ctx .suggestions or # ctx .suggestions == 0 then
505
+ return false
506
+ end
507
+
508
+ return (ctx .choice < # ctx .suggestions or not ctx .cycling )
509
+ end
510
+
511
+ local function advance (count , ctx )
512
+ if ctx ~= get_ctx () then
513
+ return
514
+ end
515
+
516
+ ctx .choice = (ctx .choice + count ) % # ctx .suggestions
517
+ if ctx .choice < 1 then
518
+ ctx .choice = # ctx .suggestions
519
+ end
520
+
521
+ update_preview (ctx )
522
+ end
523
+
508
524
--- @param ctx copilot_suggestion_context
509
525
--- @param caller_context string
510
526
--- @return boolean
511
527
function M .first_request_scheduled (ctx , caller_context )
512
528
if not ctx .first then
513
529
logger .trace (" suggestion " .. caller_context .. " , no first request" , ctx )
514
- c .buf_attach ()
515
- schedule (ctx )
530
+ request_suggestion (caller_context )
516
531
return true
517
532
end
518
533
@@ -720,22 +735,16 @@ local function on_buf_leave()
720
735
end
721
736
722
737
local function on_insert_enter ()
723
- if should_auto_trigger () then
724
- logger .trace (" suggestion on insert enter" )
725
- c .buf_attach ()
726
- schedule ()
727
- end
738
+ request_suggestion_when_auto_trigger (" insert enter" )
728
739
end
729
740
730
741
local function on_filetype ()
731
- logger .trace (" suggestion on file type" )
732
- on_insert_enter ()
742
+ request_suggestion_when_auto_trigger (" file type" )
733
743
end
734
744
735
745
local function on_buf_enter ()
736
746
if vim .fn .mode ():match (" ^[iR]" ) then
737
- logger .trace (" suggestion on buf enter" )
738
- on_insert_enter ()
747
+ request_suggestion_when_auto_trigger (" buf enter" )
739
748
end
740
749
end
741
750
@@ -747,18 +756,15 @@ local function on_cursor_moved_i()
747
756
748
757
local ctx = get_ctx ()
749
758
if copilot ._copilot_timer or ctx .params or should_auto_trigger () then
750
- logger .trace (" suggestion on cursor moved insert" )
751
- c .buf_attach ()
752
- schedule (ctx )
759
+ request_suggestion (" cursor moved insert" )
753
760
end
754
761
end
755
762
756
763
local function on_text_changed_p ()
757
764
local ctx = get_ctx ()
765
+
758
766
if not copilot .hide_during_completion and (copilot ._copilot_timer or ctx .params or should_auto_trigger ()) then
759
- logger .trace (" suggestion on text changed pum" )
760
- c .buf_attach ()
761
- schedule (ctx )
767
+ request_suggestion (" text changed pum" )
762
768
end
763
769
end
764
770
0 commit comments