@@ -312,120 +312,72 @@ T["ui_preview"]["cursor_aware_suggestion_clearing"] = function()
312
312
ref (child .get_screenshot ())
313
313
end
314
314
315
- T [" ui_preview" ][" suggestion_preserves_on_movement_towards" ] = function ()
316
- set_content (" line1\n line2\n line3\n line4\n line5\n line6\n line7\n line8" )
317
- ref (child .get_screenshot ())
318
-
319
- -- Position cursor at line 8
320
- child .cmd (" normal! gg7j" )
321
-
322
- -- Create a suggestion at line 3
323
- local edit = {
324
- range = {
325
- start = { line = 2 , character = 0 },
326
- [" end" ] = { line = 2 , character = 0 },
327
- },
328
- newText = " suggested text " ,
329
- }
330
-
331
- -- Display suggestion
332
- child .g .test_edit = edit
333
- child .lua_func (function ()
334
- local ns_id = vim .api .nvim_create_namespace (" nes_test" )
335
- local edits = { vim .g .test_edit }
336
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , edits )
337
- end )
338
- ref (child .get_screenshot ())
339
-
340
- -- Test: Moving cursor towards the suggestion (even outside buffer zone) shouldn't clear it
341
- child .cmd (" normal! 4k" ) -- Move to line 4, moving towards the suggestion
342
- child .lua_func (function ()
343
- vim .uv .sleep (500 )
344
- end )
345
-
346
- -- Verify suggestion still exists
347
- local suggestion_exists = child .lua_func (function ()
348
- return vim .b [0 ].nes_state ~= nil
349
- end )
350
- eq (suggestion_exists , true )
351
- ref (child .get_screenshot ())
352
- end
353
-
354
315
T [" ui_preview" ][" suggestion_history_basic_cycle" ] = function ()
355
316
set_content (" line1\n line2\n line3" )
356
-
357
- -- Create first suggestion
317
+ -- Create first suggestion and display it
358
318
local edit1 = {
359
- range = {
360
- start = { line = 1 , character = 0 },
361
- [" end" ] = { line = 1 , character = 0 },
362
- },
319
+ range = { start = { line = 1 , character = 0 }, [" end" ] = { line = 1 , character = 0 } },
363
320
newText = " -- first suggestion" ,
364
321
}
365
-
366
- -- Display first suggestion
367
322
child .g .test_edit = edit1
368
323
child .lua_func (function ()
369
324
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
370
- local edits = { vim .g . test_edit }
371
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , edits )
325
+ local bufnr = vim .api . nvim_get_current_buf ()
326
+ require (" copilot-lsp.nes.ui" )._display_next_suggestion (bufnr , ns_id , { vim . g . test_edit } )
372
327
vim .uv .sleep (300 )
373
328
end )
374
329
375
- -- Create and display second suggestion (should store first in history)
330
+ -- Create and display second suggestion
376
331
local edit2 = {
377
- range = {
378
- start = { line = 2 , character = 0 },
379
- [" end" ] = { line = 2 , character = 0 },
380
- },
332
+ range = { start = { line = 2 , character = 0 }, [" end" ] = { line = 2 , character = 0 } },
381
333
newText = " -- second suggestion" ,
382
334
}
383
-
384
335
child .g .test_edit = edit2
385
336
child .lua_func (function ()
386
337
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
387
- local edits = { vim .g . test_edit }
388
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , edits )
338
+ local bufnr = vim .api . nvim_get_current_buf ()
339
+ require (" copilot-lsp.nes.ui" )._display_next_suggestion (bufnr , ns_id , { vim . g . test_edit } )
389
340
vim .uv .sleep (300 )
390
341
end )
391
342
392
- -- Clear current suggestion (second should now be in history too)
393
343
child .lua_func (function ()
394
344
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
395
- require (" copilot-lsp.nes.ui" ).clear_suggestion (0 , ns_id )
345
+ local bufnr = vim .api .nvim_get_current_buf ()
346
+ require (" copilot-lsp.nes.ui" ).clear_suggestion (bufnr , ns_id )
396
347
vim .uv .sleep (300 )
397
348
end )
398
349
399
- -- First restore should show second suggestion (most recent)
350
+ local has_history = child .lua_func (function ()
351
+ local bufnr = vim .api .nvim_get_current_buf ()
352
+ return require (" copilot-lsp.nes.ui" ).has_history (bufnr )
353
+ end )
354
+ eq (has_history , true )
355
+
356
+ -- Test cycling through suggestions
400
357
local restored1 = child .lua_func (function ()
401
358
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
402
- local result = require (" copilot-lsp.nes.ui" ).restore_suggestion (0 , ns_id )
359
+ local bufnr = vim .api .nvim_get_current_buf ()
360
+ local result = require (" copilot-lsp.nes.ui" ).restore_suggestion (bufnr , ns_id )
403
361
vim .uv .sleep (300 )
404
362
return result
405
363
end )
406
364
eq (restored1 , true )
407
365
408
- -- Verify we can check history content
409
- local history_size = child .lua_func (function ()
410
- return # (vim .b [0 ].copilotlsp_nes_history or {})
411
- end )
412
- eq (history_size , 2 )
413
-
414
- -- Second restore should show first suggestion
415
366
local restored2 = child .lua_func (function ()
416
367
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
417
- local restored = require (" copilot-lsp.nes.ui" ).restore_suggestion (0 , ns_id )
368
+ local bufnr = vim .api .nvim_get_current_buf ()
369
+ local result = require (" copilot-lsp.nes.ui" ).restore_suggestion (bufnr , ns_id )
418
370
vim .uv .sleep (300 )
419
- return restored
371
+ return result
420
372
end )
421
373
eq (restored2 , true )
422
374
423
- -- Third restore should cycle back to second suggestion
424
375
local restored3 = child .lua_func (function ()
425
376
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
426
- local restored = require (" copilot-lsp.nes.ui" ).restore_suggestion (0 , ns_id )
377
+ local bufnr = vim .api .nvim_get_current_buf ()
378
+ local result = require (" copilot-lsp.nes.ui" ).restore_suggestion (bufnr , ns_id )
427
379
vim .uv .sleep (300 )
428
- return restored
380
+ return result
429
381
end )
430
382
eq (restored3 , true )
431
383
end
@@ -452,37 +404,39 @@ T["ui_preview"]["suggestion_history_max_two_items"] = function()
452
404
child .g .test_edit = edit
453
405
child .lua_func (function ()
454
406
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
455
- local edits = { vim .g . test_edit }
456
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , edits )
407
+ local bufnr = vim .api . nvim_get_current_buf ()
408
+ require (" copilot-lsp.nes.ui" )._display_next_suggestion (bufnr , ns_id , { vim . g . test_edit } )
457
409
vim .uv .sleep (300 )
458
410
end )
459
411
end
460
412
461
- -- Clear current suggestion
462
413
child .lua_func (function ()
463
414
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
464
- require (" copilot-lsp.nes.ui" ).clear_suggestion (0 , ns_id )
415
+ local bufnr = vim .api .nvim_get_current_buf ()
416
+ require (" copilot-lsp.nes.ui" ).clear_suggestion (bufnr , ns_id )
465
417
vim .uv .sleep (300 )
466
418
end )
467
419
468
- -- Verify history only keeps 2 most recent
469
- local history_size = child . lua_func ( function ()
470
- return # ( vim . b [ 0 ]. copilotlsp_nes_history or {} )
420
+ local has_history = child . lua_func ( function ()
421
+ local bufnr = vim . api . nvim_get_current_buf ()
422
+ return require ( " copilot-lsp.nes.ui " ). has_history ( bufnr )
471
423
end )
472
- eq (history_size , 2 )
424
+ eq (has_history , true )
473
425
474
- -- Verify we can only cycle between 2 suggestions
426
+ -- Verify we can cycle through suggestions (should only have 2 most recent)
475
427
local restore_results = {}
476
- for _ = 1 , 4 do -- Try 4 restores to test cycling
428
+ for i = 1 , 4 do -- Try 4 restores to test cycling
477
429
local restored = child .lua_func (function ()
478
430
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
479
- return require (" copilot-lsp.nes.ui" ).restore_suggestion (0 , ns_id )
431
+ local bufnr = vim .api .nvim_get_current_buf ()
432
+ local result = require (" copilot-lsp.nes.ui" ).restore_suggestion (bufnr , ns_id )
433
+ vim .uv .sleep (300 )
434
+ return result
480
435
end )
481
436
table.insert (restore_results , restored )
482
- vim .uv .sleep (300 )
483
437
end
484
438
485
- -- All restores should succeed
439
+ -- All restores should succeed (cycling between 2 items)
486
440
for _ , result in ipairs (restore_results ) do
487
441
eq (result , true )
488
442
end
@@ -491,146 +445,72 @@ end
491
445
T [" ui_preview" ][" suggestion_history_invalid_after_text_changes" ] = function ()
492
446
set_content (" line1\n line2\n line3\n line4\n line5" )
493
447
494
- -- Create suggestion on line 4 (0-indexed)
495
448
local edit = {
496
- range = {
497
- start = { line = 4 , character = 0 },
498
- [" end" ] = { line = 4 , character = 0 },
499
- },
449
+ range = { start = { line = 4 , character = 0 }, [" end" ] = { line = 4 , character = 0 } },
500
450
newText = " -- comment on line 5" ,
501
451
}
502
452
503
453
child .g .test_edit = edit
504
454
child .lua_func (function ()
505
455
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
506
- local edits = { vim .g . test_edit }
507
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , edits )
456
+ local bufnr = vim .api . nvim_get_current_buf ()
457
+ require (" copilot-lsp.nes.ui" )._display_next_suggestion (bufnr , ns_id , { vim . g . test_edit } )
508
458
vim .uv .sleep (300 )
509
459
end )
510
460
511
461
-- Clear suggestion to store in history
512
462
child .lua_func (function ()
513
463
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
514
- require (" copilot-lsp.nes.ui" ).clear_suggestion (0 , ns_id )
464
+ local bufnr = vim .api .nvim_get_current_buf ()
465
+ require (" copilot-lsp.nes.ui" ).clear_suggestion (bufnr , ns_id )
515
466
vim .uv .sleep (300 )
516
467
end )
517
468
518
- -- Verify history exists
519
- local history_size_before = child .lua_func (function ()
520
- return # (vim .b [0 ].copilotlsp_nes_history or {})
469
+ -- Verify history exists before deletion
470
+ local has_history_before = child .lua_func (function ()
471
+ local bufnr = vim .api .nvim_get_current_buf ()
472
+ return require (" copilot-lsp.nes.ui" ).has_history (bufnr )
521
473
end )
522
- eq (history_size_before , 1 )
474
+ eq (has_history_before , true )
523
475
524
- -- Delete lines to make history invalid (keep only first 3 lines)
476
+ -- Delete lines to make history invalid
525
477
child .api .nvim_buf_set_lines (0 , 3 , - 1 , false , {})
478
+ child .lua_func (function ()
479
+ vim .uv .sleep (300 )
480
+ end )
526
481
527
482
-- Try to restore (should fail and clear history)
528
483
local restored = child .lua_func (function ()
529
484
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
530
- local result = require (" copilot-lsp.nes.ui" ).restore_suggestion (0 , ns_id )
485
+ local bufnr = vim .api .nvim_get_current_buf ()
486
+ local result = require (" copilot-lsp.nes.ui" ).restore_suggestion (bufnr , ns_id )
531
487
vim .uv .sleep (300 )
532
488
return result
533
489
end )
534
490
eq (restored , false )
535
491
536
- -- Verify history was cleared
537
- local history_size_after = child .lua_func (function ()
538
- return # (vim .b [0 ].copilotlsp_nes_history or {})
539
- end )
540
- eq (history_size_after , 0 )
541
- end
542
-
543
- T [" ui_preview" ][" suggestion_history_restore_index_reset" ] = function ()
544
- set_content (" line1\n line2\n line3" )
545
- -- Create and display two suggestions to build history
546
- local edit1 = {
547
- range = {
548
- start = { line = 1 , character = 0 },
549
- [" end" ] = { line = 1 , character = 0 },
550
- },
551
- newText = " -- first" ,
552
- }
553
- local edit2 = {
554
- range = {
555
- start = { line = 2 , character = 0 },
556
- [" end" ] = { line = 2 , character = 0 },
557
- },
558
- newText = " -- second" ,
559
- }
560
-
561
- -- Display first, then second (first goes to history)
562
- child .g .test_edit = edit1
563
- child .lua_func (function ()
564
- local ns_id = vim .api .nvim_create_namespace (" nes_test" )
565
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , { vim .g .test_edit })
566
- vim .uv .sleep (300 )
567
- end )
568
-
569
- child .g .test_edit = edit2
570
- child .lua_func (function ()
571
- local ns_id = vim .api .nvim_create_namespace (" nes_test" )
572
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , { vim .g .test_edit })
573
- vim .uv .sleep (300 )
574
- end )
575
-
576
- -- Clear to add second to history
577
- child .lua_func (function ()
578
- local ns_id = vim .api .nvim_create_namespace (" nes_test" )
579
- require (" copilot-lsp.nes.ui" ).clear_suggestion (0 , ns_id )
580
- vim .uv .sleep (300 )
581
- end )
582
-
583
- -- Restore once (should show second, index becomes 1)
584
- child .lua_func (function ()
585
- local ns_id = vim .api .nvim_create_namespace (" nes_test" )
586
- require (" copilot-lsp.nes.ui" ).restore_suggestion (0 , ns_id )
587
- vim .uv .sleep (300 )
492
+ local has_history_after = child .lua_func (function ()
493
+ local bufnr = vim .api .nvim_get_current_buf ()
494
+ return require (" copilot-lsp.nes.ui" ).has_history (bufnr )
588
495
end )
589
-
590
- -- Get restore index
591
- local index_before = child .lua_func (function ()
592
- return vim .b [0 ].copilotlsp_nes_restore_index or 0
593
- end )
594
- eq (index_before , 1 )
595
-
596
- -- Display new suggestion (should reset index)
597
- local edit3 = {
598
- range = {
599
- start = { line = 0 , character = 0 },
600
- [" end" ] = { line = 0 , character = 0 },
601
- },
602
- newText = " -- third" ,
603
- }
604
-
605
- child .g .test_edit = edit3
606
- child .lua_func (function ()
607
- local ns_id = vim .api .nvim_create_namespace (" nes_test" )
608
- require (" copilot-lsp.nes.ui" )._display_next_suggestion (0 , ns_id , { vim .g .test_edit })
609
- vim .uv .sleep (300 )
610
- end )
611
-
612
- -- Verify index was reset
613
- local index_after = child .lua_func (function ()
614
- return vim .b [0 ].copilotlsp_nes_restore_index or 0
615
- end )
616
- eq (index_after , 0 )
496
+ eq (has_history_after , false )
617
497
end
618
498
619
499
T [" ui_preview" ][" suggestion_history_no_restore_when_empty" ] = function ()
620
500
set_content (" line1\n line2\n line3" )
621
-
622
501
-- Try to restore when no history exists
623
502
local restored = child .lua_func (function ()
624
503
local ns_id = vim .api .nvim_create_namespace (" nes_test" )
625
- return require (" copilot-lsp.nes.ui" ).restore_suggestion (0 , ns_id )
504
+ local bufnr = vim .api .nvim_get_current_buf ()
505
+ return require (" copilot-lsp.nes.ui" ).restore_suggestion (bufnr , ns_id )
626
506
end )
627
507
eq (restored , false )
628
508
629
- -- Verify no history exists
630
- local history_size = child . lua_func ( function ()
631
- return # ( vim . b [ 0 ]. copilotlsp_nes_history or {} )
509
+ local has_history = child . lua_func ( function ()
510
+ local bufnr = vim . api . nvim_get_current_buf ()
511
+ return require ( " copilot-lsp.nes.ui " ). has_history ( bufnr )
632
512
end )
633
- eq (history_size , 0 )
513
+ eq (has_history , false )
634
514
end
635
515
636
516
return T
0 commit comments