robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local describe , it , before_each , finally =t . describe , t . it , t . before_each , t . finally 3local pcall_err =t . pcall_err 4local n =require ( 'test.functional.testnvim' )() 5local Screen =require ( 'test.functional.ui.screen' ) 6 7local assert_alive =n . assert_alive 8local clear , feed =n . clear , n . feed 9local eval , eq , ok =n . eval , t . eq , t . ok 10local source , expect =n . source , n . expect 11local fn =n . fn 12local command =n . command 13local api =n . api 14local poke_eventloop =n . poke_eventloop 15local exec_lua =n . exec_lua 16 17describe ( 'completion' , function () 18local screen 19 20before_each ( function () 21clear () 22source ( [[ 23set completeopt-=noselect 24" Avoid tags completion (if running test locally). 25set complete-=t 26]] ) 27screen =Screen . new ( 60 , 8 ) 28screen : add_extra_attr_ids { 29[ 100 ] ={ foreground =Screen . colors . Gray0 , background =Screen . colors . Yellow } , 30[ 101 ] ={ background =Screen . colors . Gray0 } , 31[ 102 ] ={ foreground =Screen . colors . SeaGreen } , 32} 33end ) 34 35it ( 'ctrl-x_ctrl-f completes Windows drive letter' , function () 36t . skip ( not t . is_os ( 'win' ), 'N/A for non-Windows' ) 37feed ( 'iblablaC:/W<C-x><C-f>' ) 38screen : expect { 39any =[[C:\Windows\]] , 40} 41end ) 42 43describe ( 'v:completed_item' , function () 44it ( 'is empty dict until completion' , function () 45eq ( {} , eval ( 'v:completed_item' )) 46end ) 47it ( 'is empty dict if the candidate is not inserted' , function () 48feed ( 'ifoo<ESC>o<C-x><C-n>' ) 49screen : expect ( [[ 50foo | 51foo^ | 52{1:~ }|*5 53{5:-- Keyword Local completion (^N^P) The only match} | 54]] ) 55feed ( '<C-e>' ) 56screen : expect ( [[ 57foo | 58^ | 59{1:~ }|*5 60{5:-- INSERT --} | 61]] ) 62feed ( '<ESC>' ) 63eq ( {} , eval ( 'v:completed_item' )) 64end ) 65it ( 'returns expected dict in normal completion' , function () 66feed ( 'ifoo<ESC>o<C-x><C-n>' ) 67eq ( 'foo' , eval ( 'getline(2)' )) 68eq ( { 69word ='foo' , 70abbr ='' , 71menu ='' , 72info ='' , 73kind ='' , 74user_data ='' , 75abbr_hlgroup ='' , 76kind_hlgroup ='' , 77} , eval ( 'v:completed_item' )) 78end ) 79it ( 'is readonly' , function () 80screen : try_resize ( 80 , 8 ) 81feed ( 'ifoo<ESC>o<C-x><C-n><ESC>' ) 82t . matches ( 'E46%: ' , t . pcall_err ( command , 'let v:completed_item.word = "bar"' )) 83t . matches ( 'E46%: ' , t . pcall_err ( command , 'let v:completed_item.abbr = "bar"' )) 84t . matches ( 'E46%: ' , t . pcall_err ( command , 'let v:completed_item.menu = "bar"' )) 85t . matches ( 'E46%: ' , t . pcall_err ( command , 'let v:completed_item.info = "bar"' )) 86t . matches ( 'E46%: ' , t . pcall_err ( command , 'let v:completed_item.kind = "bar"' )) 87t . matches ( 'E46%: ' , t . pcall_err ( command , 'let v:completed_item.user_data = "bar"' )) 88end ) 89it ( 'returns expected dict in omni completion' , function () 90source ( [[ 91function! TestOmni(findstart, base) abort 92return a:findstart ? 0 : [{'word': 'foo', 'abbr': 'bar', 93\ 'menu': 'baz', 'info': 'foobar', 'kind': 'foobaz'}, 94\ {'word': 'word', 'abbr': 'abbr', 'menu': 'menu', 95\ 'info': 'info', 'kind': 'kind'}] 96endfunction 97setlocal omnifunc=TestOmni 98]] ) 99feed ( 'i<C-x><C-o>' ) 100eq ( 'foo' , eval ( 'getline(1)' )) 101screen : expect ( [[ 102foo^ | 103{12:bar foobaz baz }{1: }| 104{4:abbr kind menu }{1: }| 105{1:~ }|*4 106{5:-- Omni completion (^O^N^P) }{6:match 1 of 2} | 107]] ) 108eq ( { 109word ='foo' , 110abbr ='bar' , 111menu ='baz' , 112info ='foobar' , 113kind ='foobaz' , 114abbr_hlgroup ='' , 115kind_hlgroup ='' , 116user_data ='' , 117} , eval ( 'v:completed_item' )) 118end ) 119end ) 120 121describe ( 'completeopt' , function () 122before_each ( function () 123source ( [[ 124function! TestComplete() abort 125call complete(1, ['foo']) 126return '' 127endfunction 128]] ) 129end ) 130 131it ( 'inserts the first candidate if default' , function () 132command ( 'set completeopt+=menuone' ) 133feed ( 'ifoo<ESC>o' ) 134screen : expect ( [[ 135foo | 136^ | 137{1:~ }|*5 138{5:-- INSERT --} | 139]] ) 140feed ( '<C-x>' ) 141-- the ^X prompt, only test this once 142screen : expect ( [[ 143foo | 144^ | 145{1:~ }|*5 146{5:-- ^X mode (^]^D^E^F^I^K^L^N^O^P^Rs^U^V^Y)} | 147]] ) 148feed ( '<C-n>' ) 149screen : expect ( [[ 150foo | 151foo^ | 152{12:foo }{1: }| 153{1:~ }|*4 154{5:-- Keyword Local completion (^N^P) The only match} | 155]] ) 156feed ( 'bar<ESC>' ) 157eq ( 'foobar' , eval ( 'getline(2)' )) 158feed ( 'o<C-r>=TestComplete()<CR>' ) 159screen : expect ( [[ 160foo | 161foobar | 162foo^ | 163{12:foo }{1: }| 164{1:~ }|*3 165{5:-- INSERT --} | 166]] ) 167eq ( 'foo' , eval ( 'getline(3)' )) 168end ) 169it ( 'selects the first candidate if noinsert' , function () 170command ( 'set completeopt+=menuone,noinsert' ) 171feed ( 'ifoo<ESC>o<C-x><C-n>' ) 172screen : expect ( [[ 173foo | 174^ | 175{12:foo }{1: }| 176{1:~ }|*4 177{5:-- Keyword Local completion (^N^P) The only match} | 178]] ) 179feed ( '<C-y>' ) 180screen : expect ( [[ 181foo | 182foo^ | 183{1:~ }|*5 184{5:-- INSERT --} | 185]] ) 186feed ( '<ESC>' ) 187eq ( 'foo' , eval ( 'getline(2)' )) 188feed ( 'o<C-r>=TestComplete()<CR>' ) 189screen : expect ( [[ 190foo |*2 191^ | 192{12:foo }{1: }| 193{1:~ }|*3 194{5:-- INSERT --} | 195]] ) 196feed ( '<C-y><ESC>' ) 197eq ( 'foo' , eval ( 'getline(3)' )) 198end ) 199it ( 'does not insert the first candidate if noselect' , function () 200command ( 'set completeopt+=menuone,noselect' ) 201feed ( 'ifoo<ESC>o<C-x><C-n>' ) 202screen : expect ( [[ 203foo | 204^ | 205{4:foo }{1: }| 206{1:~ }|*4 207{5:-- Keyword Local completion (^N^P) }{19:Back at original} | 208]] ) 209feed ( 'b' ) 210screen : expect ( [[ 211foo | 212b^ | 213{1:~ }|*5 214{5:-- Keyword Local completion (^N^P) }{19:Back at original} | 215]] ) 216feed ( 'ar<ESC>' ) 217eq ( 'bar' , eval ( 'getline(2)' )) 218feed ( 'o<C-r>=TestComplete()<CR>' ) 219screen : expect ( [[ 220foo | 221bar | 222^ | 223{4:foo }{1: }| 224{1:~ }|*3 225{5:-- INSERT --} | 226]] ) 227feed ( 'bar<ESC>' ) 228eq ( 'bar' , eval ( 'getline(3)' )) 229end ) 230it ( 'does not select/insert the first candidate if noselect and noinsert' , function () 231command ( 'set completeopt+=menuone,noselect,noinsert' ) 232feed ( 'ifoo<ESC>o<C-x><C-n>' ) 233screen : expect ( [[ 234foo | 235^ | 236{4:foo }{1: }| 237{1:~ }|*4 238{5:-- Keyword Local completion (^N^P) }{19:Back at original} | 239]] ) 240feed ( '<ESC>' ) 241screen : expect ( [[ 242foo | 243^ | 244{1:~ }|*5 245| 246]] ) 247eq ( '' , eval ( 'getline(2)' )) 248feed ( 'o<C-r>=TestComplete()<CR>' ) 249screen : expect ( [[ 250foo | 251| 252^ | 253{4:foo }{1: }| 254{1:~ }|*3 255{5:-- INSERT --} | 256]] ) 257feed ( '<ESC>' ) 258screen : expect ( [[ 259foo | 260| 261^ | 262{1:~ }|*4 263| 264]] ) 265eq ( '' , eval ( 'getline(3)' )) 266end ) 267it ( 'does not change modified state if noinsert' , function () 268command ( 'set completeopt+=menuone,noinsert' ) 269command ( 'setlocal nomodified' ) 270feed ( 'i<C-r>=TestComplete()<CR><ESC>' ) 271eq ( 0 , eval ( '&l:modified' )) 272end ) 273it ( 'does not change modified state if noselect' , function () 274command ( 'set completeopt+=menuone,noselect' ) 275command ( 'setlocal nomodified' ) 276feed ( 'i<C-r>=TestComplete()<CR><ESC>' ) 277eq ( 0 , eval ( '&l:modified' )) 278end ) 279 280describe ( '"preselect"' , function () 281it ( 'respects "preselect" of complete items' , function () 282source ( [[ 283function! TestComplete() abort 284call complete(1, g:comp_items) 285return '' 286endfunction 287]] ) 288local function complete ( items ) 289api . nvim_set_var ( 'comp_items' , items ) 290feed ( '<Esc>S<C-r>=TestComplete()<CR>' ) 291end 292local items ={ 293{ word ='aaa' } , 294{ word ='bbb' } , 295{ word ='ccc' , preselect =true } , 296{ word ='ddd' } , 297} 298command ( 'set completeopt=menuone,preselect' ) 299complete ( items ) 300screen : expect ( [[ 301ccc^ | 302{4:aaa }{1: }| 303{4:bbb }{1: }| 304{12:ccc }{1: }| 305{4:ddd }{1: }| 306{1:~ }|*2 307{5:-- INSERT --} | 308]] ) 309 310-- scrolls pum when preselected item is far down 311command ( 'set pumheight=5' ) 312local many ={} ---@type table[] 313for i =0 , 19 do 314many [ i + 1 ] ={ word ='item' .. i } 315end 316many [ 16 ]. preselect =true ---@type boolean 317complete ( many ) 318screen : expect ( [[ 319item15^ | 320{4:item13 }{12: }{1: }| 321{4:item14 }{12: }{1: }| 322{12:item15 }{1: }| 323{4:item16 }{101: }{1: }| 324{4:item17 }{12: }{1: }| 325{1:~ }| 326{5:-- INSERT --} | 327]] ) 328 329-- preselected item is selected but not inserted with "noselect" 330command ( 'set completeopt=menuone,noselect,preselect pumheight=0' ) 331complete ( items ) 332screen : expect ( [[ 333^ | 334{4:aaa }{1: }| 335{4:bbb }{1: }| 336{12:ccc }{1: }| 337{4:ddd }{1: }| 338{1:~ }|*2 339{5:-- INSERT --} | 340]] ) 341 342-- selects the first preselected item that still matches 343complete ( { 344{ word ='aaa' , preselect =true } , 345{ word ='abb' } , 346{ word ='abc' , preselect =true } , 347} ) 348feed ( 'ab<C-Y>' ) 349eq ( 'abc' , api . nvim_get_current_line ()) 350 351-- without "preselect" in 'completeopt' the attribute is ignored 352command ( 'set completeopt-=preselect' ) 353complete ( items ) 354feed ( '<C-N><C-Y>' ) 355eq ( 'aaa' , api . nvim_get_current_line ()) 356end ) 357 358it ( 'does not insert text if noinsert' , function () 359source ( [[ 360function! TestComplete() abort 361call complete(1, [{'word': 'aaa', 'preselect': v:true}, {'word': 'bbb'}]) 362return '' 363endfunction 364]] ) 365command ( 'set completeopt=menuone,noinsert,preselect' ) 366feed ( 'i<C-r>=TestComplete()<CR>' ) 367eq ( '' , api . nvim_get_current_line ()) 368end ) 369end ) 370end ) 371 372describe ( 'completeopt+=noinsert does not add blank undo items' , function () 373before_each ( function () 374source ( [[ 375function! TestComplete() abort 376call complete(1, ['foo', 'bar']) 377return '' 378endfunction 379]] ) 380command ( 'set completeopt+=noselect,noinsert' ) 381command ( 'inoremap <right> <c-r>=TestComplete()<cr>' ) 382end ) 383 384local tests ={ 385[ '<up>, <down>, <cr>' ] ={ '<down><cr>' , '<up><cr>' } , 386[ '<c-n>, <c-p>, <c-y>' ] ={ '<c-n><c-y>' , '<c-p><c-y>' } , 387} 388 389for name , seq in pairs ( tests ) do 390it ( 'using ' .. name , function () 391feed ( 'iaaa<esc>' ) 392feed ( 'A<right>' .. seq [ 1 ] .. '<esc>' ) 393feed ( 'A<right><esc>A<right><esc>' ) 394feed ( 'A<cr>bbb<esc>' ) 395feed ( 'A<right>' .. seq [ 2 ] .. '<esc>' ) 396feed ( 'A<right><esc>A<right><esc>' ) 397feed ( 'A<cr>ccc<esc>' ) 398feed ( 'A<right>' .. seq [ 1 ] .. '<esc>' ) 399feed ( 'A<right><esc>A<right><esc>' ) 400 401local expected ={ 402{ 'foo' , 'bar' , 'foo' } , 403{ 'foo' , 'bar' , 'ccc' } , 404{ 'foo' , 'bar' } , 405{ 'foo' , 'bbb' } , 406{ 'foo' } , 407{ 'aaa' } , 408{ '' } , 409} 410 411for i =1 , # expected do 412if i > 1 then 413feed ( 'u' ) 414end 415eq ( expected [ i ], eval ( 'getline(1, "$")' )) 416end 417 418for i =# expected , 1 , - 1 do 419if i < # expected then 420feed ( '<c-r>' ) 421end 422eq ( expected [ i ], eval ( 'getline(1, "$")' )) 423end 424end ) 425end 426end ) 427 428describe ( 'with refresh:always and noselect' , function () 429before_each ( function () 430source ( [[ 431function! TestCompletion(findstart, base) abort 432if a:findstart 433let line = getline('.') 434let start = col('.') - 1 435while start > 0 && line[start - 1] =~ '\a' 436let start -= 1 437endwhile 438return start 439else 440let ret = [] 441for m in split("January February March April May June July August September October November December") 442if m =~ a:base " match by regex 443call add(ret, m) 444endif 445endfor 446return {'words':ret, 'refresh':'always'} 447endif 448endfunction 449 450set completeopt=menuone,noselect 451set completefunc=TestCompletion 452]] ) 453end ) 454 455it ( 'completes on each input char' , function () 456feed ( 'i<C-x><C-u>' ) 457screen : expect ( [[ 458^ | 459{4:January }{101: }{1: }| 460{4:February }{101: }{1: }| 461{4:March }{101: }{1: }| 462{4:April }{12: }{1: }| 463{4:May }{12: }{1: }| 464{4:June }{12: }{1: }| 465{5:-- User defined completion (^U^N^P) }{19:Back at original} | 466]] ) 467feed ( 'u' ) 468screen : expect ( [[ 469u^ | 470{4:January }{1: }| 471{4:February }{1: }| 472{4:June }{1: }| 473{4:July }{1: }| 474{4:August }{1: }| 475{1:~ }| 476{5:-- User defined completion (^U^N^P) }{19:Back at original} | 477]] ) 478feed ( 'g' ) 479screen : expect ( [[ 480ug^ | 481{4:August }{1: }| 482{1:~ }|*5 483{5:-- User defined completion (^U^N^P) }{19:Back at original} | 484]] ) 485feed ( '<Down>' ) 486screen : expect ( [[ 487ug^ | 488{12:August }{1: }| 489{1:~ }|*5 490{5:-- User defined completion (^U^N^P) The only match} | 491]] ) 492feed ( '<C-y>' ) 493screen : expect ( [[ 494August^ | 495{1:~ }|*6 496{5:-- INSERT --} | 497]] ) 498expect ( 'August' ) 499end ) 500 501it ( 'repeats correctly after backspace #2674' , function () 502feed ( 'o<C-x><C-u>Ja' ) 503screen : expect ( [[ 504| 505Ja^ | 506{4:January }{1: }| 507{1:~ }|*4 508{5:-- User defined completion (^U^N^P) }{19:Back at original} | 509]] ) 510feed ( '<BS>' ) 511screen : expect ( [[ 512| 513J^ | 514{4:January }{1: }| 515{4:June }{1: }| 516{4:July }{1: }| 517{1:~ }|*2 518{5:-- User defined completion (^U^N^P) }{19:Back at original} | 519]] ) 520feed ( '<C-n>' ) 521screen : expect ( [[ 522| 523January^ | 524{12:January }{1: }| 525{4:June }{1: }| 526{4:July }{1: }| 527{1:~ }|*2 528{5:-- User defined completion (^U^N^P) }{6:match 1 of 3} | 529]] ) 530feed ( '<C-n>' ) 531screen : expect ( [[ 532| 533June^ | 534{4:January }{1: }| 535{12:June }{1: }| 536{4:July }{1: }| 537{1:~ }|*2 538{5:-- User defined completion (^U^N^P) }{6:match 2 of 3} | 539]] ) 540feed ( '<Esc>' ) 541screen : expect ( [[ 542| 543Jun^e | 544{1:~ }|*5 545| 546]] ) 547feed ( '.' ) 548screen : expect ( [[ 549| 550June | 551Jun^e | 552{1:~ }|*4 553| 554]] ) 555expect ( [[ 556 557June 558June]] ) 559end ) 560 561it ( 'Enter inserts newline at original text after adding leader' , function () 562feed ( 'iJ<C-x><C-u>' ) 563poke_eventloop () 564feed ( 'u' ) 565poke_eventloop () 566feed ( '<CR>' ) 567expect ( [[ 568Ju 569]] ) 570feed ( 'J<C-x><C-u>' ) 571poke_eventloop () 572feed ( '<CR>' ) 573expect ( [[ 574Ju 575J 576]] ) 577end ) 578end ) 579 580describe ( 'with noselect but not refresh:always' , function () 581before_each ( function () 582source ( [[ 583function! TestCompletion(findstart, base) abort 584if a:findstart 585let line = getline('.') 586let start = col('.') - 1 587while start > 0 && line[start - 1] =~ '\a' 588let start -= 1 589endwhile 590return start 591else 592let ret = [] 593for m in split("January February March April May June July August September October November December") 594if m =~ a:base " match by regex 595call add(ret, m) 596endif 597endfor 598return {'words':ret} 599endif 600endfunction 601 602set completeopt=menuone,noselect 603set completefunc=TestCompletion 604]] ) 605end ) 606 607it ( 'Enter inserts newline at original text after adding leader' , function () 608feed ( 'iJ<C-x><C-u>' ) 609poke_eventloop () 610feed ( 'u' ) 611poke_eventloop () 612feed ( '<CR>' ) 613expect ( [[ 614Ju 615]] ) 616feed ( '<Esc>' ) 617poke_eventloop () 618-- The behavior should be the same when completion has been interrupted, 619-- which can happen interactively if the completion function is slow. 620feed ( 'ggVGSJ<C-x><C-u>u<CR>' ) 621expect ( [[ 622Ju 623]] ) 624end ) 625end ) 626 627describe ( 'with a lot of items' , function () 628before_each ( function () 629source ( [[ 630function! TestComplete() abort 631call complete(1, map(range(0,100), "string(v:val)")) 632return '' 633endfunction 634]] ) 635command ( 'set completeopt=menuone,noselect' ) 636end ) 637 638it ( 'works' , function () 639feed ( 'i<C-r>=TestComplete()<CR>' ) 640screen : expect ( [[ 641^ | 642{4:0 }{101: }{1: }| 643{4:1 }{12: }{1: }| 644{4:2 }{12: }{1: }| 645{4:3 }{12: }{1: }| 646{4:4 }{12: }{1: }| 647{4:5 }{12: }{1: }| 648{5:-- INSERT --} | 649]] ) 650feed ( '7' ) 651screen : expect ( [[ 6527^ | 653{4:7 }{101: }{1: }| 654{4:70 }{101: }{1: }| 655{4:71 }{101: }{1: }| 656{4:72 }{12: }{1: }| 657{4:73 }{12: }{1: }| 658{4:74 }{12: }{1: }| 659{5:-- INSERT --} | 660]] ) 661feed ( '<c-n>' ) 662screen : expect ( [[ 6637^ | 664{12:7 }{101: }{1: }| 665{4:70 }{101: }{1: }| 666{4:71 }{101: }{1: }| 667{4:72 }{12: }{1: }| 668{4:73 }{12: }{1: }| 669{4:74 }{12: }{1: }| 670{5:-- INSERT --} | 671]] ) 672feed ( '<c-n>' ) 673screen : expect ( [[ 67470^ | 675{4:7 }{101: }{1: }| 676{12:70 }{101: }{1: }| 677{4:71 }{101: }{1: }| 678{4:72 }{12: }{1: }| 679{4:73 }{12: }{1: }| 680{4:74 }{12: }{1: }| 681{5:-- INSERT --} | 682]] ) 683end ) 684 685it ( 'can be navigated with <PageDown>, <PageUp>' , function () 686feed ( 'i<C-r>=TestComplete()<CR>' ) 687screen : expect ( [[ 688^ | 689{4:0 }{101: }{1: }| 690{4:1 }{12: }{1: }| 691{4:2 }{12: }{1: }| 692{4:3 }{12: }{1: }| 693{4:4 }{12: }{1: }| 694{4:5 }{12: }{1: }| 695{5:-- INSERT --} | 696]] ) 697feed ( '<PageDown>' ) 698screen : expect ( [[ 699^ | 700{4:0 }{101: }{1: }| 701{4:1 }{12: }{1: }| 702{4:2 }{12: }{1: }| 703{12:3 }{1: }| 704{4:4 }{12: }{1: }| 705{4:5 }{12: }{1: }| 706{5:-- INSERT --} | 707]] ) 708feed ( '<PageDown>' ) 709screen : expect ( [[ 710^ | 711{4:5 }{101: }{1: }| 712{4:6 }{12: }{1: }| 713{12:7 }{1: }| 714{4:8 }{12: }{1: }| 715{4:9 }{12: }{1: }| 716{4:10 }{12: }{1: }| 717{5:-- INSERT --} | 718]] ) 719feed ( '<Down>' ) 720screen : expect ( [[ 721^ | 722{4:5 }{101: }{1: }| 723{4:6 }{12: }{1: }| 724{4:7 }{12: }{1: }| 725{12:8 }{1: }| 726{4:9 }{12: }{1: }| 727{4:10 }{12: }{1: }| 728{5:-- INSERT --} | 729]] ) 730feed ( '<PageUp>' ) 731screen : expect ( [[ 732^ | 733{4:2 }{101: }{1: }| 734{4:3 }{12: }{1: }| 735{12:4 }{1: }| 736{4:5 }{12: }{1: }| 737{4:6 }{12: }{1: }| 738{4:7 }{12: }{1: }| 739{5:-- INSERT --} | 740]] ) 741feed ( '<PageUp>' ) -- stop on first item 742screen : expect ( [[ 743^ | 744{12:0 }{101: }{1: }| 745{4:1 }{12: }{1: }| 746{4:2 }{12: }{1: }| 747{4:3 }{12: }{1: }| 748{4:4 }{12: }{1: }| 749{4:5 }{12: }{1: }| 750{5:-- INSERT --} | 751]] ) 752feed ( '<PageUp>' ) -- when on first item, unselect 753screen : expect ( [[ 754^ | 755{4:0 }{101: }{1: }| 756{4:1 }{12: }{1: }| 757{4:2 }{12: }{1: }| 758{4:3 }{12: }{1: }| 759{4:4 }{12: }{1: }| 760{4:5 }{12: }{1: }| 761{5:-- INSERT --} | 762]] ) 763feed ( '<PageUp>' ) -- when unselected, select last item 764screen : expect ( [[ 765^ | 766{4:95 }{12: }{1: }| 767{4:96 }{12: }{1: }| 768{4:97 }{12: }{1: }| 769{4:98 }{12: }{1: }| 770{4:99 }{12: }{1: }| 771{12:100 }{101: }{1: }| 772{5:-- INSERT --} | 773]] ) 774feed ( '<PageUp>' ) 775screen : expect ( [[ 776^ | 777{4:94 }{12: }{1: }| 778{4:95 }{12: }{1: }| 779{12:96 }{1: }| 780{4:97 }{12: }{1: }| 781{4:98 }{12: }{1: }| 782{4:99 }{101: }{1: }| 783{5:-- INSERT --} | 784]] ) 785feed ( '<cr>' ) 786screen : expect ( [[ 78796^ | 788{1:~ }|*6 789{5:-- INSERT --} | 790]] ) 791end ) 792end ) 793 794it ( 'does not indent until an item is selected #8345' , function () 795-- Indents on "ind", unindents on "unind". 796source ( [[ 797function! TestIndent() 798let line = getline(v:lnum) 799if (line =~ '^\s*ind') 800return indent(v:lnum-1) + shiftwidth() 801elseif (line =~ '^\s*unind') 802return indent(v:lnum-1) - shiftwidth() 803else 804return indent(v:lnum-1) 805endif 806endfunction 807set indentexpr=TestIndent() 808set indentkeys=o,O,!^F,=ind,=unind 809set completeopt+=menuone 810]] ) 811 812-- Give some words to complete. 813feed ( 'iinc uninc indent unindent<CR>' ) 814 815-- Does not indent when "ind" is typed. 816feed ( 'in<C-X><C-N>' ) 817-- Completion list is generated incorrectly if we send everything at once 818-- via nvim_input(). So poke_eventloop() before sending <BS>. #8480 819poke_eventloop () 820feed ( '<BS>d' ) 821 822screen : expect ( [[ 823inc uninc indent unindent | 824ind^ | 825{12:indent }{1: }| 826{1:~ }|*4 827{5:-- Keyword Local completion (^N^P) }{6:match 1 of 2} | 828]] ) 829 830-- Indents when the item is selected 831feed ( '<C-Y>' ) 832screen : expect ( [[ 833inc uninc indent unindent | 834indent^ | 835{1:~ }|*5 836{5:-- INSERT --} | 837]] ) 838-- Indents when completion is exited using ESC. 839feed ( '<CR>in<C-N><BS>d<Esc>' ) 840screen : expect ( [[ 841inc uninc indent unindent | 842indent | 843in^d | 844{1:~ }|*4 845| 846]] ) 847-- Works for unindenting too. 848feed ( 'ounin<C-X><C-N>' ) 849poke_eventloop () 850feed ( '<BS>d' ) 851screen : expect ( [[ 852inc uninc indent unindent | 853indent | 854ind | 855unind^ | 856{1:~ }{12: unindent }{1: }| 857{1:~ }|*2 858{5:-- Keyword Local completion (^N^P) }{6:match 1 of 2} | 859]] ) 860-- Works when going back and forth. 861feed ( '<BS>c' ) 862screen : expect ( [[ 863inc uninc indent unindent | 864indent | 865ind | 866uninc^ | 867{1:~ }{12: uninc }{1: }| 868{1:~ }|*2 869{5:-- Keyword Local completion (^N^P) }{6:match 1 of 2} | 870]] ) 871feed ( '<BS>d' ) 872screen : expect ( [[ 873inc uninc indent unindent | 874indent | 875ind | 876unind^ | 877{1:~ }{12: unindent }{1: }| 878{1:~ }|*2 879{5:-- Keyword Local completion (^N^P) }{6:match 1 of 2} | 880]] ) 881feed ( '<C-N><C-N><C-Y><Esc>' ) 882screen : expect ( [[ 883inc uninc indent unindent | 884indent | 885ind | 886uninden^t | 887{1:~ }|*3 888| 889]] ) 890end ) 891 892it ( 'disables folding during completion' , function () 893command ( 'set foldmethod=indent' ) 894feed ( 'i<Tab>foo<CR><Tab>bar<Esc>gg' ) 895screen : expect ( [[ 896^foo | 897bar | 898{1:~ }|*5 899| 900]] ) 901feed ( 'A<C-x><C-l>' ) 902screen : expect ( [[ 903foo^ | 904bar | 905{1:~ }|*5 906{5:-- Whole line completion (^L^N^P) }{9:Pattern not found} | 907]] ) 908eq ( - 1 , eval ( 'foldclosed(1)' )) 909end ) 910 911it ( 'popupmenu is not interrupted by events' , function () 912command ( 'set complete=.' ) 913 914feed ( 'ifoobar fooegg<cr>f<c-p>' ) 915screen : expect ( [[ 916foobar fooegg | 917fooegg^ | 918{4:foobar }{1: }| 919{12:fooegg }{1: }| 920{1:~ }|*3 921{5:-- Keyword completion (^N^P) }{6:match 1 of 2} | 922]] ) 923 924assert_alive () 925-- popupmenu still visible 926screen : expect { 927grid =[[ 928foobar fooegg | 929fooegg^ | 930{4:foobar }{1: }| 931{12:fooegg }{1: }| 932{1:~ }|*3 933{5:-- Keyword completion (^N^P) }{6:match 1 of 2} | 934]] , 935unchanged =true , 936} 937 938feed ( '<c-p>' ) 939-- Didn't restart completion: old matches still used 940screen : expect ( [[ 941foobar fooegg | 942foobar^ | 943{12:foobar }{1: }| 944{4:fooegg }{1: }| 945{1:~ }|*3 946{5:-- Keyword completion (^N^P) }{6:match 2 of 2} | 947]] ) 948end ) 949 950describe ( 'lua completion' , function () 951it ( 'expands when there is only one match' , function () 952feed ( ':lua CURRENT_TESTING_VAR = 1<CR>' ) 953feed ( ':lua CURRENT_TESTING_<TAB>' ) 954screen : expect { 955grid =[[ 956| 957{1:~ }|*6 958:lua CURRENT_TESTING_VAR^ | 959]] , 960} 961end ) 962 963it ( 'expands when there is only one match' , function () 964feed ( ':lua CURRENT_TESTING_FOO = 1<CR>' ) 965feed ( ':lua CURRENT_TESTING_BAR = 1<CR>' ) 966feed ( ':lua CURRENT_TESTING_<TAB>' ) 967screen : expect { 968grid =[[ 969| 970{1:~ }|*5 971{100:CURRENT_TESTING_BAR}{3: CURRENT_TESTING_FOO }| 972:lua CURRENT_TESTING_BAR^ | 973]] , 974unchanged =true , 975} 976end ) 977 978it ( 'prefix is not included in completion for cmdline mode' , function () 979feed ( ':lua math.a<Tab>' ) 980screen : expect ( [[ 981| 982{1:~ }|*5 983{100:abs}{3: acos asin atan atan2 }| 984:lua math.abs^ | 985]] ) 986feed ( '<Tab>' ) 987screen : expect ( [[ 988| 989{1:~ }|*5 990{3:abs }{100:acos}{3: asin atan atan2 }| 991:lua math.acos^ | 992]] ) 993end ) 994 995it ( 'prefix is not included in completion for i_CTRL-X_CTRL-V #19623' , function () 996feed ( 'ilua math.a<C-X><C-V>' ) 997screen : expect ( [[ 998lua math.abs^ | 999{1:~ }{12: abs }{1: }| 1000{1:~ }{4: acos }{1: }| 1001{1:~ }{4: asin }{1: }| 1002{1:~ }{4: atan }{1: }| 1003{1:~ }{4: atan2 }{1: }| 1004{1:~ }| 1005{5:-- Command-line completion (^V^N^P) }{6:match 1 of 5} | 1006]] ) 1007feed ( '<C-V>' ) 1008screen : expect ( [[ 1009lua math.acos^ | 1010{1:~ }{4: abs }{1: }| 1011{1:~ }{12: acos }{1: }| 1012{1:~ }{4: asin }{1: }| 1013{1:~ }{4: atan }{1: }| 1014{1:~ }{4: atan2 }{1: }| 1015{1:~ }| 1016{5:-- Command-line completion (^V^N^P) }{6:match 2 of 5} | 1017]] ) 1018end ) 1019 1020it ( 'works when cursor is in the middle of cmdline #29586' , function () 1021feed ( ':lua math.a(); 1<Left><Left><Left><Left><Left><Tab>' ) 1022screen : expect ( [[ 1023| 1024{1:~ }|*5 1025{100:abs}{3: acos asin atan atan2 }| 1026:lua math.abs^(); 1 | 1027]] ) 1028end ) 1029 1030it ( 'provides completion from `getcompletion()`' , function () 1031eq ( { 'vim' } , fn . getcompletion ( 'vi' , 'lua' )) 1032eq ( { 'api' } , fn . getcompletion ( 'vim.ap' , 'lua' )) 1033eq ( { 'tbl_filter' } , fn . getcompletion ( 'vim.tbl_fil' , 'lua' )) 1034eq ( { 'vim' } , fn . getcompletion ( 'print(vi' , 'lua' )) 1035eq ( { 'abs' , 'acos' , 'asin' , 'atan' , 'atan2' } , fn . getcompletion ( 'math.a' , 'lua' )) 1036eq ( { 'abs' , 'acos' , 'asin' , 'atan' , 'atan2' } , fn . getcompletion ( 'lua math.a' , 'cmdline' )) 1037-- fuzzy completion is not supported, so the result should be the same 1038command ( 'set wildoptions+=fuzzy' ) 1039eq ( { 'vim' } , fn . getcompletion ( 'vi' , 'lua' )) 1040end ) 1041 1042it ( 'completes _defer_require() modules' , function () 1043-- vim.lsp.c<tab> -> vim.lsp.completion 1044ok ( vim . tbl_contains ( fn . getcompletion ( 'lua vim.lsp.c' , 'cmdline' ), 'completion' )) 1045-- vim.lsp.completion.g<tab> -> vim.lsp.completion.get 1046ok ( vim . tbl_contains ( fn . getcompletion ( 'lua vim.lsp.completion.g' , 'cmdline' ), 'get' )) 1047end ) 1048end ) 1049 1050it ( 'cmdline completion supports various string options' , function () 1051eq ( 'auto' , fn . getcompletion ( 'set foldcolumn=' , 'cmdline' )[ 2 ]) 1052eq ( { 'nosplit' , 'split' } , fn . getcompletion ( 'set inccommand=' , 'cmdline' )) 1053eq ( { 'ver:3,hor:6' , 'hor:' , 'ver:' } , fn . getcompletion ( 'set mousescroll=' , 'cmdline' )) 1054eq ( 'BS' , fn . getcompletion ( 'set termpastefilter=' , 'cmdline' )[ 2 ]) 1055eq ( 'SpecialKey' , fn . getcompletion ( 'set winhighlight=' , 'cmdline' )[ 1 ]) 1056eq ( 'SpecialKey' , fn . getcompletion ( 'set winhighlight=NonText:' , 'cmdline' )[ 1 ]) 1057end ) 1058 1059it ( 'cmdline completion for -complete does not contain spaces' , function () 1060for _ , str in ipairs ( fn . getcompletion ( 'command -complete=' , 'cmdline' )) do 1061ok ( not str : find ( ' ' ), 'string without spaces' , str ) 1062end 1063end ) 1064 1065it ( 'cmdline completion for :restart' , function () 1066eq ( 'qall' , fn . getcompletion ( 'restart +qa' , 'cmdline' )[ 1 ]) 1067eq ( 'edit' , fn . getcompletion ( 'restart +qall ed' , 'cmdline' )[ 1 ]) 1068eq ( 'edit' , fn . getcompletion ( 'restart ed' , 'cmdline' )[ 1 ]) 1069end ) 1070 1071it ( 'cmdline completion for :*cd family shows dirs only' , function () 1072local dir ='Xtest-cd-compl/' 1073local subdir1 =dir .. 'subdir1/' 1074local subdir2 =dir .. 'subdir2/' 1075finally ( function () 1076n . rmdir ( dir ) 1077end ) 1078fn . mkdir ( subdir1 , 'p' ) 1079fn . mkdir ( subdir2 , 'p' ) 1080fn . writefile ( { '' } , dir .. 'file' ) 1081for _ , cmd in ipairs ( { 'cd' , 'lcd' , 'tcd' , 'bcd' } ) do 1082eq ( { subdir1 , subdir2 } , fn . getcompletion ( cmd .. ' ' .. dir , 'cmdline' )) 1083end 1084end ) 1085 1086describe ( 'from the commandline window' , function () 1087it ( 'is cleared after CTRL-C' , function () 1088feed ( 'q:' ) 1089feed ( 'ifoo faa fee f' ) 1090screen : expect ( [[ 1091| 1092{2:[No Name] }| 1093{1::}foo faa fee f^ | 1094{1:~ }|*3 1095{3:[Command Line] }| 1096{5:-- INSERT --} | 1097]] ) 1098feed ( '<c-x><c-n>' ) 1099screen : expect ( [[ 1100| 1101{2:[No Name] }| 1102{1::}foo faa fee foo^ | 1103{1:~ }{12: foo }{1: }| 1104{1:~ }{4: faa }{1: }| 1105{1:~ }{4: fee }{1: }| 1106{3:[Command Line] }| 1107{5:-- Keyword Local completion (^N^P) }{6:match 1 of 3} | 1108]] ) 1109feed ( '<c-c>' ) 1110-- Non-blocking cmdwin (#40312): <C-C> closes the cmdwin and drops back 1111-- into ":" cmdline mode pre-filled with the line under the cursor. 1112screen : expect ( [[ 1113| 1114{1:~ }|*6 1115:foo faa fee foo^ | 1116]] ) 1117end ) 1118end ) 1119 1120describe ( 'with numeric items' , function () 1121before_each ( function () 1122source ( [[ 1123function! TestComplete() abort 1124call complete(1, g:_complist) 1125return '' 1126endfunction 1127]] ) 1128api . nvim_set_option_value ( 'completeopt' , 'menuone,noselect' , {} ) 1129api . nvim_set_var ( '_complist' , { 1130{ 1131word =0 , 1132abbr =1 , 1133menu =2 , 1134kind =3 , 1135info =4 , 1136icase =5 , 1137dup =6 , 1138empty =7 , 1139} , 1140} ) 1141end ) 1142 1143it ( 'shows correct variant as word' , function () 1144feed ( 'i<C-r>=TestComplete()<CR>' ) 1145screen : expect ( [[ 1146^ | 1147{4:1 3 2 }{1: }| 1148{1:~ }|*5 1149{5:-- INSERT --} | 1150]] ) 1151end ) 1152end ) 1153 1154it ( "'ignorecase' 'infercase' CTRL-X CTRL-N #6451" , function () 1155command ( 'set ignorecase infercase' ) 1156command ( 'edit runtime/doc/credits.txt' ) 1157feed ( 'oX<C-X><C-N>' ) 1158screen : expect { 1159grid =[[ 1160*credits.txt* Nvim | 1161Xvi^ | 1162{12:Xvi }{101: } | 1163{4:Xvim }{101: } | 1164{4:X11 }{12: } NVIM REFERENCE MANUAL | 1165{4:Xnull }{12: } | 1166{4:Xoxomoon }{12: } | 1167{5:-- Keyword Local completion (^N^P) }{6:match 1 of 10} | 1168]] , 1169} 1170end ) 1171 1172it ( 'CompleteChanged autocommand' , function () 1173api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'foo' , 'bar' , 'foobar' , '' } ) 1174source ( [[ 1175set complete=. completeopt=noinsert,noselect,menuone 1176function! OnPumChange() 1177let g:event = copy(v:event) 1178let g:item = get(v:event, 'completed_item', {}) 1179let g:word = get(g:item, 'word', v:null) 1180endfunction 1181autocmd! CompleteChanged * :call OnPumChange() 1182call cursor(4, 1) 1183]] ) 1184 1185-- v:event.size should be set with ext_popupmenu #20646 1186screen : set_option ( 'ext_popupmenu' , true ) 1187feed ( 'Sf<C-N>' ) 1188screen : expect ( { 1189grid =[[ 1190foo | 1191bar | 1192foobar | 1193f^ | 1194{1:~ }|*3 1195{5:-- Keyword completion (^N^P) }{19:Back at original} | 1196]] , 1197popupmenu ={ 1198anchor ={ 1 , 3 , 0 } , 1199items ={ { 'foo' , '' , '' , '' } , { 'foobar' , '' , '' , '' } } , 1200pos =- 1 , 1201} , 1202} ) 1203eq ( 1204{ completed_item ={} , width =0 , height =2 , size =2 , col =0 , row =4 , scrollbar =false } , 1205eval ( 'g:event' ) 1206) 1207feed ( 'oob' ) 1208screen : expect ( { 1209grid =[[ 1210foo | 1211bar | 1212foobar | 1213foob^ | 1214{1:~ }|*3 1215{5:-- Keyword completion (^N^P) }{19:Back at original} | 1216]] , 1217popupmenu ={ 1218anchor ={ 1 , 3 , 0 } , 1219items ={ { 'foobar' , '' , '' , '' } } , 1220pos =- 1 , 1221} , 1222} ) 1223eq ( 1224{ completed_item ={} , width =0 , height =1 , size =1 , col =0 , row =4 , scrollbar =false } , 1225eval ( 'g:event' ) 1226) 1227feed ( '<Esc>' ) 1228screen : set_option ( 'ext_popupmenu' , false ) 1229 1230feed ( 'Sf<C-N>' ) 1231screen : expect ( [[ 1232foo | 1233bar | 1234foobar | 1235f^ | 1236{4:foo }{1: }| 1237{4:foobar }{1: }| 1238{1:~ }| 1239{5:-- Keyword completion (^N^P) }{19:Back at original} | 1240]] ) 1241eq ( 1242{ completed_item ={} , width =15 , height =2 , size =2 , col =0 , row =4 , scrollbar =false } , 1243eval ( 'g:event' ) 1244) 1245feed ( '<C-N>' ) 1246screen : expect ( [[ 1247foo | 1248bar | 1249foobar | 1250foo^ | 1251{12:foo }{1: }| 1252{4:foobar }{1: }| 1253{1:~ }| 1254{5:-- Keyword completion (^N^P) }{6:match 1 of 2} | 1255]] ) 1256eq ( 'foo' , eval ( 'g:word' )) 1257feed ( '<C-N>' ) 1258screen : expect ( [[ 1259foo | 1260bar | 1261foobar | 1262foobar^ | 1263{4:foo }{1: }| 1264{12:foobar }{1: }| 1265{1:~ }| 1266{5:-- Keyword completion (^N^P) }{6:match 2 of 2} | 1267]] ) 1268eq ( 'foobar' , eval ( 'g:word' )) 1269feed ( '<up>' ) 1270screen : expect ( [[ 1271foo | 1272bar | 1273foobar | 1274foobar^ | 1275{12:foo }{1: }| 1276{4:foobar }{1: }| 1277{1:~ }| 1278{5:-- Keyword completion (^N^P) }{6:match 1 of 2} | 1279]] ) 1280eq ( 'foo' , eval ( 'g:word' )) 1281feed ( '<down>' ) 1282screen : expect ( [[ 1283foo | 1284bar | 1285foobar | 1286foobar^ | 1287{4:foo }{1: }| 1288{12:foobar }{1: }| 1289{1:~ }| 1290{5:-- Keyword completion (^N^P) }{6:match 2 of 2} | 1291]] ) 1292eq ( 'foobar' , eval ( 'g:word' )) 1293feed ( '<esc>' ) 1294end ) 1295 1296it ( 'is stopped by :stopinsert from timer #12976' , function () 1297screen : try_resize ( 32 , 14 ) 1298command ( [[call setline(1, ['hello', 'hullo', 'heeee', ''])]] ) 1299feed ( 'Gah<c-x><c-n>' ) 1300screen : expect ( [[ 1301hello | 1302hullo | 1303heeee | 1304hello^ | 1305{12:hello }{1: }| 1306{4:hullo }{1: }| 1307{4:heeee }{1: }| 1308{1:~ }|*6 1309{5:-- }{6:match 1 of 3} | 1310]] ) 1311command ( [[call timer_start(100, { -> execute('stopinsert') })]] ) 1312vim . uv . sleep ( 200 ) 1313feed ( 'k' ) -- cursor should move up in Normal mode 1314screen : expect ( [[ 1315hello | 1316hullo | 1317heee^e | 1318hello | 1319{1:~ }|*9 1320| 1321]] ) 1322end ) 1323 1324-- oldtest: Test_complete_included_file_name() 1325it ( 'shows included files by relative path' , function () 1326t . mkdir ( 'Xincl' ) 1327finally ( function () 1328n . rmdir ( 'Xincl' ) 1329end ) 1330t . mkdir ( 'Xincl/sub' ) 1331t . write_file ( 'Xincl/sub/inc.vim' , 'let included_word = 1\n' ) 1332t . write_file ( 'Xincl/main.vim' , 'source ./sub/inc.vim\n\n' ) 1333n . exec ( [[ 1334edit Xincl/main.vim 1335setlocal include=^\\s*source\\s\\+ complete=i completeopt=menuone,noselect 1336]] ) 1337feed ( 'Goincluded_<C-N>' ) 1338screen : expect ( [[ 1339source ./sub/inc.vim | 1340| 1341included_^ | 1342{4:included_word Xincl/sub/inc.vim }{1: }| 1343{1:~ }|*3 1344{5:-- Keyword completion (^N^P) }{19:Back at original} | 1345]] ) 1346feed ( '<Esc>' ) 1347end ) 1348 1349-- oldtest: Test_complete_changed_complete_info() 1350it ( 'no crash calling complete_info() in CompleteChanged' , function () 1351source ( [[ 1352set completeopt=menuone 1353autocmd CompleteChanged * call complete_info(['items']) 1354call feedkeys("iii\<cr>\<c-p>") 1355]] ) 1356screen : expect ( [[ 1357ii | 1358ii^ | 1359{12:ii }{1: }| 1360{1:~ }|*4 1361{5:-- Keyword completion (^N^P) The only match} | 1362]] ) 1363assert_alive () 1364end ) 1365 1366it ( 'no crash if text changed by first call to complete function #17489' , function () 1367source ( [[ 1368func Complete(findstart, base) abort 1369if a:findstart 1370let col = col('.') 1371call complete_add('#') 1372return col - 1 1373else 1374return [] 1375endif 1376endfunc 1377 1378set completeopt=longest 1379set completefunc=Complete 1380]] ) 1381feed ( 'ifoo#<C-X><C-U>' ) 1382assert_alive () 1383end ) 1384 1385it ( 'no crash using i_CTRL-X_CTRL-V to complete non-existent colorscheme' , function () 1386feed ( 'icolorscheme NOSUCHCOLORSCHEME<C-X><C-V>' ) 1387expect ( 'colorscheme NOSUCHCOLORSCHEME' ) 1388assert_alive () 1389end ) 1390 1391it ( 'complete with f flag #25598' , function () 1392screen : try_resize ( 20 , 9 ) 1393command ( 'set complete+=f | edit foo | edit bar |edit foa |edit .hidden' ) 1394feed ( 'i<C-n>' ) 1395screen : expect { 1396grid =[[ 1397foo^ | 1398{12:foo }{1: }| 1399{4:bar }{1: }| 1400{4:foa }{1: }| 1401{4:.hidden }{1: }| 1402{1:~ }|*3 1403{5:-- }{6:match 1 of 4} | 1404]] , 1405} 1406feed ( '<Esc>ccf<C-n>' ) 1407screen : expect { 1408grid =[[ 1409foo^ | 1410{12:foo }{1: }| 1411{4:foa }{1: }| 1412{1:~ }|*5 1413{5:-- }{6:match 1 of 2} | 1414]] , 1415} 1416end ) 1417 1418it ( 'restores extmarks if original text is restored #23653' , function () 1419screen : try_resize ( screen . _width , 4 ) 1420command ( [[ 1421call setline(1, ['aaaa']) 1422let ns_id = nvim_create_namespace('extmark') 1423let mark_id = nvim_buf_set_extmark(0, ns_id, 0, 0, { 'end_col':2, 'hl_group':'Error' }) 1424let mark = nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 }) 1425inoremap <C-x> <C-r>=Complete()<CR> 1426function Complete() abort 1427call complete(1, [{ 'word': 'aaaaa' }]) 1428return '' 1429endfunction 1430]] ) 1431feed ( 'A<C-X><C-E><Esc>' ) 1432eq ( eval ( 'mark' ), eval ( "nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })" )) 1433feed ( 'A<C-N>' ) 1434eq ( eval ( 'mark' ), eval ( "nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })" )) 1435feed ( '<Esc>0Yppia<Esc>ggI<C-N>' ) 1436screen : expect ( [[ 1437aaaa{9:^aa}aa | 1438{12:aaaa } | 1439{4:aaaaa } | 1440{5:-- Keyword completion (^N^P) }{6:match 1 of 2} | 1441]] ) 1442feed ( '<C-N><C-N><Esc>' ) 1443eq ( eval ( 'mark' ), eval ( "nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })" )) 1444feed ( 'A<C-N>' ) 1445eq ( eval ( 'mark' ), eval ( "nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })" )) 1446feed ( '<C-N>' ) 1447screen : expect ( [[ 1448aaaaa^ | 1449{4:aaaa } | 1450{12:aaaaa } | 1451{5:-- Keyword completion (^N^P) }{6:match 2 of 2} | 1452]] ) 1453feed ( '<C-E>' ) 1454screen : expect ( [[ 1455{9:aa}aa^ | 1456aaaa | 1457aaaaa | 1458{5:-- INSERT --} | 1459]] ) 1460-- Also when completion leader is changed #31384 1461feed ( '<Esc>hi<C-N><C-P>a' ) 1462screen : expect ( { 1463grid =[[ 1464{9:aa}a^aa | 1465{4:aaaa } | 1466{4:aaaaa } | 1467{5:-- Keyword completion (^N^P) }{19:Back at original} | 1468]] , 1469} ) 1470-- But still grows with end_right_gravity #31437 1471command ( 1472"call nvim_buf_set_extmark(0, ns_id, 1, 0, { 'end_col':2, 'hl_group':'Error', 'end_right_gravity': 1 })" 1473) 1474feed ( '<Esc>ji<C-N>a' ) 1475screen : expect ( { 1476grid =[[ 1477{9:aa}aaa | 1478{9:aaa}^aa | 1479aaaaa | 1480{5:-- INSERT --} | 1481]] , 1482} ) 1483end ) 1484 1485describe ( 'nvim__complete_set' , function () 1486it ( "fails when 'completeopt' does not include popup" , function () 1487exec_lua ( [[ 1488function _G.omni_test(findstart, base) 1489if findstart == 1 then 1490return vim.fn.col('.') - 1 1491end 1492return { { word = 'one' } } 1493end 1494vim.api.nvim_create_autocmd('CompleteChanged', { 1495callback = function() 1496local ok, err = pcall(vim.api.nvim__complete_set, 0, { info = '1info' }) 1497if not ok then 1498vim.g.err_msg = err 1499end 1500end, 1501}) 1502vim.opt.completeopt = 'menu,menuone' 1503vim.opt.omnifunc = 'v:lua.omni_test' 1504]] ) 1505feed ( 'S<C-X><C-O>' ) 1506eq ( 'completeopt option does not include popup' , api . nvim_get_var ( 'err_msg' )) 1507end ) 1508end ) 1509 1510it ( [[does not include buffer from non-focusable window for 'complete' "w"]] , function () 1511local buf =api . nvim_create_buf ( false , true ) 1512local cfg ={ focusable =false , relative ='win' , bufpos ={ 1 , 0 } , width =1 , height =1 } 1513local win =api . nvim_open_win ( buf , false , cfg ) 1514api . nvim_buf_set_lines ( buf , 0 , - 1 , false , { 'foo' } ) 1515feed ( 'i<C-N>' ) 1516screen : expect ( [[ 1517^ | 1518{4:f}{1: }| 1519{1:~ }|*5 1520{5:-- Keyword completion (^N^P) }{9:Pattern not found} | 1521]] ) 1522api . nvim_win_set_config ( win , { focusable =true } ) 1523feed ( '<Esc>i<C-N>' ) 1524screen : expect ( [[ 1525foo^ | 1526{4:f}{1: }| 1527{1:~ }|*5 1528{5:-- Keyword completion (^N^P) The only match} | 1529]] ) 1530end ) 1531 1532-- oldtest: Test_shortmess() 1533it ( 'shortmess+=c turns off completion messages' , function () 1534command ( 'set ruler' ) 1535command ( [[call setline(1, ['hello', 'hullo', 'heee'])]] ) 1536feed ( 'Goh<C-N>' ) 1537screen : expect ( [[ 1538hello | 1539hullo | 1540heee | 1541hello^ | 1542{12:hello }{1: }| 1543{4:hullo }{1: }| 1544{4:heee }{1: }| 1545{5:-- Keyword completion (^N^P) }{6:match 1 of 3} | 1546]] ) 1547feed ( '<Esc>' ) 1548command ( 'set shm+=c' ) 1549feed ( 'Sh<C-N>' ) 1550screen : expect ( [[ 1551hello | 1552hullo | 1553heee | 1554hello^ | 1555{12:hello }{1: }| 1556{4:hullo }{1: }| 1557{4:heee }{1: }| 1558{5:-- INSERT --} 4,6 All| 1559]] ) 1560end ) 1561 1562-- oldtest: Test_autocompletedelay() 1563it ( "'autocompletedelay' option" , function () 1564screen : try_resize ( 60 , 10 ) 1565source ( [[ 1566call setline(1, ['foo', 'foobar', 'foobarbaz']) 1567setlocal autocomplete 1568]] ) 1569screen : expect ( [[ 1570^foo | 1571foobar | 1572foobarbaz | 1573{1:~ }|*6 1574| 1575]] ) 1576screen . timeout =400 1577 1578feed ( 'Gof' ) 1579screen : expect ( [[ 1580foo | 1581foobar | 1582foobarbaz | 1583f^ | 1584{4:foobarbaz }{1: }| 1585{4:foobar }{1: }| 1586{4:foo }{1: }| 1587{1:~ }|*2 1588{5:-- INSERT --} | 1589]] ) 1590 1591feed ( '<Esc>' ) 1592command ( 'set autocompletedelay=500' ) 1593feed ( 'Sf' ) 1594screen : expect ( [[ 1595foo | 1596foobar | 1597foobarbaz | 1598f^ | 1599{1:~ }|*5 1600{5:-- INSERT --} | 1601]] ) 1602feed ( 'o' ) 1603screen : expect ( [[ 1604foo | 1605foobar | 1606foobarbaz | 1607fo^ | 1608{1:~ }|*5 1609{5:-- INSERT --} | 1610]] ) 1611vim . uv . sleep ( 500 ) 1612screen : expect ( [[ 1613foo | 1614foobar | 1615foobarbaz | 1616fo^ | 1617{4:foobarbaz }{1: }| 1618{4:foobar }{1: }| 1619{4:foo }{1: }| 1620{1:~ }|*2 1621{5:-- INSERT --} | 1622]] ) 1623feed ( '<BS>' ) 1624screen : expect ( [[ 1625foo | 1626foobar | 1627foobarbaz | 1628f^ | 1629{4:foobarbaz }{1: }| 1630{4:foobar }{1: }| 1631{4:foo }{1: }| 1632{1:~ }|*2 1633{5:-- INSERT --} | 1634]] ) 1635vim . uv . sleep ( 500 ) 1636screen : expect_unchanged () 1637 1638-- During delay wait, user can open menu using CTRL_N completion 1639feed ( '<Esc>' ) 1640command ( 'set completeopt=menuone' ) 1641feed ( 'Sf<C-N>' ) 1642screen : expect ( [[ 1643foo | 1644foobar | 1645foobarbaz | 1646foo^ | 1647{12:foo }{1: }| 1648{4:foobar }{1: }| 1649{4:foobarbaz }{1: }| 1650{1:~ }|*2 1651{5:-- Keyword completion (^N^P) }{6:match 1 of 3} | 1652]] ) 1653 1654-- After the menu is open, ^N/^P and Up/Down should not delay. 1655-- Wait a bit longer than 'autocompletedelay' so the popup is surely shown 1656-- before sending CTRL-N, otherwise the keys race with the deferred popup. 1657feed ( '<Esc>' ) 1658command ( 'set completeopt=menu' ) 1659feed ( 'Sf' ) 1660screen : expect ( [[ 1661foo | 1662foobar | 1663foobarbaz | 1664f^ | 1665{1:~ }|*5 1666{5:-- INSERT --} | 1667]] ) 1668vim . uv . sleep ( 600 ) 1669screen : expect ( [[ 1670foo | 1671foobar | 1672foobarbaz | 1673f^ | 1674{4:foobarbaz }{1: }| 1675{4:foobar }{1: }| 1676{4:foo }{1: }| 1677{1:~ }|*2 1678{5:-- INSERT --} | 1679]] ) 1680feed ( '<C-N>' ) 1681screen : expect ( [[ 1682foo | 1683foobar | 1684foobarbaz | 1685foobarbaz^ | 1686{12:foobarbaz }{1: }| 1687{4:foobar }{1: }| 1688{4:foo }{1: }| 1689{1:~ }|*2 1690{5:-- INSERT --} | 1691]] ) 1692feed ( '<Down>' ) 1693screen : expect ( [[ 1694foo | 1695foobar | 1696foobarbaz | 1697foobarbaz^ | 1698{4:foobarbaz }{1: }| 1699{12:foobar }{1: }| 1700{4:foo }{1: }| 1701{1:~ }|*2 1702{5:-- INSERT --} | 1703]] ) 1704 1705-- When menu is not open Up/Down moves cursor to different line 1706feed ( '<Esc>Sf' ) 1707screen : expect ( [[ 1708foo | 1709foobar | 1710foobarbaz | 1711f^ | 1712{1:~ }|*5 1713{5:-- INSERT --} | 1714]] ) 1715feed ( '<Up>' ) 1716screen : expect ( [[ 1717foo | 1718foobar | 1719f^oobarbaz | 1720f | 1721{1:~ }|*5 1722{5:-- INSERT --} | 1723]] ) 1724feed ( '<Down>' ) 1725screen : expect ( [[ 1726foo | 1727foobar | 1728foobarbaz | 1729f^ | 1730{1:~ }|*5 1731{5:-- INSERT --} | 1732]] ) 1733 1734feed ( '<esc>' ) 1735end ) 1736 1737local function run_test_autocompletedelay_ctrl_k ( delay1 , delay2 ) 1738source ( [[ 1739new 1740call setline(1, 'foo bar baz') 1741set autocomplete autocompletedelay=200 1742]] ) 1743 1744feed ( 'ob' ) 1745vim . uv . sleep ( delay1 ) 1746feed ( '<C-K>' ) 1747vim . uv . sleep ( delay2 ) 1748feed ( '.,<Esc>' ) 1749poke_eventloop () 1750expect ( 'foo bar baz\nb…' ) 1751 1752source ( [[ 1753set autocomplete& autocompletedelay& 1754bwipe! 1755]] ) 1756end 1757 1758-- oldtest: Test_autocompletedelay_ctrl_k() 1759it ( "'autocompletedelay' doesn't interfere with i_CTRL-K" , function () 1760-- Ctrl-K typed after 'autocompletedelay' expires 1761run_test_autocompletedelay_ctrl_k ( 250 , 500 ) 1762-- Ctrl-K typed before 'autocompletedelay' expires 1763run_test_autocompletedelay_ctrl_k ( 150 , 500 ) 1764end ) 1765 1766-- oldtest: Test_fuzzy_select_item_when_acl() 1767it ( [[first item isn't selected with "fuzzy" and 'acl']] , function () 1768screen : try_resize ( 60 , 10 ) 1769source ( [[ 1770call setline(1, ["v", "vi", "vim"]) 1771set autocomplete completeopt=menuone,noinsert,fuzzy autocompletedelay=300 1772]] ) 1773 1774feed ( 'Govi' ) 1775screen : expect ( [[ 1776v | 1777vi | 1778vim | 1779vi^ | 1780{4:vi }{1: }| 1781{4:vim }{1: }| 1782{1:~ }|*3 1783{5:-- INSERT --} | 1784]] ) 1785 1786feed ( '<Esc>Sv' ) 1787screen : expect ( [[ 1788v | 1789vi | 1790vim | 1791v^ | 1792{4:v }{1: }| 1793{4:vi }{1: }| 1794{4:vim }{1: }| 1795{1:~ }|*2 1796{5:-- INSERT --} | 1797]] ) 1798feed ( 'i' ) 1799screen : expect ( [[ 1800v | 1801vi | 1802vim | 1803vi^ | 1804{4:vi }{1: }| 1805{4:vim }{1: }| 1806{1:~ }|*3 1807{5:-- INSERT --} | 1808]] ) 1809end ) 1810 1811-- oldtest: Test_autocompletedelay_longest_preinsert() 1812it ( "'autocompletedelay' with 'completeopt' longest/preinsert" , function () 1813source ( [[ 1814call setline(1, ['autocomplete', 'autocomxxx']) 1815set autocomplete completeopt+=longest autocompletedelay=500 1816]] ) 1817screen : expect ( [[ 1818^autocomplete | 1819autocomxxx | 1820{1:~ }|*5 1821| 1822]] ) 1823screen . timeout =400 1824 1825-- No spurious characters when autocompletedelay is in effect 1826feed ( 'Goau' ) 1827screen : expect ( [[ 1828autocomplete | 1829autocomxxx | 1830au^ | 1831{1:~ }|*4 1832{5:-- INSERT --} | 1833]] ) 1834feed ( 'toc' ) 1835screen : expect ( [[ 1836autocomplete | 1837autocomxxx | 1838autoc^ | 1839{1:~ }|*4 1840{5:-- INSERT --} | 1841]] ) 1842vim . uv . sleep ( 500 ) 1843screen : expect ( [[ 1844autocomplete | 1845autocomxxx | 1846autoc{102:^om} | 1847{4:autocomxxx }{1: }| 1848{4:autocomplete }{1: }| 1849{1:~ }|*2 1850{5:-- INSERT --} | 1851]] ) 1852 1853-- Deleting a char should still show longest text 1854feed ( '<Esc>Saut' ) 1855screen : expect ( [[ 1856autocomplete | 1857autocomxxx | 1858aut^ | 1859{1:~ }|*4 1860{5:-- INSERT --} | 1861]] ) 1862feed ( '<BS>' ) 1863screen : expect ( [[ 1864autocomplete | 1865autocomxxx | 1866au^ | 1867{1:~ }|*4 1868{5:-- INSERT --} | 1869]] ) 1870vim . uv . sleep ( 500 ) 1871screen : expect ( [[ 1872autocomplete | 1873autocomxxx | 1874au{102:^tocom} | 1875{4:autocomxxx }{1: }| 1876{4:autocomplete }{1: }| 1877{1:~ }|*2 1878{5:-- INSERT --} | 1879]] ) 1880 1881-- Preinsert 1882command ( 'set completeopt& completeopt+=preinsert' ) 1883feed ( '<Esc>Sau' ) 1884screen : expect ( [[ 1885autocomplete | 1886autocomxxx | 1887au^ | 1888{1:~ }|*4 1889{5:-- INSERT --} | 1890]] ) 1891vim . uv . sleep ( 500 ) 1892screen : expect ( [[ 1893autocomplete | 1894autocomxxx | 1895au{102:^tocomplete} | 1896{12:autocomplete }{1: }| 1897{4:autocomxxx }{1: }| 1898{1:~ }|*2 1899{5:-- INSERT --} | 1900]] ) 1901end ) 1902 1903-- oldtest: Test_fuzzy_filenames_compl_autocompl() 1904it ( 'fuzzy file name does not crash with autocomplete' , function () 1905feed ( 'iset ac cot=fuzzy,longest<Esc>' ) 1906command ( 'source' ) 1907feed ( 'o' ) 1908poke_eventloop () 1909feed ( '.' ) 1910poke_eventloop () 1911feed ( 'n' ) 1912poke_eventloop () 1913feed ( 'a' ) 1914poke_eventloop () 1915feed ( '<C-X><C-F>' ) -- this used to cause segfault 1916screen : expect ( [[ 1917set ac cot=fuzzy,longest | 1918.na^ | 1919{1:~ }|*5 1920{5:-- File name completion (^F^N^P) }{9:Pattern not found} | 1921]] ) 1922end ) 1923 1924it ( 'returns all entries in the directory #25791' , function () 1925n . mkdir_p ( 'Xtest/sub1' ) 1926n . mkdir_p ( 'Xtest/sub2' ) 1927t . write_file ( 'Xtest/A' , '' ) 1928t . write_file ( 'Xtest/sub1/B' , '' ) 1929t . write_file ( 'Xtest/sub2/C' , '' ) 1930finally ( function () 1931n . rmdir ( 'Xtest' ) 1932end ) 1933local path =( '%s/Xtest/**' ): format ( vim . fs . normalize ( vim . uv . cwd ())) 1934command (( 'set path=%s' ): format ( path )) 1935eq ( { 'A' , 'B' , 'C' , 'sub1/' , 'sub2/' } , fn . getcompletion ( 'find ' , 'cmdline' )) 1936end ) 1937 1938it ( 'complete-items commit_chars' , function () 1939exec_lua ( function () 1940_G . Omnifunc =function ( findstart , _ ) 1941return findstart == 1 and 0 or { { word ='foobar' , commit_chars ='(' } } 1942end 1943vim . bo . omnifunc ='v:lua.Omnifunc' 1944end ) 1945n . command ( 'set completeopt=menuone,noinsert' ) 1946feed ( 'i<C-x><C-o>' ) 1947poke_eventloop () 1948feed ( '(' ) 1949eq ( 'foobar(' , n . api . nvim_get_current_line ()) 1950end ) 1951 1952it ( 'complete_info() reports equal, preselect and commit_chars' , function () 1953command ( 'set completeopt=menuone,noinsert' ) 1954source ( [[ 1955function! TestComplete() abort 1956call complete(1, [ 1957\ {'word': 'foo', 'equal': 1}, 1958\ {'word': 'bar', 'preselect': v:true}, 1959\ {'word': 'baz', 'commit_chars': '('}, 1960\ {'word': 'qux'}, 1961\ ]) 1962return '' 1963endfunction 1964]] ) 1965feed ( 'i<C-r>=TestComplete()<CR>' ) 1966poke_eventloop () 1967 1968local items =fn . complete_info ( { 'items' } ). items 1969eq ( 4 , # items ) 1970eq ( { 1 , nil } , { items [ 1 ]. equal , items [ 1 ]. preselect } ) 1971eq ( 1 , items [ 2 ]. preselect ) 1972eq ( '(' , items [ 3 ]. commit_chars ) 1973eq ( { nil , nil , nil } , { items [ 4 ]. equal , items [ 4 ]. preselect , items [ 4 ]. commit_chars } ) 1974end ) 1975end )