51
51
local function get_ctx (bufnr )
52
52
bufnr = bufnr or vim .api .nvim_get_current_buf ()
53
53
local ctx = copilot .context [bufnr ]
54
+ logger .trace (" suggestion context" , ctx )
54
55
if not ctx then
55
56
ctx = {}
56
57
copilot .context [bufnr ] = ctx
58
+ logger .trace (" suggestion new context" , ctx )
57
59
end
58
60
return ctx
59
61
end
60
62
61
63
--- @param ctx copilot_suggestion_context
62
64
local function reset_ctx (ctx )
65
+ logger .trace (" suggestion reset context" , ctx )
63
66
ctx .first = nil
64
67
ctx .cycling = nil
65
68
ctx .cycling_callbacks = nil
149
152
150
153
local function stop_timer ()
151
154
if copilot ._copilot_timer then
155
+ logger .trace (" suggestion stop timer" )
152
156
vim .fn .timer_stop (copilot ._copilot_timer )
153
157
copilot ._copilot_timer = nil
154
158
end
@@ -172,28 +176,34 @@ end
172
176
173
177
--- @param ctx ? copilot_suggestion_context
174
178
local function cancel_inflight_requests (ctx )
179
+ logger .trace (" suggestion cancel inflight requests" , ctx )
175
180
ctx = ctx or get_ctx ()
176
181
177
182
with_client (function (client )
178
183
if ctx .first then
179
184
client .cancel_request (ctx .first )
180
185
ctx .first = nil
186
+ logger .trace (" suggestion cancel first request" )
181
187
end
182
188
if ctx .cycling then
183
189
client .cancel_request (ctx .cycling )
184
190
ctx .cycling = nil
191
+ logger .trace (" suggestion cancel cycling request" )
185
192
end
186
193
end )
187
194
end
188
195
189
196
local function clear_preview ()
197
+ logger .trace (" suggestion clear preview" )
190
198
vim .api .nvim_buf_del_extmark (0 , copilot .ns_id , copilot .extmark_id )
191
199
end
192
200
193
201
--- @param ctx ? copilot_suggestion_context
194
202
--- @return copilot_get_completions_data_completion | nil
195
203
local function get_current_suggestion (ctx )
204
+ logger .trace (" suggestion get current suggestion" , ctx )
196
205
ctx = ctx or get_ctx ()
206
+ logger .trace (" suggestion current suggestion" , ctx )
197
207
198
208
local ok , choice = pcall (function ()
199
209
if
229
239
--- @param ctx ? copilot_suggestion_context
230
240
local function update_preview (ctx )
231
241
ctx = ctx or get_ctx ()
242
+ logger .trace (" suggestion update preview" , ctx )
232
243
233
244
local suggestion = get_current_suggestion (ctx )
234
245
local displayLines = suggestion and vim .split (suggestion .displayText , " \n " , { plain = true }) or {}
286
297
287
298
--- @param ctx ? copilot_suggestion_context
288
299
local function clear (ctx )
300
+ logger .trace (" suggestion clear" , ctx )
289
301
ctx = ctx or get_ctx ()
290
302
stop_timer ()
291
303
cancel_inflight_requests (ctx )
295
307
296
308
--- @param callback fun ( err : any | nil , data : copilot_get_completions_data ): nil
297
309
local function complete (callback )
310
+ logger .trace (" suggestion complete" )
298
311
stop_timer ()
299
312
300
313
local ctx = get_ctx ()
@@ -314,6 +327,7 @@ local function handle_trigger_request(err, data)
314
327
if err then
315
328
logger .error (err )
316
329
end
330
+ logger .trace (" suggestion handle trigger request" , data )
317
331
local ctx = get_ctx ()
318
332
ctx .suggestions = data and data .completions or {}
319
333
ctx .choice = 1
@@ -322,10 +336,12 @@ local function handle_trigger_request(err, data)
322
336
end
323
337
324
338
local function trigger (bufnr , timer )
339
+ logger .trace (" suggestion trigger" , bufnr )
325
340
local _timer = copilot ._copilot_timer
326
341
copilot ._copilot_timer = nil
327
342
328
343
if bufnr ~= vim .api .nvim_get_current_buf () or (_timer ~= nil and timer ~= _timer ) or vim .fn .mode () ~= " i" then
344
+ logger .trace (" suggestion trigger, not in insert mode" )
329
345
return
330
346
end
331
347
334
350
335
351
--- @param ctx copilot_suggestion_context
336
352
local function get_suggestions_cycling_callback (ctx , err , data )
353
+ logger .trace (" suggestion get suggestions cycling callback" , data )
337
354
local callbacks = ctx .cycling_callbacks or {}
338
355
ctx .cycling_callbacks = nil
339
356
367
384
--- @param callback fun ( ctx : copilot_suggestion_context ): nil
368
385
--- @param ctx copilot_suggestion_context
369
386
local function get_suggestions_cycling (callback , ctx )
387
+ logger .trace (" suggestion get suggestions cycling" , ctx )
388
+
370
389
if ctx .cycling_callbacks then
371
390
table.insert (ctx .cycling_callbacks , callback )
372
391
return
@@ -407,19 +426,23 @@ local function schedule(ctx)
407
426
clear ()
408
427
return
409
428
end
429
+ logger .trace (" suggestion schedule" , ctx )
410
430
411
431
update_preview (ctx )
412
432
local bufnr = vim .api .nvim_get_current_buf ()
413
433
copilot ._copilot_timer = vim .fn .timer_start (copilot .debounce , function (timer )
434
+ logger .trace (" suggestion schedule timer" , bufnr )
414
435
trigger (bufnr , timer )
415
436
end )
416
437
end
417
438
418
439
function mod .next ()
419
440
local ctx = get_ctx ()
441
+ logger .trace (" suggestion next" , ctx )
420
442
421
443
-- no suggestion request yet
422
444
if not ctx .first then
445
+ logger .trace (" suggestion next, no first request" )
423
446
schedule (ctx )
424
447
return
425
448
end
431
454
432
455
function mod .prev ()
433
456
local ctx = get_ctx ()
457
+ logger .trace (" suggestion prev" , ctx )
434
458
435
459
-- no suggestion request yet
436
460
if not ctx .first then
461
+ logger .trace (" suggestion prev, no first request" , ctx )
437
462
schedule (ctx )
438
463
return
439
464
end
446
471
--- @param modifier ? (fun ( suggestion : copilot_get_completions_data_completion ): copilot_get_completions_data_completion )
447
472
function mod .accept (modifier )
448
473
local ctx = get_ctx ()
474
+ logger .trace (" suggestion accept" , ctx )
449
475
450
476
-- no suggestion request yet
451
477
if not ctx .first then
478
+ logger .trace (" suggestion accept, not first request" , ctx )
452
479
schedule (ctx )
453
480
return
454
481
end
@@ -466,7 +493,7 @@ function mod.accept(modifier)
466
493
end
467
494
468
495
with_client (function (client )
469
- local ok , err = pcall (function ()
496
+ local ok , _ = pcall (function ()
470
497
api .notify_accepted (
471
498
client ,
472
499
{ uuid = suggestion .uuid , acceptedLength = util .strutf16len (suggestion .text ) },
568
595
569
596
local function on_insert_enter ()
570
597
if should_auto_trigger () then
598
+ logger .trace (" suggestion on insert enter" )
571
599
schedule ()
572
600
end
573
601
end
@@ -581,13 +609,15 @@ end
581
609
local function on_cursor_moved_i ()
582
610
local ctx = get_ctx ()
583
611
if copilot ._copilot_timer or ctx .params or should_auto_trigger () then
612
+ logger .trace (" suggestion on cursor moved insert" )
584
613
schedule (ctx )
585
614
end
586
615
end
587
616
588
617
local function on_text_changed_p ()
589
618
local ctx = get_ctx ()
590
619
if not copilot .hide_during_completion and (copilot ._copilot_timer or ctx .params or should_auto_trigger ()) then
620
+ logger .trace (" suggestion on text changed pum" )
591
621
schedule (ctx )
592
622
end
593
623
end
0 commit comments