robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Test for insert completion 2 3source screendump.vim 4source check.vim 5source vim9.vim 6 7" Test for insert expansion 8func Test_ins_complete () 9edit test_ins_complete.vim 10" The files in the current directory interferes with the files 11" used by this test. So use a separate directory for the test. 12call mkdir ( 'Xdir' ) 13cd Xdir 14 15set ff = unix 16call writefile ([ "test11\t36Gepeto\t/Tag/" , 17 \"asd\ttest11file\t36G" , 18 \"Makefile\tto\trun" ], 'Xtestfile' ) 19call writefile ([ '' , 'start of testfile' , 20 \'ru' , 21 \'run1' , 22 \'run2' , 23 \'STARTTEST' , 24 \'ENDTEST' , 25 \'end of testfile' ], 'Xtestdata' ) 26set ff & 27 28enew ! 29edit Xtestdata 30new 31call append ( 0 , [ '#include "Xtestfile"' , '' ]) 32call cursor ( 2 , 1 ) 33 34set cot = 35set cpt = .,w 36" add-expands (word from next line) from other window 37exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>" 38call assert_equal ( 'run1 run3' , getline ( '.' )) 39" add-expands (current buffer first) 40exe "normal o\<C-P>\<C-X>\<C-N>" 41call assert_equal ( 'run3 run3' , getline ( '.' )) 42" Local expansion, ends in an empty line (unless it becomes a global 43" expansion) 44exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>" 45call assert_equal ( '' , getline ( '.' )) 46" starts Local and switches to global add-expansion 47exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>" 48call assert_equal ( 'run1 run2' , getline ( '.' )) 49 50set cpt = .,\ ,w,i 51" i-add-expands and switches to local 52exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>" 53call assert_equal ( "Makefile\tto\trun3" , getline ( '.' )) 54" add-expands lines (it would end in an empty line if it didn't ignore 55" itself) 56exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>" 57call assert_equal ( "Makefile\tto\trun3" , getline ( '.' )) 58call assert_equal ( "Makefile\tto\trun3" , getline ( line ( '.' ) - 1 )) 59 60set cpt = kXtestfile 61" checks k-expansion, and file expansion (use Xtest11 instead of test11, 62" because TEST11.OUT may match first on DOS) 63write Xtest11.one 64write Xtest11.two 65exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>" 66call assert_equal ( 'Xtest11.two' , getline ( '.' )) 67 68" use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X 69" CTRL-F again to verify this doesn't cause trouble. 70exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>" 71call assert_equal ( 'Xtest11.one' , getline ( '.' )) 72normal ddk 73 74" Test for expanding a non-existing filename 75exe "normal oa1b2X3Y4\<C-X>\<C-F>" 76call assert_equal ( 'a1b2X3Y4' , getline ( '.' )) 77normal ddk 78 79set cpt = w 80" checks make_cyclic in other window 81exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>" 82call assert_equal ( 'STARTTEST' , getline ( '.' )) 83 84set cpt = unohid 85" checks unloaded buffer expansion 86only 87exe "normal oEN\<C-N>" 88call assert_equal ( 'ENDTEST' , getline ( '.' )) 89" checks adding mode abortion 90exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>" 91call assert_equal ( 'unless' , getline ( '.' )) 92 93set cpt = t,ddef = ^\\k*tags = Xtestfilenotagbsearch 94" tag expansion, define add-expansion interrupted 95exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>" 96call assert_equal ( 'test11file 36Gepeto /Tag/ asd' , getline ( '.' )) 97" t-expansion 98exe "normal oa\<C-N>\<Esc>" 99call assert_equal ( 'asd' , getline ( '.' )) 100 101 %bw ! 102call delete ( 'Xtestfile' ) 103call delete ( 'Xtest11.one' ) 104call delete ( 'Xtest11.two' ) 105call delete ( 'Xtestdata' ) 106set cpt & cot & def & tags & tagbsearch & hidden & 107cd .. 108call delete ( 'Xdir' , 'rf' ) 109endfunc 110 111func Test_ins_complete_invalid_byte () 112if has ( 'unix' ) && executable ( 'base64' ) 113" this weird command was causing an illegal memory access 114call writefile ([ 'bm9ybTlvMDCAMM4Dbw4OGA4ODg==' ], 'Xinvalid64' ) 115call system ( 'base64 -d Xinvalid64 > Xinvalid' ) 116call writefile ([ 'qa!' ], 'Xexit' ) 117call RunVim ([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit" ) 118call delete ( 'Xinvalid64' ) 119call delete ( 'Xinvalid' ) 120call delete ( 'Xexit' ) 121endif 122endfunc 123 124func Test_omni_dash () 125func Omni ( findstart , base ) 126if a: findstart 127return 5 128else 129return [ '-help' , '-v' ] 130endif 131endfunc 132set omnifunc = Omni 133new 134exe "normal Gofind -\<C-x>\<C-o>" 135call assert_equal ( "find -help" , getline ( '$' )) 136 %d 137set complete = o 138exe "normal Gofind -\<C-n>" 139call assert_equal ( "find -help" , getline ( '$' )) 140 141bwipe ! 142delfunc Omni 143set omnifunc = complete & 144endfunc 145 146func Test_omni_throw () 147let g: CallCount = 0 148func Omni ( findstart , base ) 149let g: CallCount += 1 150if a: findstart 151throw "he he he" 152endif 153endfunc 154set omnifunc = Omni 155new 156try 157exe "normal ifoo\<C-x>\<C-o>" 158call assert_false ( v: true , 'command should have failed' ) 159catch 160call assert_exception ( 'he he he' ) 161call assert_equal ( 1 , g: CallCount ) 162endtry 163 %d 164set complete = o 165let g: CallCount = 0 166try 167exe "normal ifoo\<C-n>" 168call assert_false ( v: true , 'command should have failed' ) 169catch 170call assert_exception ( 'he he he' ) 171call assert_equal ( 1 , g: CallCount ) 172endtry 173 174bwipe ! 175delfunc Omni 176unlet g: CallCount 177set omnifunc = complete & 178endfunc 179 180func Test_omni_autoload () 181let save_rtp = & rtp 182set rtp = Xruntime/some 183let dir = 'Xruntime/some/autoload' 184call mkdir ( dir , 'pR' ) 185 186let lines =<< trim END 187func omni#Func(findstart, base) 188if a:findstart 189return 1 190else 191return ['match'] 192endif 193endfunc 194 195eval 1 + 2 196END 197call writefile ( lines , dir .. '/omni.vim' ) 198 199new 200setlocal omnifunc = omni#Func 201call feedkeys ( "i\<C-X>\<C-O>\<Esc>" , 'xt' ) 202 203setlocal complete = .,Fomni#Func 204call feedkeys ( "S\<C-N>\<Esc>" , 'xt' ) 205setlocal complete & 206 207bwipe ! 208set omnifunc = 209let & rtp = save_rtp 210endfunc 211 212func Test_completefunc_args () 213let s: args = [] 214func ! CompleteFunc ( findstart , base ) 215let s: args += [[ a: findstart , empty ( a: base )]] 216endfunc 217new 218 219set completefunc = CompleteFunc 220call feedkeys ( "i\<C-X>\<C-U>\<Esc>" , 'x' ) 221call assert_equal ([ 1 , 1 ], s: args [ 0 ]) 222call assert_equal ( 0 , s: args [ 1 ][ 0 ]) 223set completefunc = 224 225let s: args = [] 226set omnifunc = CompleteFunc 227call feedkeys ( "i\<C-X>\<C-O>\<Esc>" , 'x' ) 228call assert_equal ([ 1 , 1 ], s: args [ 0 ]) 229call assert_equal ( 0 , s: args [ 1 ][ 0 ]) 230set omnifunc = 231 232set complete = FCompleteFunc 233call feedkeys ( "i\<C-N>\<Esc>" , 'x' ) 234call assert_equal ([ 1 , 1 ], s: args [ 0 ]) 235call assert_equal ( 0 , s: args [ 1 ][ 0 ]) 236set complete = o 237call feedkeys ( "i\<C-N>\<Esc>" , 'x' ) 238call assert_equal ([ 1 , 1 ], s: args [ 0 ]) 239call assert_equal ( 0 , s: args [ 1 ][ 0 ]) 240set complete & 241 242bwipe ! 243unlet s: args 244delfunc CompleteFunc 245endfunc 246 247func s: CompleteDone_CompleteFuncNone ( findstart , base ) 248throw 'skipped: Nvim does not support v:none' 249if a: findstart 250return 0 251endif 252 253return v: none 254endfunc 255 256func s: CompleteDone_CompleteFuncDict ( findstart , base ) 257if a: findstart 258return 0 259endif 260 261return { 262 \'words' : [ 263 \{ 264 \'word' : 'aword' , 265 \'abbr' : 'wrd' , 266 \'menu' : 'extra text' , 267 \'info' : 'words are cool' , 268 \'kind' : 'W' , 269 \'user_data' : [ 'one' , 'two' ] 270 \} 271 \] 272 \} 273endfunc 274 275func s: CompleteDone_CheckCompletedItemNone () 276let s: called_completedone = 1 277endfunc 278 279func s: CompleteDone_CheckCompletedItemDict ( pre ) 280call assert_equal ( 'aword' , v: completed_item [ 'word' ] ) 281call assert_equal ( 'wrd' , v: completed_item [ 'abbr' ] ) 282call assert_equal ( 'extra text' , v: completed_item [ 'menu' ] ) 283call assert_equal ( 'words are cool' , v: completed_item [ 'info' ] ) 284call assert_equal ( 'W' , v: completed_item [ 'kind' ] ) 285call assert_equal ( [ 'one' , 'two' ], v: completed_item [ 'user_data' ] ) 286 287if a: pre 288call assert_equal ( a: pre == 1 ? 'function' : 'keyword' , complete_info (). mode ) 289endif 290 291let s: called_completedone = 1 292endfunc 293 294func Test_CompleteDoneNone () 295throw 'skipped: Nvim does not support v:none' 296au CompleteDone * : call <SID> CompleteDone_CheckCompletedItemNone () 297let oldline = join ( map ( range ( & columns ), 'nr2char(screenchar(&lines-1, v:val+1))' ), '' ) 298 299set completefunc = <SID>CompleteDone_CompleteFuncNone 300execute "normal a\<C-X>\<C-U>\<C-Y>" 301set completefunc & 302let newline = join ( map ( range ( & columns ), 'nr2char(screenchar(&lines-1, v:val+1))' ), '' ) 303 304call assert_true ( s: called_completedone ) 305call assert_equal ( oldline , newline ) 306let s: called_completedone = 0 307 308set complete = F<SID>CompleteDone_CompleteFuncNone 309execute "normal a\<C-N>\<C-Y>" 310set complete & 311let newline = join ( map ( range ( & columns ), 'nr2char(screenchar(&lines-1, v:val+1))' ), '' ) 312 313call assert_true ( s: called_completedone ) 314call assert_equal ( oldline , newline ) 315let s: called_completedone = 0 316au ! CompleteDone 317endfunc 318 319func Test_CompleteDone_vevent_keys () 320func OnDone () 321let g: complete_word = get ( v: event , 'complete_word' , v: null ) 322let g: complete_type = get ( v: event , 'complete_type' , v: null ) 323endfunction 324 325autocmd CompleteDone * : call OnDone () 326 327func CompleteFunc ( findstart , base ) 328if a: findstart 329return col ( "." ) 330endif 331return [#{ word : "foo" }, #{ word : "bar" }] 332endfunc 333set omnifunc = CompleteFunc 334set completefunc = CompleteFunc 335set complete = .,FCompleteFunc 336set completeopt += menuone 337 338new 339call feedkeys ( "A\<C-X>\<C-O>\<Esc>" , 'tx' ) 340call assert_equal ( '' , g: complete_word ) 341call assert_equal ( 'omni' , g: complete_type ) 342 343call feedkeys ( "S\<C-X>\<C-O>\<C-Y>\<Esc>" , 'tx' ) 344call assert_equal ( 'foo' , g: complete_word ) 345call assert_equal ( 'omni' , g: complete_type ) 346 347call feedkeys ( "S\<C-X>\<C-O>\<C-N>\<C-Y>\<Esc>0" , 'tx' ) 348call assert_equal ( 'bar' , g: complete_word ) 349call assert_equal ( 'omni' , g: complete_type ) 350 351call feedkeys ( "Shello vim visual v\<C-X>\<C-N>\<ESC>" , 'tx' ) 352call assert_equal ( '' , g: complete_word ) 353call assert_equal ( 'keyword' , g: complete_type ) 354 355call feedkeys ( "Shello vim visual v\<C-X>\<C-N>\<C-Y>" , 'tx' ) 356call assert_equal ( 'vim' , g: complete_word ) 357call assert_equal ( 'keyword' , g: complete_type ) 358 359call feedkeys ( "Shello vim visual v\<C-N>\<ESC>" , 'tx' ) 360call assert_equal ( '' , g: complete_word ) 361call assert_equal ( 'keyword' , g: complete_type ) 362 363call feedkeys ( "Shello vim visual v\<C-N>\<C-Y>" , 'tx' ) 364call assert_equal ( 'vim' , g: complete_word ) 365call assert_equal ( 'keyword' , g: complete_type ) 366 367call feedkeys ( "Shello vim\<CR>completion test\<CR>\<C-X>\<C-l>\<C-Y>" , 'tx' ) 368call assert_equal ( 'completion test' , g: complete_word ) 369call assert_equal ( 'whole_line' , g: complete_type ) 370 371call feedkeys ( "S\<C-X>\<C-U>\<C-Y>" , 'tx' ) 372call assert_equal ( 'foo' , g: complete_word ) 373call assert_equal ( 'function' , g: complete_type ) 374 375inoremap <buffer> < f3 > <cmd> call complete ( 1 , [ "red" , "blue" ]) <cr> 376call feedkeys ( "S\<f3>\<C-Y>" , 'tx' ) 377call assert_equal ( 'red' , g: complete_word ) 378call assert_equal ( 'eval' , g: complete_type ) 379 380call feedkeys ( "S\<C-X>\<C-V>\<C-Y>" , 'tx' ) 381call assert_equal ( '!' , g: complete_word ) 382call assert_equal ( 'cmdline' , g: complete_type ) 383 384call writefile ([ '' ], 'foo_test' , 'D' ) 385call feedkeys ( "Sfoo\<C-X>\<C-F>\<C-Y>\<Esc>" , 'tx' ) 386call assert_equal ( 'foo_test' , g: complete_word ) 387call assert_equal ( 'files' , g: complete_type ) 388 389call writefile ([ 'hello help' ], 'Xtest_case.txt' , 'D' ) 390set dictionary = Xtest_case.txt 391call feedkeys ( "ggdGSh\<C-X>\<C-K>\<C-Y>\<Esc>" , 'tx' ) 392call assert_equal ( 'hello' , g: complete_word ) 393call assert_equal ( 'dictionary' , g: complete_type ) 394 395set spell spelllang = en_us 396call feedkeys ( "STheatre\<C-X>s\<C-Y>\<Esc>" , 'tx' ) 397call assert_equal ( 'Theater' , g: complete_word ) 398call assert_equal ( 'spell' , g: complete_type ) 399 400bwipe ! 401set completeopt & omnifunc & completefunc & spell & spelllang & dictionary & complete & 402autocmd ! CompleteDone 403delfunc OnDone 404delfunc CompleteFunc 405unlet g: complete_word 406unlet g: complete_type 407endfunc 408 409func Test_CompleteDoneDict () 410au CompleteDonePre * : call <SID> CompleteDone_CheckCompletedItemDict ( 1 ) 411au CompleteDone * : call <SID> CompleteDone_CheckCompletedItemDict ( 0 ) 412 413set completefunc = <SID>CompleteDone_CompleteFuncDict 414execute "normal a\<C-X>\<C-U>\<C-Y>" 415set completefunc & 416 417call assert_equal ([ 'one' , 'two' ], v: completed_item [ 'user_data' ]) 418call assert_true ( s: called_completedone ) 419 420let s: called_completedone = 0 421au ! CompleteDonePre 422au ! CompleteDone 423 424au CompleteDonePre * : call <SID> CompleteDone_CheckCompletedItemDict ( 2 ) 425au CompleteDone * : call <SID> CompleteDone_CheckCompletedItemDict ( 0 ) 426 427set complete = .,F<SID>CompleteDone_CompleteFuncDict 428execute "normal a\<C-N>\<C-Y>" 429set complete & 430 431call assert_equal ([ 'one' , 'two' ], v: completed_item [ 'user_data' ]) 432call assert_true ( s: called_completedone ) 433 434let s: called_completedone = 0 435au ! CompleteDonePre 436au ! CompleteDone 437endfunc 438 439func s: CompleteDone_CompleteFuncDictNoUserData ( findstart , base ) 440if a: findstart 441return 0 442endif 443 444return { 445 \'words' : [ 446 \{ 447 \'word' : 'aword' , 448 \'abbr' : 'wrd' , 449 \'menu' : 'extra text' , 450 \'info' : 'words are cool' , 451 \'kind' : 'W' , 452 \} 453 \] 454 \} 455endfunc 456 457func s: CompleteDone_CheckCompletedItemDictNoUserData () 458call assert_equal ( 'aword' , v: completed_item [ 'word' ] ) 459call assert_equal ( 'wrd' , v: completed_item [ 'abbr' ] ) 460call assert_equal ( 'extra text' , v: completed_item [ 'menu' ] ) 461call assert_equal ( 'words are cool' , v: completed_item [ 'info' ] ) 462call assert_equal ( 'W' , v: completed_item [ 'kind' ] ) 463call assert_equal ( '' , v: completed_item [ 'user_data' ] ) 464 465let s: called_completedone = 1 466endfunc 467 468func Test_CompleteDoneDictNoUserData () 469au CompleteDone * : call <SID> CompleteDone_CheckCompletedItemDictNoUserData () 470 471set completefunc = <SID>CompleteDone_CompleteFuncDictNoUserData 472execute "normal a\<C-X>\<C-U>\<C-Y>" 473set completefunc & 474 475call assert_equal ( '' , v: completed_item [ 'user_data' ]) 476call assert_true ( s: called_completedone ) 477 478let s: called_completedone = 0 479 480set complete = .,F<SID>CompleteDone_CompleteFuncDictNoUserData 481execute "normal a\<C-N>\<C-Y>" 482set complete & 483 484call assert_equal ( '' , v: completed_item [ 'user_data' ]) 485call assert_true ( s: called_completedone ) 486 487let s: called_completedone = 0 488au ! CompleteDone 489endfunc 490 491func s: CompleteDone_CompleteFuncList ( findstart , base ) 492if a: findstart 493return 0 494endif 495 496return [ 'aword' ] 497endfunc 498 499func s: CompleteDone_CheckCompletedItemList () 500call assert_equal ( 'aword' , v: completed_item [ 'word' ] ) 501call assert_equal ( '' , v: completed_item [ 'abbr' ] ) 502call assert_equal ( '' , v: completed_item [ 'menu' ] ) 503call assert_equal ( '' , v: completed_item [ 'info' ] ) 504call assert_equal ( '' , v: completed_item [ 'kind' ] ) 505call assert_equal ( '' , v: completed_item [ 'user_data' ] ) 506 507let s: called_completedone = 1 508endfunc 509 510func Test_CompleteDoneList () 511au CompleteDone * : call <SID> CompleteDone_CheckCompletedItemList () 512 513set completefunc = <SID>CompleteDone_CompleteFuncList 514execute "normal a\<C-X>\<C-U>\<C-Y>" 515set completefunc & 516 517call assert_equal ( '' , v: completed_item [ 'user_data' ]) 518call assert_true ( s: called_completedone ) 519 520let s: called_completedone = 0 521 522set complete = .,F<SID>CompleteDone_CompleteFuncList 523execute "normal a\<C-N>\<C-Y>" 524set complete & 525 526call assert_equal ( '' , v: completed_item [ 'user_data' ]) 527call assert_true ( s: called_completedone ) 528 529let s: called_completedone = 0 530 531set complete = .,F 532execute "normal a\<C-N>\<C-Y>" 533set complete & 534 535call assert_equal ( '' , v: completed_item [ 'user_data' ]) 536call assert_true ( s: called_completedone ) 537 538let s: called_completedone = 0 539au ! CompleteDone 540endfunc 541 542func Test_CompleteDone_undo () 543au CompleteDone * call append ( 0 , "prepend1" ) 544new 545call setline ( 1 , [ "line1" , "line2" ]) 546call feedkeys ( "Go\<C-X>\<C-N>\<CR>\<ESC>" , "tx" ) 547call assert_equal ([ "prepend1" , "line1" , "line2" , "line1" , "" ], 548 \getline ( 1 , '$' )) 549undo 550call assert_equal ([ "line1" , "line2" ], getline ( 1 , '$' )) 551bwipe ! 552au ! CompleteDone 553endfunc 554 555func Test_CompleteDone_modify () 556let value = { 557 \'word' : '' , 558 \'abbr' : '' , 559 \'menu' : '' , 560 \'info' : '' , 561 \'kind' : '' , 562 \'user_data' : '' , 563 \} 564let v: completed_item = value 565call assert_equal ( value , v: completed_item ) 566endfunc 567 568func CompleteTest ( findstart , query ) 569if a: findstart 570return col ( '.' ) 571endif 572return [ 'matched' ] 573endfunc 574 575func Test_completefunc_info () 576new 577set completeopt = menuone 578set completefunc = CompleteTest 579" Can be called outside of ins-completion 580call feedkeys ( "i\<C-X>\<C-U>\<C-Y>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>" , "tx" ) 581call assert_equal ( "matched{'preinserted_text': '', 'pum_visible': 0, 'mode': '', 'selected': -1, 'items': []}" , getline ( 1 )) 582 %d 583call feedkeys ( "i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>" , "tx" ) 584call assert_equal ( "matched{'preinserted_text': '', 'pum_visible': 1, 'mode': 'function', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'kind_hlgroup': '', 'abbr_hlgroup': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}" , getline ( 1 )) 585 %d 586set complete = .,FCompleteTest 587call feedkeys ( "i\<C-N>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>" , "tx" ) 588call assert_equal ( "matched{'preinserted_text': '', 'pum_visible': 1, 'mode': 'keyword', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'kind_hlgroup': '', 'abbr_hlgroup': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}" , getline ( 1 )) 589 %d 590set complete = .,F 591call feedkeys ( "i\<C-N>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>" , "tx" ) 592call assert_equal ( "matched{'preinserted_text': '', 'pum_visible': 1, 'mode': 'keyword', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'kind_hlgroup': '', 'abbr_hlgroup': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}" , getline ( 1 )) 593set completeopt & 594set complete & 595set completefunc & 596endfunc 597 598" For ^N completion, `completefunc` receives the same leader string in both the 599" 'info' and 'expansion' phases (the leader is not removed before expansion). 600" This avoids flicker when `completefunc` (e.g. an LSP client) is slow and calls 601" 'sleep', which triggers out_flush(). 602func Test_completefunc_leader () 603func CptColTest ( findstart , query ) 604if a: findstart 605call assert_equal ( b: compl_line , getline ( 1 )) 606call assert_equal ( b: cursor_col , col ( '.' )) 607return col ( '.' ) 608endif 609call assert_equal ( b: compl_line , getline ( 1 )) 610call assert_equal ( b: cursor_col , col ( '.' )) 611" return v:none 612return [] 613endfunc 614 615set complete = FCptColTest 616new 617 618" Replace mode 619let b: compl_line = "foo barxyz" 620let b: cursor_col = 10 621call feedkeys ( "ifoo barbaz\<Esc>2hRxy\<C-N>" , "tx" ) 622 623" Insert mode 624let b: compl_line = "foo bar" 625let b: cursor_col = 8 626call feedkeys ( "Sfoo bar\<C-N>" , "tx" ) 627 628set completeopt = longest 629call feedkeys ( "Sfoo bar\<C-N>" , "tx" ) 630 631set completeopt = menuone 632call feedkeys ( "Sfoo bar\<C-N>" , "tx" ) 633 634set completeopt = menuone,preinsert 635call feedkeys ( "Sfoo bar\<C-N>" , "tx" ) 636bwipe ! 637set complete & completeopt & 638delfunc CptColTest 639endfunc 640 641func ScrollInfoWindowUserDefinedFn ( findstart , query ) 642" User defined function (i_CTRL-X_CTRL-U) 643if a: findstart 644return col ( '.' ) 645endif 646let infostr = range ( 20 ) ->mapnew ({ _ , v ->string ( v )}) ->join ( "\n" ) 647return [{ 'word' : 'foo' , 'info' : infostr }, { 'word' : 'bar' }] 648endfunc 649 650func ScrollInfoWindowPageDown () 651call win_execute ( popup_findinfo (), "normal! \<PageDown>" ) 652return '' 653endfunc 654 655func ScrollInfoWindowPageUp () 656call win_execute ( popup_findinfo (), "normal! \<PageUp>" ) 657return '' 658endfunc 659 660func ScrollInfoWindowTest ( mvmt , count , fline ) 661new 662set completeopt = menuone,popup,noinsert,noselect 663set completepopup = height:5 664set completefunc = ScrollInfoWindowUserDefinedFn 665let keyseq = "i\<C-X>\<C-U>\<C-N>" 666for _ in range ( a: count ) 667let keyseq .= ( a: mvmt == "pageup" ? "\<C-R>\<C-R>=ScrollInfoWindowPageUp()\<CR>" : 668 \"\<C-R>\<C-R>=ScrollInfoWindowPageDown()\<CR>" ) 669endfor 670let keyseq .= "\<C-R>\<C-R>=string(popup_getpos(popup_findinfo()))\<CR>\<ESC>" 671call feedkeys ( keyseq , "tx" ) 672call assert_match ( '''firstline'': ' . a: fline , getline ( 1 )) 673bwipe ! 674set completeopt & 675set completepopup & 676set completefunc & 677endfunc 678 679func Test_scroll_info_window () 680throw 'Skipped: popup_findinfo() is N/A' 681call ScrollInfoWindowTest ( "" , 0 , 1 ) 682call ScrollInfoWindowTest ( "pagedown" , 1 , 4 ) 683call ScrollInfoWindowTest ( "pagedown" , 2 , 7 ) 684call ScrollInfoWindowTest ( "pagedown" , 3 , 11 ) 685call ScrollInfoWindowTest ( "pageup" , 3 , 1 ) 686endfunc 687 688func CompleteInfoUserDefinedFn ( findstart , query ) 689" User defined function (i_CTRL-X_CTRL-U) 690if a: findstart 691return col ( '.' ) 692endif 693return [{ 'word' : 'foo' }, { 'word' : 'bar' }, { 'word' : 'baz' }, { 'word' : 'qux' }] 694endfunc 695 696func CompleteInfoTestUserDefinedFn ( mvmt , idx , noselect ) 697if a: noselect 698set completeopt = menuone,popup,noinsert,noselect 699else 700set completeopt = menu,preview 701endif 702let items = "[" . 703 \"{'word': 'foo', 'menu': '', 'kind_hlgroup': '', 'abbr_hlgroup': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " . 704 \"{'word': 'bar', 'menu': '', 'kind_hlgroup': '', 'abbr_hlgroup': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " . 705 \"{'word': 'baz', 'menu': '', 'kind_hlgroup': '', 'abbr_hlgroup': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " . 706 \"{'word': 'qux', 'menu': '', 'kind_hlgroup': '', 'abbr_hlgroup': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}" . 707 \"]" 708new 709set completefunc = CompleteInfoUserDefinedFn 710call feedkeys ( "i\<C-X>\<C-U>" . a: mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>" , "tx" ) 711let completed = a: idx != -1 ? [ 'foo' , 'bar' , 'baz' , 'qux' ] ->get ( a: idx ) : '' 712call assert_equal ( completed . "{'preinserted_text': '', 'pum_visible': 1, 'mode': 'function', 'selected': " . a: idx . ", 'items': " . items . "}" , getline ( 1 )) 713 %d 714set complete = .,FCompleteInfoUserDefinedFn 715call feedkeys ( "i\<C-N>" . a: mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>" , "tx" ) 716let completed = a: idx != -1 ? [ 'foo' , 'bar' , 'baz' , 'qux' ] ->get ( a: idx ) : '' 717call assert_equal ( completed . "{'preinserted_text': '', 'pum_visible': 1, 'mode': 'keyword', 'selected': " . a: idx . ", 'items': " . items . "}" , getline ( 1 )) 718 %d 719set complete = .,F 720call feedkeys ( "i\<C-N>" . a: mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>" , "tx" ) 721let completed = a: idx != -1 ? [ 'foo' , 'bar' , 'baz' , 'qux' ] ->get ( a: idx ) : '' 722call assert_equal ( completed . "{'preinserted_text': '', 'pum_visible': 1, 'mode': 'keyword', 'selected': " . a: idx . ", 'items': " . items . "}" , getline ( 1 )) 723bwipe ! 724set completeopt & completefunc & complete & 725endfunc 726 727func Test_complete_info_user_defined_fn () 728" forward 729call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>" , 1 , v: true ) 730call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-N>" , 2 , v: true ) 731call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>" , 2 , v: false ) 732call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-N>" , 3 , v: false ) 733call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-N>\<C-N>" , -1 , v: false ) 734" backward 735call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>" , 2 , v: true ) 736call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>\<C-P>" , 1 , v: true ) 737call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>" , -1 , v: true ) 738call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>" , 3 , v: false ) 739call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>\<C-P>" , 2 , v: false ) 740" forward backward 741call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-N>\<C-P>" , 1 , v: true ) 742call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-P>" , 0 , v: true ) 743call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-N>\<C-P>" , 2 , v: false ) 744call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>" , 3 , v: false ) 745call CompleteInfoTestUserDefinedFn ( "\<C-N>\<C-N>\<C-P>" , 1 , v: false ) 746" backward forward 747call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>" , 0 , v: true ) 748call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>\<C-P>\<C-N>" , 2 , v: true ) 749call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>" , 1 , v: false ) 750call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-P>\<C-P>\<C-N>" , 3 , v: false ) 751call CompleteInfoTestUserDefinedFn ( "\<C-P>\<C-N>\<C-N>" , 1 , v: false ) 752endfunc 753 754" Test that mouse scrolling/movement should not interrupt completion. 755func Test_mouse_scroll_move_during_completion () 756new 757com ! -buffer TestCommand1 echo 'TestCommand1' 758com ! -buffer TestCommand2 echo 'TestCommand2' 759call setline ( 1 , [ '' , '' , '' , '' , '' ]) 760call cursor ( 5 , 1 ) 761 762" Without completion menu scrolling can move text. 763set completeopt -= menuwrap 764call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>" , 'tx' ) 765call assert_equal ( 'TestCommand2' , getline ( '.' )) 766call assert_notequal ( 1 , winsaveview (). topline ) 767call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>" , 'tx' ) 768call assert_equal ( 'TestCommand2' , getline ( '.' )) 769call assert_equal ( 1 , winsaveview (). topline ) 770set nowrap 771call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>" , 'tx' ) 772call assert_equal ( 'TestCommand2' , getline ( '.' )) 773call assert_notequal ( 0 , winsaveview (). leftcol ) 774call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>" , 'tx' ) 775call assert_equal ( 'TestCommand2' , getline ( '.' )) 776call assert_equal ( 0 , winsaveview (). leftcol ) 777call feedkeys ( "ccT\<C-X>\<C-V>\<MouseMove>\<C-V>" , 'tx' ) 778call assert_equal ( 'TestCommand2' , getline ( '.' )) 779 780" With completion menu scrolling cannot move text. 781set completeopt += menuwrap 782call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>" , 'tx' ) 783call assert_equal ( 'TestCommand2' , getline ( '.' )) 784call assert_equal ( 1 , winsaveview (). topline ) 785call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>" , 'tx' ) 786call assert_equal ( 'TestCommand2' , getline ( '.' )) 787call assert_equal ( 1 , winsaveview (). topline ) 788set nowrap 789call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>" , 'tx' ) 790call assert_equal ( 'TestCommand2' , getline ( '.' )) 791call assert_equal ( 0 , winsaveview (). leftcol ) 792call feedkeys ( "ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>" , 'tx' ) 793call assert_equal ( 'TestCommand2' , getline ( '.' )) 794call assert_equal ( 0 , winsaveview (). leftcol ) 795call feedkeys ( "ccT\<C-X>\<C-V>\<MouseMove>\<C-V>" , 'tx' ) 796call assert_equal ( 'TestCommand2' , getline ( '.' )) 797 798bwipe ! 799set completeopt & wrap & 800endfunc 801 802" Check that when using feedkeys() typeahead does not interrupt searching for 803" completions. 804func Test_compl_feedkeys () 805new 806set completeopt = menuone,noselect 807call feedkeys ( "ajump ju\<C-X>\<C-N>\<C-P>\<ESC>" , "tx" ) 808call assert_equal ( "jump jump" , getline ( 1 )) 809bwipe ! 810set completeopt & 811endfunc 812 813func s: ComplInCmdwin_GlobalCompletion ( a , l , p ) 814return 'global' 815endfunc 816 817func s: ComplInCmdwin_LocalCompletion ( a , l , p ) 818return 'local' 819endfunc 820 821func Test_compl_in_cmdwin () 822set wildmenu wildchar = <Tab> 823com ! -nargs = 1 -complete = command GetInput let input = <q-args> 824com ! -buffer TestCommand echo 'TestCommand' 825let w: test_winvar = 'winvar' 826let b: test_bufvar = 'bufvar' 827 828" User-defined commands 829let input = '' 830call feedkeys ( "q:iGetInput T\<C-x>\<C-v>\<CR>" , 'tx!' ) 831call assert_equal ( 'TestCommand' , input ) 832 833let input = '' 834call feedkeys ( "q::GetInput T\<Tab>\<CR>:q\<CR>" , 'tx!' ) 835call assert_equal ( 'T' , input ) 836 837com ! -nargs = 1 -complete = var GetInput let input = <q-args> 838" Window-local variables 839let input = '' 840call feedkeys ( "q:iGetInput w:test_\<C-x>\<C-v>\<CR>" , 'tx!' ) 841call assert_equal ( 'w:test_winvar' , input ) 842 843let input = '' 844call feedkeys ( "q::GetInput w:test_\<Tab>\<CR>:q\<CR>" , 'tx!' ) 845call assert_equal ( 'w:test_' , input ) 846 847" Buffer-local variables 848let input = '' 849call feedkeys ( "q:iGetInput b:test_\<C-x>\<C-v>\<CR>" , 'tx!' ) 850call assert_equal ( 'b:test_bufvar' , input ) 851 852let input = '' 853call feedkeys ( "q::GetInput b:test_\<Tab>\<CR>:q\<CR>" , 'tx!' ) 854call assert_equal ( 'b:test_' , input ) 855 856 857" Argument completion of buffer-local command 858func s: ComplInCmdwin_GlobalCompletionList ( a , l , p ) 859return [ 'global' ] 860endfunc 861 862func s: ComplInCmdwin_LocalCompletionList ( a , l , p ) 863return [ 'local' ] 864endfunc 865 866func s: ComplInCmdwin_CheckCompletion ( arg ) 867call assert_equal ( 'local' , a: arg ) 868endfunc 869 870com ! -nargs = 1 -complete = custom , <SID> ComplInCmdwin_GlobalCompletion 871 \TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) 872com ! -buffer -nargs = 1 -complete = custom , <SID> ComplInCmdwin_LocalCompletion 873 \TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) 874call feedkeys ( "q:iTestCommand \<Tab>\<CR>" , 'tx!' ) 875 876com ! -nargs = 1 -complete = customlist , <SID> ComplInCmdwin_GlobalCompletionList 877 \TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) 878com ! -buffer -nargs = 1 -complete = customlist , <SID> ComplInCmdwin_LocalCompletionList 879 \TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) 880 881call feedkeys ( "q:iTestCommand \<Tab>\<CR>" , 'tx!' ) 882 883func ! s: ComplInCmdwin_CheckCompletion ( arg ) 884call assert_equal ( 'global' , a: arg ) 885endfunc 886new 887call feedkeys ( "q:iTestCommand \<Tab>\<CR>" , 'tx!' ) 888quit 889 890delfunc s:ComplInCmdwin_GlobalCompletion 891delfunc s:ComplInCmdwin_LocalCompletion 892delfunc s:ComplInCmdwin_GlobalCompletionList 893delfunc s:ComplInCmdwin_LocalCompletionList 894delfunc s:ComplInCmdwin_CheckCompletion 895 896delcom -bufferTestCommand 897delcom TestCommand 898delcom GetInput 899unlet w: test_winvar 900unlet b: test_bufvar 901set wildmenu & wildchar & 902endfunc 903 904" Test for insert path completion with completeslash option 905func Test_ins_completeslash () 906CheckMSWindows 907 908call mkdir ( 'Xdir' ) 909let orig_shellslash = & shellslash 910set cpt & 911new 912 913set noshellslash 914 915set completeslash = 916exe "normal oXd\<C-X>\<C-F>" 917call assert_equal ( 'Xdir\' , getline ( '.' )) 918 919set completeslash = backslash 920exe "normal oXd\<C-X>\<C-F>" 921call assert_equal ( 'Xdir\' , getline ( '.' )) 922 923set completeslash = slash 924exe "normal oXd\<C-X>\<C-F>" 925call assert_equal ( 'Xdir/' , getline ( '.' )) 926 927set shellslash 928 929set completeslash = 930exe "normal oXd\<C-X>\<C-F>" 931call assert_equal ( 'Xdir/' , getline ( '.' )) 932 933set completeslash = backslash 934exe "normal oXd\<C-X>\<C-F>" 935call assert_equal ( 'Xdir\' , getline ( '.' )) 936 937set completeslash = slash 938exe "normal oXd\<C-X>\<C-F>" 939call assert_equal ( 'Xdir/' , getline ( '.' )) 940 %bw ! 941call delete ( 'Xdir' , 'rf' ) 942 943" globpath() should always return forward slash separator 944set noshellslash 945set completeslash = backslash 946call assert_true ( stridx ( globpath ( & rtp , 'syntax/*.vim' , 1 , 1 )[ 0 ], '\' ) == -1 ) 947 948let & shellslash = orig_shellslash 949set completeslash = 950endfunc 951 952func Test_pum_stopped_by_timer () 953CheckScreendump 954 955let lines =<< trim END 956call setline(1, ['hello', 'hullo', 'heeee', '']) 957func StartCompl() 958call timer_start(100, { -> execute('stopinsert') }) 959call feedkeys("Gah\<C-N>") 960endfunc 961END 962 963call writefile ( lines , 'Xpumscript' , 'D' ) 964let buf = RunVimInTerminal ( '-S Xpumscript' , #{ rows : 12 }) 965call term_sendkeys ( buf , ":call StartCompl()\<CR>" ) 966call TermWait ( buf , 200 ) 967call term_sendkeys ( buf , "k" ) 968call VerifyScreenDump ( buf , 'Test_pum_stopped_by_timer' , {}) 969 970call StopVimInTerminal ( buf ) 971endfunc 972 973" The completion popup menu must line up with the start of the completed text 974" on screen, also when there is concealed text before it on the line. 975func Test_pum_position_with_concealed_text () 976CheckScreendump 977 978let lines =<< trim END 979call setline(1, ['CONCEALED foobar', 'CONCEALED foo']) 980syntax match Hidden /CONCEALED / conceal 981setlocal conceallevel=3 concealcursor=nvic 982set completeopt=menu,menuone 983END 984 985call writefile ( lines , 'Xpumconceal' , 'D' ) 986let buf = RunVimInTerminal ( '-S Xpumconceal' , #{ rows : 10 }) 987call TermWait ( buf , 50 ) 988call term_sendkeys ( buf , "2GA" ) 989call TermWait ( buf , 50 ) 990call term_sendkeys ( buf , "\<C-X>\<C-N>" ) 991call VerifyScreenDump ( buf , 'Test_pum_position_with_concealed_text' , {}) 992 993call term_sendkeys ( buf , "\<Esc>" ) 994call StopVimInTerminal ( buf ) 995endfunc 996 997" Same alignment when the concealed text comes from a match and is shown as a 998" replacement character with 'conceallevel' 2. 999func Test_pum_position_with_concealed_match () 1000CheckScreendump 1001 1002let lines =<< trim END 1003call setline(1, ['XXX foobar', 'XXX foo']) 1004call matchadd('Conceal', 'XXX ', 10, -1, {'conceal': '+'}) 1005setlocal conceallevel=2 concealcursor=nvic 1006set completeopt=menu,menuone 1007END 1008 1009call writefile ( lines , 'Xpumconcealmatch' , 'D' ) 1010let buf = RunVimInTerminal ( '-S Xpumconcealmatch' , #{ rows : 10 }) 1011call TermWait ( buf , 50 ) 1012call term_sendkeys ( buf , "2GA" ) 1013call TermWait ( buf , 50 ) 1014call term_sendkeys ( buf , "\<C-X>\<C-N>" ) 1015call VerifyScreenDump ( buf , 'Test_pum_position_with_concealed_match' , {}) 1016 1017call term_sendkeys ( buf , "\<Esc>" ) 1018call StopVimInTerminal ( buf ) 1019endfunc 1020 1021" The menu lines up with the visible text in a 'rightleft' window too, where 1022" the cursor screen column is mirrored. 1023func Test_pum_position_with_concealed_rl () 1024CheckScreendump 1025CheckFeature rightleft 1026 1027let lines =<< trim END 1028set rightleft 1029call setline(1, ['CONCEALED foobar', 'CONCEALED foo']) 1030syntax match Hidden /CONCEALED / conceal 1031setlocal conceallevel=3 concealcursor=nvic 1032set completeopt=menu,menuone 1033END 1034 1035call writefile ( lines , 'Xpumconcealrl' , 'D' ) 1036let buf = RunVimInTerminal ( '-S Xpumconcealrl' , #{ rows : 10 }) 1037call TermWait ( buf , 50 ) 1038call term_sendkeys ( buf , "2GA" ) 1039call TermWait ( buf , 50 ) 1040call term_sendkeys ( buf , "\<C-X>\<C-N>" ) 1041call VerifyScreenDump ( buf , 'Test_pum_position_with_concealed_rl' , {}) 1042 1043call term_sendkeys ( buf , "\<Esc>" ) 1044call StopVimInTerminal ( buf ) 1045endfunc 1046 1047" The recorded offset is per screen line, so the menu also lines up when the 1048" concealed text and the completion are on a wrapped continuation line. 1049func Test_pum_position_with_concealed_wrap () 1050CheckScreendump 1051 1052let lines =<< trim END 1053call setline(1, ['foobar', 'aaaaaaaaaaaaaaaaaaaa CONCEALED foo']) 1054syntax match Hidden /CONCEALED / conceal 1055setlocal conceallevel=3 concealcursor=nvic 1056set completeopt=menu,menuone 1057END 1058 1059call writefile ( lines , 'Xpumconcealwrap' , 'D' ) 1060let buf = RunVimInTerminal ( '-S Xpumconcealwrap' , #{ rows : 10 , cols : 20 }) 1061call TermWait ( buf , 50 ) 1062call term_sendkeys ( buf , "2GA" ) 1063call TermWait ( buf , 50 ) 1064call term_sendkeys ( buf , "\<C-X>\<C-N>" ) 1065call VerifyScreenDump ( buf , 'Test_pum_position_with_concealed_wrap' , {}) 1066 1067call term_sendkeys ( buf , "\<Esc>" ) 1068call StopVimInTerminal ( buf ) 1069endfunc 1070 1071func Test_complete_stopinsert_startinsert () 1072nnoremap < F2 > <Cmd> startinsert <CR> 1073inoremap < F2 > <Cmd> stopinsert <CR> 1074" This just checks if this causes an error 1075call feedkeys ( "i\<C-X>\<C-N>\<F2>\<F2>" , 'x' ) 1076nunmap <F2> 1077iunmap <F2> 1078endfunc 1079 1080func Test_pum_with_folds_two_tabs () 1081CheckScreendump 1082 1083let lines =<< trim END 1084set fdm=marker 1085call setline(1, ['" x {{{1', '" a some text']) 1086call setline(3, range(&lines)->map({_, val -> '" a' .. val})) 1087norm! zm 1088tab sp 1089call feedkeys('2Gzv', 'xt') 1090call feedkeys("0fa", 'xt') 1091END 1092 1093call writefile ( lines , 'Xpumscript' ) 1094let buf = RunVimInTerminal ( '-S Xpumscript' , #{ rows : 10 }) 1095call TermWait ( buf , 50 ) 1096call term_sendkeys ( buf , "a\<C-N>" ) 1097call VerifyScreenDump ( buf , 'Test_pum_with_folds_two_tabs' , {}) 1098 1099call term_sendkeys ( buf , "\<Esc>" ) 1100call StopVimInTerminal ( buf ) 1101call delete ( 'Xpumscript' ) 1102endfunc 1103 1104func Test_pum_with_preview_win () 1105CheckScreendump 1106 1107let lines =<< trim END 1108funct Omni_test(findstart, base) 1109if a:findstart 1110return col(".") - 1 1111endif 1112return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}] 1113endfunc 1114set omnifunc=Omni_test 1115set completeopt+=longest 1116END 1117 1118call writefile ( lines , 'Xpreviewscript' ) 1119let buf = RunVimInTerminal ( '-S Xpreviewscript' , #{ rows : 12 }) 1120call term_sendkeys ( buf , "Gi\<C-X>\<C-O>" ) 1121call TermWait ( buf , 200 ) 1122call term_sendkeys ( buf , "\<C-N>" ) 1123call VerifyScreenDump ( buf , 'Test_pum_with_preview_win' , {}) 1124 1125call term_sendkeys ( buf , "\<Esc>" ) 1126call StopVimInTerminal ( buf ) 1127call delete ( 'Xpreviewscript' ) 1128endfunc 1129 1130func Test_scrollbar_on_wide_char () 1131CheckScreendump 1132 1133let lines =<< trim END 1134call setline(1, ['a', ' 啊啊啊', 1135\ ' 哦哦哦', 1136\ ' 呃呃呃']) 1137call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'})) 1138END 1139call writefile ( lines , 'Xwidescript' ) 1140let buf = RunVimInTerminal ( '-S Xwidescript' , #{ rows : 10 }) 1141call term_sendkeys ( buf , "A\<C-N>" ) 1142call VerifyScreenDump ( buf , 'Test_scrollbar_on_wide_char' , {}) 1143 1144call StopVimInTerminal ( buf ) 1145call delete ( 'Xwidescript' ) 1146endfunc 1147 1148" Test for inserting the tag search pattern in insert mode 1149func Test_ins_compl_tag_sft () 1150call writefile ([ 1151 \"!_TAG_FILE_ENCODING\tutf-8\t//" , 1152 \"first\tXfoo\t/^int first() {}$/" , 1153 \"second\tXfoo\t/^int second() {}$/" , 1154 \"third\tXfoo\t/^int third() {}$/" ], 1155 \'Xtags' ) 1156set tags = Xtags 1157let code =<< trim [CODE] 1158int first() {} 1159int second() {} 1160int third() {} 1161[CODE] 1162call writefile ( code , 'Xfoo' ) 1163 1164enew 1165set showfulltag 1166exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>" 1167call assert_equal ( 'int second() {}' , getline ( 1 )) 1168set noshowfulltag 1169 1170call delete ( 'Xtags' ) 1171call delete ( 'Xfoo' ) 1172set tags & 1173 %bwipe ! 1174endfunc 1175 1176" Test for 'completefunc' deleting text 1177func Test_completefunc_error () 1178new 1179" delete text when called for the first time 1180func CompleteFunc ( findstart , base ) 1181if a: findstart == 1 1182normal dd 1183return col ( '.' ) - 1 1184endif 1185return [ 'a' , 'b' ] 1186endfunc 1187set completefunc = CompleteFunc 1188call setline ( 1 , [ '' , 'abcd' , '' ]) 1189call assert_fails ( 'exe "normal 2G$a\<C-X>\<C-U>"' , 'E565:' ) 1190set complete = FCompleteFunc 1191call assert_fails ( 'exe "normal 2G$a\<C-N>"' , 'E565:' ) 1192set complete = F 1193call assert_fails ( 'exe "normal 2G$a\<C-N>"' , 'E565:' ) 1194 1195" delete text when called for the second time 1196func CompleteFunc2 ( findstart , base ) 1197if a: findstart == 1 1198return col ( '.' ) - 1 1199endif 1200normal dd 1201return [ 'a' , 'b' ] 1202endfunc 1203set completefunc = CompleteFunc2 1204call setline ( 1 , [ '' , 'abcd' , '' ]) 1205call assert_fails ( 'exe "normal 2G$a\<C-X>\<C-U>"' , 'E565:' ) 1206set complete = FCompleteFunc2 1207call assert_fails ( 'exe "normal 2G$a\<C-N>"' , 'E565:' ) 1208set complete = F 1209call assert_fails ( 'exe "normal 2G$a\<C-N>"' , 'E565:' ) 1210 1211" Jump to a different window from the complete function 1212func CompleteFunc3 ( findstart , base ) 1213if a: findstart == 1 1214return col ( '.' ) - 1 1215endif 1216wincmd p 1217return [ 'a' , 'b' ] 1218endfunc 1219set completefunc = CompleteFunc3 1220new 1221call assert_fails ( 'exe "normal a\<C-X>\<C-U>"' , 'E565:' ) 1222 %d 1223set complete = FCompleteFunc3 1224call assert_fails ( 'exe "normal a\<C-N>"' , 'E565:' ) 1225 %d 1226set complete = F 1227call assert_fails ( 'exe "normal a\<C-N>"' , 'E565:' ) 1228bw ! 1229 1230set completefunc & complete & 1231delfunc CompleteFunc 1232delfunc CompleteFunc2 1233delfunc CompleteFunc3 1234bw ! 1235endfunc 1236 1237" Test for returning non-string values from 'completefunc' 1238func Test_completefunc_invalid_data () 1239new 1240func ! CompleteFunc ( findstart , base ) 1241if a: findstart == 1 1242return col ( '.' ) - 1 1243endif 1244return [{}, '' , 'moon' ] 1245endfunc 1246set completefunc = CompleteFunc 1247exe "normal i\<C-X>\<C-U>" 1248call assert_equal ( 'moon' , getline ( 1 )) 1249 %d 1250set complete = FCompleteFunc 1251exe "normal i\<C-N>" 1252call assert_equal ( 'moon' , getline ( 1 )) 1253 %d 1254set complete = F 1255exe "normal i\<C-N>" 1256call assert_equal ( 'moon' , getline ( 1 )) 1257set completefunc & complete & 1258delfunc ! CompleteFunc 1259bw ! 1260endfunc 1261 1262" Test for errors in using complete() function 1263func Test_complete_func_error () 1264call assert_fails ( 'call complete(1, ["a"])' , 'E785:' ) 1265func ListColors () 1266call complete ( col ( '.' ), "blue" ) 1267endfunc 1268call assert_fails ( 'exe "normal i\<C-R>=ListColors()\<CR>"' , 'E474:' ) 1269func ListMonths () 1270call complete ( col ( '.' ), test_null_list ()) 1271endfunc 1272" Nvim allows a NULL list 1273" call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:') 1274delfunc ListColors 1275delfunc ListMonths 1276call assert_fails ( 'call complete_info({})' , 'E714:' ) 1277call assert_equal ([], complete_info ([ 'items' ]). items ) 1278endfunc 1279 1280" Test for recursively starting completion mode using complete() 1281func Test_recursive_complete_func () 1282func ListColors () 1283call complete ( 5 , [ "red" , "blue" ]) 1284return '' 1285endfunc 1286new 1287call setline ( 1 , [ 'a1' , 'a2' ]) 1288set complete = . 1289exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>" 1290call assert_equal ( 'a2blue' , getline ( 3 )) 1291delfunc ListColors 1292bw ! 1293endfunc 1294 1295" Test for using complete() with completeopt+=longest 1296func Test_complete_with_longest () 1297new 1298inoremap <buffer> < f3 > <cmd> call complete ( 1 , [ "iaax" , "iaay" , "iaaz" ]) <cr> 1299 1300" default: insert first match 1301set completeopt & 1302call setline ( 1 , [ 'i' ]) 1303exe "normal Aa\<f3>\<esc>" 1304call assert_equal ( 'iaax' , getline ( 1 )) 1305 1306" with longest: insert longest prefix 1307set completeopt += longest 1308call setline ( 1 , [ 'i' ]) 1309exe "normal Aa\<f3>\<esc>" 1310call assert_equal ( 'iaa' , getline ( 1 )) 1311set completeopt & 1312bwipe ! 1313endfunc 1314 1315" Test for buffer-local value of 'completeopt' 1316func Test_completeopt_buffer_local () 1317set completeopt = menu 1318new 1319call setline ( 1 , [ 'foofoo' , 'foobar' , 'foobaz' , '' ]) 1320call assert_equal ( '' , & l: completeopt ) 1321call assert_equal ( 'menu' , & completeopt ) 1322call assert_equal ( 'menu' , & g: completeopt ) 1323 1324setlocal bufhidden = hide 1325enew 1326call setline ( 1 , [ 'foofoo' , 'foobar' , 'foobaz' , '' ]) 1327call assert_equal ( '' , & l: completeopt ) 1328call assert_equal ( 'menu' , & completeopt ) 1329call assert_equal ( 'menu' , & g: completeopt ) 1330 1331setlocal completeopt += fuzzy,noinsert 1332call assert_equal ( 'menu,fuzzy,noinsert' , & l: completeopt ) 1333call assert_equal ( 'menu,fuzzy,noinsert' , & completeopt ) 1334call assert_equal ( 'menu' , & g: completeopt ) 1335call feedkeys ( "Gccf\<C-X>\<C-N>bz\<C-Y>" , 'tnix' ) 1336call assert_equal ( 'foobaz' , getline ( '.' )) 1337 1338setlocal completeopt = 1339call assert_equal ( '' , & l: completeopt ) 1340call assert_equal ( 'menu' , & completeopt ) 1341call assert_equal ( 'menu' , & g: completeopt ) 1342call feedkeys ( "Gccf\<C-X>\<C-N>\<C-Y>" , 'tnix' ) 1343call assert_equal ( 'foofoo' , getline ( '.' )) 1344 1345setlocal completeopt += longest 1346call assert_equal ( 'menu,longest' , & l: completeopt ) 1347call assert_equal ( 'menu,longest' , & completeopt ) 1348call assert_equal ( 'menu' , & g: completeopt ) 1349call feedkeys ( "Gccf\<C-X>\<C-N>\<C-X>\<C-Z>" , 'tnix' ) 1350call assert_equal ( 'foo' , getline ( '.' )) 1351 1352setlocal bufhidden = hide 1353buffer # 1354call assert_equal ( '' , & l: completeopt ) 1355call assert_equal ( 'menu' , & completeopt ) 1356call assert_equal ( 'menu' , & g: completeopt ) 1357call feedkeys ( "Gccf\<C-X>\<C-N>\<C-Y>" , 'tnix' ) 1358call assert_equal ( 'foofoo' , getline ( '.' )) 1359 1360setlocal completeopt += fuzzy,noinsert 1361call assert_equal ( 'menu,fuzzy,noinsert' , & l: completeopt ) 1362call assert_equal ( 'menu,fuzzy,noinsert' , & completeopt ) 1363call assert_equal ( 'menu' , & g: completeopt ) 1364call feedkeys ( "Gccf\<C-X>\<C-N>bz\<C-Y>" , 'tnix' ) 1365call assert_equal ( 'foobaz' , getline ( '.' )) 1366 1367buffer # 1368call assert_equal ( 'menu,longest' , & l: completeopt ) 1369call assert_equal ( 'menu,longest' , & completeopt ) 1370call assert_equal ( 'menu' , & g: completeopt ) 1371call feedkeys ( "Gccf\<C-X>\<C-N>\<C-X>\<C-Z>" , 'tnix' ) 1372call assert_equal ( 'foo' , getline ( '.' )) 1373 1374setlocal bufhidden = wipe 1375buffer ! # 1376bwipe ! 1377call assert_equal ( '' , & l: completeopt ) 1378call assert_equal ( 'menu' , & completeopt ) 1379call assert_equal ( 'menu' , & g: completeopt ) 1380 1381new |only 1382call setline ( 1 , [ 'foofoo' , 'foobar' , 'foobaz' , '' ]) 1383set completeopt & 1384setlocal completeopt = menu,fuzzy,noinsert 1385setglobal completeopt=menu,longest 1386call assert_equal ( 'menu,fuzzy,noinsert' , & completeopt ) 1387call assert_equal ( 'menu,fuzzy,noinsert' , & l: completeopt ) 1388call assert_equal ( 'menu,longest' , & g: completeopt ) 1389call feedkeys ( "Gccf\<C-X>\<C-N>bz\<C-Y>" , 'tnix' ) 1390call assert_equal ( 'foobaz' , getline ( '.' )) 1391setlocal bufhidden = wipe 1392new |only ! 1393call setline ( 1 , [ 'foofoo' , 'foobar' , 'foobaz' , '' ]) 1394call assert_equal ( 'menu,longest' , & completeopt ) 1395call assert_equal ( 'menu,longest' , & g: completeopt ) 1396call assert_equal ( '' , & l: completeopt ) 1397call feedkeys ( "Gccf\<C-X>\<C-N>\<C-X>\<C-Z>" , 'tnix' ) 1398call assert_equal ( 'foo' , getline ( '.' )) 1399bwipe ! 1400 1401new |only 1402call setline ( 1 , [ 'foofoo' , 'foobar' , 'foobaz' , '' ]) 1403set completeopt & 1404setlocal completeopt = menu,fuzzy,noinsert 1405set completeopt = menu,longest 1406call assert_equal ( 'menu,longest' , & completeopt ) 1407call assert_equal ( 'menu,longest' , & g: completeopt ) 1408call assert_equal ( '' , & l: completeopt ) 1409call feedkeys ( "Gccf\<C-X>\<C-N>\<C-X>\<C-Z>" , 'tnix' ) 1410call assert_equal ( 'foo' , getline ( '.' )) 1411setlocal bufhidden = wipe 1412new |only ! 1413call setline ( 1 , [ 'foofoo' , 'foobar' , 'foobaz' , '' ]) 1414call assert_equal ( 'menu,longest' , & completeopt ) 1415call assert_equal ( 'menu,longest' , & g: completeopt ) 1416call assert_equal ( '' , & l: completeopt ) 1417call feedkeys ( "Gccf\<C-X>\<C-N>\<C-X>\<C-Z>" , 'tnix' ) 1418call assert_equal ( 'foo' , getline ( '.' )) 1419bwipe ! 1420 1421set completeopt & 1422endfunc 1423 1424" Test for completing words following a completed word in a line 1425func Test_complete_wrapscan () 1426" complete words from another buffer 1427new 1428call setline ( 1 , [ 'one two' , 'three four' ]) 1429new 1430setlocal complete = w 1431call feedkeys ( "itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>" , 'xt' ) 1432call assert_equal ( 'two three four' , getline ( 1 )) 1433bw ! 1434" complete words from the current buffer 1435setlocal complete = . 1436 %d 1437call setline ( 1 , [ 'one two' , '' ]) 1438call cursor ( 2 , 1 ) 1439call feedkeys ( "ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>" , 'xt' ) 1440call assert_equal ( 'one two one two' , getline ( 2 )) 1441bw ! 1442endfunc 1443 1444" Test for completing special characters 1445func Test_complete_special_chars () 1446new 1447call setline ( 1 , 'int .*[-\^$ func float' ) 1448call feedkeys ( "oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" , 'xt' ) 1449call assert_equal ( 'int .*[-\^$ func float' , getline ( 2 )) 1450bw ! 1451endfunc 1452 1453" Test for completion when text is wrapped across lines. 1454func Test_complete_across_line () 1455new 1456call setline ( 1 , [ 'red green blue' , 'one two three' ]) 1457setlocal textwidth = 20 1458exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" 1459call assert_equal ([ 'one two three red' , 'green blue one' ], getline ( 2 , '$' )) 1460bw ! 1461endfunc 1462 1463" Test for completing words with a '.' at the end of a word. 1464func Test_complete_joinspaces () 1465new 1466call setline ( 1 , [ 'one two.' , 'three. four' ]) 1467set joinspaces 1468exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" 1469call assert_equal ( "one two. three. four" , getline ( 3 )) 1470set joinspaces & 1471bw ! 1472endfunc 1473 1474" Test for using CTRL-L to add one character when completing matching 1475func Test_complete_add_onechar () 1476new 1477call setline ( 1 , [ 'wool' , 'woodwork' ]) 1478call feedkeys ( "Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f" , 'xt' ) 1479call assert_equal ( 'woof' , getline ( 3 )) 1480 1481" use 'ignorecase' and backspace to erase characters from the prefix string 1482" and then add letters using CTRL-L 1483 %d 1484set ignorecase backspace = 2 1485setlocal complete = . 1486call setline ( 1 , [ 'workhorse' , 'workload' ]) 1487normal Go 1488exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>" 1489call assert_equal ( 'workh' , getline ( 3 )) 1490set ignorecase & backspace & 1491bw ! 1492endfunc 1493 1494" Test for using CTRL-X CTRL-L to complete whole lines lines 1495func Test_complete_wholeline () 1496new 1497" complete one-line 1498call setline ( 1 , [ 'a1' , 'a2' ]) 1499exe "normal ggoa\<C-X>\<C-L>" 1500call assert_equal ([ 'a1' , 'a1' , 'a2' ], getline ( 1 , '$' )) 1501" go to the next match (wrapping around the buffer) 1502exe "normal 2GCa\<C-X>\<C-L>\<C-N>" 1503call assert_equal ([ 'a1' , 'a' , 'a2' ], getline ( 1 , '$' )) 1504" go to the next match 1505exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>" 1506call assert_equal ([ 'a1' , 'a2' , 'a2' ], getline ( 1 , '$' )) 1507exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>" 1508call assert_equal ([ 'a1' , 'a1' , 'a2' ], getline ( 1 , '$' )) 1509" repeat the test using CTRL-L 1510" go to the next match (wrapping around the buffer) 1511exe "normal 2GCa\<C-X>\<C-L>\<C-L>" 1512call assert_equal ([ 'a1' , 'a2' , 'a2' ], getline ( 1 , '$' )) 1513" go to the next match 1514exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>" 1515call assert_equal ([ 'a1' , 'a' , 'a2' ], getline ( 1 , '$' )) 1516exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>" 1517call assert_equal ([ 'a1' , 'a1' , 'a2' ], getline ( 1 , '$' )) 1518 %d 1519" use CTRL-X CTRL-L to add one more line 1520call setline ( 1 , [ 'a1' , 'b1' ]) 1521setlocal complete = . 1522exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>" 1523call assert_equal ([ 'a1' , 'b1' , '' , 'a1' , 'b1' ], getline ( 1 , '$' )) 1524bw ! 1525endfunc 1526 1527" Test for using CTRL-X CTRL-S to complete spell suggestions 1528func Test_complete_spell () 1529new 1530setlocal spell 1531" without fuzzy 1532call setline ( 1 , 'The rigth thing' ) 1533exe "normal! A\<C-X>\<C-S>" 1534call assert_equal ( 'The right thing' , getline ( 1 )) 1535 %d 1536" with fuzzy 1537setlocal completeopt += fuzzy 1538call setline ( 1 , 'The rigth thing' ) 1539exe "normal! A\<C-X>\<C-S>" 1540call assert_equal ( 'The right thing' , getline ( 1 )) 1541bw ! 1542endfunc 1543 1544" Test insert completion with 'cindent' (adjust the indent) 1545func Test_complete_with_cindent () 1546new 1547setlocal cindent 1548call setline ( 1 , [ 'if (i == 1)' , " j = 2;" ]) 1549exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}" 1550call assert_equal ([ '{' , "\tif (i == 1)" , "\t\tj = 2;" , '}' ], getline ( 3 , '$' )) 1551 1552 %d 1553call setline ( 1 , [ 'when while' , '{' , '' ]) 1554setlocal cinkeys += =while 1555exe "normal Giwh\<C-P> " 1556call assert_equal ( "\twhile " , getline ( '$' )) 1557bw ! 1558endfunc 1559 1560" Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions 1561func Test_complete_cmdline () 1562new 1563exe "normal icaddb\<C-X>\<C-V>" 1564call assert_equal ( 'caddbuffer' , getline ( 1 )) 1565exe "normal ocall getqf\<C-X>\<C-V>" 1566call assert_equal ( 'call getqflist(' , getline ( 2 )) 1567exe "normal oabcxyz(\<C-X>\<C-V>" 1568call assert_equal ( 'abcxyz(' , getline ( 3 )) 1569com ! -buffer TestCommand1 echo 'TestCommand1' 1570com ! -buffer TestCommand2 echo 'TestCommand2' 1571write ! TestCommand1Test 1572write ! TestCommand2Test 1573" Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode 1574exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>" 1575call assert_equal ( 'TestCommand2Test' , getline ( 4 )) 1576call delete ( 'TestCommand1Test' ) 1577call delete ( 'TestCommand2Test' ) 1578delcom TestCommand1 1579delcom TestCommand2 1580bw ! 1581endfunc 1582 1583" Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match 1584func Test_complete_stop () 1585new 1586func Save_mode1 () 1587let g: mode1 = mode ( 1 ) 1588return '' 1589endfunc 1590func Save_mode2 () 1591let g: mode2 = mode ( 1 ) 1592return '' 1593endfunc 1594inoremap < F1 > < C-R > =Save_mode1()< CR > 1595inoremap < F2 > < C-R > =Save_mode2()< CR > 1596call setline ( 1 , [ 'aaa bbb ccc ' ]) 1597exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" 1598call assert_equal ( 'ic' , g: mode1 ) 1599call assert_equal ( 'i' , g: mode2 ) 1600call assert_equal ( 'aaa bbb ccc ' , getline ( 1 )) 1601exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" 1602call assert_equal ( 'ic' , g: mode1 ) 1603call assert_equal ( 'i' , g: mode2 ) 1604call assert_equal ( 'aaa bbb ccc aaa' , getline ( 1 )) 1605set completeopt += noselect 1606exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" 1607call assert_equal ( 'ic' , g: mode1 ) 1608call assert_equal ( 'i' , g: mode2 ) 1609call assert_equal ( 'aaa bbb ccc aaa bb' , getline ( 1 )) 1610set completeopt & 1611exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" 1612call assert_equal ( 'ic' , g: mode1 ) 1613call assert_equal ( 'i' , g: mode2 ) 1614call assert_equal ( 'aaa bbb ccc aaa bb d' , getline ( 1 )) 1615com ! -buffer TestCommand1 echo 'TestCommand1' 1616com ! -buffer TestCommand2 echo 'TestCommand2' 1617exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>" 1618call assert_equal ( 'ic' , g: mode1 ) 1619call assert_equal ( 'i' , g: mode2 ) 1620call assert_equal ( 'TestCommand2' , getline ( 2 )) 1621delcom TestCommand1 1622delcom TestCommand2 1623unlet g: mode1 1624unlet g: mode2 1625iunmap <F1> 1626iunmap <F2> 1627delfunc Save_mode1 1628delfunc Save_mode2 1629bw ! 1630endfunc 1631 1632" Test for typing CTRL-R in insert completion mode to insert a register 1633" content. 1634func Test_complete_reginsert () 1635new 1636call setline ( 1 , [ 'a1' , 'a12' , 'a123' , 'a1234' ]) 1637 1638" if a valid CTRL-X mode key is returned from <C-R>=, then it should be 1639" processed. Otherwise, CTRL-X mode should be stopped and the key should be 1640" inserted. 1641exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>" 1642call assert_equal ( 'a123' , getline ( 5 )) 1643let @r= "\<C-P>\<C-P>" 1644exe "normal GCa\<C-P>\<C-R>=@r\<CR>" 1645call assert_equal ( 'a12' , getline ( 5 )) 1646exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>" 1647call assert_equal ( 'a1234x' , getline ( 5 )) 1648bw ! 1649endfunc 1650 1651func Test_issue_7021 () 1652CheckMSWindows 1653 1654" Test that 'completeslash' doesn't affect the result of expand() 1655set completeslash = backslash 1656call assert_false ( expand ( '~' ) =~ '\\' ) 1657 1658set completeslash = 1659endfunc 1660 1661" Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings 1662func Test_complete_longest_match () 1663" for e in ['latin1', 'utf-8'] 1664for e in [ 'utf-8' ] 1665exe 'set encoding=' .. e 1666new 1667set complete = . 1668set completeopt = menu,longest 1669call setline ( 1 , [ 'pfx_a1' , 'pfx_a12' , 'pfx_a123' , 'pfx_b1' ]) 1670exe "normal Gopfx\<C-P>" 1671call assert_equal ( 'pfx_' , getline ( 5 )) 1672bw ! 1673endfor 1674 1675" Test for completing additional words with longest match set 1676new 1677call setline ( 1 , [ 'abc1' , 'abd2' ]) 1678exe "normal Goab\<C-P>\<C-X>\<C-P>" 1679call assert_equal ( 'ab' , getline ( 3 )) 1680bw ! 1681set complete & completeopt & 1682endfunc 1683 1684" Test for removing the first displayed completion match and selecting the 1685" match just before that. 1686func Test_complete_erase_firstmatch () 1687new 1688call setline ( 1 , [ 'a12' , 'a34' , 'a56' ]) 1689set complete = . 1690exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>" 1691call assert_equal ( 'a34' , getline ( '$' )) 1692set complete & 1693bw ! 1694endfunc 1695 1696" Test for completing words from unloaded buffers 1697func Test_complete_from_unloadedbuf () 1698call writefile ([ 'abc' ], "Xfile1" ) 1699call writefile ([ 'def' ], "Xfile2" ) 1700edit Xfile1 1701edit Xfile2 1702new |close 1703enew 1704bunload Xfile1 Xfile2 1705set complete = u 1706" complete from an unloaded buffer 1707exe "normal! ia\<C-P>" 1708call assert_equal ( 'abc' , getline ( 1 )) 1709exe "normal! od\<C-P>" 1710call assert_equal ( 'def' , getline ( 2 )) 1711set complete & 1712 %bw ! 1713call delete ( "Xfile1" ) 1714call delete ( "Xfile2" ) 1715endfunc 1716 1717" Test for completing whole lines from unloaded buffers 1718func Test_complete_wholeline_unloadedbuf () 1719call writefile ([ 'a line1' , 'a line2' , 'a line3' ], "Xfile1" ) 1720edit Xfile1 1721enew 1722set complete = u 1723exe "normal! ia\<C-X>\<C-L>\<C-P>" 1724call assert_equal ( 'a line2' , getline ( 1 )) 1725 %d 1726" completing from an unlisted buffer should fail 1727bdel Xfile1 1728exe "normal! ia\<C-X>\<C-L>\<C-P>" 1729call assert_equal ( 'a' , getline ( 1 )) 1730set complete & 1731 %bw ! 1732call delete ( "Xfile1" ) 1733endfunc 1734 1735" Test for completing words from unlisted buffers 1736func Test_complete_from_unlistedbuf () 1737call writefile ([ 'abc' ], "Xfile1" ) 1738call writefile ([ 'def' ], "Xfile2" ) 1739edit Xfile1 1740edit Xfile2 1741new |close 1742bdel Xfile1 Xfile2 1743set complete = U 1744" complete from an unlisted buffer 1745exe "normal! ia\<C-P>" 1746call assert_equal ( 'abc' , getline ( 1 )) 1747exe "normal! od\<C-P>" 1748call assert_equal ( 'def' , getline ( 2 )) 1749set complete & 1750 %bw ! 1751call delete ( "Xfile1" ) 1752call delete ( "Xfile2" ) 1753endfunc 1754 1755" Test for completing whole lines from unlisted buffers 1756func Test_complete_wholeline_unlistedbuf () 1757call writefile ([ 'a line1' , 'a line2' , 'a line3' ], "Xfile1" ) 1758edit Xfile1 1759enew 1760set complete = U 1761" completing from an unloaded buffer should fail 1762exe "normal! ia\<C-X>\<C-L>\<C-P>" 1763call assert_equal ( 'a' , getline ( 1 )) 1764 %d 1765bdel Xfile1 1766exe "normal! ia\<C-X>\<C-L>\<C-P>" 1767call assert_equal ( 'a line2' , getline ( 1 )) 1768set complete & 1769 %bw ! 1770call delete ( "Xfile1" ) 1771endfunc 1772 1773" Test for adding a multibyte character using CTRL-L in completion mode 1774func Test_complete_mbyte_char_add () 1775new 1776set complete = . 1777call setline ( 1 , 'abė' ) 1778exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>" 1779call assert_equal ( 'abė' , getline ( 2 )) 1780" Test for a leader with multibyte character 1781 %d 1782call setline ( 1 , 'abėĕ' ) 1783exe "normal! oabė\<C-P>" 1784call assert_equal ( 'abėĕ' , getline ( 2 )) 1785bw ! 1786endfunc 1787 1788" Test for using <C-X><C-P> for local expansion even if 'complete' is set to 1789" not to complete matches from the local buffer. Also test using multiple 1790" <C-X> to cancel the current completion mode. 1791func Test_complete_local_expansion () 1792new 1793set complete = t 1794call setline ( 1 , [ 'abc' , 'def' ]) 1795exe "normal! Go\<C-X>\<C-P>" 1796call assert_equal ( "def" , getline ( 3 )) 1797exe "normal! Go\<C-P>" 1798call assert_equal ( "" , getline ( 4 )) 1799exe "normal! Go\<C-X>\<C-N>" 1800call assert_equal ( "abc" , getline ( 5 )) 1801exe "normal! Go\<C-N>" 1802call assert_equal ( "" , getline ( 6 )) 1803 1804" use multiple <C-X> to cancel the previous completion mode 1805exe "normal! Go\<C-P>\<C-X>\<C-P>" 1806call assert_equal ( "" , getline ( 7 )) 1807exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>" 1808call assert_equal ( "" , getline ( 8 )) 1809exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>" 1810call assert_equal ( "abc" , getline ( 9 )) 1811 1812" interrupt the current completion mode 1813set completeopt = menu,noinsert 1814exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>" 1815call assert_equal ( "abc" , getline ( 10 )) 1816 1817" when only one <C-X> is used to interrupt, do normal expansion 1818exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>" 1819call assert_equal ( "" , getline ( 11 )) 1820set completeopt & 1821 1822" using two <C-X> in non-completion mode and restarting the same mode 1823exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>" 1824call assert_equal ( "def" , getline ( 12 )) 1825 1826" test for adding a match from the original empty text 1827 %d 1828call setline ( 1 , 'abc def g' ) 1829exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>" 1830call assert_equal ( 'def' , getline ( 2 )) 1831exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>" 1832call assert_equal ( 'abc' , getline ( 2 )) 1833 1834bw ! 1835endfunc 1836 1837" Test for undoing changes after a insert-mode completion 1838func Test_complete_undo () 1839new 1840set complete = . 1841" undo with 'ignorecase' 1842call setline ( 1 , [ 'ABOVE' , 'BELOW' ]) 1843set ignorecase 1844exe "normal! Goab\<C-G>u\<C-P>" 1845call assert_equal ( "ABOVE" , getline ( 3 )) 1846undo 1847call assert_equal ( "ab" , getline ( 3 )) 1848set ignorecase & 1849 %d 1850" undo with longest match 1851set completeopt = menu,longest 1852call setline ( 1 , [ 'above' , 'about' ]) 1853exe "normal! Goa\<C-G>u\<C-P>" 1854call assert_equal ( "abo" , getline ( 3 )) 1855undo 1856call assert_equal ( "a" , getline ( 3 )) 1857set completeopt & 1858 %d 1859" undo for line completion 1860call setline ( 1 , [ 'above that change' , 'below that change' ]) 1861exe "normal! Goabove\<C-G>u\<C-X>\<C-L>" 1862call assert_equal ( "above that change" , getline ( 3 )) 1863undo 1864call assert_equal ( "above" , getline ( 3 )) 1865 1866bw ! 1867endfunc 1868 1869" Test for completing a very long word 1870func Test_complete_long_word () 1871set complete & 1872new 1873call setline ( 1 , repeat ( 'x' , 950 ) .. ' one two three' ) 1874exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" 1875call assert_equal ( repeat ( 'x' , 950 ) .. ' one two three' , getline ( 2 )) 1876 %d 1877" should fail when more than 950 characters are in a word 1878call setline ( 1 , repeat ( 'x' , 951 ) .. ' one two three' ) 1879exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>" 1880call assert_equal ( repeat ( 'x' , 951 ), getline ( 2 )) 1881 1882" Test for adding a very long word to an existing completion 1883 %d 1884call setline ( 1 , [ 'abc' , repeat ( 'x' , 1016 ) .. '012345' ]) 1885exe "normal! Goab\<C-P>\<C-X>\<C-P>" 1886call assert_equal ( 'abc ' .. repeat ( 'x' , 1016 ) .. '0123' , getline ( 3 )) 1887bw ! 1888endfunc 1889 1890" Test for some fields in the complete items used by complete() 1891func Test_complete_items () 1892func CompleteItems ( idx ) 1893let items = [[#{ word : "one" , dup : 1 , user_data : 'u1' }, #{ word : "one" , dup : 1 , user_data : 'u2' }], 1894 \[#{ word : "one" , dup : 0 , user_data : 'u3' }, #{ word : "one" , dup : 0 , user_data : 'u4' }], 1895 \[#{ word : "one" , icase : 1 , user_data : 'u7' }, #{ word : "oNE" , icase : 1 , user_data : 'u8' }], 1896 \[#{ user_data : 'u9' }], 1897 \[#{ word : "" , user_data : 'u10' }], 1898 \[#{ word : "" , empty : 1 , user_data : 'u11' }]] 1899call complete ( col ( '.' ), items [ a: idx ]) 1900return '' 1901endfunc 1902new 1903exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>" 1904call assert_equal ( 'u2' , v: completed_item . user_data ) 1905call assert_equal ( 'one' , getline ( 1 )) 1906exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>" 1907call assert_equal ( 'u3' , v: completed_item . user_data ) 1908call assert_equal ( 'one' , getline ( 2 )) 1909exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>" 1910call assert_equal ( '' , getline ( 3 )) 1911set completeopt = menu,noinsert 1912exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>" 1913call assert_equal ( 'oNE' , getline ( 4 )) 1914call assert_equal ( 'u8' , v: completed_item . user_data ) 1915set completeopt & 1916exe "normal! o\<C-R>=CompleteItems(3)\<CR>" 1917call assert_equal ( '' , getline ( 5 )) 1918exe "normal! o\<C-R>=CompleteItems(4)\<CR>" 1919call assert_equal ( '' , getline ( 6 )) 1920exe "normal! o\<C-R>=CompleteItems(5)\<CR>" 1921call assert_equal ( '' , getline ( 7 )) 1922call assert_equal ( 'u11' , v: completed_item . user_data ) 1923" pass invalid argument to complete() 1924let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>" 1925call assert_fails ( 'exe cmd' , 'E730:' ) 1926bw ! 1927delfunc CompleteItems 1928endfunc 1929 1930" Test for the "refresh" item in the dict returned by an insert completion 1931" function 1932func Test_complete_item_refresh_always () 1933let g: CallCount = 0 1934func ! Tcomplete ( findstart , base ) 1935if a: findstart 1936" locate the start of the word 1937let line = getline ( '.' ) 1938let start = col ( '.' ) - 1 1939while start > 0 && line [ start - 1 ] =~ '\a' 1940let start -= 1 1941endwhile 1942return start 1943else 1944let g: CallCount += 1 1945let res = [ "update1" , "update12" , "update123" ] 1946return #{ words : res , refresh : 'always' } 1947endif 1948endfunc 1949set completeopt = menu,longest 1950set completefunc = Tcomplete 1951new 1952exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>" 1953call assert_equal ( 'up' , getline ( 1 )) 1954call assert_equal ( 6 , g: CallCount ) 1955 %d 1956let g: CallCount = 0 1957set complete = FTcomplete 1958exe "normal! iup\<C-N>\<BS>\<BS>\<BS>\<BS>\<BS>" 1959call assert_equal ( 'up' , getline ( 1 )) 1960call assert_equal ( 6 , g: CallCount ) 1961 %d 1962let g: CallCount = 0 1963set complete = F 1964exe "normal! iup\<C-N>\<BS>\<BS>\<BS>\<BS>\<BS>" 1965call assert_equal ( 'up' , getline ( 1 )) 1966call assert_equal ( 6 , g: CallCount ) 1967 %d 1968let g: CallCount = 0 1969set omnifunc = Tcomplete 1970set complete = o 1971exe "normal! iup\<C-N>\<BS>\<BS>\<BS>\<BS>\<BS>" 1972call assert_equal ( 'up' , getline ( 1 )) 1973call assert_equal ( 6 , g: CallCount ) 1974bw ! 1975set completeopt & 1976set complete & 1977set completefunc & 1978delfunc Tcomplete 1979endfunc 1980 1981" Test for 'cpt' user func that fails (return -2/-3) when refresh:always 1982func Test_cpt_func_refresh_always_fail () 1983func ! CompleteFail ( retval , findstart , base ) 1984if a: findstart 1985return a: retval 1986endif 1987call assert_equal ( -999 , a: findstart ) " Should not reach here 1988endfunc 1989new 1990set complete = Ffunction('CompleteFail'\\,\ [-2]) 1991exe "normal! ia\<C-N>" 1992 %d 1993set complete = Ffunction('CompleteFail'\\,\ [-3]) 1994exe "normal! ia\<C-N>" 1995bw ! 1996 1997func ! CompleteFailIntermittent ( retval , findstart , base ) 1998if a: findstart 1999if g: CallCount == 2 2000let g: CallCount += 1 2001return a: retval 2002endif 2003return col ( '.' ) - 1 2004endif 2005let g: CallCount += 1 2006let res = [[], [ 'foo' , 'fbar' ], [ 'foo1' , 'foo2' ], [ 'foofail' ], [ 'fooo3' ]] 2007return #{ words : res [ g: CallCount ], refresh : 'always' } 2008endfunc 2009new 2010set completeopt = menuone,noselect 2011set complete = Ffunction('CompleteFailIntermittent'\\,\ [-2]) 2012let g: CallCount = 0 2013exe "normal! if\<C-N>\<c-r>=complete_info([\"items\"])\<cr>" 2014call assert_match ( '''word'': ''foo''.*''word'': ''fbar''' , getline ( 1 )) 2015call assert_equal ( 1 , g: CallCount ) 2016 %d 2017let g: CallCount = 0 2018exe "normal! if\<C-N>o\<c-r>=complete_info([\"items\", \"selected\"])\<cr>" 2019call assert_match ( '''selected'': -1.*''word'': ''foo1''.*''word'': ''foo2''' , getline ( 1 )) 2020call assert_equal ( 2 , g: CallCount ) 2021 %d 2022set complete = Ffunction('CompleteFailIntermittent'\\,\ [-3]) 2023let g: CallCount = 0 2024exe "normal! if\<C-N>o\<c-r>=complete_info([\"items\", \"selected\"])\<cr>" 2025call assert_match ( '''selected'': -1.*''word'': ''foo1''.*''word'': ''foo2''' , getline ( 1 )) 2026call assert_equal ( 2 , g: CallCount ) 2027 %d 2028set complete = Ffunction('CompleteFailIntermittent'\\,\ [-2]) 2029" completion mode is dismissed when there are no matches in list 2030let g: CallCount = 0 2031exe "normal! if\<C-N>oo\<c-r>=complete_info([\"items\"])\<cr>" 2032call assert_equal ( 'foo{''items'': []}' , getline ( 1 )) 2033call assert_equal ( 3 , g: CallCount ) 2034 %d 2035let g: CallCount = 0 2036exe "normal! if\<C-N>oo\<bs>\<c-r>=complete_info([\"items\"])\<cr>" 2037call assert_equal ( 'fo{''items'': []}' , getline ( 1 )) 2038call assert_equal ( 3 , g: CallCount ) 2039 %d 2040" completion mode continues when matches from other sources present 2041set complete = .,Ffunction('CompleteFailIntermittent'\\,\ [-2]) 2042call setline ( 1 , 'fooo1' ) 2043let g: CallCount = 0 2044exe "normal! Gof\<C-N>oo\<c-r>=complete_info([\"items\", \"selected\"])\<cr>" 2045call assert_equal ( 'foo{''selected'': -1, ''items'': [{''word'': ''fooo1'', ''menu'': '''', ' 2046 \. '''kind_hlgroup'': '''', ''abbr_hlgroup'': '''', ''user_data'': '''', ''info'': '''', ''kind'': '''', ''abbr'': ''''}]}' , 2047 \getline ( 2 )) 2048call assert_equal ( 3 , g: CallCount ) 2049 %d 2050call setline ( 1 , 'fooo1' ) 2051let g: CallCount = 0 2052exe "normal! Gof\<C-N>oo\<bs>\<c-r>=complete_info([\"items\"])\<cr>" 2053call assert_match ( '''word'': ''fooo1''.*''word'': ''fooo3''' , getline ( 2 )) 2054call assert_equal ( 4 , g: CallCount ) 2055 %d 2056" refresh will stop when -3 is returned 2057set complete = .,,\ Ffunction('CompleteFailIntermittent'\\,\ [-3]) 2058call setline ( 1 , 'fooo1' ) 2059let g: CallCount = 0 2060exe "normal! Gof\<C-N>o\<bs>\<c-r>=complete_info([\"items\", \"selected\"])\<cr>" 2061call assert_equal ( 'f{''selected'': -1, ''items'': [{''word'': ''fooo1'', ''menu'': '''', ' 2062 \. '''kind_hlgroup'': '''', ''abbr_hlgroup'': '''', ''user_data'': '''', ''info'': '''', ''kind'': '''', ''abbr'': ''''}]}' , 2063 \getline ( 2 )) 2064call assert_equal ( 3 , g: CallCount ) 2065 %d 2066call setline ( 1 , 'fooo1' ) 2067let g: CallCount = 0 2068exe "normal! Gof\<C-N>oo\<bs>\<c-r>=complete_info([\"items\", \"selected\"])\<cr>" 2069call assert_equal ( 'fo{''selected'': -1, ''items'': [{''word'': ''fooo1'', ''menu'': '''', ' 2070 \. '''kind_hlgroup'': '''', ''abbr_hlgroup'': '''', ''user_data'': '''', ''info'': '''', ''kind'': '''', ''abbr'': ''''}]}' , 2071 \getline ( 2 )) 2072call assert_equal ( 3 , g: CallCount ) 2073bw ! 2074 2075set complete & completeopt & 2076delfunc CompleteFail 2077delfunc CompleteFailIntermittent 2078endfunc 2079 2080" Select items before they are removed by refresh:always 2081func Test_cpt_select_item_refresh_always () 2082 2083func CompleteMenuWords () 2084let info = complete_info ([ "items" , "selected" ]) 2085call map ( info . items , { _ , v ->v . word }) 2086return info 2087endfunc 2088 2089func ! CompleteItemsSelect ( compl , findstart , base ) 2090if a: findstart 2091return col ( '.' ) - 1 2092endif 2093let g: CallCount += 1 2094if g: CallCount == 2 2095return #{ words : a: compl , refresh : 'always' } 2096endif 2097let res = [[], [ 'fo' , 'foobar' ], [], [ 'foo1' , 'foo2' ]] 2098return #{ words : res [ g: CallCount ], refresh : 'always' } 2099endfunc 2100 2101new 2102set complete = .,Ffunction('CompleteItemsSelect'\\,\ [[]]) 2103call setline ( 1 , "foobarbar" ) 2104let g: CallCount = 0 2105exe "normal! Gof\<c-n>\<c-n>\<c-r>=CompleteMenuWords()\<cr>" 2106call assert_equal ( 'fo{''selected'': 1, ''items'': [''foobarbar'', ''fo'', ''foobar'']}' , getline ( 2 )) 2107call assert_equal ( 1 , g: CallCount ) 2108 %d 2109call setline ( 1 , "foobarbar" ) 2110let g: CallCount = 0 2111exe "normal! Gof\<c-p>\<c-p>\<c-p>\<c-r>=CompleteMenuWords()\<cr>" 2112call assert_equal ( 'fo{''selected'': 0, ''items'': [''fo'', ''foobar'', ''foobarbar'']}' , getline ( 2 )) 2113call assert_equal ( 1 , g: CallCount ) 2114 %d 2115call setline ( 1 , "foobarbar" ) 2116let g: CallCount = 0 2117exe "normal! Gof\<c-n>\<c-n>o\<c-r>=CompleteMenuWords()\<cr>" 2118call assert_equal ( 'foo{''selected'': -1, ''items'': []}' , getline ( 2 )) 2119call assert_equal ( 1 , g: CallCount ) 2120 %d 2121call setline ( 1 , "foobarbar" ) 2122let g: CallCount = 0 2123exe "normal! Gof\<c-n>\<c-n>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2124call assert_equal ( 'f{''selected'': -1, ''items'': [''foobarbar'']}' , getline ( 2 )) 2125call assert_equal ( 2 , g: CallCount ) 2126 %d 2127call setline ( 1 , "foobarbar" ) 2128let g: CallCount = 0 2129exe "normal! Gof\<c-p>\<c-p>\<c-p>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2130call assert_equal ( 'f{''selected'': -1, ''items'': [''foobarbar'']}' , getline ( 2 )) 2131call assert_equal ( 2 , g: CallCount ) 2132 2133 %d 2134set complete = .,Ffunction('CompleteItemsSelect'\\,\ [['foonext']]) 2135call setline ( 1 , "foobarbar" ) 2136let g: CallCount = 0 2137exe "normal! Gof\<c-n>\<c-n>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2138call assert_equal ( 'f{''selected'': -1, ''items'': [''foobarbar'', ''foonext'']}' , getline ( 2 )) 2139call assert_equal ( 2 , g: CallCount ) 2140 %d 2141call setline ( 1 , "foobarbar" ) 2142let g: CallCount = 0 2143exe "normal! Gof\<c-p>\<c-p>\<c-p>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2144call assert_equal ( 'f{''selected'': -1, ''items'': [''foonext'', ''foobarbar'']}' , getline ( 2 )) 2145call assert_equal ( 2 , g: CallCount ) 2146 2147 %d 2148call setline ( 1 , "foob" ) 2149let g: CallCount = 0 2150exe "normal! Gof\<c-n>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2151call assert_equal ( 'foo{''selected'': 0, ''items'': [''foob'', ''foonext'']}' , getline ( 2 )) 2152call assert_equal ( 2 , g: CallCount ) 2153 %d 2154call setline ( 1 , "foob" ) 2155let g: CallCount = 0 2156exe "normal! Gof\<c-n>\<bs>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2157call assert_equal ( 'fo{''selected'': 0, ''items'': [''foob'', ''foo1'', ''foo2'']}' , getline ( 2 )) 2158call assert_equal ( 3 , g: CallCount ) 2159 2160 %d 2161call setline ( 1 , "foob" ) 2162let g: CallCount = 0 2163exe "normal! Gof\<c-p>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2164call assert_equal ( 'foo{''selected'': 1, ''items'': [''foonext'', ''foob'']}' , getline ( 2 )) 2165call assert_equal ( 2 , g: CallCount ) 2166 %d 2167call setline ( 1 , "foob" ) 2168let g: CallCount = 0 2169exe "normal! Gof\<c-p>\<bs>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2170call assert_equal ( 'fo{''selected'': 2, ''items'': [''foo1'', ''foo2'', ''foob'']}' , getline ( 2 )) 2171call assert_equal ( 3 , g: CallCount ) 2172 2173 %d 2174set complete = .,Ffunction('CompleteItemsSelect'\\,\ [['fo'\\,\ 'foonext']]) 2175call setline ( 1 , "foobarbar" ) 2176let g: CallCount = 0 2177exe "normal! Gof\<c-n>\<c-n>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2178call assert_equal ( 'f{''selected'': -1, ''items'': [''foobarbar'', ''fo'', ''foonext'']}' , getline ( 2 )) 2179call assert_equal ( 2 , g: CallCount ) 2180 %d 2181call setline ( 1 , "foobarbar" ) 2182let g: CallCount = 0 2183exe "normal! Gof\<c-p>\<c-p>\<c-p>\<bs>\<c-r>=CompleteMenuWords()\<cr>" 2184call assert_equal ( 'f{''selected'': -1, ''items'': [''fo'', ''foonext'', ''foobarbar'']}' , getline ( 2 )) 2185call assert_equal ( 2 , g: CallCount ) 2186bw ! 2187 2188set complete & 2189delfunc CompleteMenuWords 2190delfunc CompleteItemsSelect 2191endfunc 2192 2193" Test two functions together, each returning refresh:always 2194func Test_cpt_multi_func_refresh_always () 2195 2196func CompleteMenuMatches () 2197let info = complete_info ([ "matches" , "selected" ]) 2198call map ( info . matches , { _ , v ->v . word }) 2199return info 2200endfunc 2201 2202func ! CompleteItems1 ( findstart , base ) 2203if a: findstart 2204return col ( '.' ) - 1 2205endif 2206let g: CallCount1 += 1 2207let res = [[], [], [ 'foo1' , 'foobar1' ], [], [ 'foo11' , 'foo12' ], [], [ 'foo13' , 'foo14' ]] 2208return #{ words : res [ g: CallCount1 ], refresh : 'always' } 2209endfunc 2210 2211func ! CompleteItems2 ( findstart , base ) 2212if a: findstart 2213return col ( '.' ) - 1 2214endif 2215let g: CallCount2 += 1 2216let res = [[], [], [], [ 'foo2' , 'foobar2' ], [ 'foo21' , 'foo22' ], [ 'foo23' ], []] 2217return #{ words : res [ g: CallCount2 ], refresh : 'always' } 2218endfunc 2219 2220set complete = 2221exe "normal! if\<C-N>\<c-r>=CompleteMenuMatches()\<cr>" 2222" \x0e is <c-n> 2223call assert_equal ( "f\x0e" . '{''matches'': [], ''selected'': -1}' , getline ( 1 )) 2224 2225set completeopt = menuone,noselect 2226set complete = FCompleteItems1,FCompleteItems2 2227 2228new 2229let g: CallCount1 = 0 2230let g: CallCount2 = 0 2231exe "normal! if\<c-n>o\<c-n>o\<c-r>=CompleteMenuMatches()\<cr>" 2232call assert_equal ( 'foo{''matches'': [''foo2'', ''foobar2''], ''selected'': -1}' , getline ( 1 )) 2233call assert_equal ( 3 , g: CallCount1 ) 2234call assert_equal ( 3 , g: CallCount2 ) 2235 %d 2236let g: CallCount1 = 0 2237let g: CallCount2 = 0 2238exe "normal! if\<c-p>o\<c-p>o\<c-r>=CompleteMenuMatches()\<cr>" 2239call assert_equal ( 'foo{''matches'': [''foo2'', ''foobar2''], ''selected'': -1}' , getline ( 1 )) 2240call assert_equal ( 3 , g: CallCount1 ) 2241call assert_equal ( 3 , g: CallCount2 ) 2242 %d 2243let g: CallCount1 = 0 2244let g: CallCount2 = 0 2245exe "normal! if\<c-p>\<c-r>=CompleteMenuMatches()\<cr>" 2246call assert_equal ( 'f{''matches'': [], ''selected'': -1}' , getline ( 1 )) 2247call assert_equal ( 1 , g: CallCount1 ) 2248call assert_equal ( 1 , g: CallCount2 ) 2249 %d 2250let g: CallCount1 = 1 2251let g: CallCount2 = 1 2252exe "normal! if\<c-n>\<c-r>=CompleteMenuMatches()\<cr>" 2253call assert_equal ( 'f{''matches'': [''foo1'', ''foobar1''], ''selected'': -1}' , getline ( 1 )) 2254call assert_equal ( 2 , g: CallCount2 ) 2255call assert_equal ( 2 , g: CallCount2 ) 2256 %d 2257let g: CallCount1 = 1 2258let g: CallCount2 = 1 2259exe "normal! if\<c-n>o\<c-r>=CompleteMenuMatches()\<cr>" 2260call assert_equal ( 'fo{''matches'': [''foo2'', ''foobar2''], ''selected'': -1}' , getline ( 1 )) 2261call assert_equal ( 3 , g: CallCount2 ) 2262call assert_equal ( 3 , g: CallCount2 ) 2263 %d 2264let g: CallCount1 = 1 2265let g: CallCount2 = 1 2266exe "normal! if\<c-p>o\<c-r>=CompleteMenuMatches()\<cr>" 2267call assert_equal ( 'fo{''matches'': [''foo2'', ''foobar2''], ''selected'': -1}' , getline ( 1 )) 2268call assert_equal ( 3 , g: CallCount2 ) 2269call assert_equal ( 3 , g: CallCount2 ) 2270 %d 2271let g: CallCount1 = 1 2272let g: CallCount2 = 1 2273exe "normal! if\<c-n>oo\<c-r>=CompleteMenuMatches()\<cr>" 2274call assert_equal ( 'foo{''matches'': [''foo11'', ''foo12'', ''foo21'', ''foo22''], ''selected'': -1}' , getline ( 1 )) 2275call assert_equal ( 4 , g: CallCount2 ) 2276call assert_equal ( 4 , g: CallCount2 ) 2277 %d 2278let g: CallCount1 = 1 2279let g: CallCount2 = 1 2280exe "normal! if\<c-n>oo\<bs>\<c-r>=CompleteMenuMatches()\<cr>" 2281call assert_equal ( 'fo{''matches'': [''foo23''], ''selected'': -1}' , getline ( 1 )) 2282call assert_equal ( 5 , g: CallCount2 ) 2283call assert_equal ( 5 , g: CallCount2 ) 2284 %d 2285let g: CallCount1 = 1 2286let g: CallCount2 = 1 2287exe "normal! if\<c-p>oo\<bs>\<c-r>=CompleteMenuMatches()\<cr>" 2288call assert_equal ( 'fo{''matches'': [''foo23''], ''selected'': -1}' , getline ( 1 )) 2289call assert_equal ( 5 , g: CallCount2 ) 2290call assert_equal ( 5 , g: CallCount2 ) 2291 %d 2292let g: CallCount1 = 1 2293let g: CallCount2 = 1 2294exe "normal! if\<c-n>oo\<bs>o\<c-r>=CompleteMenuMatches()\<cr>" 2295call assert_equal ( 'foo{''matches'': [''foo13'', ''foo14''], ''selected'': -1}' , getline ( 1 )) 2296call assert_equal ( 6 , g: CallCount2 ) 2297call assert_equal ( 6 , g: CallCount2 ) 2298bw ! 2299 2300set complete & completeopt & 2301delfunc CompleteMenuMatches 2302delfunc CompleteItems1 2303delfunc CompleteItems2 2304endfunc 2305 2306" Test for completing from a thesaurus file without read permission 2307func Test_complete_unreadable_thesaurus_file () 2308CheckUnix 2309CheckNotRoot 2310 2311call writefile ([ 'about' , 'above' ], 'Xfile' ) 2312call setfperm ( 'Xfile' , '---r--r--' ) 2313new 2314set complete = sXfile 2315exe "normal! ia\<C-P>" 2316call assert_equal ( 'a' , getline ( 1 )) 2317bw ! 2318call delete ( 'Xfile' ) 2319set complete & 2320endfunc 2321 2322" Test to ensure 'Scanning...' messages are not recorded in messages history 2323func Test_z1_complete_no_history () 2324new 2325messages clear 2326let currmess = execute ( 'messages' ) 2327setlocal dictionary = README.txt 2328exe "normal owh\<C-X>\<C-K>" 2329exe "normal owh\<C-N>" 2330call assert_equal ( currmess , execute ( 'messages' )) 2331bwipe ! 2332endfunc 2333 2334" A mapping is not used for the key after CTRL-X. 2335func Test_no_mapping_for_ctrl_x_key () 2336new 2337inoremap <buffer> < C-K > <Cmd> let was_mapped = 'yes'<CR> 2338setlocal dictionary = README.txt 2339call feedkeys ( "aexam\<C-X>\<C-K> " , 'xt' ) 2340call assert_equal ( 'example ' , getline ( 1 )) 2341call assert_false ( exists ( 'was_mapped' )) 2342bwipe ! 2343endfunc 2344 2345" Test for different ways of setting a function in 'complete' option 2346func Test_cpt_func_callback () 2347func CompleteFunc1 ( callnr , findstart , base ) 2348call add ( g: CompleteFunc1Args , [ a: callnr , a: findstart , a: base ]) 2349return a: findstart ? 0 : [] 2350endfunc 2351func CompleteFunc2 ( findstart , base ) 2352call add ( g: CompleteFunc2Args , [ a: findstart , a: base ]) 2353return a: findstart ? 0 : [] 2354endfunc 2355 2356let lines =<< trim END 2357#" Test for using a global function name 2358set complete=Fg:CompleteFunc2 2359new 2360call setline(1, 'global') 2361LET g:CompleteFunc2Args = [] 2362call feedkeys("A\<C-N>\<Esc>", 'x') 2363call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args) 2364set complete& 2365bw! 2366 2367#" Test for using a function() 2368set complete=Ffunction('g:CompleteFunc1'\\,\ [10]) 2369new 2370call setline(1, 'one') 2371LET g:CompleteFunc1Args = [] 2372call feedkeys("A\<C-N>\<Esc>", 'x') 2373call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args) 2374set complete& 2375bw! 2376 2377#" Using a funcref variable 2378set complete=Ffuncref('g:CompleteFunc1'\\,\ [11]) 2379new 2380call setline(1, 'two') 2381LET g:CompleteFunc1Args = [] 2382call feedkeys("A\<C-N>\<Esc>", 'x') 2383call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args) 2384set complete& 2385bw! 2386 2387END 2388call CheckLegacyAndVim9Success ( lines ) 2389 2390" Test for using a script-local function name 2391func s: CompleteFunc3 ( findstart , base ) 2392call add ( g: CompleteFunc3Args , [ a: findstart , a: base ]) 2393return a: findstart ? 0 : [] 2394endfunc 2395set complete = Fs:CompleteFunc3 2396new 2397call setline ( 1 , 'script1' ) 2398let g: CompleteFunc3Args = [] 2399call feedkeys ( "A\<C-N>\<Esc>" , 'x' ) 2400call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: CompleteFunc3Args ) 2401set complete & 2402bw ! 2403 2404let & complete = 'Fs:CompleteFunc3' 2405new 2406call setline ( 1 , 'script2' ) 2407let g: CompleteFunc3Args = [] 2408call feedkeys ( "A\<C-N>\<Esc>" , 'x' ) 2409call assert_equal ([[ 1 , '' ], [ 0 , 'script2' ]], g: CompleteFunc3Args ) 2410bw ! 2411delfunc s:CompleteFunc3 2412set complete & 2413 2414" In Vim9 script s: can be omitted 2415let lines =<< trim END 2416vim9script 2417var CompleteFunc4Args = [] 2418def CompleteFunc4(findstart: bool, base: string): any 2419add(CompleteFunc4Args, [findstart, base]) 2420return findstart ? 0 : [] 2421enddef 2422set complete=FCompleteFunc4 2423new 2424setline(1, 'script1') 2425feedkeys("A\<C-N>\<Esc>", 'x') 2426assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args) 2427set complete& 2428bw! 2429END 2430call CheckScriptSuccess ( lines ) 2431 2432" Vim9 tests 2433let lines =<< trim END 2434vim9script 2435 2436def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any 2437add(g:Vim9completeFuncArgs, [callnr, findstart, base]) 2438return findstart ? 0 : [] 2439enddef 2440 2441# Test for using a def function with completefunc 2442set complete=Ffunction('Vim9CompleteFunc'\\,\ [60]) 2443new | only 2444setline(1, 'one') 2445g:Vim9completeFuncArgs = [] 2446feedkeys("A\<C-N>\<Esc>", 'x') 2447assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) 2448bw! 2449 2450# Test for using a global function name 2451&complete = 'Fg:CompleteFunc2' 2452new | only 2453setline(1, 'two') 2454g:CompleteFunc2Args = [] 2455feedkeys("A\<C-N>\<Esc>", 'x') 2456assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args) 2457bw! 2458 2459# Test for using a script-local function name 2460def LocalCompleteFunc(findstart: number, base: string): any 2461add(g:LocalCompleteFuncArgs, [findstart, base]) 2462return findstart ? 0 : [] 2463enddef 2464&complete = 'FLocalCompleteFunc' 2465new | only 2466setline(1, 'three') 2467g:LocalCompleteFuncArgs = [] 2468feedkeys("A\<C-N>\<Esc>", 'x') 2469assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs) 2470bw! 2471END 2472call CheckScriptSuccess ( lines ) 2473 2474" cleanup 2475set completefunc & complete & 2476delfunc CompleteFunc1 2477delfunc CompleteFunc2 2478unlet g: CompleteFunc1Args g: CompleteFunc2Args 2479 %bw ! 2480endfunc 2481 2482" Test for different ways of setting the 'completefunc' option 2483func Test_completefunc_callback () 2484func CompleteFunc1 ( callnr , findstart , base ) 2485call add ( g: CompleteFunc1Args , [ a: callnr , a: findstart , a: base ]) 2486return a: findstart ? 0 : [] 2487endfunc 2488func CompleteFunc2 ( findstart , base ) 2489call add ( g: CompleteFunc2Args , [ a: findstart , a: base ]) 2490return a: findstart ? 0 : [] 2491endfunc 2492 2493let lines =<< trim END 2494#" Test for using a global function name 2495LET &completefunc = 'g:CompleteFunc2' 2496new 2497call setline(1, 'global') 2498LET g:CompleteFunc2Args = [] 2499call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2500call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args) 2501bw! 2502 2503#" Test for using a function() 2504set completefunc=function('g:CompleteFunc1',\ [10]) 2505new 2506call setline(1, 'one') 2507LET g:CompleteFunc1Args = [] 2508call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2509call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args) 2510bw! 2511 2512#" Using a funcref variable to set 'completefunc' 2513VAR Fn = function('g:CompleteFunc1', [11]) 2514LET &completefunc = Fn 2515new 2516call setline(1, 'two') 2517LET g:CompleteFunc1Args = [] 2518call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2519call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args) 2520bw! 2521 2522#" Using string(funcref_variable) to set 'completefunc' 2523LET Fn = function('g:CompleteFunc1', [12]) 2524LET &completefunc = string(Fn) 2525new 2526call setline(1, 'two') 2527LET g:CompleteFunc1Args = [] 2528call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2529call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args) 2530bw! 2531 2532#" Test for using a funcref() 2533set completefunc=funcref('g:CompleteFunc1',\ [13]) 2534new 2535call setline(1, 'three') 2536LET g:CompleteFunc1Args = [] 2537call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2538call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args) 2539bw! 2540 2541#" Using a funcref variable to set 'completefunc' 2542LET Fn = funcref('g:CompleteFunc1', [14]) 2543LET &completefunc = Fn 2544new 2545call setline(1, 'four') 2546LET g:CompleteFunc1Args = [] 2547call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2548call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args) 2549bw! 2550 2551#" Using a string(funcref_variable) to set 'completefunc' 2552LET Fn = funcref('g:CompleteFunc1', [15]) 2553LET &completefunc = string(Fn) 2554new 2555call setline(1, 'four') 2556LET g:CompleteFunc1Args = [] 2557call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2558call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args) 2559bw! 2560 2561#" Test for using a lambda function with set 2562VAR optval = "LSTART a, b LMIDDLE CompleteFunc1(16, a, b) LEND" 2563LET optval = substitute(optval, ' ', '\\ ', 'g') 2564exe "set completefunc=" .. optval 2565new 2566call setline(1, 'five') 2567LET g:CompleteFunc1Args = [] 2568call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2569call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args) 2570bw! 2571 2572#" Set 'completefunc' to a lambda expression 2573LET &completefunc = LSTART a, b LMIDDLE CompleteFunc1(17, a, b) LEND 2574new 2575call setline(1, 'six') 2576LET g:CompleteFunc1Args = [] 2577call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2578call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args) 2579bw! 2580 2581#" Set 'completefunc' to string(lambda_expression) 2582LET &completefunc = 'LSTART a, b LMIDDLE CompleteFunc1(18, a, b) LEND' 2583new 2584call setline(1, 'six') 2585LET g:CompleteFunc1Args = [] 2586call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2587call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args) 2588bw! 2589 2590#" Set 'completefunc' to a variable with a lambda expression 2591VAR Lambda = LSTART a, b LMIDDLE CompleteFunc1(19, a, b) LEND 2592LET &completefunc = Lambda 2593new 2594call setline(1, 'seven') 2595LET g:CompleteFunc1Args = [] 2596call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2597call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args) 2598bw! 2599 2600#" Set 'completefunc' to a string(variable with a lambda expression) 2601LET Lambda = LSTART a, b LMIDDLE CompleteFunc1(20, a, b) LEND 2602LET &completefunc = string(Lambda) 2603new 2604call setline(1, 'seven') 2605LET g:CompleteFunc1Args = [] 2606call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2607call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args) 2608bw! 2609 2610#" Test for using a lambda function with incorrect return value 2611LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND 2612LET &completefunc = Lambda 2613new 2614call setline(1, 'eight') 2615call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2616bw! 2617 2618#" Test for clearing the 'completefunc' option 2619set completefunc='' 2620set completefunc& 2621call assert_fails("set completefunc=function('abc')", "E700:") 2622call assert_fails("set completefunc=funcref('abc')", "E700:") 2623 2624#" set 'completefunc' to a non-existing function 2625set completefunc=CompleteFunc2 2626call setline(1, 'five') 2627call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:') 2628call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:') 2629LET g:CompleteFunc2Args = [] 2630call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2631call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args) 2632bw! 2633 2634#" :setlocal and :setglobal 2635set complete=F completefunc& 2636setlocal completefunc=function('g:CompleteFunc1',\ [22]) 2637call setline(1, 'sun') 2638LET g:CompleteFunc1Args = [] 2639call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2640call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:CompleteFunc1Args) 2641LET g:CompleteFunc1Args = [] 2642call feedkeys("A\<C-N>\<Esc>", 'x') 2643call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:CompleteFunc1Args) 2644new 2645call setline(1, 'sun') 2646LET g:CompleteFunc1Args = [] 2647call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E764:') 2648call assert_equal([], g:CompleteFunc1Args) 2649LET g:CompleteFunc1Args = [] 2650call feedkeys("A\<C-N>\<Esc>", 'x') 2651call assert_equal([], g:CompleteFunc1Args) 2652bw! 2653setglobal completefunc=function('g:CompleteFunc1',\ [23]) 2654call setline(1, 'sun') 2655LET g:CompleteFunc1Args = [] 2656call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2657call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:CompleteFunc1Args) 2658LET g:CompleteFunc1Args = [] 2659call feedkeys("A\<C-N>\<Esc>", 'x') 2660call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:CompleteFunc1Args) 2661setlocal completefunc& 2662call setline(1, 'sun') 2663LET g:CompleteFunc1Args = [] 2664call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E764:') 2665call assert_equal([], g:CompleteFunc1Args) 2666LET g:CompleteFunc1Args = [] 2667call feedkeys("A\<C-N>\<Esc>", 'x') 2668call assert_equal([], g:CompleteFunc1Args) 2669new 2670call setline(1, 'sun') 2671LET g:CompleteFunc1Args = [] 2672call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2673call assert_equal([[23, 1, ''], [23, 0, 'sun']], g:CompleteFunc1Args) 2674LET g:CompleteFunc1Args = [] 2675call feedkeys("A\<C-N>\<Esc>", 'x') 2676call assert_equal([[23, 1, ''], [23, 0, 'sun']], g:CompleteFunc1Args) 2677set complete& completefunc& 2678:%bw! 2679END 2680call CheckLegacyAndVim9Success ( lines ) 2681 2682" Test for using a script-local function name 2683func s: CompleteFunc3 ( findstart , base ) 2684call add ( g: CompleteFunc3Args , [ a: findstart , a: base ]) 2685return a: findstart ? 0 : [] 2686endfunc 2687set completefunc = s:CompleteFunc3 2688new 2689call setline ( 1 , 'script1' ) 2690let g: CompleteFunc3Args = [] 2691call feedkeys ( "A\<C-X>\<C-U>\<Esc>" , 'x' ) 2692call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: CompleteFunc3Args ) 2693bw ! 2694 2695let & completefunc = 's:CompleteFunc3' 2696new 2697call setline ( 1 , 'script2' ) 2698let g: CompleteFunc3Args = [] 2699call feedkeys ( "A\<C-X>\<C-U>\<Esc>" , 'x' ) 2700call assert_equal ([[ 1 , '' ], [ 0 , 'script2' ]], g: CompleteFunc3Args ) 2701bw ! 2702delfunc s:CompleteFunc3 2703 2704" In Vim9 script s: can be omitted 2705let lines =<< trim END 2706vim9script 2707var CompleteFunc4Args = [] 2708def CompleteFunc4(findstart: bool, base: string): any 2709add(CompleteFunc4Args, [findstart, base]) 2710return findstart ? 0 : [] 2711enddef 2712set completefunc=CompleteFunc4 2713new 2714setline(1, 'script1') 2715feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2716assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args) 2717bw! 2718END 2719call CheckScriptSuccess ( lines ) 2720 2721" invalid return value 2722let & completefunc = { a ->'abc' } 2723call feedkeys ( "A\<C-X>\<C-U>\<Esc>" , 'x' ) 2724 2725" Using Vim9 lambda expression in legacy context should fail 2726set completefunc = (a,\ b)\ =>\ CompleteFunc1(21,\ a,\ b) 2727new |only 2728let g: CompleteFunc1Args = [] 2729call assert_fails ( 'call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")' , 'E117:' ) 2730call assert_equal ([], g: CompleteFunc1Args ) 2731 2732" set 'completefunc' to a partial with dict. This used to cause a crash. 2733func SetCompleteFunc () 2734let params = { 'complete' : function ( 'g:DictCompleteFunc' )} 2735let & completefunc = params . complete 2736endfunc 2737func g: DictCompleteFunc ( _ ) dict 2738endfunc 2739call SetCompleteFunc () 2740new 2741call SetCompleteFunc () 2742bw 2743call test_garbagecollect_now () 2744new 2745set completefunc = 2746wincmd w 2747set completefunc = 2748 %bw ! 2749delfunc g:DictCompleteFunc 2750delfunc SetCompleteFunc 2751 2752" Vim9 tests 2753let lines =<< trim END 2754vim9script 2755 2756def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any 2757add(g:Vim9completeFuncArgs, [callnr, findstart, base]) 2758return findstart ? 0 : [] 2759enddef 2760 2761# Test for using a def function with completefunc 2762set completefunc=function('Vim9CompleteFunc',\ [60]) 2763new | only 2764setline(1, 'one') 2765g:Vim9completeFuncArgs = [] 2766feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2767assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs) 2768bw! 2769 2770# Test for using a global function name 2771&completefunc = g:CompleteFunc2 2772new | only 2773setline(1, 'two') 2774g:CompleteFunc2Args = [] 2775feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2776assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args) 2777bw! 2778 2779# Test for using a script-local function name 2780def LocalCompleteFunc(findstart: number, base: string): any 2781add(g:LocalCompleteFuncArgs, [findstart, base]) 2782return findstart ? 0 : [] 2783enddef 2784&completefunc = LocalCompleteFunc 2785new | only 2786setline(1, 'three') 2787g:LocalCompleteFuncArgs = [] 2788feedkeys("A\<C-X>\<C-U>\<Esc>", 'x') 2789assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs) 2790bw! 2791END 2792call CheckScriptSuccess ( lines ) 2793 2794" cleanup 2795set completefunc & 2796delfunc CompleteFunc1 2797delfunc CompleteFunc2 2798unlet g: CompleteFunc1Args g: CompleteFunc2Args 2799 %bw ! 2800endfunc 2801 2802" Test for different ways of setting the 'omnifunc' option 2803func Test_omnifunc_callback () 2804func OmniFunc1 ( callnr , findstart , base ) 2805call add ( g: OmniFunc1Args , [ a: callnr , a: findstart , a: base ]) 2806return a: findstart ? 0 : [] 2807endfunc 2808func OmniFunc2 ( findstart , base ) 2809call add ( g: OmniFunc2Args , [ a: findstart , a: base ]) 2810return a: findstart ? 0 : [] 2811endfunc 2812 2813let lines =<< trim END 2814#" Test for using a function name 2815LET &omnifunc = 'g:OmniFunc2' 2816new 2817call setline(1, 'zero') 2818LET g:OmniFunc2Args = [] 2819call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2820call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args) 2821bw! 2822 2823#" Test for using a function() 2824set omnifunc=function('g:OmniFunc1',\ [10]) 2825new 2826call setline(1, 'one') 2827LET g:OmniFunc1Args = [] 2828call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2829call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args) 2830bw! 2831 2832#" Using a funcref variable to set 'omnifunc' 2833VAR Fn = function('g:OmniFunc1', [11]) 2834LET &omnifunc = Fn 2835new 2836call setline(1, 'two') 2837LET g:OmniFunc1Args = [] 2838call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2839call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args) 2840bw! 2841 2842#" Using a string(funcref_variable) to set 'omnifunc' 2843LET Fn = function('g:OmniFunc1', [12]) 2844LET &omnifunc = string(Fn) 2845new 2846call setline(1, 'two') 2847LET g:OmniFunc1Args = [] 2848call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2849call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args) 2850bw! 2851 2852#" Test for using a funcref() 2853set omnifunc=funcref('g:OmniFunc1',\ [13]) 2854new 2855call setline(1, 'three') 2856LET g:OmniFunc1Args = [] 2857call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2858call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args) 2859bw! 2860 2861#" Use let to set 'omnifunc' to a funcref 2862LET Fn = funcref('g:OmniFunc1', [14]) 2863LET &omnifunc = Fn 2864new 2865call setline(1, 'four') 2866LET g:OmniFunc1Args = [] 2867call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2868call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args) 2869bw! 2870 2871#" Using a string(funcref) to set 'omnifunc' 2872LET Fn = funcref("g:OmniFunc1", [15]) 2873LET &omnifunc = string(Fn) 2874new 2875call setline(1, 'four') 2876LET g:OmniFunc1Args = [] 2877call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2878call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args) 2879bw! 2880 2881#" Test for using a lambda function with set 2882VAR optval = "LSTART a, b LMIDDLE OmniFunc1(16, a, b) LEND" 2883LET optval = substitute(optval, ' ', '\\ ', 'g') 2884exe "set omnifunc=" .. optval 2885new 2886call setline(1, 'five') 2887LET g:OmniFunc1Args = [] 2888call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2889call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args) 2890bw! 2891 2892#" Set 'omnifunc' to a lambda expression 2893LET &omnifunc = LSTART a, b LMIDDLE OmniFunc1(17, a, b) LEND 2894new 2895call setline(1, 'six') 2896LET g:OmniFunc1Args = [] 2897call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2898call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args) 2899bw! 2900 2901#" Set 'omnifunc' to a string(lambda_expression) 2902LET &omnifunc = 'LSTART a, b LMIDDLE OmniFunc1(18, a, b) LEND' 2903new 2904call setline(1, 'six') 2905LET g:OmniFunc1Args = [] 2906call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2907call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args) 2908bw! 2909 2910#" Set 'omnifunc' to a variable with a lambda expression 2911VAR Lambda = LSTART a, b LMIDDLE OmniFunc1(19, a, b) LEND 2912LET &omnifunc = Lambda 2913new 2914call setline(1, 'seven') 2915LET g:OmniFunc1Args = [] 2916call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2917call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args) 2918bw! 2919 2920#" Set 'omnifunc' to a string(variable with a lambda expression) 2921LET Lambda = LSTART a, b LMIDDLE OmniFunc1(20, a, b) LEND 2922LET &omnifunc = string(Lambda) 2923new 2924call setline(1, 'seven') 2925LET g:OmniFunc1Args = [] 2926call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2927call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args) 2928bw! 2929 2930#" Test for using a lambda function with incorrect return value 2931LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND 2932LET &omnifunc = Lambda 2933new 2934call setline(1, 'eight') 2935call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2936bw! 2937 2938#" Test for clearing the 'omnifunc' option 2939set omnifunc='' 2940set omnifunc& 2941call assert_fails("set omnifunc=function('abc')", "E700:") 2942call assert_fails("set omnifunc=funcref('abc')", "E700:") 2943 2944#" set 'omnifunc' to a non-existing function 2945set omnifunc=OmniFunc2 2946call setline(1, 'nine') 2947call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:') 2948call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:') 2949LET g:OmniFunc2Args = [] 2950call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2951call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args) 2952bw! 2953 2954#" :setlocal and :setglobal 2955set complete=o omnifunc& 2956setlocal omnifunc=function('g:OmniFunc1',\ [22]) 2957call setline(1, 'sun') 2958LET g:OmniFunc1Args = [] 2959call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2960call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:OmniFunc1Args) 2961LET g:OmniFunc1Args = [] 2962call feedkeys("A\<C-N>\<Esc>", 'x') 2963call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:OmniFunc1Args) 2964new 2965call setline(1, 'sun') 2966LET g:OmniFunc1Args = [] 2967call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E764:') 2968call assert_equal([], g:OmniFunc1Args) 2969LET g:OmniFunc1Args = [] 2970call feedkeys("A\<C-N>\<Esc>", 'x') 2971call assert_equal([], g:OmniFunc1Args) 2972bw! 2973setglobal omnifunc=function('g:OmniFunc1',\ [23]) 2974call setline(1, 'sun') 2975LET g:OmniFunc1Args = [] 2976call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2977call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:OmniFunc1Args) 2978LET g:OmniFunc1Args = [] 2979call feedkeys("A\<C-N>\<Esc>", 'x') 2980call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:OmniFunc1Args) 2981setlocal omnifunc& 2982call setline(1, 'sun') 2983LET g:OmniFunc1Args = [] 2984call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E764:') 2985call assert_equal([], g:OmniFunc1Args) 2986LET g:OmniFunc1Args = [] 2987call feedkeys("A\<C-N>\<Esc>", 'x') 2988call assert_equal([], g:OmniFunc1Args) 2989new 2990call setline(1, 'sun') 2991LET g:OmniFunc1Args = [] 2992call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 2993call assert_equal([[23, 1, ''], [23, 0, 'sun']], g:OmniFunc1Args) 2994LET g:OmniFunc1Args = [] 2995call feedkeys("A\<C-N>\<Esc>", 'x') 2996call assert_equal([[23, 1, ''], [23, 0, 'sun']], g:OmniFunc1Args) 2997set complete& omnifunc& 2998:%bw! 2999END 3000call CheckLegacyAndVim9Success ( lines ) 3001 3002" Test for using a script-local function name 3003func s: OmniFunc3 ( findstart , base ) 3004call add ( g: OmniFunc3Args , [ a: findstart , a: base ]) 3005return a: findstart ? 0 : [] 3006endfunc 3007set omnifunc = s:OmniFunc3 3008new 3009call setline ( 1 , 'script1' ) 3010let g: OmniFunc3Args = [] 3011call feedkeys ( "A\<C-X>\<C-O>\<Esc>" , 'x' ) 3012call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: OmniFunc3Args ) 3013bw ! 3014 3015set complete = Fs:OmniFunc3 3016new 3017call setline ( 1 , 'script1' ) 3018let g: OmniFunc3Args = [] 3019call feedkeys ( "A\<C-N>\<Esc>" , 'x' ) 3020call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: OmniFunc3Args ) 3021bw ! 3022set complete & 3023 3024let & omnifunc = 's:OmniFunc3' 3025new 3026call setline ( 1 , 'script2' ) 3027let g: OmniFunc3Args = [] 3028call feedkeys ( "A\<C-X>\<C-O>\<Esc>" , 'x' ) 3029call assert_equal ([[ 1 , '' ], [ 0 , 'script2' ]], g: OmniFunc3Args ) 3030bw ! 3031delfunc s:OmniFunc3 3032 3033" invalid return value 3034let & omnifunc = { a ->'abc' } 3035call feedkeys ( "A\<C-X>\<C-O>\<Esc>" , 'x' ) 3036 3037" Using Vim9 lambda expression in legacy context should fail 3038set omnifunc = (a,\ b)\ =>\ OmniFunc1(21,\ a,\ b) 3039new |only 3040let g: OmniFunc1Args = [] 3041call assert_fails ( 'call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")' , 'E117:' ) 3042call assert_equal ([], g: OmniFunc1Args ) 3043 3044" set 'omnifunc' to a partial with dict. This used to cause a crash. 3045func SetOmniFunc () 3046let params = { 'omni' : function ( 'g:DictOmniFunc' )} 3047let & omnifunc = params . omni 3048endfunc 3049func g: DictOmniFunc ( _ ) dict 3050endfunc 3051call SetOmniFunc () 3052new 3053call SetOmniFunc () 3054bw 3055call test_garbagecollect_now () 3056new 3057set omnifunc = 3058wincmd w 3059set omnifunc = 3060 %bw ! 3061delfunc g:DictOmniFunc 3062delfunc SetOmniFunc 3063 3064" Vim9 tests 3065let lines =<< trim END 3066vim9script 3067 3068def Vim9omniFunc(callnr: number, findstart: number, base: string): any 3069add(g:Vim9omniFunc_Args, [callnr, findstart, base]) 3070return findstart ? 0 : [] 3071enddef 3072 3073# Test for using a def function with omnifunc 3074set omnifunc=function('Vim9omniFunc',\ [60]) 3075new | only 3076setline(1, 'one') 3077g:Vim9omniFunc_Args = [] 3078feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 3079assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args) 3080bw! 3081 3082# Test for using a global function name 3083&omnifunc = g:OmniFunc2 3084new | only 3085setline(1, 'two') 3086g:OmniFunc2Args = [] 3087feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 3088assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args) 3089bw! 3090 3091# Test for using a script-local function name 3092def LocalOmniFunc(findstart: number, base: string): any 3093add(g:LocalOmniFuncArgs, [findstart, base]) 3094return findstart ? 0 : [] 3095enddef 3096&omnifunc = LocalOmniFunc 3097new | only 3098setline(1, 'three') 3099g:LocalOmniFuncArgs = [] 3100feedkeys("A\<C-X>\<C-O>\<Esc>", 'x') 3101assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs) 3102bw! 3103END 3104call CheckScriptSuccess ( lines ) 3105 3106" cleanup 3107set omnifunc & 3108delfunc OmniFunc1 3109delfunc OmniFunc2 3110unlet g: OmniFunc1Args g: OmniFunc2Args 3111 %bw ! 3112endfunc 3113 3114" Test for different ways of setting the 'thesaurusfunc' option 3115func Test_thesaurusfunc_callback () 3116func TsrFunc1 ( callnr , findstart , base ) 3117call add ( g: TsrFunc1Args , [ a: callnr , a: findstart , a: base ]) 3118return a: findstart ? 0 : [] 3119endfunc 3120func TsrFunc2 ( findstart , base ) 3121call add ( g: TsrFunc2Args , [ a: findstart , a: base ]) 3122return a: findstart ? 0 : [ 'sunday' ] 3123endfunc 3124 3125let lines =<< trim END 3126#" Test for using a function name 3127LET &thesaurusfunc = 'g:TsrFunc2' 3128new 3129call setline(1, 'zero') 3130LET g:TsrFunc2Args = [] 3131call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3132call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args) 3133bw! 3134 3135#" Test for using a function() 3136set thesaurusfunc=function('g:TsrFunc1',\ [10]) 3137new 3138call setline(1, 'one') 3139LET g:TsrFunc1Args = [] 3140call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3141call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args) 3142bw! 3143 3144#" Using a funcref variable to set 'thesaurusfunc' 3145VAR Fn = function('g:TsrFunc1', [11]) 3146LET &thesaurusfunc = Fn 3147new 3148call setline(1, 'two') 3149LET g:TsrFunc1Args = [] 3150call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3151call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args) 3152bw! 3153 3154#" Using a string(funcref_variable) to set 'thesaurusfunc' 3155LET Fn = function('g:TsrFunc1', [12]) 3156LET &thesaurusfunc = string(Fn) 3157new 3158call setline(1, 'two') 3159LET g:TsrFunc1Args = [] 3160call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3161call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args) 3162bw! 3163 3164#" Test for using a funcref() 3165set thesaurusfunc=funcref('g:TsrFunc1',\ [13]) 3166new 3167call setline(1, 'three') 3168LET g:TsrFunc1Args = [] 3169call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3170call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args) 3171bw! 3172 3173#" Using a funcref variable to set 'thesaurusfunc' 3174LET Fn = funcref('g:TsrFunc1', [14]) 3175LET &thesaurusfunc = Fn 3176new 3177call setline(1, 'four') 3178LET g:TsrFunc1Args = [] 3179call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3180call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args) 3181bw! 3182 3183#" Using a string(funcref_variable) to set 'thesaurusfunc' 3184LET Fn = funcref('g:TsrFunc1', [15]) 3185LET &thesaurusfunc = string(Fn) 3186new 3187call setline(1, 'four') 3188LET g:TsrFunc1Args = [] 3189call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3190call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args) 3191bw! 3192 3193#" Test for using a lambda function 3194VAR optval = "LSTART a, b LMIDDLE TsrFunc1(16, a, b) LEND" 3195LET optval = substitute(optval, ' ', '\\ ', 'g') 3196exe "set thesaurusfunc=" .. optval 3197new 3198call setline(1, 'five') 3199LET g:TsrFunc1Args = [] 3200call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3201call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args) 3202bw! 3203 3204#" Test for using a lambda function with set 3205LET &thesaurusfunc = LSTART a, b LMIDDLE TsrFunc1(17, a, b) LEND 3206new 3207call setline(1, 'six') 3208LET g:TsrFunc1Args = [] 3209call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3210call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args) 3211bw! 3212 3213#" Set 'thesaurusfunc' to a string(lambda expression) 3214LET &thesaurusfunc = 'LSTART a, b LMIDDLE TsrFunc1(18, a, b) LEND' 3215new 3216call setline(1, 'six') 3217LET g:TsrFunc1Args = [] 3218call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3219call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args) 3220bw! 3221 3222#" Set 'thesaurusfunc' to a variable with a lambda expression 3223VAR Lambda = LSTART a, b LMIDDLE TsrFunc1(19, a, b) LEND 3224LET &thesaurusfunc = Lambda 3225new 3226call setline(1, 'seven') 3227LET g:TsrFunc1Args = [] 3228call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3229call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args) 3230bw! 3231 3232#" Set 'thesaurusfunc' to a string(variable with a lambda expression) 3233LET Lambda = LSTART a, b LMIDDLE TsrFunc1(20, a, b) LEND 3234LET &thesaurusfunc = string(Lambda) 3235new 3236call setline(1, 'seven') 3237LET g:TsrFunc1Args = [] 3238call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3239call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args) 3240bw! 3241 3242#" Test for using a lambda function with incorrect return value 3243LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND 3244LET &thesaurusfunc = Lambda 3245new 3246call setline(1, 'eight') 3247call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3248bw! 3249 3250#" Test for clearing the 'thesaurusfunc' option 3251set thesaurusfunc='' 3252set thesaurusfunc& 3253call assert_fails("set thesaurusfunc=function('abc')", "E700:") 3254call assert_fails("set thesaurusfunc=funcref('abc')", "E700:") 3255 3256#" set 'thesaurusfunc' to a non-existing function 3257set thesaurusfunc=TsrFunc2 3258call setline(1, 'ten') 3259call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:') 3260call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:') 3261LET g:TsrFunc2Args = [] 3262call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3263call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args) 3264bw! 3265 3266#" Use a buffer-local value and a global value 3267set thesaurusfunc& 3268setlocal thesaurusfunc=function('g:TsrFunc1',\ [22]) 3269call setline(1, 'sun') 3270LET g:TsrFunc1Args = [] 3271call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") 3272call assert_equal('sun', getline(1)) 3273call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) 3274new 3275call setline(1, 'sun') 3276LET g:TsrFunc1Args = [] 3277" Nvim: message is an actual error rather than message with error highlight 3278silent! call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") 3279call assert_equal('sun', getline(1)) 3280call assert_equal([], g:TsrFunc1Args) 3281set thesaurusfunc=function('g:TsrFunc1',\ [23]) 3282wincmd w 3283call setline(1, 'sun') 3284LET g:TsrFunc1Args = [] 3285call feedkeys("A\<C-X>\<C-T>\<Esc>", "x") 3286call assert_equal('sun', getline(1)) 3287call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args) 3288:%bw! 3289END 3290call CheckLegacyAndVim9Success ( lines ) 3291 3292" Test for using a script-local function name 3293func s: TsrFunc3 ( findstart , base ) 3294call add ( g: TsrFunc3Args , [ a: findstart , a: base ]) 3295return a: findstart ? 0 : [] 3296endfunc 3297 3298set tsrfu = s:TsrFunc3 3299new 3300call setline ( 1 , 'script1' ) 3301let g: TsrFunc3Args = [] 3302call feedkeys ( "A\<C-X>\<C-T>\<Esc>" , 'x' ) 3303call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: TsrFunc3Args ) 3304bw ! 3305 3306let & tsrfu = 's:TsrFunc3' 3307new 3308call setline ( 1 , 'script2' ) 3309let g: TsrFunc3Args = [] 3310call feedkeys ( "A\<C-X>\<C-T>\<Esc>" , 'x' ) 3311call assert_equal ([[ 1 , '' ], [ 0 , 'script2' ]], g: TsrFunc3Args ) 3312bw ! 3313 3314new |only 3315set thesaurusfunc = 3316setlocal thesaurusfunc = NoSuchFunc 3317setglobal thesaurusfunc=s:TsrFunc3 3318" A callback-option (e.g. 'findfunc') stores funcref as its unambiguous <SNR> form. 3319let tsrfunc3 = expand ( '<SID>' ) .. 'TsrFunc3' 3320call assert_equal ( 'NoSuchFunc' , & thesaurusfunc ) 3321call assert_equal ( 'NoSuchFunc' , & l: thesaurusfunc ) 3322call assert_equal ( tsrfunc3 , & g: thesaurusfunc ) 3323new |only 3324call assert_equal ( tsrfunc3 , & thesaurusfunc ) 3325call assert_equal ( tsrfunc3 , & g: thesaurusfunc ) 3326call assert_equal ( '' , & l: thesaurusfunc ) 3327call setline ( 1 , 'script1' ) 3328let g: TsrFunc3Args = [] 3329call feedkeys ( "A\<C-X>\<C-T>\<Esc>" , 'x' ) 3330call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: TsrFunc3Args ) 3331bw ! 3332 3333new |only 3334set thesaurusfunc = 3335setlocal thesaurusfunc = NoSuchFunc 3336set thesaurusfunc = s:TsrFunc3 3337call assert_equal ( tsrfunc3 , & thesaurusfunc ) 3338call assert_equal ( tsrfunc3 , & g: thesaurusfunc ) 3339call assert_equal ( '' , & l: thesaurusfunc ) 3340call setline ( 1 , 'script1' ) 3341let g: TsrFunc3Args = [] 3342call feedkeys ( "A\<C-X>\<C-T>\<Esc>" , 'x' ) 3343call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: TsrFunc3Args ) 3344setlocal bufhidden = wipe 3345new |only ! 3346call assert_equal ( tsrfunc3 , & thesaurusfunc ) 3347call assert_equal ( tsrfunc3 , & g: thesaurusfunc ) 3348call assert_equal ( '' , & l: thesaurusfunc ) 3349call setline ( 1 , 'script1' ) 3350let g: TsrFunc3Args = [] 3351call feedkeys ( "A\<C-X>\<C-T>\<Esc>" , 'x' ) 3352call assert_equal ([[ 1 , '' ], [ 0 , 'script1' ]], g: TsrFunc3Args ) 3353bw ! 3354 3355delfunc s:TsrFunc3 3356 3357" invalid return value 3358let & thesaurusfunc = { a ->'abc' } 3359call feedkeys ( "A\<C-X>\<C-T>\<Esc>" , 'x' ) 3360 3361" Using Vim9 lambda expression in legacy context should fail 3362set thesaurusfunc = (a,\ b)\ =>\ TsrFunc1(21,\ a,\ b) 3363new |only 3364let g: TsrFunc1Args = [] 3365call assert_fails ( 'call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")' , 'E117:' ) 3366call assert_equal ([], g: TsrFunc1Args ) 3367bw ! 3368 3369" set 'thesaurusfunc' to a partial with dict. This used to cause a crash. 3370func SetTsrFunc () 3371let params = { 'thesaurus' : function ( 'g:DictTsrFunc' )} 3372let & thesaurusfunc = params . thesaurus 3373endfunc 3374func g: DictTsrFunc ( _ ) dict 3375endfunc 3376call SetTsrFunc () 3377new 3378call SetTsrFunc () 3379bw 3380call test_garbagecollect_now () 3381new 3382set thesaurusfunc = 3383wincmd w 3384 %bw ! 3385delfunc SetTsrFunc 3386 3387" set buffer-local 'thesaurusfunc' to a partial with dict. This used to 3388" cause a crash. 3389func SetLocalTsrFunc () 3390let params = { 'thesaurus' : function ( 'g:DictTsrFunc' )} 3391let & l: thesaurusfunc = params . thesaurus 3392endfunc 3393call SetLocalTsrFunc () 3394call test_garbagecollect_now () 3395call SetLocalTsrFunc () 3396set thesaurusfunc = 3397bw ! 3398delfunc g:DictTsrFunc 3399delfunc SetLocalTsrFunc 3400 3401" Vim9 tests 3402let lines =<< trim END 3403vim9script 3404 3405def Vim9tsrFunc(callnr: number, findstart: number, base: string): any 3406add(g:Vim9tsrFunc_Args, [callnr, findstart, base]) 3407return findstart ? 0 : [] 3408enddef 3409 3410# Test for using a def function with thesaurusfunc 3411set thesaurusfunc=function('Vim9tsrFunc',\ [60]) 3412new | only 3413setline(1, 'one') 3414g:Vim9tsrFunc_Args = [] 3415feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3416assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args) 3417bw! 3418 3419# Test for using a global function name 3420&thesaurusfunc = g:TsrFunc2 3421new | only 3422setline(1, 'two') 3423g:TsrFunc2Args = [] 3424feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3425assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args) 3426bw! 3427 3428# Test for using a script-local function name 3429def LocalTsrFunc(findstart: number, base: string): any 3430add(g:LocalTsrFuncArgs, [findstart, base]) 3431return findstart ? 0 : [] 3432enddef 3433&thesaurusfunc = LocalTsrFunc 3434new | only 3435setline(1, 'three') 3436g:LocalTsrFuncArgs = [] 3437feedkeys("A\<C-X>\<C-T>\<Esc>", 'x') 3438assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs) 3439bw! 3440END 3441call CheckScriptSuccess ( lines ) 3442 3443" cleanup 3444set thesaurusfunc & 3445delfunc TsrFunc1 3446delfunc TsrFunc2 3447unlet g: TsrFunc1Args g: TsrFunc2Args 3448 %bw ! 3449endfunc 3450 3451func FooBarComplete ( findstart , base ) 3452if a: findstart 3453return col ( '.' ) - 1 3454else 3455return [ "Foo" , "Bar" , "}" ] 3456endif 3457endfunc 3458 3459func Test_complete_smartindent () 3460new 3461setlocal smartindent completefunc = FooBarComplete 3462exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>" 3463let result = getline ( 1 , '$' ) 3464call assert_equal ([ '' , '{' , '}' , '' ], result ) 3465 %d 3466setlocal complete = FFooBarComplete 3467exe "norm! o{\<cr>\<c-n>\<c-p>}\<cr>\<esc>" 3468let result = getline ( 1 , '$' ) 3469call assert_equal ([ '' , '{' , '}' , '' ], result ) 3470 %d 3471setlocal complete = F 3472exe "norm! o{\<cr>\<c-n>\<c-p>}\<cr>\<esc>" 3473let result = getline ( 1 , '$' ) 3474call assert_equal ([ '' , '{' , '}' , '' ], result ) 3475bw ! 3476delfunction ! FooBarComplete 3477endfunc 3478 3479func Test_complete_overrun () 3480" this was going past the end of the copied text 3481new 3482sil norm si0s0 3483bwipe ! 3484endfunc 3485 3486func Test_infercase_very_long_line () 3487" this was truncating the line when inferring case 3488new 3489let longLine = "blah " ->repeat ( 300 ) 3490let verylongLine = "blah " ->repeat ( 400 ) 3491call setline ( 1 , verylongLine ) 3492call setline ( 2 , longLine ) 3493set ic infercase 3494exe "normal 2Go\<C-X>\<C-L>\<Esc>" 3495call assert_equal ( longLine , getline ( 3 )) 3496 3497" check that the too long text is NUL terminated 3498 %del 3499norm o 3500norm 1987ax 3501exec "norm ox\<C-X>\<C-L>" 3502call assert_equal ( repeat ( 'x' , 1987 ), getline ( 3 )) 3503 3504bwipe ! 3505set noic noinfercase 3506endfunc 3507 3508func Test_ins_complete_add () 3509" this was reading past the end of allocated memory 3510new 3511norm o 3512norm 7o 3513sil ! norm o 3514 3515bwipe ! 3516endfunc 3517 3518func Test_ins_complete_end_of_line () 3519" this was reading past the end of the line 3520new 3521" Note that the 'space' at the end of the expression below is a non-breaking 3522" space, U+00a0. 3523execute "norm 8oý " 3524sil ! norm o 3525 3526bwipe ! 3527endfunc 3528 3529func s: Tagfunc ( t , f , o ) 3530bwipe ! 3531return [] 3532endfunc 3533 3534" A match from an included file shows the file by its name under the current 3535" directory, without the "./" of the include line. 3536func Test_complete_included_file_name () 3537CheckRunVimInTerminal 3538call mkdir ( 'Xincl/sub' , 'pR' ) 3539call writefile ([ 'let included_word = 1' ], 'Xincl/sub/inc.vim' ) 3540call writefile ([ 'source ./sub/inc.vim' , '' ], 'Xincl/main.vim' ) 3541let lines =<< trim END 3542setlocal include=^\\s*source\\s\\+ complete=i completeopt=menuone,noselect 3543END 3544call writefile ( lines , 'Xincl/setup.vim' ) 3545let buf = RunVimInTerminal ( '-S Xincl/setup.vim Xincl/main.vim' , 3546 \{ 'rows' : 8 , 'cols' : 120 }) 3547call term_sendkeys ( buf , "Goincluded_\<C-N>" ) 3548call WaitForAssert ({ ->assert_match ( 'included_word\s\+Xincl/sub/inc.vim\s' , 3549 \term_getline ( buf , 4 ))}) 3550call term_sendkeys ( buf , "\<Esc>" ) 3551call StopVimInTerminal ( buf ) 3552endfunc 3553 3554" This was using freed memory, since 'complete' was in a wiped out buffer. 3555" Also using a window that was closed. 3556func Test_tagfunc_wipes_out_buffer () 3557new 3558set complete = .,t,w,b,u,i 3559se tagfunc = s:Tagfunc 3560sil norm i 3561 3562bwipe ! 3563endfunc 3564 3565func s: TagfuncComplete ( t , f , o ) 3566call complete ( 1 , [ 'ddd' , 'eee' , 'fff' ]) 3567return [] 3568endfunc 3569 3570" 'tagfunc' calling complete() should not cause hang or E684. 3571func Test_tagfunc_calls_complete () 3572new 3573call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' ]) 3574setlocal tagfunc = s:TagfuncComplete 3575setlocal completeopt = menu,noselect 3576 3577let v: errmsg = '' 3578 3579" This used to hang. 3580setlocal complete = .,t 3581call feedkeys ( "Go\<C-N>\<C-E>\<Esc>" , 'tx' ) 3582call assert_equal ( '' , getline ( '.' )) 3583call assert_equal ( '' , v: errmsg ) 3584 3585" This used to cause E684. 3586setlocal complete = t,. 3587call feedkeys ( "cc\<C-N>\<C-E>\<Esc>" , 'tx' ) 3588call assert_equal ( '' , getline ( '.' )) 3589call assert_equal ( '' , v: errmsg ) 3590 3591bwipe ! 3592endfunc 3593 3594func Test_ins_complete_popup_position () 3595CheckScreendump 3596 3597let lines =<< trim END 3598vim9script 3599set nowrap 3600setline(1, ['one', 'two', 'this is line ', 'four']) 3601prop_type_add('test', {highlight: 'Error'}) 3602prop_add(3, 0, { 3603text_align: 'above', 3604text: 'The quick brown fox jumps over the lazy dog', 3605type: 'test' 3606}) 3607END 3608call writefile ( lines , 'XinsPopup' , 'D' ) 3609let buf = RunVimInTerminal ( '-S XinsPopup' , #{ rows : 10 }) 3610 3611call term_sendkeys ( buf , "3GA\<C-N>" ) 3612call VerifyScreenDump ( buf , 'Test_ins_complete_popup_position_1' , {}) 3613 3614call StopVimInTerminal ( buf ) 3615endfunc 3616 3617func GetCompleteInfo () 3618let g: compl_info = complete_info () 3619return '' 3620endfunc 3621 3622func Test_completion_restart () 3623new 3624set complete = .completeopt = menuonebackspace = 2 3625call setline ( 1 , 'workhorse workhorse' ) 3626exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>" 3627call assert_equal ( 1 , len ( g: compl_info [ 'items' ])) 3628call assert_equal ( 'workhorse' , g: compl_info [ 'items' ][ 0 ][ 'word' ]) 3629set complete & completeopt & backspace & 3630bwipe ! 3631endfunc 3632 3633func Test_complete_info_index () 3634new 3635call setline ( 1 , [ "aaa" , "bbb" , "ccc" , "ddd" , "eee" , "fff" ]) 3636inoremap <buffer> < F5 > < C-R > =GetCompleteInfo()< CR > 3637 3638" Ensure 'index' in complete_info() is coherent with the 'items' array. 3639 3640set completeopt = menu,preview 3641" Search forward 3642call feedkeys ( "Go\<C-X>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3643call assert_equal ( "aaa" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3644call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3645call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3646call assert_equal ( "bbb" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3647call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3648call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3649call assert_equal ( "ccc" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3650call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3651call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3652call assert_equal ( "ddd" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3653call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3654call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3655call assert_equal ( "eee" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3656call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3657call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3658call assert_equal ( "fff" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3659call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3660" Search forward: unselected item 3661call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3662call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3663call assert_equal ( -1 , g: compl_info [ 'selected' ]) 3664 3665" Search backward 3666call feedkeys ( "Go\<C-X>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3667call assert_equal ( "fff" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3668call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3669call feedkeys ( "Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3670call assert_equal ( "eee" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3671call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3672call feedkeys ( "Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3673call assert_equal ( "ddd" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3674call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3675call feedkeys ( "Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3676call assert_equal ( "ccc" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3677call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3678call feedkeys ( "Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3679call assert_equal ( "bbb" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3680call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3681call feedkeys ( "Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3682call assert_equal ( "aaa" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3683call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3684" search backwards: unselected item 3685call feedkeys ( "Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3686call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3687call assert_equal ( -1 , g: compl_info [ 'selected' ]) 3688 3689" switch direction: forwards, then backwards 3690call feedkeys ( "Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3691call assert_equal ( "fff" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3692call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3693" switch direction: forwards, then backwards, then forwards again 3694call feedkeys ( "Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3695call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3696call assert_equal ( "aaa" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3697call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3698 3699" switch direction: backwards, then forwards 3700call feedkeys ( "Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3701call assert_equal ( "aaa" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3702call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3703" switch direction: backwards, then forwards, then backwards again 3704call feedkeys ( "Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3705call assert_equal ( "fff" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3706call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3707 3708" Add 'noselect', check that 'selected' is -1 when nothing is selected. 3709set completeopt += noselect 3710" Search forward. 3711call feedkeys ( "Go\<C-X>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3712call assert_equal ( -1 , g: compl_info [ 'selected' ]) 3713 3714" Search backward. 3715call feedkeys ( "Go\<C-X>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3716call assert_equal ( -1 , g: compl_info [ 'selected' ]) 3717 3718call feedkeys ( "Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd" , 'tx' ) 3719call assert_equal ( 5 , g: compl_info [ 'selected' ]) 3720call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3721call assert_equal ( "fff" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3722call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3723call assert_equal ( "aaa" , g: compl_info [ 'items' ][ g: compl_info [ 'selected' ]][ 'word' ]) 3724call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3725call feedkeys ( "Go\<C-X>\<C-N>\<F5>\<Esc>_dd" , 'tx' ) 3726call assert_equal ( -1 , g: compl_info [ 'selected' ]) 3727call assert_equal ( 6 , len ( g: compl_info [ 'items' ])) 3728 3729set completeopt & 3730bwipe ! 3731endfunc 3732 3733func Test_complete_changed_complete_info () 3734CheckRunVimInTerminal 3735" this used to crash vim, see #13929 3736let lines =<< trim END 3737set completeopt=menuone 3738autocmd CompleteChanged * call complete_info(['items']) 3739call feedkeys("iii\<cr>\<c-p>") 3740END 3741call writefile ( lines , 'Xsegfault' , 'D' ) 3742let buf = RunVimInTerminal ( '-S Xsegfault' , #{ rows : 5 }) 3743call WaitForAssert ({ ->assert_match ( '^ii' , term_getline ( buf , 1 ))}, 1000 ) 3744call StopVimInTerminal ( buf ) 3745endfunc 3746 3747func Test_completefunc_first_call_complete_add () 3748new 3749 3750func Complete ( findstart , base ) abort 3751if a: findstart 3752let col = col ( '.' ) 3753call complete_add ( '#' ) 3754return col - 1 3755else 3756return [] 3757endif 3758endfunc 3759 3760set completeopt = longestcompletefunc = Complete 3761" This used to cause heap-buffer-overflow 3762call assert_fails ( 'call feedkeys("ifoo#\<C-X>\<C-U>", "xt")' , 'E840:' ) 3763 3764delfunc Complete 3765set completeopt & completefunc & 3766bwipe ! 3767endfunc 3768 3769func Test_complete_opt_fuzzy () 3770func OnPumChange () 3771let g: item = get ( v: event , 'completed_item' , {}) 3772let g: word = get ( g: item , 'word' , v: null ) 3773let g: abbr = get ( g: item , 'abbr' , v: null ) 3774let g: selected = get ( complete_info ([ 'selected' ]), 'selected' ) 3775endfunction 3776 3777augroup AAAAA_Group 3778au ! 3779autocmd CompleteChanged * : call OnPumChange () 3780augroup END 3781 3782let g: change = 0 3783func Omni_test ( findstart , base ) 3784if a: findstart 3785return col ( "." ) 3786endif 3787if g: change == 0 3788return [#{ word : "foo" }, #{ word : "foobar" }, #{ word : "fooBaz" }, #{ word : "foobala" }, #{ word : "你好吗" }, #{ word : "我好" }] 3789elseif g: change == 1 3790return [#{ word : "cp_match_array" }, #{ word : "cp_str" }, #{ word : "cp_score" }] 3791else 3792return [#{ word : "for i = .." }, #{ word : "bar" }, #{ word : "foo" }, #{ word : "for .. ipairs" }, #{ word : "for .. pairs" }] 3793endif 3794endfunc 3795 3796new 3797set omnifunc = Omni_test 3798set completeopt += noinsert,fuzzy 3799call feedkeys ( "Gi\<C-x>\<C-o>" , 'tx' ) 3800call assert_equal ( 'foo' , g: word ) 3801call feedkeys ( "S\<C-x>\<C-o>fb" , 'tx' ) 3802call assert_equal ( 'fooBaz' , g: word ) 3803call feedkeys ( "S\<C-x>\<C-o>fa" , 'tx' ) 3804call assert_equal ( 'foobar' , g: word ) 3805" select next 3806call feedkeys ( "S\<C-x>\<C-o>fb\<C-n>" , 'tx' ) 3807call assert_equal ( 'foobar' , g: word ) 3808" can cyclically select next 3809call feedkeys ( "S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>" , 'tx' ) 3810call assert_equal ( v: null , g: word ) 3811" select prev 3812call feedkeys ( "S\<C-x>\<C-o>fb\<C-p>" , 'tx' ) 3813call assert_equal ( v: null , g: word ) 3814" can cyclically select prev 3815call feedkeys ( "S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>" , 'tx' ) 3816call assert_equal ( 'fooBaz' , g: word ) 3817 3818func Comp () 3819call complete ( col ( '.' ), [ "fooBaz" , "foobar" , "foobala" ]) 3820return '' 3821endfunc 3822call feedkeys ( "i\<C-R>=Comp()\<CR>" , 'tx' ) 3823call assert_equal ( 'fooBaz' , g: word ) 3824 3825" respect noselect 3826set completeopt += noselect 3827call feedkeys ( "S\<C-x>\<C-o>fb" , 'tx' ) 3828call assert_equal ( v: null , g: word ) 3829call feedkeys ( "S\<C-x>\<C-o>fb\<C-n>" , 'tx' ) 3830call assert_equal ( 'fooBaz' , g: word ) 3831 3832" test case for nosort option 3833set cot = menuone,menu,noinsert,fuzzy,nosort 3834" "fooBaz" should have a higher score when the leader is "fb". 3835" With "nosort", "foobar" should still be shown first in the popup menu. 3836call feedkeys ( "S\<C-x>\<C-o>fb" , 'tx' ) 3837call assert_equal ( 'foobar' , g: word ) 3838call feedkeys ( "S\<C-x>\<C-o>好" , 'tx' ) 3839call assert_equal ( "你好吗" , g: word ) 3840 3841set cot += noselect 3842call feedkeys ( "S\<C-x>\<C-o>好" , 'tx' ) 3843call assert_equal ( v: null , g: word ) 3844call feedkeys ( "S\<C-x>\<C-o>好\<C-N>" , 'tx' ) 3845call assert_equal ( '你好吗' , g: word ) 3846 3847" "nosort" shouldn't enable fuzzy filtering when "fuzzy" isn't present. 3848set cot = menuone,noinsert,nosort 3849call feedkeys ( "S\<C-x>\<C-o>fooB\<C-Y>" , 'tx' ) 3850call assert_equal ( 'fooBaz' , getline ( '.' )) 3851 3852set cot = menuone,fuzzy,nosort 3853func CompAnother () 3854call complete ( col ( '.' ), [#{ word : "do" }, #{ word : "echo" }, #{ word : "for (${1:expr1}, ${2:expr2}, ${3:expr3}) {\n\t$0\n}" , abbr : "for" }, #{ word : "foo" }]) 3855return '' 3856endfunc 3857call feedkeys ( "i\<C-R>=CompAnother()\<CR>\<C-N>\<C-N>" , 'tx' ) 3858call assert_equal ( "for" , g: abbr ) 3859call assert_equal ( 2 , g: selected ) 3860 3861set cot += noinsert 3862call feedkeys ( "i\<C-R>=CompAnother()\<CR>f" , 'tx' ) 3863call assert_equal ( "for" , g: abbr ) 3864call assert_equal ( 2 , g: selected ) 3865 3866set cot = menu,menuone,noselect,fuzzy 3867call feedkeys ( "i\<C-R>=CompAnother()\<CR>\<C-N>\<C-N>\<C-N>\<C-N>" , 'tx' ) 3868call assert_equal ( "foo" , g: word ) 3869call feedkeys ( "i\<C-R>=CompAnother()\<CR>\<C-P>" , 'tx' ) 3870call assert_equal ( "foo" , g: word ) 3871call feedkeys ( "i\<C-R>=CompAnother()\<CR>\<C-P>\<C-P>" , 'tx' ) 3872call assert_equal ( "for" , g: abbr ) 3873 3874 %d 3875set cot = menu,fuzzy 3876call feedkeys ( "Sblue\<CR>bar\<CR>b\<C-X>\<C-P>\<C-Y>\<ESC>" , 'tx' ) 3877call assert_equal ( 'blue' , getline ( '.' )) 3878call feedkeys ( "Sb\<C-X>\<C-N>\<C-Y>\<ESC>" , 'tx' ) 3879call assert_equal ( 'bar' , getline ( '.' )) 3880call feedkeys ( "Sb\<C-X>\<C-P>\<C-N>\<C-Y>\<ESC>" , 'tx' ) 3881call assert_equal ( 'b' , getline ( '.' )) 3882 3883" chain completion 3884call feedkeys ( "Slore spum\<CR>lor\<C-X>\<C-P>\<C-X>\<C-P>\<ESC>" , 'tx' ) 3885call assert_equal ( 'lore spum' , getline ( '.' )) 3886 3887" issue #15412 3888call feedkeys ( "Salpha bravio charlie\<CR>alpha\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<ESC>" , 'tx' ) 3889call assert_equal ( 'alpha bravio charlie' , getline ( '.' )) 3890 3891set cot = fuzzy,menu,noinsert 3892call feedkeys ( ":let g:change=2\<CR>" ) 3893call feedkeys ( "S\<C-X>\<C-O>for\<C-N>\<C-N>\<C-N>" , 'tx' ) 3894call assert_equal ( 'for' , getline ( '.' )) 3895call feedkeys ( "S\<C-X>\<C-O>for\<C-P>" , 'tx' ) 3896call assert_equal ( 'for' , getline ( '.' )) 3897call feedkeys ( "S\<C-X>\<C-O>for\<C-P>\<C-P>" , 'tx' ) 3898call assert_equal ( 'for .. ipairs' , getline ( '.' )) 3899 3900call feedkeys ( ":let g:change=1\<CR>" ) 3901call feedkeys ( "S\<C-X>\<C-O>c\<C-Y>" , 'tx' ) 3902call assert_equal ( 'cp_str' , getline ( '.' )) 3903 3904" Issue 18488: sort after collection when "fuzzy" (unless "nosort") 3905 %d 3906set completeopt & 3907set completeopt += fuzzy,noselect 3908func ! PrintMenuWords () 3909let info = complete_info ([ "items" ]) 3910call map ( info . items , { _ , v ->v . word }) 3911return info 3912endfunc 3913call setline ( 1 , [ 'func1' , 'xfunc' , 'func2' ]) 3914call feedkeys ( "Gof\<C-N>\<C-R>=PrintMenuWords()\<CR>\<Esc>0" , 'tx' ) 3915call assert_equal ( 'f{''items'': [''func1'', ''func2'', ''xfunc'']}' , getline ( '.' )) 3916 3917" Issue #18802: Reset selected item after fuzzy sort 3918 %d 3919call setline ( 1 , [ 'aa' , 'aaa' , 'aaaa' ]) 3920set completeopt = menuone,noinsert,fuzzy 3921call feedkeys ( "Goa\<C-N>\<C-Y>\<Esc>" , 'tx' ) 3922call assert_equal ( 'aa' , getline ( '.' )) 3923call feedkeys ( "Goa\<C-P>\<C-Y>\<Esc>" , 'tx' ) 3924call assert_equal ( 'aaaa' , getline ( '.' )) 3925 3926 %d 3927set autoindent 3928set completeopt = menuone,fuzzy 3929call feedkeys ( "A\<TAB>hello world\<CR>word is on fire\<CR>w\<C-X>\<C-L>\<C-Y>" , 'tx' ) 3930call assert_equal ( "\thello world" , getline ( '.' )) 3931 3932" clean up 3933set omnifunc = 3934bw ! 3935set complete & completeopt & autoindent & 3936autocmd ! AAAAA_Group 3937augroup ! AAAAA_Group 3938delfunc OnPumChange 3939delfunc Omni_test 3940delfunc Comp 3941delfunc PrintMenuWords 3942unlet g: item 3943unlet g: word 3944unlet g: abbr 3945endfunc 3946 3947func Test_complete_fuzzy_collect () 3948new 3949redraw " need this to prevent NULL dereference in Nvim 3950set completeopt += fuzzy 3951call setline ( 1 , [ 'hello help hero h' ]) 3952" Use "!" flag of feedkeys() so that ex_normal_busy is not set and 3953" ins_compl_check_keys() is not skipped. 3954" Add a "0" after the <Esc> to avoid waiting for an escape sequence. 3955call feedkeys ( "A\<C-X>\<C-N>\<Esc>0" , 'tx!' ) 3956call assert_equal ( 'hello help hero hello' , getline ( '.' )) 3957set completeopt += noinsert 3958call setline ( 1 , [ 'hello help hero h' ]) 3959call feedkeys ( "A\<C-X>\<C-N>\<Esc>0" , 'tx!' ) 3960call assert_equal ( 'hello help hero h' , getline ( '.' )) 3961 3962set completeopt -= noinsert 3963call setline ( 1 , [ 'xyz yxz x' ]) 3964call feedkeys ( "A\<C-X>\<C-N>\<Esc>0" , 'tx!' ) 3965call assert_equal ( 'xyz yxz xyz' , getline ( '.' )) 3966" can fuzzy get yxz when use Ctrl-N twice 3967call setline ( 1 , [ 'xyz yxz x' ]) 3968call feedkeys ( "A\<C-X>\<C-N>\<C-N>\<Esc>0" , 'tx!' ) 3969call assert_equal ( 'xyz yxz yxz' , getline ( '.' )) 3970 3971call setline ( 1 , [ '你好 你' ]) 3972call feedkeys ( "A\<C-X>\<C-N>\<Esc>0" , 'tx!' ) 3973call assert_equal ( '你好 你好' , getline ( '.' )) 3974call setline ( 1 , [ '你的 我的 的' ]) 3975call feedkeys ( "A\<C-X>\<C-N>\<Esc>0" , 'tx!' ) 3976call assert_equal ( '你的 我的 你的' , getline ( '.' )) 3977" can fuzzy get multiple-byte word when use Ctrl-N twice 3978call setline ( 1 , [ '你的 我的 的' ]) 3979call feedkeys ( "A\<C-X>\<C-N>\<C-N>\<Esc>0" , 'tx!' ) 3980call assert_equal ( '你的 我的 我的' , getline ( '.' )) 3981 3982" check that "adding" expansion works 3983call setline ( 1 , [ 'hello world foo bar' ]) 3984call feedkeys ( "Ohlo\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<Esc>0" , 'tx!' ) 3985call assert_equal ( 'hello world foo bar' , getline ( '.' )) 3986call feedkeys ( "Swld\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<Esc>0" , 'tx!' ) 3987call assert_equal ( 'world foo bar' , getline ( '.' )) 3988% delete 3989 3990" fuzzy on file 3991call writefile ([ '' ], 'fobar' , 'D' ) 3992call writefile ([ '' ], 'foobar' , 'D' ) 3993call setline ( 1 , [ 'fob' ]) 3994call cursor ( 1 , 1 ) 3995call feedkeys ( "A\<C-X>\<C-f>\<Esc>0" , 'tx!' ) 3996call assert_equal ( 'fobar' , getline ( '.' )) 3997call feedkeys ( "Sfob\<C-X>\<C-f>\<C-N>\<Esc>0" , 'tx!' ) 3998call assert_equal ( 'foobar' , getline ( '.' )) 3999call feedkeys ( "S../\<C-X>\<C-f>\<Esc>0" , 'tx!' ) 4000call assert_match ( '../*' , getline ( '.' )) 4001call feedkeys ( "S../td\<C-X>\<C-f>\<Esc>0" , 'tx!' ) 4002call assert_match ( '../testdir' , getline ( '.' )) 4003 4004" can get completion from other buffer 4005vnew 4006call setline ( 1 , [ "completeness," , "compatibility" , "Composite" , "Omnipotent" ]) 4007wincmd p 4008call feedkeys ( "Somp\<C-N>\<Esc>0" , 'tx!' ) 4009call assert_equal ( 'completeness' , getline ( '.' )) 4010call feedkeys ( "Somp\<C-N>\<C-N>\<Esc>0" , 'tx!' ) 4011call assert_equal ( 'compatibility' , getline ( '.' )) 4012call feedkeys ( "Somp\<C-P>\<Esc>0" , 'tx!' ) 4013call assert_equal ( 'Omnipotent' , getline ( '.' )) 4014call feedkeys ( "Somp\<C-P>\<C-P>\<Esc>0" , 'tx!' ) 4015call assert_equal ( 'Composite' , getline ( '.' )) 4016call feedkeys ( "S omp\<C-N>\<Esc>0" , 'tx!' ) 4017call assert_equal ( ' completeness' , getline ( '.' )) 4018 4019" fuzzy on whole line completion 4020call setline ( 1 , [ "world is on fire" , "no one can save me but you" , 'user can execute' , '' ]) 4021call cursor ( 4 , 1 ) 4022call feedkeys ( "Swio\<C-X>\<C-L>\<Esc>0" , 'tx!' ) 4023call assert_equal ( 'world is on fire' , getline ( '.' )) 4024call feedkeys ( "Su\<C-X>\<C-L>\<C-P>\<Esc>0" , 'tx!' ) 4025call assert_equal ( 'no one can save me but you' , getline ( '.' )) 4026 4027" issue #15526 4028set completeopt = menuone,menu,noselect,fuzzy 4029call setline ( 1 , [ 'Text' , 'ToText' , '' ]) 4030call cursor ( 3 , 1 ) 4031call feedkeys ( "STe\<C-X>\<C-N>x\<CR>\<Esc>0" , 'tx!' ) 4032call assert_equal ( 'Tex' , getline ( line ( '.' ) - 1 )) 4033 4034call setline ( 1 , [ 'fuzzy' , 'fuzzycollect' , 'completefuzzycollect' ]) 4035call feedkeys ( "Gofuzzy\<C-X>\<C-N>\<C-N>\<C-N>\<CR>\<Esc>0" , 'tx!' ) 4036call assert_equal ( 'fuzzycollect' , getline ( line ( '.' ) - 1 )) 4037call feedkeys ( "Gofuzzy\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<CR>\<Esc>0" , 'tx!' ) 4038call assert_equal ( 'completefuzzycollect' , getline ( line ( '.' ) - 1 )) 4039 4040" keywords in 'dictionary' 4041call writefile ([ 'hello' , 'think' ], 'Xtest_dict.txt' , 'D' ) 4042set dict = Xtest_dict.txt 4043call feedkeys ( "Sh\<C-X>\<C-K>\<C-N>\<CR>\<Esc>0" , 'tx!' ) 4044call assert_equal ( 'hello' , getline ( line ( '.' ) - 1 )) 4045call feedkeys ( "Sh\<C-X>\<C-K>\<C-N>\<C-N>\<CR>\<Esc>0" , 'tx!' ) 4046call assert_equal ( 'think' , getline ( line ( '.' ) - 1 )) 4047 4048call setline ( 1 , [ 'foo bar fuzzy' , 'completefuzzycollect' ]) 4049call feedkeys ( "Gofuzzy\<C-X>\<C-N>\<C-N>\<C-N>\<C-Y>\<Esc>0" , 'tx!' ) 4050call assert_equal ( 'completefuzzycollect' , getline ( '.' )) 4051 4052% d _ 4053call setline ( 1 , [ 'fuzzy' , 'fuzzy foo' , "fuzzy bar" , 'fuzzycollect' ]) 4054call feedkeys ( "Gofuzzy\<C-X>\<C-N>\<C-N>\<C-N>\<C-Y>\<Esc>0" , 'tx!' ) 4055call assert_equal ( 'fuzzycollect' , getline ( '.' )) 4056 4057" when 'fuzzy' is not set, and 'infercase' and 'ignorecase' are set, then 4058" uppercase completes from lowercase words in dictionary 4059set completeopt & 4060set infercase ignorecase 4061call writefile ([ 'hello' ], 'Xtest_case.txt' , 'D' ) 4062set dictionary = Xtest_case.txt 4063call feedkeys ( "ggdGSH\<C-X>\<C-K>\<C-Y>\<Esc>" , 'tx' ) 4064call assert_equal ( 'Hello' , getline ( '.' )) 4065call feedkeys ( "ggdGSHE\<C-X>\<C-K>\<C-Y>\<Esc>" , 'tx' ) 4066call assert_equal ( 'HELLO' , getline ( '.' )) 4067 4068bw ! 4069bw ! 4070set dict & 4071set completeopt & cpt & ignorecase & infercase & 4072endfunc 4073 4074" Issue #19434 4075" Fuzzy whole-line completion should not loop infinitely when the cursor is in 4076" the middle of the line (non-zero column). 4077func Test_complete_fuzzy_wholeline_no_hang () 4078new 4079set completeopt = preview,fuzzy,noinsert,menuone 4080call setline ( 1 , [ 4081\ '<!DOCTYPE html>' , 4082\ '<html lang="en-US">' , 4083\ ' <head>' , 4084\ ' </head>' , 4085\ ' <body>' , 4086\ ' <div class="page-landscape">' , 4087\ ' </div>' , 4088\ ' </body>' , 4089\ '</html>' , 4090\ ]) 4091call cursor ( 6 , 1 ) 4092call feedkeys ( "faC\<C-X>\<C-L>\<Esc>0" , 'tx!' ) 4093call assert_equal ( ' <div cl' , getline ( 6 )) 4094 4095bw ! 4096set completeopt & 4097endfunc 4098 4099" Issue #18752 4100func Test_complete_fuzzy_collect_multiwin () 4101new 4102set completeopt = fuzzy 4103 4104vnew 4105call setline ( 1 , [ "completeness," , "compatibility" , "Composite" , "Omnipotent" ]) 4106wincmd p 4107call feedkeys ( "Somp\<C-P>\<Esc>0" , 'tx!' ) 4108call assert_equal ( 'Omnipotent' , getline ( '.' )) 4109 4110bw ! 4111set completeopt & 4112endfunc 4113 4114func Test_cfc_with_longest () 4115new 4116set completeopt = menu,menuone,longest,fuzzy 4117 4118" keyword 4119exe "normal ggdGShello helio think h\<C-X>\<C-N>\<ESC>" 4120call assert_equal ( "hello helio think hel" , getline ( '.' )) 4121exe "normal hello helio think h\<C-X>\<C-P>\<ESC>" 4122call assert_equal ( "hello helio think hel" , getline ( '.' )) 4123 4124" skip non-consecutive prefixes 4125exe "normal ggdGShello helio heo\<C-X>\<C-N>\<ESC>" 4126call assert_equal ( "hello helio heo" , getline ( '.' )) 4127 4128" dict 4129call writefile ([ 'help' ], 'test_keyword.txt' , 'D' ) 4130set complete = ktest_keyword.txt 4131exe "normal ggdGSh\<C-N>\<ESC>" 4132" auto insert help when only have one match 4133call assert_equal ( "help" , getline ( '.' )) 4134call writefile ([ 'hello' , 'help' , 'think' ], 'xtest_keyword.txt' , 'D' ) 4135set complete = kxtest_keyword.txt 4136" auto insert hel 4137exe "normal ggdGSh\<C-N>\<ESC>" 4138call assert_equal ( "hel" , getline ( '.' )) 4139 4140" line start with a space 4141call writefile ([ ' hello' ], 'test_case1.txt' , 'D' ) 4142set complete = ktest_case1.txt 4143exe "normal ggdGSh\<C-N>\<ESC>" 4144call assert_equal ( "hello" , getline ( '.' )) 4145 4146" multiple matches 4147set complete = ktest_case2.txt 4148call writefile ([ ' hello help what' ], 'test_case2.txt' , 'D' ) 4149exe "normal ggdGSh\<C-N>\<C-N>\<C-N>\<C-N>\<ESC>" 4150call assert_equal ( "what" , getline ( '.' )) 4151 4152" multiple lines of matches 4153set complete = ktest_case3.txt 4154call writefile ([ ' hello help what' , 'hola' , ' hey' ], 'test_case3.txt' , 'D' ) 4155exe "normal ggdGSh\<C-N>\<C-N>\<ESC>" 4156call assert_equal ( "hey" , getline ( '.' )) 4157exe "normal ggdGSh\<C-N>\<C-N>\<C-N>\<C-N>\<ESC>" 4158call assert_equal ( "hola" , getline ( '.' )) 4159 4160set complete = ktest_case4.txt 4161call writefile ([ ' auto int enum register' , 'why' ], 'test_case4.txt' , 'D' ) 4162exe "normal ggdGSe\<C-N>\<C-N>\<ESC>" 4163call assert_equal ( "enum" , getline ( '.' )) 4164 4165set complete = ktest_case5.txt 4166call writefile ([ 'hello friends' , 'go' , 'hero' ], 'test_case5.txt' , 'D' ) 4167exe "normal ggdGSh\<C-N>\<C-N>\<ESC>" 4168call assert_equal ( "hero" , getline ( '.' )) 4169set complete & 4170 4171" file 4172call writefile ([ '' ], 'hello' , 'D' ) 4173call writefile ([ '' ], 'helio' , 'D' ) 4174exe "normal ggdGS./h\<C-X>\<C-f>\<ESC>" 4175call assert_equal ( './hel' , getline ( '.' )) 4176 4177" word 4178call setline ( 1 , [ 'what do you think' , 'why i have that' , '' ]) 4179call cursor ( 3 , 1 ) 4180call feedkeys ( "Sw\<C-X>\<C-l>\<C-N>\<Esc>0" , 'tx!' ) 4181call assert_equal ( 'wh' , getline ( '.' )) 4182 4183exe "normal ggdG" 4184" auto complete when only one match 4185exe "normal Shello\<CR>h\<C-X>\<C-N>\<esc>" 4186call assert_equal ( 'hello' , getline ( '.' )) 4187exe "normal Sh\<C-N>\<C-P>\<esc>" 4188call assert_equal ( 'hello' , getline ( '.' )) 4189 4190exe "normal Shello\<CR>h\<C-X>\<C-N>\<Esc>cch\<C-X>\<C-N>\<Esc>" 4191call assert_equal ( 'hello' , getline ( '.' )) 4192 4193" continue search for new leader after insert common prefix 4194exe "normal ohellokate\<CR>h\<C-X>\<C-N>k\<C-N>\<C-y>\<esc>" 4195call assert_equal ( 'hellokate' , getline ( '.' )) 4196 4197bw ! 4198set completeopt & 4199endfunc 4200 4201func Test_completefuzzycollect_with_completeslash () 4202CheckMSWindows 4203 4204call writefile ([ '' ], 'fobar' , 'D' ) 4205let orig_shellslash = & shellslash 4206set cpt & 4207new 4208set completeopt += fuzzy 4209set noshellslash 4210 4211" Test with completeslash unset 4212set completeslash = 4213call setline ( 1 , [ '.\fob' ]) 4214call feedkeys ( "A\<C-X>\<C-F>\<Esc>0" , 'tx!' ) 4215call assert_equal ( '.\fobar' , getline ( '.' )) 4216 4217" Test with completeslash=backslash 4218set completeslash = backslash 4219call feedkeys ( "S.\\fob\<C-X>\<C-F>\<Esc>0" , 'tx!' ) 4220call assert_equal ( '.\fobar' , getline ( '.' )) 4221 4222" Test with completeslash=slash 4223set completeslash = slash 4224call feedkeys ( "S.\\fob\<C-X>\<C-F>\<Esc>0" , 'tx!' ) 4225call assert_equal ( './fobar' , getline ( '.' )) 4226 4227" Reset and clean up 4228let & shellslash = orig_shellslash 4229set completeslash = 4230% bw ! 4231endfunc 4232 4233" Check that tie breaking is stable for completeopt+=fuzzy (which should 4234" behave the same on different platforms). 4235func Test_complete_fuzzy_match_tie () 4236new 4237set completeopt += fuzzy,noselect 4238call setline ( 1 , [ 'aaabbccc' , 'aaabbCCC' , 'aaabbcccc' , 'aaabbCCCC' , '' ]) 4239 4240call feedkeys ( "Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>" , 'tx' ) 4241call assert_equal ( 'aaabbccc' , getline ( '.' )) 4242call feedkeys ( "Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>" , 'tx' ) 4243call assert_equal ( 'aaabbCCC' , getline ( '.' )) 4244call feedkeys ( "Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>" , 'tx' ) 4245call assert_equal ( 'aaabbcccc' , getline ( '.' )) 4246call feedkeys ( "Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>" , 'tx' ) 4247call assert_equal ( 'aaabbCCCC' , getline ( '.' )) 4248 4249bwipe ! 4250set completeopt & 4251endfunc 4252 4253func Test_complete_backwards_default () 4254new 4255call append ( 1 , [ 'foobar' , 'foobaz' ]) 4256new 4257call feedkeys ( "i\<c-p>" , 'tx' ) 4258call assert_equal ( 'foobaz' , getline ( '.' )) 4259bw ! 4260bw ! 4261endfunc 4262 4263func Test_complete_info_matches () 4264let g: what = [ 'matches' ] 4265func ShownInfo () 4266let g: compl_info = complete_info ( g: what ) 4267return '' 4268endfunc 4269set completeopt += noinsert 4270 4271new 4272call setline ( 1 , [ 'aaa' , 'aab' , 'aba' , 'abb' ]) 4273inoremap <buffer> < F5 > < C-R > =ShownInfo() < CR > 4274 4275call feedkeys ( "Go\<C-X>\<C-N>\<F5>\<Esc>dd" , 'tx' ) 4276call assert_equal ([ 4277\ { 'word' : 'aaa' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4278\ { 'word' : 'aab' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4279\ { 'word' : 'aba' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4280\ { 'word' : 'abb' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4281\ ], g: compl_info [ 'matches' ]) 4282 4283call feedkeys ( "Goa\<C-X>\<C-N>b\<F5>\<Esc>dd" , 'tx' ) 4284call assert_equal ([ 4285\ { 'word' : 'aba' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4286\ { 'word' : 'abb' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4287\ ], g: compl_info [ 'matches' ]) 4288 4289" items and matches both in what 4290let g: what = [ 'items' , 'matches' ] 4291call feedkeys ( "Goa\<C-X>\<C-N>b\<F5>\<Esc>dd" , 'tx' ) 4292call assert_equal ([ 4293\ { 'word' : 'aaa' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'match' : v: false , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4294\ { 'word' : 'aab' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'match' : v: false , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4295\ { 'word' : 'aba' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'match' : v: true , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4296\ { 'word' : 'abb' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'match' : v: true , 'info' : '' , 'kind' : '' , 'abbr' : '' }, 4297\ ], g: compl_info [ 'items' ]) 4298call assert_false ( has_key ( g: compl_info , 'matches' )) 4299 4300bw ! 4301unlet g: what 4302delfunc ShownInfo 4303set cot & 4304endfunc 4305 4306func Test_complete_info_completed () 4307func ShownInfo () 4308let g: compl_info = complete_info ([ 'completed' ]) 4309return '' 4310endfunc 4311set completeopt += noinsert 4312 4313new 4314call setline ( 1 , [ 'aaa' , 'aab' , 'aba' , 'abb' ]) 4315inoremap <buffer> < F5 > < C-R > =ShownInfo() < CR > 4316 4317call feedkeys ( "Go\<C-X>\<C-N>\<F5>\<Esc>dd" , 'tx' ) 4318call assert_equal ({ 'word' : 'aaa' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, g: compl_info [ 'completed' ]) 4319 4320call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>dd" , 'tx' ) 4321call assert_equal ({ 'word' : 'aab' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, g: compl_info [ 'completed' ]) 4322 4323call feedkeys ( "Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>dd" , 'tx' ) 4324call assert_equal ({ 'word' : 'abb' , 'menu' : '' , 'kind_hlgroup' : '' , 'abbr_hlgroup' : '' , 'user_data' : '' , 'info' : '' , 'kind' : '' , 'abbr' : '' }, g: compl_info [ 'completed' ]) 4325 4326set completeopt += noselect 4327call feedkeys ( "Go\<C-X>\<C-N>\<F5>\<Esc>dd" , 'tx' ) 4328call assert_equal ({}, g: compl_info ) 4329 4330bw ! 4331delfunc ShownInfo 4332set cot & 4333endfunc 4334 4335func Test_complete_info_selected () 4336set completeopt = menuone,noselect 4337new 4338call setline ( 1 , [ "ward" , "werd" , "wurd" , "wxrd" ]) 4339 4340exe "normal! Gow\<c-n>u\<c-n>\<c-r>=complete_info().selected\<cr>" 4341call assert_equal ( 'wurd2' , getline ( 5 )) 4342 4343exe "normal! Sw\<c-n>u\<c-n>\<c-r>=complete_info(['selected']).selected\<cr>" 4344call assert_equal ( 'wurd2' , getline ( 5 )) 4345 4346exe "normal! Sw\<c-n>u\<c-n>\<c-r>=complete_info(['items', 'selected']).selected\<cr>" 4347call assert_equal ( 'wurd2' , getline ( 5 )) 4348 4349exe "normal! Sw\<c-n>u\<c-n>\<c-r>=complete_info(['matches', 'selected']).selected\<cr>" 4350call assert_equal ( 'wurd0' , getline ( 5 )) 4351 4352bw ! 4353set cot & 4354endfunc 4355 4356func Test_completeopt_preinsert () 4357func Omni_test ( findstart , base ) 4358if a: findstart 4359return col ( "." ) 4360endif 4361return [#{ word : "fobar" }, #{ word : "foobar" }, #{ word : "你的" }, #{ word : "你好世界" }] 4362endfunc 4363set omnifunc = Omni_test 4364set completeopt = menu,menuone,preinsert 4365func GetLine () 4366let g: line = getline ( '.' ) 4367let g: col = col ( '.' ) 4368endfunc 4369 4370new 4371inoremap <buffer> < F5 > < C-R > =GetLine() < CR > 4372call feedkeys ( "S\<C-X>\<C-O>f\<F5>\<ESC>" , 'tx' ) 4373call assert_equal ( "fobar" , g: line ) 4374call assert_equal ( 2 , g: col ) 4375 4376call feedkeys ( "S\<C-X>\<C-O>foo\<F5>\<ESC>" , 'tx' ) 4377call assert_equal ( "foobar" , g: line ) 4378 4379call feedkeys ( "S\<C-X>\<C-O>foo\<BS>\<BS>\<BS>" , 'tx' ) 4380call assert_equal ( "" , getline ( '.' )) 4381 4382" delete a character and input new leader 4383call feedkeys ( "S\<C-X>\<C-O>foo\<BS>b\<F5>\<ESC>" , 'tx' ) 4384call assert_equal ( "fobar" , g: line ) 4385call assert_equal ( 4 , g: col ) 4386 4387" delete preinsert when prepare completion 4388call feedkeys ( "S\<C-X>\<C-O>f\<Space>" , 'tx' ) 4389call assert_equal ( "f " , getline ( '.' )) 4390 4391call feedkeys ( "S\<C-X>\<C-O>你\<F5>\<ESC>" , 'tx' ) 4392call assert_equal ( "你的" , g: line ) 4393call assert_equal ( 4 , g: col ) 4394 4395call feedkeys ( "S\<C-X>\<C-O>你好\<F5>\<ESC>" , 'tx' ) 4396call assert_equal ( "你好世界" , g: line ) 4397call assert_equal ( 7 , g: col ) 4398 4399call feedkeys ( "Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f\<F5>\<ESC>" , 'tx' ) 4400call assert_equal ( "hello fobar wo" , g: line ) 4401call assert_equal ( 9 , g: col ) 4402 4403call feedkeys ( "Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f\<BS>\<F5>\<ESC>" , 'tx' ) 4404call assert_equal ( "hello wo" , g: line ) 4405call assert_equal ( 8 , g: col ) 4406 4407call feedkeys ( "Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo\<F5>\<ESC>" , 'tx' ) 4408call assert_equal ( "hello foobar wo" , g: line ) 4409call assert_equal ( 11 , g: col ) 4410 4411call feedkeys ( "Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo\<BS>b\<F5>\<ESC>" , 'tx' ) 4412call assert_equal ( "hello fobar wo" , g: line ) 4413call assert_equal ( 11 , g: col ) 4414 4415" confirm 4416call feedkeys ( "S\<C-X>\<C-O>f\<C-Y>" , 'tx' ) 4417call assert_equal ( "fobar" , getline ( '.' )) 4418call assert_equal ( 5 , col ( '.' )) 4419 4420" cancel 4421call feedkeys ( "S\<C-X>\<C-O>fo\<C-E>" , 'tx' ) 4422call assert_equal ( "fo" , getline ( '.' )) 4423call assert_equal ( 2 , col ( '.' )) 4424 4425call feedkeys ( "S hello hero\<CR>h\<C-X>\<C-N>\<F5>\<ESC>" , 'tx' ) 4426call assert_equal ( "hello" , g: line ) 4427call assert_equal ( 2 , col ( '.' )) 4428 4429call feedkeys ( "Sh\<C-X>\<C-N>\<C-Y>" , 'tx' ) 4430call assert_equal ( "hello" , getline ( '.' )) 4431call assert_equal ( 5 , col ( '.' )) 4432 4433" delete preinsert part 4434call feedkeys ( "S\<C-X>\<C-O>fo " , 'tx' ) 4435call assert_equal ( "fo " , getline ( '.' )) 4436call assert_equal ( 3 , col ( '.' )) 4437 4438call feedkeys ( "She\<C-X>\<C-N>\<C-U>" , 'tx' ) 4439call assert_equal ( "" , getline ( '.' )) 4440call assert_equal ( 1 , col ( '.' )) 4441 4442call feedkeys ( "She\<C-X>\<C-N>\<C-W>" , 'tx' ) 4443call assert_equal ( "" , getline ( '.' )) 4444call assert_equal ( 1 , col ( '.' )) 4445 4446" whole line 4447call feedkeys ( "Shello hero\<CR>\<C-X>\<C-L>\<F5>\<ESC>" , 'tx' ) 4448call assert_equal ( "hello hero" , g: line ) 4449call assert_equal ( 1 , g: col ) 4450 4451call feedkeys ( "Shello hero\<CR>he\<C-X>\<C-L>\<F5>\<ESC>" , 'tx' ) 4452call assert_equal ( "hello hero" , g: line ) 4453call assert_equal ( 3 , g: col ) 4454 4455call feedkeys ( "Shello hero\<CR>h\<C-X>\<C-N>er\<F5>\<ESC>" , 'tx' ) 4456call assert_equal ( "hero" , g: line ) 4457call assert_equal ( 4 , g: col ) 4458 4459" can not work with fuzzy 4460set cot += fuzzy 4461call feedkeys ( "S\<C-X>\<C-O>" , 'tx' ) 4462call assert_equal ( "fobar" , getline ( '.' )) 4463call assert_equal ( 5 , col ( '.' )) 4464 4465" test for fuzzy and noinsert 4466set cot += noinsert 4467call feedkeys ( "S\<C-X>\<C-O>fb\<F5>\<ESC>" , 'tx' ) 4468call assert_equal ( "fb" , g: line ) 4469call assert_equal ( 3 , g: col ) 4470 4471call feedkeys ( "S\<C-X>\<C-O>你\<F5>\<ESC>" , 'tx' ) 4472call assert_equal ( "你" , g: line ) 4473call assert_equal ( 4 , g: col ) 4474 4475call feedkeys ( "S\<C-X>\<C-O>fb\<C-Y>" , 'tx' ) 4476call assert_equal ( "fobar" , getline ( '.' )) 4477call assert_equal ( 5 , col ( '.' )) 4478 4479" When the pum is not visible, the preinsert has no effect 4480set cot = preinsert 4481call feedkeys ( "Sfoo1 foo2\<CR>f\<C-X>\<C-N>bar" , 'tx' ) 4482call assert_equal ( "foo1bar" , getline ( '.' )) 4483call assert_equal ( 7 , col ( '.' )) 4484 4485set cot = preinsert,menuone 4486call feedkeys ( "Sfoo1 foo2\<CR>f\<C-X>\<C-N>\<F5>\<ESC>" , 'tx' ) 4487call assert_equal ( "foo1" , g: line ) 4488call assert_equal ( 2 , g: col ) 4489 4490inoremap <buffer> < f3 > <cmd> call complete ( 4 , [{ 'word' : "fobar" }, { 'word' : "foobar" }]) <CR> 4491call feedkeys ( "Swp.\<F3>\<F5>\<BS>\<ESC>" , 'tx' ) 4492call assert_equal ( "wp.fobar" , g: line ) 4493call assert_equal ( 4 , g: col ) 4494call assert_equal ( "wp." , getline ( '.' )) 4495 4496% delete _ 4497let & l: undolevels = & l: undolevels 4498normal ! ifoo 4499let & l: undolevels = & l: undolevels 4500normal ! obar 4501let & l: undolevels = & l: undolevels 4502normal ! obaz 4503let & l: undolevels = & l: undolevels 4504 4505func CheckUndo () 4506let g: errmsg = '' 4507call assert_equal ([ 'foo' , 'bar' , 'baz' ], getline ( 1 , '$' )) 4508undo 4509call assert_equal ([ 'foo' , 'bar' ], getline ( 1 , '$' )) 4510undo 4511call assert_equal ([ 'foo' ], getline ( 1 , '$' )) 4512undo 4513call assert_equal ([ '' ], getline ( 1 , '$' )) 4514later 3 4515call assert_equal ([ 'foo' , 'bar' , 'baz' ], getline ( 1 , '$' )) 4516call assert_equal ( '' , v: errmsg ) 4517endfunc 4518 4519" Check that switching buffer with "preinsert" doesn't corrupt undo. 4520new 4521setlocal bufhidden = wipe 4522inoremap <buffer> < F2 > <Cmd> enew ! <CR> 4523call feedkeys ( "i\<C-X>\<C-O>\<F2>\<Esc>" , 'tx' ) 4524bwipe ! 4525call CheckUndo () 4526 4527" Check that closing window with "preinsert" doesn't corrupt undo. 4528new 4529setlocal bufhidden = wipe 4530inoremap <buffer> < F2 > <Cmd> close !<CR> 4531call feedkeys ( "i\<C-X>\<C-O>\<F2>\<Esc>" , 'tx' ) 4532call CheckUndo () 4533 4534% delete _ 4535delfunc CheckUndo 4536 4537bw ! 4538set cot & 4539set omnifunc & 4540delfunc Omni_test 4541endfunc 4542 4543func Test_autocomplete_completeopt_preinsert () 4544func Omni_test ( findstart , base ) 4545if a: findstart 4546return col ( "." ) - 1 4547endif 4548return [#{ word : "fobar" }, #{ word : "foobar" }] 4549endfunc 4550set omnifunc = Omni_test complete += o 4551set completeopt = preinsert autocomplete 4552func GetLine () 4553let g: line = getline ( '.' ) 4554let g: col = col ( '.' ) 4555endfunc 4556 4557call Ntest_override ( "char_avail" , 1 ) 4558new 4559inoremap <buffer> < F5 > < C-R > =GetLine() < CR > 4560call feedkeys ( "Sfo\<F5>\<ESC>" , 'tx' ) 4561call assert_equal ( "fobar" , g: line ) 4562call assert_equal ( 3 , g: col ) 4563 4564call feedkeys ( "Sfoo\<F5>\<ESC>" , 'tx' ) 4565call assert_equal ( "foobar" , g: line ) 4566 4567call feedkeys ( "Sfoo\<BS>\<BS>\<BS>" , 'tx' ) 4568call assert_equal ( "" , getline ( '.' )) 4569 4570" delete a character 4571call feedkeys ( "Sfoo\<BS>b\<F5>\<ESC>" , 'tx' ) 4572call assert_equal ( "fobar" , g: line ) 4573call assert_equal ( 4 , g: col ) 4574 4575set complete & 4576% d 4577call setline ( 1 , [ 'fobar' , 'foobar' ]) 4578 4579call feedkeys ( "Gofoo\<BS>\<BS>\<F5>\<ESC>" , 'tx' ) 4580call assert_equal ( "fobar" , g: line ) 4581call assert_equal ( 2 , g: col ) 4582 4583call feedkeys ( "Shello wo\<Left>\<Left>\<Left>f\<F5>\<ESC>" , 'tx' ) 4584call assert_equal ( "hello fobar wo" , g: line ) 4585call assert_equal ( 9 , g: col ) 4586 4587call feedkeys ( "Shello wo\<Left>\<Left>\<Left>f\<BS>\<F5>\<ESC>" , 'tx' ) 4588call assert_equal ( "hello wo" , g: line ) 4589call assert_equal ( 8 , g: col ) 4590 4591call feedkeys ( "Shello wo\<Left>\<Left>\<Left>foo\<F5>\<ESC>" , 'tx' ) 4592call assert_equal ( "hello foobar wo" , g: line ) 4593call assert_equal ( 11 , g: col ) 4594 4595call feedkeys ( "Shello wo\<Left>\<Left>\<Left>foo\<BS>b\<F5>\<ESC>" , 'tx' ) 4596call assert_equal ( "hello fobar wo" , g: line ) 4597call assert_equal ( 11 , g: col ) 4598 4599" confirm 4600call feedkeys ( "Sf\<C-Y>" , 'tx' ) 4601call assert_equal ( "fobar" , getline ( '.' )) 4602call assert_equal ( 5 , col ( '.' )) 4603 4604" cancel 4605call feedkeys ( "Sfo\<C-E>" , 'tx' ) 4606call assert_equal ( "fo" , getline ( '.' )) 4607call assert_equal ( 2 , col ( '.' )) 4608 4609" delete preinsert part 4610call feedkeys ( "Sfo " , 'tx' ) 4611call assert_equal ( "fo " , getline ( '.' )) 4612call assert_equal ( 3 , col ( '.' )) 4613 4614" can not work with fuzzy 4615set cot += fuzzy 4616call feedkeys ( "Sf" , 'tx' ) 4617call assert_equal ( "f" , getline ( '.' )) 4618set cot -= fuzzy 4619 4620" does not work with 'ignorecase' unless 'infercase' is also enabled 4621% d 4622call setline ( 1 , [ 'FIX' , 'fobar' , 'foobar' ]) 4623set ignorecase 4624call feedkeys ( "Gof\<F5>\<ESC>" , 'tx' ) 4625call assert_equal ( "f" , g: line ) " should not produce 'FIX' 4626set infercase 4627call feedkeys ( "Gof\<F5>\<ESC>" , 'tx' ) 4628call assert_equal ( "fix" , g: line ) 4629set ignorecase & infercase & 4630 4631% delete _ 4632let & l: undolevels = & l: undolevels 4633normal ! ifoo 4634let & l: undolevels = & l: undolevels 4635normal ! obar 4636let & l: undolevels = & l: undolevels 4637normal ! obaz 4638let & l: undolevels = & l: undolevels 4639 4640func CheckUndo () 4641let g: errmsg = '' 4642call assert_equal ([ 'foo' , 'bar' , 'baz' ], getline ( 1 , '$' )) 4643undo 4644call assert_equal ([ 'foo' , 'bar' ], getline ( 1 , '$' )) 4645undo 4646call assert_equal ([ 'foo' ], getline ( 1 , '$' )) 4647undo 4648call assert_equal ([ '' ], getline ( 1 , '$' )) 4649later 3 4650call assert_equal ([ 'foo' , 'bar' , 'baz' ], getline ( 1 , '$' )) 4651call assert_equal ( '' , v: errmsg ) 4652endfunc 4653 4654" Check that switching buffer with "preinsert" doesn't corrupt undo. 4655new 4656setlocal bufhidden = wipe 4657inoremap <buffer> < F2 > <Cmd> enew ! <CR> 4658call feedkeys ( "if\<F2>\<Esc>" , 'tx' ) 4659bwipe ! 4660call CheckUndo () 4661 4662" Check that closing window with "preinsert" doesn't corrupt undo. 4663new 4664setlocal bufhidden = wipe 4665inoremap <buffer> < F2 > <Cmd> close !<CR> 4666call feedkeys ( "if\<F2>\<Esc>" , 'tx' ) 4667call CheckUndo () 4668 4669% delete _ 4670delfunc CheckUndo 4671 4672bw ! 4673set cot & 4674set omnifunc & 4675set autocomplete & 4676call Ntest_override ( "char_avail" , 0 ) 4677delfunc Omni_test 4678delfunc GetLine 4679endfunc 4680 4681" Check that mark positions are correct after triggering multiline completion. 4682func Test_complete_multiline_marks () 4683func Omni_test ( findstart , base ) 4684if a: findstart 4685return col ( "." ) 4686endif 4687return [ 4688\ #{ word : "func ()\n\t\nend" }, 4689\ #{ word : "foobar" }, 4690\ #{ word : "你好\n\t\n我好" } 4691\ ] 4692endfunc 4693set omnifunc = Omni_test 4694 4695new 4696let lines = mapnew ( range ( 10 ), 'string(v:val)' ) 4697call setline ( 1 , lines ) 4698call setpos ( "'a" , [ 0 , 3 , 1 , 0 ]) 4699 4700call feedkeys ( "A \<C-X>\<C-O>\<C-E>\<BS>" , 'tx' ) 4701call assert_equal ( lines , getline ( 1 , '$' )) 4702call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( "'a" )) 4703 4704call feedkeys ( "A \<C-X>\<C-O>\<C-N>\<C-E>\<BS>" , 'tx' ) 4705call assert_equal ( lines , getline ( 1 , '$' )) 4706call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( "'a" )) 4707 4708call feedkeys ( "A \<C-X>\<C-O>\<C-N>\<C-N>\<C-E>\<BS>" , 'tx' ) 4709call assert_equal ( lines , getline ( 1 , '$' )) 4710call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( "'a" )) 4711 4712call feedkeys ( "A \<C-X>\<C-O>\<C-N>\<C-N>\<C-N>\<C-E>\<BS>" , 'tx' ) 4713call assert_equal ( lines , getline ( 1 , '$' )) 4714call assert_equal ([ 0 , 3 , 1 , 0 ], getpos ( "'a" )) 4715 4716call feedkeys ( "A \<C-X>\<C-O>\<C-Y>" , 'tx' ) 4717call assert_equal ([ '0 func ()' , "\t" , 'end' ] + lines [ 1 :], getline ( 1 , '$' )) 4718call assert_equal ([ 0 , 5 , 1 , 0 ], getpos ( "'a" )) 4719 4720bw ! 4721set omnifunc & 4722delfunc Omni_test 4723endfunc 4724 4725func Test_complete_match_count () 4726func ! PrintMenuWords () 4727let info = complete_info ([ "selected" , "matches" ]) 4728call map ( info . matches , { _ , v -> v . word }) 4729return info 4730endfunc 4731 4732new 4733set cpt = .^0,w 4734call setline ( 1 , [ "fo" , "foo" , "foobar" , "fobarbaz" ]) 4735exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4736call assert_equal ( 'fo{''matches'': [''fo'', ''foo'', ''foobar'', ''fobarbaz''], ''selected'': 0}' , getline ( 5 )) 47375 d 4738set cpt = .^0,w 4739exe "normal! Gof\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4740call assert_equal ( 'fobarbaz{''matches'': [''fo'', ''foo'', ''foobar'', ''fobarbaz''], ''selected'': 3}' , getline ( 5 )) 47415 d 4742set cpt = .^1,w 4743exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4744call assert_equal ( 'fo{''matches'': [''fo''], ''selected'': 0}' , getline ( 5 )) 47455 d 4746" max_matches is ignored for backward search 4747exe "normal! Gof\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4748call assert_equal ( 'fobarbaz{''matches'': [''fo'', ''foo'', ''foobar'', ''fobarbaz''], ''selected'': 3}' , getline ( 5 )) 47495 d 4750set cpt = .^2,w 4751exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4752call assert_equal ( 'fo{''matches'': [''fo'', ''foo''], ''selected'': 0}' , getline ( 5 )) 47535 d 4754set cot = menuone,noselect 4755set cpt = .^1,w 4756exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4757call assert_equal ( 'f{''matches'': [''fo''], ''selected'': -1}' , getline ( 5 )) 4758" With non-matching items 4759% d 4760call setline ( 1 , [ "free" , "freebar" , "foo" , "fobarbaz" ]) 4761set cpt = .^2,w 4762exe "normal! Gofo\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4763call assert_equal ( 'fo{''matches'': [''foo'', ''fobarbaz''], ''selected'': -1}' , getline ( 5 )) 4764set cot & 4765 4766func ComplFunc ( findstart , base ) 4767if a: findstart 4768return col ( "." ) 4769endif 4770return [ "foo1" , "foo2" , "foo3" , "foo4" ] 4771endfunc 4772 4773% d 4774set completefunc = ComplFunc 4775set cpt = .^1,F^2 4776call setline ( 1 , [ "fo" , "foo" , "foobar" , "fobarbaz" ]) 4777exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4778call assert_equal ( 'fo{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 0}' , getline ( 5 )) 47795 d 4780set cpt = .^1,,,F^2,,, 4781call setline ( 1 , [ "fo" , "foo" , "foobar" , "fobarbaz" ]) 4782exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4783call assert_equal ( 'fo{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 0}' , getline ( 5 )) 47845 d 4785exe "normal! Gof\<c-n>\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4786call assert_equal ( 'foo1{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 1}' , getline ( 5 )) 47875 d 4788exe "normal! Gof\<c-n>\<c-n>\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4789call assert_equal ( 'foo2{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 2}' , getline ( 5 )) 47905 d 4791exe "normal! Gof\<c-n>\<c-n>\<c-n>\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4792call assert_equal ( 'f{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': -1}' , getline ( 5 )) 47935 d 4794exe "normal! Gof\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4795call assert_equal ( 'fo{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 0}' , getline ( 5 )) 4796 47975 d 4798exe "normal! Gof\<c-n>\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4799call assert_equal ( 'f{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': -1}' , getline ( 5 )) 48005 d 4801exe "normal! Gof\<c-n>\<c-p>\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4802call assert_equal ( 'foo2{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 2}' , getline ( 5 )) 48035 d 4804exe "normal! Gof\<c-n>\<c-p>\<c-p>\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4805call assert_equal ( 'foo1{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 1}' , getline ( 5 )) 48065 d 4807exe "normal! Gof\<c-n>\<c-p>\<c-p>\<c-p>\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4808call assert_equal ( 'fo{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': 0}' , getline ( 5 )) 48095 d 4810exe "normal! Gof\<c-n>\<c-p>\<c-p>\<c-p>\<c-p>\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4811call assert_equal ( 'f{''matches'': [''fo'', ''foo1'', ''foo2''], ''selected'': -1}' , getline ( 5 )) 4812 4813% d 4814call setline ( 1 , [ "foo" ]) 4815set cpt = FComplFunc^2,. 4816exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4817call assert_equal ( 'foo1{''matches'': [''foo1'', ''foo2'', ''foo''], ''selected'': 0}' , getline ( 2 )) 4818bw ! 4819 4820" Test refresh:always with max_items 4821let g: CallCount = 0 4822func ! CompleteItemsSelect ( findstart , base ) 4823if a: findstart 4824return col ( '.' ) - 1 4825endif 4826let g: CallCount += 1 4827let res = [[], [ 'foobar' ], [ 'foo1' , 'foo2' , 'foo3' ], [ 'foo4' , 'foo5' , 'foo6' ]] 4828return #{ words : res [ g: CallCount ], refresh : 'always' } 4829endfunc 4830 4831new 4832set complete = .,Ffunction('CompleteItemsSelect')^2 4833call setline ( 1 , "foobarbar" ) 4834let g: CallCount = 0 4835exe "normal! Gof\<c-n>\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4836call assert_equal ( 'foobar{''matches'': [''foobarbar'', ''foobar''], ''selected'': 1}' , getline ( 2 )) 4837call assert_equal ( 1 , g: CallCount ) 4838% d 4839call setline ( 1 , "foobarbar" ) 4840let g: CallCount = 0 4841exe "normal! Gof\<c-n>\<c-p>o\<c-r>=PrintMenuWords()\<cr>" 4842call assert_equal ( 'fo{''matches'': [''foobarbar'', ''foo1'', ''foo2''], ''selected'': -1}' , getline ( 2 )) 4843call assert_equal ( 2 , g: CallCount ) 4844% d 4845call setline ( 1 , "foobarbar" ) 4846let g: CallCount = 0 4847exe "normal! Gof\<c-n>\<c-p>o\<bs>\<c-r>=PrintMenuWords()\<cr>" 4848call assert_equal ( 'f{''matches'': [''foobarbar'', ''foo4'', ''foo5''], ''selected'': -1}' , getline ( 2 )) 4849call assert_equal ( 3 , g: CallCount ) 4850bw ! 4851 4852" Test 'fuzzy' with max_items 4853new 4854set completeopt = menu,noselect,fuzzy 4855set complete = . 4856call setline ( 1 , [ "abcd" , "abac" , "abdc" ]) 4857exe "normal! Goa\<c-n>c\<c-r>=PrintMenuWords()\<cr>" 4858call assert_equal ( 'ac{''matches'': [''abac'', ''abcd'', ''abdc''], ''selected'': -1}' , getline ( 4 )) 4859exe "normal! Sa\<c-n>c\<c-n>\<c-n>\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4860call assert_equal ( 'abac{''matches'': [''abac'', ''abcd'', ''abdc''], ''selected'': 0}' , getline ( 4 )) 4861execute "normal Sa\<c-n>c\<c-n>" 4862call assert_equal ( 'abac' , getline ( 4 )) 4863execute "normal Sa\<c-n>c\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>" 4864call assert_equal ( 'abac' , getline ( 4 )) 4865set complete = .^1 4866exe "normal! Sa\<c-n>c\<c-n>\<c-n>\<c-p>\<c-r>=PrintMenuWords()\<cr>" 4867call assert_equal ( 'abac{''matches'': [''abac''], ''selected'': 0}' , getline ( 4 )) 4868execute "normal Sa\<c-n>c\<c-n>\<c-n>\<c-n>" 4869call assert_equal ( 'abac' , getline ( 4 )) 4870set complete = .^2 4871execute "normal Sa\<c-n>c\<c-n>\<c-n>\<c-n>\<c-n>" 4872call assert_equal ( 'abac' , getline ( 4 )) 4873set complete = .^3 4874execute "normal Sa\<c-n>c\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>" 4875call assert_equal ( 'abac' , getline ( 4 )) 4876set complete = .^4 4877execute "normal Sa\<c-n>c\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>" 4878call assert_equal ( 'abac' , getline ( 4 )) 4879 4880func ! ComplFunc ( findstart , base ) 4881if a: findstart 4882return col ( "." ) 4883endif 4884return [ "abcde" , "abacr" ] 4885endfunc 4886 4887set complete = .,FComplFunc^1 4888execute "normal Sa\<c-n>c\<c-n>\<c-n>" 4889call assert_equal ( 'abacr' , getline ( 4 )) 4890execute "normal Sa\<c-n>c\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>" 4891call assert_equal ( 'abac' , getline ( 4 )) 4892set complete = .^1,FComplFunc^1 4893execute "normal Sa\<c-n>c\<c-n>\<c-n>\<c-n>\<c-n>" 4894call assert_equal ( 'abac' , getline ( 4 )) 4895bw ! 4896 4897" Items with '\n' that cause menu to shift, with no leader (issue #17394) 4898func ! ComplFunc ( findstart , base ) 4899if a: findstart == 1 4900return col ( '.' ) - 1 4901endif 4902return [ "one\ntwo\nthree" , "four five six" , "hello\nworld\nhere" ] 4903endfunc 4904set completeopt = menuone,popup,noselect,fuzzy infercase 4905set complete = .^1,FComplFunc^5 4906new 4907call setline ( 1 , [ "foo" , "bar" , "baz" ]) 4908execute "normal Go\<c-n>\<c-n>\<c-n>" 4909call assert_equal ([ 'one' , 'two' , 'three' ], getline ( 4 , 6 )) 4910% d 4911call setline ( 1 , [ "foo" , "bar" , "baz" ]) 4912execute "normal Go\<c-n>\<c-n>\<c-n>\<c-p>" 4913call assert_equal ( 'foo' , getline ( 4 )) 4914execute "normal S\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>" 4915call assert_equal ( 'foo' , getline ( 4 )) 4916set complete = .^1,FComplFunc^2 4917execute "normal S\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>" 4918call assert_equal ( 'foo' , getline ( 4 )) 4919execute "normal S\<c-n>\<c-p>\<c-p>\<c-p>\<c-n>\<c-n>" 4920call assert_equal ( 'four five six' , getline ( 4 )) 4921bw ! 4922 4923set completeopt & complete & infercase & 4924delfunc PrintMenuWords 4925delfunc ComplFunc 4926delfunc CompleteItemsSelect 4927endfunc 4928 4929func Test_complete_append_selected_match_default () 4930" when typing a normal character during completion, 4931" completion is ended, see 4932" :h popupmenu-completion ("There are three states:") 4933func PrintMenuWords () 4934let info = complete_info ([ "selected" , "matches" ]) 4935call map ( info . matches , { _ , v -> v . word }) 4936return info 4937endfunc 4938 4939new 4940call setline ( 1 , [ "fo" , "foo" , "foobar" , "fobarbaz" ]) 4941exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 4942call assert_equal ( 'fo{''matches'': [''fo'', ''foo'', ''foobar'', ''fobarbaz''], ''selected'': 0}' , getline ( 5 )) 4943% d 4944call setline ( 1 , [ "fo" , "foo" , "foobar" , "fobarbaz" ]) 4945exe "normal! Gof\<c-n>o\<c-r>=PrintMenuWords()\<cr>" 4946call assert_equal ( 'foo{''matches'': [], ''selected'': -1}' , getline ( 5 )) 4947% d 4948set completeopt = menu,noselect 4949call setline ( 1 , [ "fo" , "foo" , "foobar" , "fobarbaz" ]) 4950exe "normal! Gof\<c-n>\<c-n>o\<c-r>=PrintMenuWords()\<cr>" 4951call assert_equal ( 'foo{''matches'': [], ''selected'': -1}' , getline ( 5 )) 4952bw ! 4953 4954set completeopt & 4955delfunc PrintMenuWords 4956endfunc 4957 4958" Test normal mode (^N/^P/^X^N/^X^P) with smartcase when 1) matches are first 4959" found and 2) matches are filtered (when a character is typed). 4960func Test_smartcase_normal_mode () 4961 4962func ! PrintMenu () 4963let info = complete_info ([ "matches" ]) 4964call map ( info . matches , { _ , v -> v . word }) 4965return info 4966endfunc 4967 4968func ! TestInner ( key ) 4969let pr = "\<c-r>=PrintMenu()\<cr>" 4970 4971new 4972set completeopt = menuone,noselect ignorecase smartcase 4973call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 4974exe $ "normal! ggOF{a:key}{pr}" 4975call assert_equal ( 'F{''matches'': [''Fast'', ''FAST'', ''False'', 4976\ ''FALSE'']}' , getline ( 1 )) 4977% d 4978call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 4979exe $ "normal! ggOF{a:key}a{pr}" 4980call assert_equal ( 'Fa{''matches'': [''Fast'', ''False'']}' , getline ( 1 )) 4981% d 4982call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 4983exe $ "normal! ggOF{a:key}a\<bs>{pr}" 4984call assert_equal ( 'F{''matches'': [''Fast'', ''FAST'', ''False'', 4985\ ''FALSE'']}' , getline ( 1 )) 4986% d 4987call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 4988exe $ "normal! ggOF{a:key}ax{pr}" 4989call assert_equal ( 'Fax{''matches'': []}' , getline ( 1 )) 4990% d 4991call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 4992exe $ "normal! ggOF{a:key}ax\<bs>{pr}" 4993call assert_equal ( 'Fa{''matches'': [''Fast'', ''False'']}' , getline ( 1 )) 4994 4995% d 4996call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 4997exe $ "normal! ggOF{a:key} A{pr}" 4998call assert_equal ( 'FA{''matches'': [''FAST'', ''FALSE'']}' , getline ( 1 )) 4999% d 5000call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 5001exe $ "normal! ggOF{a:key} A\<bs>{pr}" 5002call assert_equal ( 'F{''matches'': [''Fast'', ''FAST'', ''False'', 5003\ ''FALSE'']}' , getline ( 1 )) 5004% d 5005call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 5006exe $ "normal! ggOF{a:key} AL{pr}" 5007call assert_equal ( 'FAL{''matches'': [''FALSE'']}' , getline ( 1 )) 5008% d 5009call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 5010exe $ "normal! ggOF{a:key} ALx{pr}" 5011call assert_equal ( 'FALx{''matches'': []}' , getline ( 1 )) 5012% d 5013call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 5014exe $ "normal! ggOF{a:key} ALx\<bs>{pr}" 5015call assert_equal ( 'FAL{''matches'': [''FALSE'']}' , getline ( 1 )) 5016 5017% d 5018call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 5019exe $ "normal! ggOf{a:key}{pr}" 5020call assert_equal ( 'f{''matches'': [''Fast'', ''FAST'', ''False'', ''FALSE'', 5021\ ''fast'', ''false'']}' , getline ( 1 )) 5022% d 5023call setline ( 1 , [ "Fast" , "FAST" , "False" , "FALSE" , "fast" , "false" ]) 5024exe $ "normal! ggOf{a:key}a{pr}" 5025call assert_equal ( 'fa{''matches'': [''Fast'', ''FAST'', ''False'', ''FALSE'', 5026\ ''fast'', ''false'']}' , getline ( 1 )) 5027 5028% d 5029exe $ "normal! ggOf{a:key}{pr}" 5030call assert_equal ( 'f{''matches'': []}' , getline ( 1 )) 5031exe $ "normal! ggOf{a:key}a\<bs>{pr}" 5032call assert_equal ( 'f{''matches'': []}' , getline ( 1 )) 5033set ignorecase & smartcase & completeopt & 5034bw ! 5035endfunc 5036 5037call TestInner ( "\<c-n>" ) 5038call TestInner ( "\<c-p>" ) 5039call TestInner ( "\<c-x>\<c-n>" ) 5040call TestInner ( "\<c-x>\<c-p>" ) 5041delfunc PrintMenu 5042delfunc TestInner 5043endfunc 5044 5045" Test 'nearest' flag of 'completeopt' 5046func Test_nearest_cpt_option () 5047 5048func ! PrintMenuWords () 5049let info = complete_info ([ "selected" , "matches" ]) 5050call map ( info . matches , { _ , v -> v . word }) 5051return info 5052endfunc 5053 5054new 5055set completeopt += nearest 5056call setline ( 1 , [ "fo" , "foo" , "foobar" ]) 5057exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5058call assert_equal ( 'foobar{''matches'': [''foobar'', ''foo'', ''fo''], ''selected'': 0}' , getline ( 4 )) 5059% d 5060call setline ( 1 , [ "fo" , "foo" , "foobar" ]) 5061exe "normal! Of\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5062call assert_equal ( 'foobar{''matches'': [''fo'', ''foo'', ''foobar''], ''selected'': 2}' , getline ( 1 )) 5063% d 5064 5065set completeopt = menu,noselect,nearest 5066call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5067exe "normal! Gof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5068call assert_equal ( 'f{''matches'': [''foobarbaz'', ''foobar'', ''foo'', ''fo''], ''selected'': -1}' , getline ( 5 )) 5069% d 5070call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5071exe "normal! Gof\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5072call assert_equal ( 'f{''matches'': [''foobarbaz'', ''foobar'', ''foo'', ''fo''], ''selected'': -1}' , getline ( 5 )) 5073% d 5074call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5075exe "normal! Of\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5076call assert_equal ( 'f{''matches'': [''fo'', ''foo'', ''foobar'', ''foobarbaz''], ''selected'': -1}' , getline ( 1 )) 5077% d 5078call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5079exe "normal! Of\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5080call assert_equal ( 'f{''matches'': [''fo'', ''foo'', ''foobar'', ''foobarbaz''], ''selected'': -1}' , getline ( 1 )) 5081% d 5082call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5083exe "normal! of\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5084call assert_equal ( 'f{''matches'': [''foo'', ''fo'', ''foobar'', ''foobarbaz''], ''selected'': -1}' , getline ( 2 )) 5085% d 5086call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5087exe "normal! of\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5088call assert_equal ( 'f{''matches'': [''foo'', ''fo'', ''foobar'', ''foobarbaz''], ''selected'': -1}' , getline ( 2 )) 5089% d 5090call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5091exe "normal! jof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5092call assert_equal ( 'f{''matches'': [''foobar'', ''foo'', ''foobarbaz'', ''fo''], ''selected'': -1}' , getline ( 3 )) 5093% d 5094call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5095exe "normal! jof\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5096call assert_equal ( 'f{''matches'': [''foobar'', ''foo'', ''foobarbaz'', ''fo''], ''selected'': -1}' , getline ( 3 )) 5097% d 5098call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5099exe "normal! 2jof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5100call assert_equal ( 'f{''matches'': [''foobarbaz'', ''foobar'', ''foo'', ''fo''], ''selected'': -1}' , getline ( 4 )) 5101% d 5102call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5103exe "normal! 2jof\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5104call assert_equal ( 'f{''matches'': [''foobarbaz'', ''foobar'', ''foo'', ''fo''], ''selected'': -1}' , getline ( 4 )) 5105 5106% d 5107set completeopt = menuone,noselect,nearest 5108call setline ( 1 , "foo" ) 5109exe "normal! Of\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5110call assert_equal ( 'f{''matches'': [''foo''], ''selected'': -1}' , getline ( 1 )) 5111% d 5112call setline ( 1 , "foo" ) 5113exe "normal! o\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5114call assert_equal ( '{''matches'': [''foo''], ''selected'': -1}' , getline ( 2 )) 5115% d 5116exe "normal! o\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5117call assert_equal ( '' , getline ( 1 )) 5118% d 5119exe "normal! o\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5120call assert_equal ( '' , getline ( 1 )) 5121 5122" Reposition match: node is at tail but score is too small 5123% d 5124call setline ( 1 , [ "foo1" , "bar1" , "bar2" , "foo2" , "foo1" ]) 5125exe "normal! of\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5126call assert_equal ( 'f{''matches'': [''foo1'', ''foo2''], ''selected'': -1}' , getline ( 2 )) 5127" Reposition match: node is in middle but score is too big 5128% d 5129call setline ( 1 , [ "foo1" , "bar1" , "bar2" , "foo3" , "foo1" , "foo2" ]) 5130exe "normal! of\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5131call assert_equal ( 'f{''matches'': [''foo1'', ''foo3'', ''foo2''], ''selected'': -1}' , getline ( 2 )) 5132 5133" Multiple sources 5134func F1 ( findstart , base ) 5135if a: findstart 5136return col ( '.' ) - 1 5137endif 5138return [ 'foo4' , 'foo5' ] 5139endfunc 5140% d 5141set complete += FF1 5142call setline ( 1 , [ "foo1" , "foo2" , "bar1" , "foo3" ]) 5143exe "normal! 2jof\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5144call assert_equal ( 'f{''matches'': [''foo3'', ''foo2'', ''foo1'', ''foo4'', ''foo5''], 5145\ ''selected'': -1}' , getline ( 4 )) 5146set complete -= FF1 5147delfunc F1 5148 5149set completeopt = menu,longest,nearest 5150% d 5151call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5152exe "normal! of\<c-n>\<c-r>=PrintMenuWords()\<cr>" 5153call assert_equal ( 'fo{''matches'': [''foo'', ''fo'', ''foobar'', ''foobarbaz''], ''selected'': -1}' , getline ( 2 )) 5154% d 5155call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5156exe "normal! 2jof\<c-p>\<c-r>=PrintMenuWords()\<cr>" 5157call assert_equal ( 'fo{''matches'': [''foobarbaz'', ''foobar'', ''foo'', ''fo''], ''selected'': -1}' , getline ( 4 )) 5158bw ! 5159 5160set completeopt & 5161delfunc PrintMenuWords 5162endfunc 5163 5164func Test_register_completion () 5165let @a = "completion test apple application" 5166let @b = "banana behavior better best" 5167let @c = "complete completion compliment computer" 5168let g: save_reg = '' 5169func GetItems () 5170let g: result = complete_info ([ 'pum_visible' ]) 5171endfunc 5172 5173new 5174call setline ( 1 , "comp" ) 5175call cursor ( 1 , 4 ) 5176call feedkeys ( "a\<C-X>\<C-R>\<C-N>\<C-N>\<Esc>" , 'tx' ) 5177call assert_equal ( "compliment" , getline ( 1 )) 5178 5179inoremap <buffer> < F2 > < C-R > =GetItems() < CR > 5180call feedkeys ( "S\<C-X>\<C-R>\<F2>\<ESC>" , 'tx' ) 5181call assert_equal ( 1 , g: result [ 'pum_visible' ]) 5182 5183call setline ( 1 , "app" ) 5184call cursor ( 1 , 3 ) 5185call feedkeys ( "a\<C-X>\<C-R>\<C-N>\<Esc>" , 'tx' ) 5186call assert_equal ( "application" , getline ( 1 )) 5187 5188" Test completion with case differences 5189set ignorecase 5190let @e = "TestCase UPPERCASE lowercase" 5191call setline ( 1 , "testc" ) 5192call cursor ( 1 , 5 ) 5193call feedkeys ( "a\<C-X>\<C-R>\<Esc>" , 'tx' ) 5194call assert_equal ( "TestCase" , getline ( 1 )) 5195 5196" Test clipboard registers if available 5197if has ( 'clipboard_working' ) 5198let g: save_reg = getreg ( '*' ) 5199call setreg ( '*' , "clipboard selection unique words" ) 5200call setline ( 1 , "uni" ) 5201call cursor ( 1 , 3 ) 5202call feedkeys ( "a\<C-X>\<C-R>\<Esc>" , 'tx' ) 5203call assert_equal ( "unique" , getline ( 1 )) 5204call setreg ( '*' , g: save_reg ) 5205 5206let g: save_reg = getreg ( '+' ) 5207call setreg ( '+' , "system clipboard special content" ) 5208call setline ( 1 , "spe" ) 5209call cursor ( 1 , 3 ) 5210call feedkeys ( "a\<C-X>\<C-R>\<Esc>" , 'tx' ) 5211call assert_equal ( "special" , getline ( 1 )) 5212call setreg ( '+' , g: save_reg ) 5213 5214call setreg ( '*' , g: save_reg ) 5215call setreg ( 'a' , "normal register" ) 5216call setreg ( '*' , "clipboard mixed content" ) 5217call setline ( 1 , "mix" ) 5218call cursor ( 1 , 3 ) 5219call feedkeys ( "a\<C-X>\<C-R>\<Esc>" , 'tx' ) 5220call assert_equal ( "mixed" , getline ( 1 )) 5221call setreg ( '*' , g: save_reg ) 5222endif 5223 5224" Test black hole register should be skipped 5225let @_ = "blackhole content should not appear" 5226call setline ( 1 , "black" ) 5227call cursor ( 1 , 5 ) 5228call feedkeys ( "a\<C-X>\<C-R>\<Esc>" , 'tx' ) 5229call assert_equal ( "black" , getline ( 1 )) 5230 5231let @1 = "recent yank zero" 5232call setline ( 1 , "ze" ) 5233call cursor ( 1 , 2 ) 5234call feedkeys ( "a\<C-X>\<C-R>\<Esc>" , 'tx' ) 5235call assert_equal ( "zero" , getline ( 1 )) 5236 5237call feedkeys ( "Sze\<C-X>\<C-R>\<C-R>=string(complete_info(['mode']))\<CR>\<ESC>" , "tx" ) 5238call assert_equal ( "zero{'mode': 'register'}" , getline ( 1 )) 5239 5240" Test consecutive CTRL-X CTRL-R (adding mode) 5241" First CTRL-X CTRL-R should split into words, second should use full content 5242let @f = "hello world test complete" 5243call setline ( 1 , "hel" ) 5244call cursor ( 1 , 3 ) 5245call feedkeys ( "a\<C-X>\<C-R>\<C-N>\<Esc>" , 'tx' ) 5246call assert_equal ( "hello" , getline ( 1 )) 5247 5248" Second consecutive CTRL-X CTRL-R should complete with full content 5249call setline ( 1 , "hello" ) 5250call cursor ( 1 , 5 ) 5251call feedkeys ( "a\<C-X>\<C-R>\<C-X>\<C-R>\<Esc>" , 'tx' ) 5252call assert_equal ( "hello world test complete" , getline ( 1 )) 5253 5254" Test consecutive completion with multi-line register 5255let @g = "first line content\nsecond line here\nthird line data" 5256call setline ( 1 , "first" ) 5257call cursor ( 1 , 5 ) 5258call feedkeys ( "a\<C-X>\<C-R>\<C-X>\<C-R>\<Esc>" , 'tx' ) 5259call assert_equal ( "first line content" , getline ( 1 )) 5260 5261" Clean up 5262bwipe ! 5263delfunc GetItems 5264unlet g: result 5265unlet g: save_reg 5266set ignorecase & 5267endfunc 5268 5269" Test refresh:always with unloaded buffers (issue #17363) 5270func Test_complete_unloaded_buf_refresh_always () 5271func TestComplete ( findstart , base ) 5272if a: findstart 5273let line = getline ( '.' ) 5274let start = col ( '.' ) - 1 5275while start > 0 && line [ start - 1 ] =~ '\a' 5276let start -= 1 5277endwhile 5278return start 5279else 5280let g: CallCount += 1 5281let res = [ "update1" , "update12" , "update123" ] 5282return #{ words : res , refresh : 'always' } 5283endif 5284endfunc 5285 5286let g: CallCount = 0 5287set completeopt = menu,longest 5288set completefunc = TestComplete 5289set complete = b,u,t,i,F 5290badd foo1 5291badd foo2 5292new 5293exe "normal! iup\<C-N>\<BS>\<BS>\<BS>\<BS>\<BS>" 5294call assert_equal ( 'up' , getline ( 1 )) 5295call assert_equal ( 6 , g: CallCount ) 5296 5297bd ! foo1 5298bd ! foo2 5299bw ! 5300set completeopt & 5301set complete & 5302set completefunc & 5303delfunc TestComplete 5304endfunc 5305 5306" Verify that the order of matches from each source is consistent 5307" during both ^N and ^P completions (Issue #17425). 5308func Test_complete_with_multiple_function_sources () 5309func F1 ( findstart , base ) 5310if a: findstart 5311return col ( '.' ) - 1 5312endif 5313return [ 'one' , 'two' , 'three' ] 5314endfunc 5315 5316func F2 ( findstart , base ) 5317if a: findstart 5318return col ( '.' ) - 1 5319endif 5320return [ 'four' , 'five' , 'six' ] 5321endfunc 5322 5323func F3 ( findstart , base ) 5324if a: findstart 5325return col ( '.' ) - 1 5326endif 5327return [ 'seven' , 'eight' , 'nine' ] 5328endfunc 5329 5330new 5331setlocal complete = .,FF1,FF2,FF3 5332inoremap <buffer> < F2 > <Cmd> let b: matches = complete_info ([ "matches" ]). matches <CR> 5333call setline ( 1 , [ 'xxx' , 'yyy' , 'zzz' , '' ]) 5334 5335call feedkeys ( "GS\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5336call assert_equal ([ 5337\ 'xxx' , 'yyy' , 'zzz' , 5338\ 'one' , 'two' , 'three' , 5339\ 'four' , 'five' , 'six' , 5340\ 'seven' , 'eight' , 'nine' , 5341\ ], b: matches -> mapnew ( 'v:val.word' )) 5342 5343call feedkeys ( "GS\<C-P>\<F2>\<Esc>0" , 'tx!' ) 5344call assert_equal ([ 5345\ 'seven' , 'eight' , 'nine' , 5346\ 'four' , 'five' , 'six' , 5347\ 'one' , 'two' , 'three' , 5348\ 'xxx' , 'yyy' , 'zzz' , 5349\ ], b: matches -> mapnew ( 'v:val.word' )) 5350 5351% delete 5352 5353call feedkeys ( "GS\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5354call assert_equal ([ 5355\ 'one' , 'two' , 'three' , 5356\ 'four' , 'five' , 'six' , 5357\ 'seven' , 'eight' , 'nine' , 5358\ ], b: matches -> mapnew ( 'v:val.word' )) 5359 5360call feedkeys ( "GS\<C-P>\<F2>\<Esc>0" , 'tx!' ) 5361call assert_equal ([ 5362\ 'seven' , 'eight' , 'nine' , 5363\ 'four' , 'five' , 'six' , 5364\ 'one' , 'two' , 'three' , 5365\ ], b: matches -> mapnew ( 'v:val.word' )) 5366 5367bwipe ! 5368delfunc F1 5369delfunc F2 5370delfunc F3 5371endfunc 5372 5373func Test_complete_fuzzy_omnifunc_backspace () 5374let g: do_complete = v: false 5375func Omni_test ( findstart , base ) 5376if a: findstart 5377let g: do_complete = ! g: do_complete 5378endif 5379if g: do_complete 5380return a: findstart ? 0 : [#{ word : a: base .. 'def' }, #{ word : a: base .. 'ghi' }] 5381endif 5382return a: findstart ? -3 : {} 5383endfunc 5384 5385new 5386redraw " need this to prevent NULL dereference in Nvim 5387setlocal omnifunc = Omni_test 5388setlocal completeopt = menuone,fuzzy,noinsert 5389call setline ( 1 , 'abc' ) 5390call feedkeys ( "A\<C-X>\<C-O>\<BS>\<Esc>0" , 'tx!' ) 5391call assert_equal ( 'ab' , getline ( 1 )) 5392 5393bwipe ! 5394delfunc Omni_test 5395unlet g: do_complete 5396endfunc 5397 5398" Test that option shortmess=c turns off completion messages 5399func Test_shortmess () 5400CheckScreendump 5401 5402let lines =<< trim END 5403call setline(1, ['hello', 'hullo', 'heee']) 5404END 5405 5406call writefile ( lines , 'Xpumscript' , 'D' ) 5407let buf = RunVimInTerminal ( '-S Xpumscript' , #{ rows : 12 }) 5408call term_sendkeys ( buf , "Goh\<C-N>" ) 5409call TermWait ( buf , 200 ) 5410call VerifyScreenDump ( buf , 'Test_shortmess_complmsg_1' , {}) 5411call term_sendkeys ( buf , "\<ESC>:set shm+=c\<CR>" ) 5412call term_sendkeys ( buf , "Sh\<C-N>" ) 5413call TermWait ( buf , 200 ) 5414call VerifyScreenDump ( buf , 'Test_shortmess_complmsg_2' , {}) 5415 5416call StopVimInTerminal ( buf ) 5417endfunc 5418 5419" Test 'complete' containing F{func} that complete from nonkeyword 5420func Test_nonkeyword_trigger () 5421 5422" Trigger expansion even when another char is waiting in the typehead 5423call Ntest_override ( "char_avail" , 1 ) 5424 5425let g: CallCount = 0 5426func ! NonKeywordComplete ( findstart , base ) 5427let line = getline ( '.' ) -> strpart ( 0 , col ( '.' ) - 1 ) 5428let nonkeyword2 = len ( line ) > 1 && match ( line [ -2 : -2 ], '\k' ) != 0 5429if a: findstart 5430return nonkeyword2 ? col ( '.' ) - 3 : ( col ( '.' ) - 2 ) 5431else 5432let g: CallCount += 1 5433return [ $ "{a:base}foo", $"{a:base}bar"] 5434endif 5435endfunc 5436 5437new 5438inoremap <buffer> < F2 > <Cmd> let b: matches = complete_info ([ "matches" ]). matches <CR> 5439inoremap <buffer> < F3 > <Cmd> let b: selected = complete_info ([ "selected" ]). selected <CR> 5440call setline ( 1 , [ 'abc' , 'abcd' , 'fo' , 'b' , '' ]) 5441 5442" Test 1a: Nonkeyword before cursor lists words with at least two letters 5443call feedkeys ( "GS=\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5444call assert_equal ([ 'abc' , 'abcd' , 'fo' ], b: matches -> mapnew ( 'v:val.word' )) 5445call assert_equal ( '=abc' , getline ( '.' )) 5446 5447" Test 1b: With F{func} nonkeyword collects matches 5448set complete = .,FNonKeywordComplete 5449for noselect in range ( 2 ) 5450if noselect 5451set completeopt += noselect 5452endif 5453let g: CallCount = 0 5454call feedkeys ( "S=\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5455call assert_equal ([ 'abc' , 'abcd' , 'fo' , '=foo' , '=bar' ], b: matches -> mapnew ( 'v:val.word' )) 5456call assert_equal ( 1 , g: CallCount ) 5457call assert_equal ( noselect ? '=' : '=abc' , getline ( '.' )) 5458let g: CallCount = 0 5459call feedkeys ( "S->\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5460call assert_equal ([ 'abc' , 'abcd' , 'fo' , '->foo' , '->bar' ], b: matches -> mapnew ( 'v:val.word' )) 5461call assert_equal ( 1 , g: CallCount ) 5462call assert_equal ( noselect ? '->' : '->abc' , getline ( '.' )) 5463set completeopt & 5464endfor 5465 5466" Test 1c: Keyword collects from {func} 5467let g: CallCount = 0 5468call feedkeys ( "Sa\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5469call assert_equal ([ 'abc' , 'abcd' , 'afoo' , 'abar' ], b: matches -> mapnew ( 'v:val.word' )) 5470call assert_equal ( 1 , g: CallCount ) 5471call assert_equal ( 'abc' , getline ( '.' )) 5472 5473set completeopt += noselect 5474let g: CallCount = 0 5475call feedkeys ( "Sa\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5476call assert_equal ([ 'abc' , 'abcd' , 'afoo' , 'abar' ], b: matches -> mapnew ( 'v:val.word' )) 5477call assert_equal ( 1 , g: CallCount ) 5478call assert_equal ( 'a' , getline ( '.' )) 5479 5480" Test 1d: Nonkeyword after keyword collects items again 5481let g: CallCount = 0 5482call feedkeys ( "Sa\<C-N>#\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5483call assert_equal ([ 'abc' , 'abcd' , 'fo' , '#foo' , '#bar' ], b: matches -> mapnew ( 'v:val.word' )) 5484call assert_equal ( 2 , g: CallCount ) 5485call assert_equal ( 'a#' , getline ( '.' )) 5486set completeopt & 5487 5488" Test 2: Filter nonkeyword and keyword matches with different startpos 5489set completeopt += menuone,noselect 5490call feedkeys ( "S#a\<C-N>b\<F2>\<F3>\<Esc>0" , 'tx!' ) 5491call assert_equal ([ 'abc' , 'abcd' , '#abar' ], b: matches -> mapnew ( 'v:val.word' )) 5492call assert_equal ( -1 , b: selected ) 5493call assert_equal ( '#ab' , getline ( '.' )) 5494 5495set completeopt += fuzzy 5496call feedkeys ( "S#a\<C-N>b\<F2>\<F3>\<Esc>0" , 'tx!' ) 5497call assert_equal ([ '#abar' , 'abc' , 'abcd' ], b: matches -> mapnew ( 'v:val.word' )) 5498call assert_equal ( -1 , b: selected ) 5499call assert_equal ( '#ab' , getline ( '.' )) 5500set completeopt & 5501 5502" Test 3: Navigate menu containing nonkeyword and keyword items 5503call feedkeys ( "S->\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5504call assert_equal ([ 'abc' , 'abcd' , 'fo' , '->foo' , '->bar' ], b: matches -> mapnew ( 'v:val.word' )) 5505call assert_equal ( '->abc' , getline ( '.' )) 5506call feedkeys ( "S->" . repeat ( "\<C-N>" , 3 ) . "\<Esc>0" , 'tx!' ) 5507call assert_equal ( '->fo' , getline ( '.' )) 5508call feedkeys ( "S->" . repeat ( "\<C-N>" , 4 ) . "\<Esc>0" , 'tx!' ) 5509call assert_equal ( '->foo' , getline ( '.' )) 5510call feedkeys ( "S->" . repeat ( "\<C-N>" , 4 ) . "\<C-P>\<Esc>0" , 'tx!' ) 5511call assert_equal ( '->fo' , getline ( '.' )) 5512call feedkeys ( "S->" . repeat ( "\<C-N>" , 5 ) . "\<Esc>0" , 'tx!' ) 5513call assert_equal ( '->bar' , getline ( '.' )) 5514call feedkeys ( "S->" . repeat ( "\<C-N>" , 5 ) . "\<C-P>\<Esc>0" , 'tx!' ) 5515call assert_equal ( '->foo' , getline ( '.' )) 5516call feedkeys ( "S->" . repeat ( "\<C-N>" , 6 ) . "\<Esc>0" , 'tx!' ) 5517call assert_equal ( '->' , getline ( '.' )) 5518call feedkeys ( "S->" . repeat ( "\<C-N>" , 7 ) . "\<Esc>0" , 'tx!' ) 5519call assert_equal ( '->abc' , getline ( '.' )) 5520call feedkeys ( "S->" . repeat ( "\<C-P>" , 7 ) . "\<Esc>0" , 'tx!' ) 5521call assert_equal ( '->fo' , getline ( '.' )) 5522" Replace 5523call feedkeys ( "S# x y z\<Esc>0lR\<C-N>\<Esc>0" , 'tx!' ) 5524call assert_equal ( '#abcy z' , getline ( '.' )) 5525call feedkeys ( "S# x y z\<Esc>0lR" . repeat ( "\<C-P>" , 4 ) . "\<Esc>0" , 'tx!' ) 5526call assert_equal ( '#bary z' , getline ( '.' )) 5527 5528bw ! 5529call Ntest_override ( "char_avail" , 0 ) 5530delfunc NonKeywordComplete 5531set complete & 5532unlet g: CallCount 5533endfunc 5534 5535func Test_autocomplete_trigger () 5536" Trigger expansion even when another char is waiting in the typehead 5537call Ntest_override ( "char_avail" , 1 ) 5538 5539let g: CallCount = 0 5540func ! NonKeywordComplete ( findstart , base ) 5541let line = getline ( '.' ) -> strpart ( 0 , col ( '.' ) - 1 ) 5542let nonkeyword2 = len ( line ) > 1 && match ( line [ -2 : -2 ], '\k' ) != 0 5543if a: findstart 5544return nonkeyword2 ? col ( '.' ) - 3 : ( col ( '.' ) - 2 ) 5545else 5546let g: CallCount += 1 5547return [ $ "{a:base}foo", $"{a:base}bar"] 5548endif 5549endfunc 5550 5551new 5552inoremap <buffer> < F2 > <Cmd> let b: matches = complete_info ([ "matches" ]). matches <CR> 5553inoremap <buffer> < F3 > <Cmd> let b: selected = complete_info ([ "matches" , "selected" ]). selected <CR> 5554 5555call setline ( 1 , [ 'abc' , 'abcd' , 'fo' , 'b' , '' ]) 5556set autocomplete 5557 5558" Test 1a: Nonkeyword doesn't open menu without F{func} when autocomplete 5559call feedkeys ( "GS=\<F2>\<Esc>0" , 'tx!' ) 5560call assert_equal ([], b: matches ) 5561call assert_equal ( '=' , getline ( '.' )) 5562" ^N opens menu of keywords (of len > 1) 5563call feedkeys ( "S=\<C-E>\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5564call assert_equal ([ 'abc' , 'abcd' , 'fo' ], b: matches -> mapnew ( 'v:val.word' )) 5565call assert_equal ( '=abc' , getline ( '.' )) 5566 5567" Test 1b: With F{func} nonkeyword collects matches 5568set complete = .,FNonKeywordComplete 5569let g: CallCount = 0 5570call feedkeys ( "S=\<F2>\<Esc>0" , 'tx!' ) 5571call assert_equal ([ '=foo' , '=bar' ], b: matches -> mapnew ( 'v:val.word' )) 5572call assert_equal ( 1 , g: CallCount ) 5573call assert_equal ( '=' , getline ( '.' )) 5574let g: CallCount = 0 5575call feedkeys ( "S->\<F2>\<Esc>0" , 'tx!' ) 5576call assert_equal ([ '->foo' , '->bar' ], b: matches -> mapnew ( 'v:val.word' )) 5577call assert_equal ( 2 , g: CallCount ) 5578call assert_equal ( '->' , getline ( '.' )) 5579 5580" Test 1c: Keyword after nonkeyword can collect both types of items 5581let g: CallCount = 0 5582call feedkeys ( "S#a\<F2>\<Esc>0" , 'tx!' ) 5583call assert_equal ([ 'abcd' , 'abc' , '#afoo' , '#abar' ], b: matches -> mapnew ( 'v:val.word' )) 5584call assert_equal ( 2 , g: CallCount ) 5585call assert_equal ( '#a' , getline ( '.' )) 5586let g: CallCount = 0 5587call feedkeys ( "S#a.\<F2>\<Esc>0" , 'tx!' ) 5588call assert_equal ([ '.foo' , '.bar' ], b: matches -> mapnew ( 'v:val.word' )) 5589call assert_equal ( 3 , g: CallCount ) 5590call assert_equal ( '#a.' , getline ( '.' )) 5591let g: CallCount = 0 5592call feedkeys ( "S#a.a\<F2>\<Esc>0" , 'tx!' ) 5593call assert_equal ([ 'abcd' , 'abc' , '.afoo' , '.abar' ], b: matches -> mapnew ( 'v:val.word' )) 5594call assert_equal ( 4 , g: CallCount ) 5595call assert_equal ( '#a.a' , getline ( '.' )) 5596 5597" Test 1d: Nonkeyword after keyword collects items again 5598let g: CallCount = 0 5599call feedkeys ( "Sa\<F2>\<Esc>0" , 'tx!' ) 5600call assert_equal ([ 'abcd' , 'abc' , 'afoo' , 'abar' ], b: matches -> mapnew ( 'v:val.word' )) 5601call assert_equal ( 1 , g: CallCount ) 5602call assert_equal ( 'a' , getline ( '.' )) 5603let g: CallCount = 0 5604call feedkeys ( "Sa#\<F2>\<Esc>0" , 'tx!' ) 5605call assert_equal ([ '#foo' , '#bar' ], b: matches -> mapnew ( 'v:val.word' )) 5606call assert_equal ( 2 , g: CallCount ) 5607call assert_equal ( 'a#' , getline ( '.' )) 5608 5609" Test 2: Filter nonkeyword and keyword matches with different startpos 5610for fuzzy in range ( 2 ) 5611if fuzzy 5612set completeopt += fuzzy 5613endif 5614call feedkeys ( "S#ab\<F2>\<F3>\<Esc>0" , 'tx!' ) 5615if fuzzy 5616call assert_equal ([ '#abar' , 'abc' , 'abcd' ], b: matches -> mapnew ( 'v:val.word' )) 5617else " Ordering of items is by 'nearest' to cursor by default 5618call assert_equal ([ 'abcd' , 'abc' , '#abar' ], b: matches -> mapnew ( 'v:val.word' )) 5619endif 5620call assert_equal ( -1 , b: selected ) 5621call assert_equal ( '#ab' , getline ( '.' )) 5622call feedkeys ( "S#ab" . repeat ( "\<C-N>" , 3 ) . "\<F3>\<Esc>0" , 'tx!' ) 5623call assert_equal ( fuzzy ? '#abcd' : '#abar' , getline ( '.' )) 5624call assert_equal ( 2 , b: selected ) 5625 5626let g: CallCount = 0 5627call feedkeys ( "GS#aba\<F2>\<Esc>0" , 'tx!' ) 5628call assert_equal ([ '#abar' ], b: matches -> mapnew ( 'v:val.word' )) 5629call assert_equal ( 2 , g: CallCount ) 5630call assert_equal ( '#aba' , getline ( '.' )) 5631 5632let g: CallCount = 0 5633call feedkeys ( "S#abc\<F2>\<Esc>0" , 'tx!' ) 5634if fuzzy 5635call assert_equal ([ 'abc' , 'abcd' ], b: matches -> mapnew ( 'v:val.word' )) 5636else 5637call assert_equal ([ 'abcd' , 'abc' ], b: matches -> mapnew ( 'v:val.word' )) 5638endif 5639call assert_equal ( 2 , g: CallCount ) 5640set completeopt & 5641endfor 5642 5643" Test 3: Navigate menu containing nonkeyword and keyword items 5644call feedkeys ( "S#a\<F2>\<Esc>0" , 'tx!' ) 5645call assert_equal ([ 'abcd' , 'abc' , '#afoo' , '#abar' ], b: matches -> mapnew ( 'v:val.word' )) 5646call feedkeys ( "S#a" . repeat ( "\<C-N>" , 3 ) . "\<Esc>0" , 'tx!' ) 5647call assert_equal ( '#afoo' , getline ( '.' )) 5648call feedkeys ( "S#a" . repeat ( "\<C-N>" , 3 ) . "\<C-P>\<Esc>0" , 'tx!' ) 5649call assert_equal ( '#abc' , getline ( '.' )) 5650 5651call feedkeys ( "S#a.a\<F2>\<Esc>0" , 'tx!' ) 5652call assert_equal ([ 'abcd' , 'abc' , '.afoo' , '.abar' ], b: matches -> mapnew ( 'v:val.word' )) 5653call feedkeys ( "S#a.a" . repeat ( "\<C-N>" , 2 ) . "\<Esc>0" , 'tx!' ) 5654call assert_equal ( '#a.abc' , getline ( '.' )) 5655call feedkeys ( "S#a.a" . repeat ( "\<C-N>" , 3 ) . "\<Esc>0" , 'tx!' ) 5656call assert_equal ( '#a.afoo' , getline ( '.' )) 5657call feedkeys ( "S#a.a" . repeat ( "\<C-N>" , 3 ) . "\<C-P>\<Esc>0" , 'tx!' ) 5658call assert_equal ( '#a.abc' , getline ( '.' )) 5659call feedkeys ( "S#a.a" . repeat ( "\<C-P>" , 6 ) . "\<Esc>0" , 'tx!' ) 5660call assert_equal ( '#a.abar' , getline ( '.' )) 5661 5662" Test 4a: When autocomplete menu is active, ^X^N completes buffer keywords 5663let g: CallCount = 0 5664call feedkeys ( "S#a\<C-E>\<C-X>\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5665call assert_equal ([ 'abc' , 'abcd' ], b: matches -> mapnew ( 'v:val.word' )) 5666call assert_equal ( 2 , g: CallCount ) 5667 5668" Test 4b: When autocomplete menu is active, ^X^O completes omnifunc 5669let g: CallCount = 0 5670set omnifunc = NonKeywordComplete 5671call feedkeys ( "S#a\<C-X>\<C-O>\<F2>\<Esc>0" , 'tx!' ) 5672call assert_equal ([ '#afoo' , '#abar' ], b: matches -> mapnew ( 'v:val.word' )) 5673call assert_equal ( 3 , g: CallCount ) 5674 5675" Test 4c: When autocomplete menu is active, ^E^N completes keyword 5676call feedkeys ( "Sa\<C-E>\<F2>\<Esc>0" , 'tx!' ) 5677call assert_equal ([], b: matches -> mapnew ( 'v:val.word' )) 5678let g: CallCount = 0 5679call feedkeys ( "Sa\<C-E>\<C-N>\<F2>\<Esc>0" , 'tx!' ) 5680call assert_equal ([ 'abc' , 'abcd' , 'afoo' , 'abar' ], b: matches -> mapnew ( 'v:val.word' )) 5681call assert_equal ( 2 , g: CallCount ) 5682 5683" Test 4d: When autocomplete menu is active, ^X^L completes lines 5684% d 5685let g: CallCount = 0 5686call setline ( 1 , [ "afoo bar" , "barbar foo" , "foo bar" , "and" ]) 5687call feedkeys ( "Goa\<C-X>\<C-L>\<F2>\<Esc>0" , 'tx!' ) 5688call assert_equal ([ 'afoo bar' , 'and' ], b: matches -> mapnew ( 'v:val.word' )) 5689call assert_equal ( 1 , g: CallCount ) 5690 5691" Issue #18044 5692% d 5693call setline ( 1 , [ "first line" , "second line" ]) 5694call feedkeys ( "Gof\<C-X>\<C-L>\<Esc>" , 'tx!' ) 5695call assert_equal ( "first line" , getline ( 3 )) 5696call feedkeys ( "Sf\<C-X>\<C-L>\<C-X>\<C-L>\<Esc>" , 'tx!' ) 5697call assert_equal ( "second line" , getline ( 4 )) 5698 5699" Test 5: When invalid prefix stops completion, backspace should restart it 5700% d 5701set complete & 5702call setline ( 1 , [ "afoo bar" , "barbar foo" , "foo bar" , "and" ]) 5703call feedkeys ( "Goabc\<F2>\<Esc>0" , 'tx!' ) 5704call assert_equal ([], b: matches -> mapnew ( 'v:val.word' )) 5705call feedkeys ( "Sabc\<BS>\<BS>\<F2>\<Esc>0" , 'tx!' ) 5706call assert_equal ([ 'and' , 'afoo' ], b: matches -> mapnew ( 'v:val.word' )) 5707call feedkeys ( "Szx\<BS>\<F2>\<Esc>0" , 'tx!' ) 5708call assert_equal ([], b: matches -> mapnew ( 'v:val.word' )) 5709call feedkeys ( "Sazx\<Left>\<BS>\<F2>\<Esc>0" , 'tx!' ) 5710call assert_equal ([ 'and' , 'afoo' ], b: matches -> mapnew ( 'v:val.word' )) 5711 5712" Test 6: <BS> should clear the selected item (PR #18265) 5713% d 5714call setline ( 1 , [ "foobarfoo" , "foobar" , "foobarbaz" ]) 5715call feedkeys ( "Gofo\<C-N>\<C-N>\<F2>\<F3>\<Esc>0" , 'tx!' ) 5716call assert_equal ([ 'foobarbaz' , 'foobar' , 'foobarfoo' ], b: matches -> mapnew ( 'v:val.word' )) 5717call assert_equal ( 1 , b: selected ) 5718call feedkeys ( "Sfo\<C-N>\<C-N>\<BS>\<F2>\<F3>\<Esc>0" , 'tx!' ) 5719call assert_equal ([ 'foobarbaz' , 'foobar' , 'foobarfoo' ], b: matches -> mapnew ( 'v:val.word' )) 5720call assert_equal ( -1 , b: selected ) 5721call assert_equal ( 'fooba' , getline ( 4 )) 5722call feedkeys ( "Sfo\<C-N>\<C-N>\<BS>\<C-N>\<F2>\<F3>\<Esc>0" , 'tx!' ) 5723call assert_equal ([ 'foobarbaz' , 'foobar' , 'foobarfoo' ], b: matches -> mapnew ( 'v:val.word' )) 5724call assert_equal ( 0 , b: selected ) 5725call assert_equal ( 'foobarbaz' , getline ( 4 )) 5726 5727" Test 7: Remove selection when menu contents change (PR #18265) 5728% d 5729call setline ( 1 , [ "foobar" , "fodxyz" , "fodabc" ]) 5730call feedkeys ( "Gofoo\<C-N>\<BS>\<BS>\<BS>\<BS>d\<F2>\<F3>\<Esc>0" , 'tx!' ) 5731call assert_equal ([ 'fodabc' , 'fodxyz' ], b: matches -> mapnew ( 'v:val.word' )) 5732call assert_equal ( -1 , b: selected ) 5733 5734" Test 8: Ctrl_W / Ctrl_U (delete word/line) should restart autocompletion 5735func ! TestComplete ( findstart , base ) 5736if a: findstart 5737return col ( '.' ) - 1 5738endif 5739return [ 'fooze' , 'faberge' ] 5740endfunc 5741set omnifunc = TestComplete 5742set complete += o 5743call feedkeys ( "Sprefix->fo\<F2>\<Esc>0" , 'tx!' ) 5744call assert_equal ([ 'fodabc' , 'fodxyz' , 'foobar' , 'fooze' ], b: matches -> mapnew ( 'v:val.word' )) 5745call feedkeys ( "Sprefix->fo\<C-W>\<F2>\<Esc>0" , 'tx!' ) 5746call assert_equal ([ 'fooze' , 'faberge' ], b: matches -> mapnew ( 'v:val.word' )) 5747call feedkeys ( "Sprefix->\<Esc>afo\<C-U>\<F2>\<Esc>0" , 'tx!' ) 5748call assert_equal ([ 'fooze' , 'faberge' ], b: matches -> mapnew ( 'v:val.word' )) 5749 5750" Test 9: Trigger autocomplete immediately upon entering Insert mode 5751" 'faberge' is filtered out because it doesn't start with the current prefix 5752" 'foo'; non-prefix omnifunc matches are excluded from the PUM when leader 5753" is NULL (compl_orig_text is used as a fallback filter). 5754call feedkeys ( "Sprefix->foo\<Esc>a\<F2>\<Esc>0" , 'tx!' ) 5755call assert_equal ([ 'foobar' , 'fooze' ], b: matches -> mapnew ( 'v:val.word' )) 5756call feedkeys ( "Sprefix->fooxx\<Esc>hcw\<F2>\<Esc>0" , 'tx!' ) 5757call assert_equal ([ 'foobar' , 'fooze' ], b: matches -> mapnew ( 'v:val.word' )) 5758 5759bw ! 5760call Ntest_override ( "char_avail" , 0 ) 5761delfunc NonKeywordComplete 5762delfunc TestComplete 5763set autocomplete & omnifunc & complete & 5764unlet g: CallCount 5765endfunc 5766 5767" Test autocomplete timing 5768func Test_autocomplete_timer () 5769 5770let g: CallCount = 0 5771func ! TestComplete ( delay , check , refresh , findstart , base ) 5772if a: findstart 5773return col ( '.' ) - 1 5774else 5775let g: CallCount += 1 5776if a: delay 5777sleep 310m " Exceed timeout 5778endif 5779if a: check 5780while ! complete_check () 5781sleep 2m 5782endwhile 5783" return v:none " This should trigger after interrupted by timeout 5784return [] 5785endif 5786let res = [[ "ab" , "ac" , "ad" ], [ "abb" , "abc" , "abd" ], [ "acb" , "cc" , "cd" ]] 5787if a: refresh 5788return #{ words : res [ g: CallCount - 1 ], refresh : 'always' } 5789endif 5790return res [ g: CallCount - 1 ] 5791endif 5792endfunc 5793 5794" Trigger expansion even when another char is waiting in the typehead 5795call Ntest_override ( "char_avail" , 1 ) 5796 5797new 5798inoremap <buffer> < F2 > <Cmd> let b: matches = complete_info ([ "matches" ]). matches <CR> 5799inoremap <buffer> < F3 > <Cmd> let b: selected = complete_info ([ "selected" ]). selected <CR> 5800set autocomplete 5801 5802call setline ( 1 , [ 'abc' , 'bcd' , 'cde' ]) 5803 5804" Test 1: When matches are found before timeout expires, it exits 5805" 'collection' mode and transitions to 'filter' mode. 5806set complete = .,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0]) 5807let g: CallCount = 0 5808call feedkeys ( "Goa\<F2>\<Esc>0" , 'tx!' ) 5809call assert_equal ([ 'abc' , 'ab' , 'ac' , 'ad' ], b: matches -> mapnew ( 'v:val.word' )) 5810call assert_equal ( 1 , g: CallCount ) 5811 5812let g: CallCount = 0 5813call feedkeys ( "Sab\<F2>\<Esc>0" , 'tx!' ) 5814call assert_equal ([ 'abc' , 'ab' ], b: matches -> mapnew ( 'v:val.word' )) 5815call assert_equal ( 1 , g: CallCount ) 5816 5817" Test 2: When timeout expires before all matches are found, it returns 5818" with partial list but still transitions to 'filter' mode. 5819set complete = .,Ffunction('TestComplete'\\,\ [1\\,\ 0\\,\ 0]) 5820let g: CallCount = 0 5821call feedkeys ( "Sab\<F2>\<Esc>0" , 'tx!' ) 5822call assert_equal ([ 'abc' , 'ab' ], b: matches -> mapnew ( 'v:val.word' )) 5823call assert_equal ( 1 , g: CallCount ) 5824 5825" Test 3: When interrupted by ^N before timeout expires, it remains in 5826" 'collection' mode without transitioning. 5827set complete = .,Ffunction('TestComplete'\\,\ [0\\,\ 1\\,\ 0]) 5828let g: CallCount = 0 5829call feedkeys ( "Sa\<C-N>b\<F2>\<Esc>0" , 'tx!' ) 5830call assert_equal ( 2 , g: CallCount ) 5831 5832let g: CallCount = 0 5833call feedkeys ( "Sa\<C-N>b\<C-N>c\<F2>\<Esc>0" , 'tx!' ) 5834call assert_equal ( 3 , g: CallCount ) 5835 5836" Test 4: Simulate long running func that is stuck in complete_check() 5837let g: CallCount = 0 5838set complete = .,Ffunction('TestComplete'\\,\ [0\\,\ 1\\,\ 0]) 5839call feedkeys ( "Sa\<F2>\<Esc>0" , 'tx!' ) 5840call assert_equal ([ 'abc' ], b: matches -> mapnew ( 'v:val.word' )) 5841call assert_equal ( 1 , g: CallCount ) 5842 5843let g: CallCount = 0 5844call feedkeys ( "Sab\<F2>\<Esc>0" , 'tx!' ) 5845call assert_equal ([ 'abc' ], b: matches -> mapnew ( 'v:val.word' )) 5846call assert_equal ( 1 , g: CallCount ) 5847 5848" Test 5: refresh:always stays in 'collection' mode 5849set complete = .,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 1]) 5850let g: CallCount = 0 5851call feedkeys ( "Sa\<F2>\<Esc>0" , 'tx!' ) 5852call assert_equal ([ 'abc' , 'ab' , 'ac' , 'ad' ], b: matches -> mapnew ( 'v:val.word' )) 5853call assert_equal ( 1 , g: CallCount ) 5854 5855let g: CallCount = 0 5856call feedkeys ( "Sab\<F2>\<Esc>0" , 'tx!' ) 5857call assert_equal ([ 'abc' , 'abb' , 'abd' ], b: matches -> mapnew ( 'v:val.word' )) 5858call assert_equal ( 2 , g: CallCount ) 5859 5860" Test 6: <c-n> and <c-p> navigate menu 5861set complete = .,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0]) 5862let g: CallCount = 0 5863call feedkeys ( "Sab\<c-n>\<F2>\<F3>\<Esc>0" , 'tx!' ) 5864call assert_equal ([ 'abc' , 'ab' ], b: matches -> mapnew ( 'v:val.word' )) 5865call assert_equal ( 0 , b: selected ) 5866call assert_equal ( 1 , g: CallCount ) 5867call feedkeys ( "Sab\<c-n>\<c-n>\<F2>\<F3>\<Esc>0" , 'tx!' ) 5868call assert_equal ( 1 , b: selected ) 5869call feedkeys ( "Sab\<c-n>\<c-p>\<F2>\<F3>\<Esc>0" , 'tx!' ) 5870call assert_equal ( -1 , b: selected ) 5871 5872" Test 7: Following 'cot' option values have no effect 5873set completeopt = menu,menuone,noselect,noinsert,longest 5874set complete = .,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0]) 5875let g: CallCount = 0 5876call feedkeys ( "Sab\<c-n>\<F2>\<F3>\<Esc>0" , 'tx!' ) 5877call assert_equal ([ 'abc' , 'ab' ], b: matches -> mapnew ( 'v:val.word' )) 5878call assert_equal ( 0 , b: selected ) 5879call assert_equal ( 1 , g: CallCount ) 5880call assert_equal ( 'abc' , getline ( 4 )) 5881set completeopt & 5882 5883" Test 8: {func} completes after space, but not '.' 5884set complete = .,Ffunction('TestComplete'\\,\ [0\\,\ 0\\,\ 0]) 5885let g: CallCount = 0 5886call feedkeys ( "S \<F2>\<F3>\<Esc>0" , 'tx!' ) 5887call assert_equal ([ 'ab' , 'ac' , 'ad' ], b: matches -> mapnew ( 'v:val.word' )) 5888call assert_equal ( 1 , g: CallCount ) 5889set complete = . 5890call feedkeys ( "S \<F2>\<F3>\<Esc>0" , 'tx!' ) 5891call assert_equal ([], b: matches -> mapnew ( 'v:val.word' )) 5892 5893" Test 9: Matches nearest to the cursor are prioritized (by default) 5894% d 5895let g: CallCount = 0 5896set complete = . 5897call setline ( 1 , [ "fo" , "foo" , "foobar" , "foobarbaz" ]) 5898call feedkeys ( "jof\<F2>\<Esc>0" , 'tx!' ) 5899call assert_equal ([ 'foo' , 'foobar' , 'fo' , 'foobarbaz' ], b: matches -> mapnew ( 'v:val.word' )) 5900 5901bw ! 5902call Ntest_override ( "char_avail" , 0 ) 5903delfunc TestComplete 5904set autocomplete & complete & 5905unlet g: CallCount 5906endfunc 5907 5908func s: TestCompleteScriptLocal ( findstart , base ) 5909if a: findstart 5910return 1 5911else 5912return [ 'foo' , 'foobar' ] 5913endif 5914endfunc 5915 5916" Issue 17869 5917func Test_scriptlocal_autoload_func () 5918let save_rtp = & rtp 5919set rtp = Xruntime/some 5920let dir = 'Xruntime/some/autoload' 5921call mkdir ( dir , 'pR' ) 5922 5923let lines =<< trim END 5924func compl#Func(findstart, base) 5925if a:findstart 5926return 1 5927else 5928return ['match', 'matchfoo'] 5929endif 5930endfunc 5931END 5932call writefile ( lines , dir .. '/compl.vim' ) 5933 5934call Ntest_override ( "char_avail" , 1 ) 5935new 5936inoremap <buffer> < F2 > <Cmd> let b: matches = complete_info ([ "matches" ]). matches <CR> 5937setlocal autocomplete 5938 5939setlocal complete = .,Fcompl#Func 5940call feedkeys ( "im\<F2>\<Esc>0" , 'xt!' ) 5941call assert_equal ([ 'match' , 'matchfoo' ], b: matches -> mapnew ( 'v:val.word' )) 5942 5943setlocal complete = .,F<SID>TestCompleteScriptLocal 5944call feedkeys ( "Sf\<F2>\<Esc>0" , 'xt!' ) 5945call assert_equal ([ 'foo' , 'foobar' ], b: matches -> mapnew ( 'v:val.word' )) 5946 5947setlocal complete & 5948bwipe ! 5949call Ntest_override ( "char_avail" , 0 ) 5950let & rtp = save_rtp 5951endfunc 5952 5953" Issue #17907 5954func Test_omni_start_invalid_col () 5955func OmniFunc ( startcol , findstart , base ) 5956if a: findstart 5957return a: startcol 5958else 5959return [ 'foo' , 'foobar' ] 5960endif 5961endfunc 5962 5963new 5964redraw " need this to prevent NULL dereference in Nvim 5965set complete = o 5966set omnifunc = funcref('OmniFunc',\ [-1]) 5967call setline ( 1 , [ 'baz ' ]) 5968call feedkeys ( "A\<C-N>\<Esc>0" , 'tx!' ) 5969call assert_equal ( 'baz foo' , getline ( 1 )) 5970 5971set omnifunc = funcref('OmniFunc',\ [1000]) 5972call setline ( 1 , [ 'bar ' ]) 5973call feedkeys ( "A\<C-N>\<Esc>0" , 'tx!' ) 5974call assert_equal ( 'bar foo' , getline ( 1 )) 5975bw ! 5976 5977delfunc OmniFunc 5978set omnifunc & complete & 5979endfunc 5980 5981func Test_completetimeout_autocompletetimeout () 5982func OmniFunc ( findstart , base ) 5983if a: findstart 5984return 1 5985else 5986return [ 'fooOmni' ] 5987endif 5988endfunc 5989 5990set omnifunc = OmniFunc 5991call Ntest_override ( "char_avail" , 1 ) 5992inoremap < F2 > <Cmd> let b: matches = complete_info ([ "matches" ]). matches <CR> 5993 5994call setline ( 1 , [ 'foobar' , 'foobarbaz' ]) 5995new 5996call setline ( 1 , [ 'foo' , 'foobaz' , '' ]) 5997set complete = .,o,w 5998call feedkeys ( "G" , 'xt!' ) 5999 6000set autocomplete 6001for tt in [ 1 , 80 , 1000 , -1 , 0 ] 6002exec $ 'set autocompletetimeout={tt}' 6003call feedkeys ( "\<Esc>Sf\<F2>\<Esc>0" , 'xt!' ) 6004call assert_equal ([ 'foobaz' , 'foo' , 'fooOmni' , 'foobar' , 'foobarbaz' ], b: matches -> mapnew ( 'v:val.word' )) 6005endfor 6006set autocomplete & 6007 6008for tt in [ 80 , 1000 , -1 , 0 ] 6009exec $ 'set completetimeout={tt}' 6010call feedkeys ( "\<Esc>Sf\<C-N>\<F2>\<Esc>0" , 'xt!' ) 6011call assert_equal ([ 'foo' , 'foobaz' , 'fooOmni' , 'foobar' , 'foobarbaz' ], b: matches -> mapnew ( 'v:val.word' )) 6012endfor 6013 6014" Clock does not have fine granularity, so checking 'elapsed time' is only 6015" approximate. We can only test that some type of timeout is enforced. 6016call setline ( 1 , map ( range ( 60000 ), '"foo" . v:val' )) 6017set completetimeout = 1 6018call feedkeys ( "Gof\<C-N>\<F2>\<Esc>0" , 'xt!' ) 6019let match_count = len ( b: matches -> mapnew ( 'v:val.word' )) 6020" How many matches are collected in 1 msec varies with machine speed, only 6021" check the timeout truncated the collection. 6022call assert_true ( match_count < 60000 ) 6023 6024set completetimeout = 1000 6025call feedkeys ( "\<Esc>Sf\<C-N>\<F2>\<Esc>0" , 'xt!' ) 6026let match_count = len ( b: matches -> mapnew ( 'v:val.word' )) 6027call assert_true ( match_count > 2000 ) 6028 6029set autocomplete 6030set autocompletetimeout = 81 6031" Use enough long words that collecting all of them takes well over the 6032" timeout even on a fast machine. 6033let pad = repeat ( 'y' , 60 ) 6034call setline ( 1 , map ( range ( 200000 ), '"foo" . v:val . pad' )) 6035call feedkeys ( "\<Esc>Sf\<F2>\<Esc>0" , 'xt!' ) 6036let match_count = len ( b: matches -> mapnew ( 'v:val.word' )) 6037" The timeout must have truncated the collection. 6038call assert_true ( match_count < 200000 ) 6039 6040set complete & omnifunc & autocomplete & autocompletetimeout & completetimeout & 6041bwipe ! 6042% d 6043call Ntest_override ( "char_avail" , 0 ) 6044iunmap <F2> 6045delfunc OmniFunc 6046endfunc 6047 6048func Test_autocompletedelay () 6049CheckScreendump 6050 6051let lines =<< trim [SCRIPT] 6052call setline(1, ['foo', 'foobar', 'foobarbaz']) 6053setlocal autocomplete 6054[SCRIPT] 6055call writefile ( lines , 'XTest_autocomplete_delay' , 'D' ) 6056let buf = RunVimInTerminal ( '-S XTest_autocomplete_delay' , { 'rows' : 10 }) 6057 6058call term_sendkeys ( buf , "Gof" ) 6059call VerifyScreenDump ( buf , 'Test_autocompletedelay_1' , {}) 6060 6061call term_sendkeys ( buf , "\<Esc>:set autocompletedelay=500\<CR>" ) 6062call term_sendkeys ( buf , "Sf" ) 6063call VerifyScreenDump ( buf , 'Test_autocompletedelay_2' , {}) 6064call term_sendkeys ( buf , "o" ) 6065call VerifyScreenDump ( buf , 'Test_autocompletedelay_3' , {}) 6066sleep 500m 6067call VerifyScreenDump ( buf , 'Test_autocompletedelay_4' , {}) 6068call term_sendkeys ( buf , "\<BS>" ) 6069call VerifyScreenDump ( buf , 'Test_autocompletedelay_5' , {}) 6070sleep 500m 6071call VerifyScreenDump ( buf , 'Test_autocompletedelay_5' , {}) 6072 6073" During delay wait, user can open menu using CTRL_N completion 6074call term_sendkeys ( buf , "\<Esc>:set completeopt=menuone\<CR>" ) 6075call term_sendkeys ( buf , "Sf\<C-N>" ) 6076call VerifyScreenDump ( buf , 'Test_autocompletedelay_7' , {}) 6077 6078" After the menu is open, ^N/^P and Up/Down should not delay. 6079" Wait a bit longer than 'autocompletedelay' so the popup is surely shown 6080" before sending CTRL-N, otherwise the keys race with the deferred popup. 6081call term_sendkeys ( buf , "\<Esc>:set completeopt=menu noruler\<CR>" ) 6082call term_sendkeys ( buf , "\<Esc>Sf" ) 6083sleep 600ms 6084call term_sendkeys ( buf , "\<C-N>" ) 6085call VerifyScreenDump ( buf , 'Test_autocompletedelay_8' , {}) 6086call term_sendkeys ( buf , "\<Down>" ) 6087call VerifyScreenDump ( buf , 'Test_autocompletedelay_9' , {}) 6088 6089" When menu is not open Up/Down moves cursor to different line 6090call term_sendkeys ( buf , "\<Esc>Sf" ) 6091call term_sendkeys ( buf , "\<Up>" ) 6092call VerifyScreenDump ( buf , 'Test_autocompletedelay_10' , {}) 6093call term_sendkeys ( buf , "\<Down>" ) 6094call VerifyScreenDump ( buf , 'Test_autocompletedelay_11' , {}) 6095 6096call term_sendkeys ( buf , "\<esc>" ) 6097call StopVimInTerminal ( buf ) 6098endfunc 6099 6100func Run_test_autocompletedelay_ctrl_g_U ( delay1 , delay2 ) 6101new 6102call setline ( 1 , 'foo bar baz' ) 6103inoremap <buffer> ( () < C-g > U 6104set autocomplete autocompletedelay = 200 6105 6106call timer_start ( a: delay1 , { -> feedkeys ( '(' , 't' ) }) 6107call timer_start ( a: delay2 , { -> feedkeys ( "\<Left>a\<Esc>" , 't' ) }) 6108call feedkeys ( 'ob' , 'tx!' ) 6109call assert_equal ([ 'foo bar baz' , 'b(a)' ], getline ( 1 , '$' )) 6110undo 6111call assert_equal ([ 'foo bar baz' ], getline ( 1 , '$' )) 6112 6113set autocomplete & autocompletedelay & 6114bwipe ! 6115endfunc 6116 6117func Test_autocompletedelay_ctrl_g_U () 6118" '(' typed after 'autocompletedelay' expires 6119call Run_test_autocompletedelay_ctrl_g_U ( 250 , 500 ) 6120" '(' typed before 'autocompletedelay' expires 6121call Run_test_autocompletedelay_ctrl_g_U ( 150 , 500 ) 6122endfunc 6123 6124func Run_test_autocompletedelay_ctrl_k ( delay1 , delay2 ) 6125new 6126call setline ( 1 , 'foo bar baz' ) 6127set autocomplete autocompletedelay = 200 6128 6129call timer_start ( a: delay1 , { -> feedkeys ( "\<C-K>" , 't' ) }) 6130call timer_start ( a: delay2 , { -> feedkeys ( ".,\<Esc>" , 't' ) }) 6131call feedkeys ( 'ob' , 'tx!' ) 6132call assert_equal ([ 'foo bar baz' , 'b…' ], getline ( 1 , '$' )) 6133 6134set autocomplete & autocompletedelay & 6135bwipe ! 6136endfunc 6137 6138func Test_autocompletedelay_ctrl_k () 6139throw 'Skipped: use test/functional/editor/completion_spec.lua' 6140" Ctrl-K typed after 'autocompletedelay' expires 6141call Run_test_autocompletedelay_ctrl_k ( 250 , 500 ) 6142" Ctrl-K typed before 'autocompletedelay' expires 6143call Run_test_autocompletedelay_ctrl_k ( 150 , 500 ) 6144endfunc 6145 6146func Test_autocompletedelay_no_record () 6147" The K_COMPLETE_DELAY pseudo key must not be recorded into a register while 6148" recording a macro, like K_CURSORHOLD. 6149new 6150call setline ( 1 , 'foobar' ) 6151set autocomplete autocompletedelay = 100 6152 6153let @a = '' 6154" Type a char that arms the delay, idle past 'autocompletedelay' so a 6155" K_COMPLETE_DELAY would be injected, then end Insert mode and stop recording. 6156call timer_start ( 300 , { -> feedkeys ( "\<Esc>q" , 't' ) }) 6157call feedkeys ( "qaSf" , 'tx!' ) 6158call assert_equal ( "Sf\<Esc>" , @a ) 6159 6160" Delayed autocompletion still works when recording. 6161if ! has ( 'win32' ) || has ( 'nvim' ) 6162call setline ( 1 , 'foobar foofoo' ) 6163call timer_start ( 300 , { -> feedkeys ( "\<Down>\<C-Y>\<Esc>q" , 't' ) }) 6164call feedkeys ( "qaof" , 'tx!' ) 6165call assert_equal ( 'foobar' , getline ( '.' )) 6166" XXX: This doesn't produce the same result when replaying. 6167call assert_equal ( "of\<Down>\<C-Y>\<Esc>" , @a ) 6168endif 6169 6170set autocomplete & autocompletedelay & 6171bwipe ! 6172endfunc 6173 6174" Preinsert longest prefix when autocomplete 6175func Test_autocomplete_longest () 6176func GetLine () 6177let g: line = getline ( '.' ) 6178let g: col = col ( '.' ) 6179let g: preinserted = preinserted () 6180endfunc 6181 6182call Ntest_override ( "char_avail" , 1 ) 6183new 6184inoremap <buffer> < F5 > < C-R > =GetLine() < CR > 6185set completeopt += longest autocomplete 6186call setline ( 1 , [ "foobar" , "foozbar" ]) 6187call feedkeys ( "Go\<ESC>" , 'tx' ) 6188 6189func DoTest ( typed , line , col ) 6190call feedkeys ( $ " S{a:typed}\<F5>\<ESC>", 'tx') 6191call assert_equal ( a: line , g: line ) 6192call assert_equal ( a: col , g: col ) 6193endfunc 6194 6195call DoTest ( "f" , 'foo' , 2 ) 6196call DoTest ( "fo" , 'foo' , 3 ) 6197call DoTest ( "foo" , 'foo' , 4 ) 6198call DoTest ( "foob" , 'foobar' , 5 ) 6199call DoTest ( "foob\<BS>" , 'foo' , 4 ) 6200call DoTest ( "foob\<BS>\<BS>" , 'foo' , 3 ) 6201call DoTest ( "foo\<BS>" , 'foo' , 3 ) 6202call DoTest ( "foo\<BS>\<BS>" , 'foo' , 2 ) 6203call DoTest ( "foo\<BS>\<BS>\<BS>" , '' , 1 ) 6204 6205call DoTest ( "foo \<BS>" , 'foo' , 4 ) 6206call DoTest ( "foo \<BS>\<BS>" , 'foo' , 3 ) 6207 6208call DoTest ( "f\<C-N>" , 'foozbar' , 8 ) 6209call DoTest ( "f\<C-N>\<C-N>" , 'foobar' , 7 ) 6210call DoTest ( "f\<C-N>\<C-N>\<C-N>" , 'foo' , 2 ) 6211call DoTest ( "f\<C-N>\<C-N>\<C-N>\<C-N>" , 'foozbar' , 8 ) 6212call DoTest ( "f\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>" , 'foo' , 2 ) 6213call DoTest ( "f\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>\<C-P>" , 'foobar' , 7 ) 6214call DoTest ( "f\<C-P>" , 'foobar' , 7 ) 6215call DoTest ( "fo\<BS>\<C-P>" , 'foobar' , 7 ) 6216 6217" <C-Y> to accept preinserted text 6218inoremap <buffer> < F6 > < C-R > =pumvisible() < CR > 6219call DoTest ( "zar\<C-O>0f" , 'foozar' , 2 ) 6220call DoTest ( "zar\<C-O>0f\<C-Y>" , 'foozar' , 4 ) 6221call DoTest ( "zar\<C-O>0f\<C-Y>\<F6>" , 'foo1zar' , 5 ) 6222call DoTest ( "zar\<C-O>0f\<C-Y>\<BS>" , 'foozar' , 3 ) 6223call DoTest ( "zar\<C-O>0f\<C-Y>\<BS>\<F6>" , 'fo1zar' , 4 ) 6224 6225" Select items in menu 6226call DoTest ( "zar\<C-O>0f\<C-N>" , 'foozbarzar' , 8 ) 6227call DoTest ( "zar\<C-O>0f\<C-N>\<C-N>" , 'foobarzar' , 7 ) 6228call DoTest ( "zar\<C-O>0f\<C-N>\<C-N>\<C-N>" , 'foozar' , 2 ) 6229call DoTest ( "zar\<C-O>0f\<C-N>\<C-N>\<C-N>\<C-N>" , 'foozbarzar' , 8 ) 6230call DoTest ( "zar\<C-O>0f\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>" , 'foozar' , 2 ) 6231call DoTest ( "zar\<C-O>0f\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>\<C-P>" , 'foobarzar' , 7 ) 6232call DoTest ( "zar\<C-O>0f\<C-P>" , 'foobarzar' , 7 ) 6233 6234" Should not work with fuzzy 6235set cot += fuzzy 6236call DoTest ( "f" , 'f' , 2 ) 6237set cot -= fuzzy 6238 6239" preinserted() 6240call DoTest ( "f" , 'foo' , 2 ) 6241call assert_equal ( 1 , g: preinserted ) 6242call assert_equal ( 0 , preinserted ()) 6243call DoTest ( "f\<BS>" , '' , 1 ) 6244call assert_equal ( 0 , g: preinserted ) 6245call DoTest ( "f " , 'f ' , 3 ) 6246call assert_equal ( 0 , g: preinserted ) 6247call DoTest ( "foob" , 'foobar' , 5 ) 6248call assert_equal ( 1 , g: preinserted ) 6249call DoTest ( "foob\<BS>\<BS>x" , 'fox' , 4 ) 6250call assert_equal ( 0 , g: preinserted ) 6251 6252" Complete non-keyword 6253func Omni ( findstart , base ) 6254if a: findstart 6255return 5 6256else 6257return [ 'xyz:' ] 6258endif 6259endfunc 6260set omnifunc = Omni 6261set cpt += o 6262call DoTest ( "xyz:" , "xyz:xyz:" , 5 ) 6263call DoTest ( "xyz:\<BS>\<BS>" , "xyz:" , 3 ) 6264set omnifunc & cpt & 6265delfunc Omni 6266 6267" leader should match prefix of inserted word 6268% delete 6269set smartcase ignorecase 6270call setline ( 1 , [ "FOO" ]) 6271call feedkeys ( $ " Gof\<F5>\<Esc>", 'tx') 6272call assert_equal ( 'f' , g: line ) 6273call feedkeys ( $ " SF\<F5>\<Esc>", 'tx') 6274call assert_equal ( 'FOO' , g: line ) 6275set smartcase & ignorecase & 6276 6277" avoid repeating text that is already present after the cursor 6278% delete 6279call setline ( 1 , [ "foobarbaz" , "" ]) 6280call feedkeys ( $ " G", 'tx') 6281call DoTest ( "baz\<C-O>0f" , "foobarbaz" , 2 ) 6282call feedkeys ( $ " Sxfoozar\<CR>\<Esc>", 'tx') 6283call DoTest ( "baz\<C-O>0f" , "foobarbaz" , 2 ) 6284call feedkeys ( $ " Sfoozar\<CR>\<Esc>", 'tx') 6285call DoTest ( "baz\<C-O>0f" , "foobaz" , 2 ) 6286 6287" Verify that redo (dot) works 6288% delete 6289call setline ( 1 , [ "foobar" , "foozbar" , "foobaz" , "changed" , "change" ]) 6290call feedkeys ( $ "/foo\< CR>", 'tx') 6291call feedkeys ( $ "cwch\< C-N>\<Esc>n.n.", 'tx') 6292call assert_equal ( repeat ([ 'changed' ], 3 ), getline ( 1 , 3 )) 6293 6294" Select a match and delete up to text equal to another match 6295% delete 6296call setline ( 1 , [ "foobar" , "foo" ]) 6297call feedkeys ( "Go\<ESC>" , 'tx' ) 6298call DoTest ( "f\<C-N>\<C-N>\<BS>\<BS>\<BS>\<BS>" , 'foo' , 3 ) 6299 6300" Issue #18410: When both "preinsert" and "longest" are set, "preinsert" 6301" takes precedence 6302% delete 6303set autocomplete completeopt += longest,preinsert 6304call setline ( 1 , [ 'foobar' , 'foofoo' , 'foobaz' , '' ]) 6305call feedkeys ( "G" , 'tx' ) 6306call DoTest ( "f" , 'foobar' , 2 ) 6307call assert_equal ( 1 , g: preinserted ) 6308 6309" complete_info() 6310% delete 6311func GetPreinsert () 6312let g: cinfo = complete_info ([ 'preinserted_text' ]) 6313return "" 6314endfunc 6315inoremap <buffer> < F6 > < C-R > =GetPreinsert() < CR > 6316call setline ( 1 , [ "foo_bar_xyz" , "foo__xyz" ]) 6317 6318set completeopt & completeopt += preinsert 6319call feedkeys ( "G4li\<F6>\<C-Y>" , 'tx' ) 6320call assert_equal ( "bar_xyz" , g: cinfo . preinserted_text ) 6321 6322set completeopt & completeopt += longest 6323call feedkeys ( "Gof\<F6>\<ESC>" , 'tx' ) 6324call assert_equal ( "oo_bar_xyz" , g: cinfo . preinserted_text ) 6325unlet g: cinfo 6326delfunc GetPreinsert 6327set completeopt & 6328 6329" Undo 6330% delete _ 6331let & l: undolevels = & l: undolevels 6332normal ! ifoo 6333let & l: undolevels = & l: undolevels 6334normal ! ofoobar 6335let & l: undolevels = & l: undolevels 6336normal ! ofoobaz 6337let & l: undolevels = & l: undolevels 6338 6339func CheckUndo () 6340let g: errmsg = '' 6341call assert_equal ([ 'foo' , 'foobar' , 'foobaz' ], getline ( 1 , '$' )) 6342undo 6343call assert_equal ([ 'foo' , 'foobar' ], getline ( 1 , '$' )) 6344undo 6345call assert_equal ([ 'foo' ], getline ( 1 , '$' )) 6346undo 6347call assert_equal ([ '' ], getline ( 1 , '$' )) 6348later 3 6349call assert_equal ([ 'foo' , 'foobar' , 'foobaz' ], getline ( 1 , '$' )) 6350call assert_equal ( '' , v: errmsg ) 6351endfunc 6352 6353" Check that switching buffer with "longest" doesn't corrupt undo. 6354new 6355setlocal bufhidden = wipe 6356inoremap <buffer> < F2 > <Cmd> enew ! <CR> 6357call feedkeys ( "if\<F2>\<Esc>" , 'tx' ) 6358bwipe ! 6359call CheckUndo () 6360 6361" Check that closing window with "longest" doesn't corrupt undo. 6362new 6363setlocal bufhidden = wipe 6364inoremap <buffer> < F2 > <Cmd> close !<CR> 6365call feedkeys ( "if\<F2>\<Esc>" , 'tx' ) 6366call CheckUndo () 6367 6368% delete _ 6369delfunc CheckUndo 6370 6371" Check that behavior of "longest" in manual completion is unchanged. 6372for ac in [ v: false , v: true ] 6373let & ac = ac 6374set completeopt = menuone,longest 6375call feedkeys ( "Ssign u\<C-X>\<C-V>" , 'tx' ) 6376call assert_equal ( 'sign un' , getline ( '.' )) 6377call feedkeys ( "Ssign u\<C-X>\<C-V>\<C-V>" , 'tx' ) 6378call assert_equal ( 'sign undefine' , getline ( '.' )) 6379call feedkeys ( "Ssign u\<C-X>\<C-V>\<C-V>\<C-V>" , 'tx' ) 6380call assert_equal ( 'sign unplace' , getline ( '.' )) 6381call feedkeys ( "Ssign u\<C-X>\<C-V>\<C-V>\<C-V>\<C-V>" , 'tx' ) 6382call assert_equal ( 'sign u' , getline ( '.' )) 6383% delete 6384endfor 6385 6386bw ! 6387set cot & autocomplete & 6388delfunc GetLine 6389delfunc DoTest 6390call Ntest_override ( "char_avail" , 0 ) 6391endfunc 6392 6393" Issue #18326 6394func Test_fuzzy_select_item_when_acl () 6395CheckScreendump 6396let lines =<< trim [SCRIPT] 6397call setline(1, ["v", "vi", "vim"]) 6398set autocomplete completeopt=menuone,noinsert,fuzzy autocompletedelay=300 6399[SCRIPT] 6400call writefile ( lines , 'XTest_autocomplete_delay' , 'D' ) 6401let buf = RunVimInTerminal ( '-S XTest_autocomplete_delay' , { 'rows' : 10 }) 6402 6403call term_sendkeys ( buf , "Govi" ) 6404call VerifyScreenDump ( buf , 'Test_fuzzy_autocompletedelay_1' , {}) 6405 6406call term_sendkeys ( buf , "\<Esc>Sv" ) 6407call VerifyScreenDump ( buf , 'Test_fuzzy_autocompletedelay_2' , {}) 6408sleep 500m 6409call term_sendkeys ( buf , "i" ) 6410call VerifyScreenDump ( buf , 'Test_fuzzy_autocompletedelay_3' , {}) 6411 6412call term_sendkeys ( buf , "\<esc>" ) 6413call StopVimInTerminal ( buf ) 6414endfunc 6415 6416" Issue #18378: crash when fuzzy reorders items during refresh:always 6417func Test_refresh_always_with_fuzzy () 6418func ComplFunc1 ( findstart , base ) 6419if a: findstart 6420return 1 6421else 6422return [ 'foo' , 'foobar' ] 6423endif 6424endfunc 6425func ComplFunc2 ( findstart , base ) 6426if a: findstart 6427return 1 6428else 6429return #{ words : [ 'foo' ], refresh : 'always' } 6430endif 6431endfunc 6432set complete = .,FComplFunc1,FComplFunc2 6433set autocomplete 6434call Ntest_override ( "char_avail" , 1 ) 6435new 6436call setline ( 1 , [ 'fox' ]) 6437exe "normal! Gofo" 6438bw ! 6439delfunc ComplFunc1 6440delfunc ComplFunc2 6441set complete & autocomplete & 6442call Ntest_override ( "char_avail" , 0 ) 6443endfunc 6444 6445func Test_autocompletedelay_longest_preinsert () 6446CheckScreendump 6447let lines =<< trim [SCRIPT] 6448call setline(1, ['autocomplete', 'autocomxxx']) 6449set autocomplete completeopt+=longest autocompletedelay=500 6450[SCRIPT] 6451call writefile ( lines , 'XTest_autocompletedelay' , 'D' ) 6452let buf = RunVimInTerminal ( '-S XTest_autocompletedelay' , { 'rows' : 10 }) 6453 6454" No spurious characters when autocompletedelay is in effect 6455call term_sendkeys ( buf , "Goau" ) 6456sleep 10m 6457call term_sendkeys ( buf , "toc" ) 6458sleep 100m 6459call VerifyScreenDump ( buf , 'Test_autocompletedelay_longest_1' , {}) 6460sleep 500m 6461call VerifyScreenDump ( buf , 'Test_autocompletedelay_longest_2' , {}) 6462 6463" Deleting a char should still show longest text 6464call term_sendkeys ( buf , "\<Esc>Saut" ) 6465sleep 10m 6466call term_sendkeys ( buf , "\<BS>" ) 6467sleep 100m 6468call VerifyScreenDump ( buf , 'Test_autocompletedelay_longest_3' , {}) 6469sleep 500m 6470call VerifyScreenDump ( buf , 'Test_autocompletedelay_longest_4' , {}) 6471 6472" Preinsert 6473call term_sendkeys ( buf , "\<Esc>:set completeopt& completeopt+=preinsert\<CR>" ) 6474call term_sendkeys ( buf , "\<Esc>Sau" ) 6475sleep 100m 6476call VerifyScreenDump ( buf , 'Test_autocompletedelay_preinsert_1' , {}) 6477sleep 500m 6478call VerifyScreenDump ( buf , 'Test_autocompletedelay_preinsert_2' , {}) 6479 6480call term_sendkeys ( buf , "\<esc>" ) 6481call StopVimInTerminal ( buf ) 6482endfunc 6483 6484" Issue 18493 6485func Test_longest_preinsert_accept () 6486call Ntest_override ( "char_avail" , 1 ) 6487new 6488call setline ( 1 , [ 'func1' , 'xfunc' , 'func2' ]) 6489set completeopt += noselect 6490 6491call feedkeys ( "Gof\<C-N>\<Down>\<C-Y>" , 'tx' ) 6492call assert_equal ( 'func1' , getline ( '.' )) 6493 6494set completeopt += longest autocomplete 6495call feedkeys ( "Sf\<Down>\<C-Y>" , 'tx' ) 6496call assert_equal ( 'func2' , getline ( '.' )) 6497call feedkeys ( "Sf\<C-Y>" , 'tx' ) 6498call assert_equal ( 'func' , getline ( '.' )) 6499 6500set completeopt & autocomplete & 6501bw ! 6502call Ntest_override ( "char_avail" , 0 ) 6503endfunc 6504 6505" Issue 19114 6506func Test_fuzzy_filenames_compl_autocompl () 6507CheckScreendump 6508let dir = 'Xtempdir' 6509call mkdir ( dir , 'pR' ) 6510call writefile ([], dir .. '/.name' ) 6511call writefile ([], dir .. '/name' ) 6512call writefile ([], dir .. '/test.vim' ) 6513 6514let buf = RunVimInTerminal ( '' , { 'rows' : 10 }) 6515call term_sendkeys ( buf , ':call test_override("char_avail", 1)' ) 6516call term_sendkeys ( buf , "\<CR>" ) 6517call term_sendkeys ( buf , "iset ac cot=fuzzy,longest\<ESC>" ) 6518call term_sendkeys ( buf , ":source\<CR>" ) 6519call term_sendkeys ( buf , "o.na\<C-X>\<C-F>" ) " this used to cause segfault 6520call TermWait ( buf , 200 ) 6521call VerifyScreenDump ( buf , 'Test_fuzzy_filenames_compl_autocompl' , {}) 6522call StopVimInTerminal ( buf ) 6523endfunc 6524 6525" Issue 19130 6526func Test_helptags_autocomplete_timeout () 6527func ! TestComplete ( findstart , base ) 6528if a: findstart 6529return col ( '.' ) - 1 6530else 6531sleep 310m " Exceed timeout 6532return [ "foo" ] 6533endif 6534endfunc 6535 6536call Ntest_override ( "char_avail" , 1 ) 6537new 6538set autocomplete completeopt = fuzzy complete = .,FTestComplete 6539call feedkeys ( "Goa\<Esc>0" , 'tx!' ) 6540call feedkeys ( ":h\<CR>" , 'tx' ) " used to throw E149 exception 6541call Ntest_override ( "char_avail" , 0 ) 6542set autocomplete & completeopt & complete & 6543bw ! 6544endfunc 6545 6546func Test_autocomplete_preinsert_null_leader () 6547" Test that non-prefix matches from omnifunc are filtered when leader is NULL. 6548" When autocomplete first fires, compl_leader is NULL. Previously the prefix 6549" filter was bypassed, allowing non-prefix fuzzy matches to be incorrectly 6550" shown in the PUM and preinserted. 6551func NonPrefixOmni ( findstart , base ) 6552if a: findstart 6553return col ( "." ) - 1 6554endif 6555" Return "key" (doesn't start with 'y') and "yellow" (starts with 'y'). 6556" Simulates what a fuzzy omnifunc returns (e.g. vimcomplete#Complete with 6557" wildoptions=fuzzy). 6558return [ "key" , "yellow" ] 6559endfunc 6560 6561call Ntest_override ( "char_avail" , 1 ) 6562new 6563set omnifunc = NonPrefixOmni complete = o 6564set completeopt = preinsert autocomplete 6565 6566func GetState () 6567let g: line = getline ( '.' ) 6568let g: col = col ( '.' ) 6569let g: matches = complete_info ([ 'matches' ]). matches -> mapnew ( 'v:val.word' ) 6570endfunc 6571inoremap <buffer> < F5 > < C-R > =GetState() < CR > 6572 6573" Type 'y': "key" should be filtered out (doesn't start with 'y'), 6574" "yellow" should be the only PUM entry and preinserted with cursor after 'y'. 6575call feedkeys ( "iy\<F5>\<C-E>\<Esc>" , 'tx' ) 6576call assert_equal ( "yellow" , g: line ) 6577call assert_equal ( 2 , g: col ) 6578call assert_equal ([ 'yellow' ], g: matches ) 6579 6580bw ! 6581set omnifunc & complete & completeopt & autocomplete & 6582call Ntest_override ( "char_avail" , 0 ) 6583delfunc NonPrefixOmni 6584delfunc GetState 6585endfunc 6586 6587" Issue #19329: When register contents are inserted, remove preinserted text 6588func Test_ins_register_preinsert_autocomplete () 6589func TestOmni ( findstart , base ) 6590if a: findstart 6591return col ( "." ) - 1 6592endif 6593return [ "foo" , "foobar" ] 6594endfunc 6595 6596call Ntest_override ( "char_avail" , 1 ) 6597new 6598set omnifunc = TestOmni complete ^=o 6599set completeopt = preinsert autocomplete 6600 6601call feedkeys ( "ifoo \<C-R>\<C-P>=\"xyz\"\<CR>\<Esc>" , 'tx' ) 6602call assert_equal ( "foo xyz" , getline ( '.' )) 6603bw ! 6604set omnifunc & complete & completeopt & autocomplete & 6605call Ntest_override ( "char_avail" , 0 ) 6606delfunc TestOmni 6607endfunc 6608 6609func Test_autocomplete_with_auto_format () 6610call Ntest_override ( "char_avail" , 1 ) 6611new 6612setlocal formatoptions = tcq textwidth = 9 autocomplete noautoindent 6613call feedkeys ( "ia b c d\<Esc>ie f g\<Esc>" , 'tx' ) 6614call assert_equal ([ 'a b c e f' , 'gd' ], getline ( 1 , '$' )) 6615 6616% delete 6617setlocal autoindent 6618call feedkeys ( "ia b c d\<Esc>ie f g\<Esc>" , 'tx' ) 6619call assert_equal ([ 'a b c e f' , 'gd' ], getline ( 1 , '$' )) 6620 6621bw ! 6622call Ntest_override ( "char_avail" , 0 ) 6623endfunc 6624 6625func Test_completion_with_mapped_ctrl_r () 6626new 6627let b: n = 0 6628let @a = 'AABBCCDDEE' 6629" Ctrl-R mapping is triggered 6630inoremap <buffer> < C-R > <Cmd> let b: n += 1 <CR> 6631inoremap <buffer> < F2 > <Cmd> call complete ( col ( '.' ), []) <CR> 6632call feedkeys ( "i\<F2>\<*C-R>abcde\<Esc>" , 'tx' ) 6633call assert_equal ( 1 , b: n ) 6634call assert_equal ( 'abcde' , getline ( '.' )) 6635 6636" Ctrl-X Ctrl-R still works with Ctrl-R mapped 6637call feedkeys ( "ccAAB\<*C-X>\<*C-R>\<*C-Y>\<Esc>" , 'tx' ) 6638call assert_equal ( 1 , b: n ) 6639call assert_equal ( 'AABBCCDDEE' , getline ( '.' )) 6640 6641let @a = '' 6642bwipe ! 6643endfunc 6644 6645" Keys are mapped during completion started by complete(), but not in other 6646" CTRL-X modes. 6647func Test_mapped_ctrl_n_during_complete_function () 6648new 6649inoremap <buffer> < F2 > <Cmd> call complete ( 1 , [ 'foo' , 'foobar' ]) <CR> 6650inoremap <buffer> < F3 > <Cmd> let b: info = 6651\ [ getline ( '.' ), complete_info ([ 'selected' ]). selected ] <CR> 6652 6653" During completion started by complete() the <C-N> mapping applies: 6654" <Down> moves the selection without inserting it. 6655inoremap <buffer> <expr> < C-N > complete_info (). mode ==# 'eval' ? '<Down>' : '<C-N>' 6656call feedkeys ( "i\<F2>\<*C-N>\<F3>\<C-Y>\<Esc>" , 'tx' ) 6657call assert_equal ([ 'foo' , 1 ], b: info ) 6658call assert_equal ( 'foobar' , getline ( 1 )) 6659 6660" In other CTRL-X modes the mapping is ignored: the builtin <C-N> selects 6661" and inserts the next match. 6662% delete _ 6663call setline ( 1 , [ 'foo' , 'foobar' , '' ]) 6664inoremap <buffer> <expr> < C-N > pumvisible () ? '<Down>' : '<C-N>' 6665call feedkeys ( "3GAf\<C-X>\<C-N>\<C-N>\<F3>\<C-Y>\<Esc>" , 'tx' ) 6666call assert_equal ([ 'foobar' , 1 ], b: info ) 6667 6668bwipe ! 6669endfunc 6670 6671func Test_smartcase_longest () 6672func ! GetMatches () 6673let info = complete_info ([ "matches" ]) 6674return map ( copy ( info . matches ), { _ , v -> v . word }) 6675endfunc 6676 6677func ! TestInner ( key ) 6678let pr = "\<c-r>=string(GetMatches())\<cr>" 6679let words = [ "InputEvent" , "inputmap" , "INPUT_MAP" ] 6680 6681new 6682set completeopt = menuone,noselect,longest ignorecase smartcase 6683 6684" Lowercase 'inp' all three (case-insensitive). 6685call setline ( 1 , words ) 6686exe $ "normal! ggOinp{a:key}{pr}" 6687let line = getline ( 1 ) 6688call assert_match ( '\c^input' , line , 'inp prefix, key=' .. strtrans ( a: key )) 6689call assert_equal ( "['InputEvent', 'inputmap', 'INPUT_MAP']" , 6690\ substitute ( line , '\c^input' , '' , '' ), 6691\ 'inp matches, key=' .. strtrans ( a: key )) 6692 6693" Uppercase 'I' excludes lowercase 'inputmap' 6694% d 6695call setline ( 1 , words ) 6696exe $ "normal! ggOI{a:key}{pr}" 6697let line = getline ( 1 ) 6698call assert_match ( '\c^input' , line , 'I prefix, key=' .. strtrans ( a: key )) 6699call assert_equal ( "['InputEvent', 'INPUT_MAP']" , 6700\ substitute ( line , '\c^input' , '' , '' ), 6701\ 'I matches, key=' .. strtrans ( a: key )) 6702 6703set ignorecase & smartcase & completeopt & 6704bw ! 6705endfunc 6706 6707call TestInner ( "\<c-n>" ) 6708call TestInner ( "\<c-p>" ) 6709call TestInner ( "\<c-x>\<c-n>" ) 6710call TestInner ( "\<c-x>\<c-p>" ) 6711delfunc GetMatches 6712delfunc TestInner 6713endfunc 6714 6715" Check that calling complete() while filtering Ctrl-N completion doesn't 6716" break dot-repeat. 6717func Test_call_complete_while_filtering () 6718new 6719setlocal complete = . completeopt = menuone,noselect 6720inoremap <buffer> < F2 > <Cmd> call complete ( 3 , [ 'obar' , 'obaz' ]) <CR> 6721call setline ( 1 , [ 'foobar' , 'foobaz' ]) 6722 6723call feedkeys ( "Gofo\<C-N>ob\<F2>cd\<Esc>" , 'tx' ) 6724call assert_equal ([ 'foobar' , 'foobaz' , 'foobcd' ], getline ( 1 , '$' )) 6725normal ! . 6726call assert_equal ([ 'foobar' , 'foobaz' , 'foobcd' , 'foobcd' ], getline ( 1 , '$' )) 6727 6728bwipe ! 6729endfunc 6730 6731func Test_complete_check_mapped_typed_key () 6732func SlowComplete ( findstart , base ) 6733if a: findstart 6734return col ( '.' ) - 1 6735endif 6736call complete_add ( 'foobar' ) 6737let g: compl_iterations = 0 6738while ! complete_check () && g: compl_iterations < 100 6739let g: compl_iterations += 1 6740sleep 5m 6741endwhile 6742return [] 6743endfunc 6744 6745new 6746setlocal completefunc = SlowComplete 6747setlocal completeopt = menuone,noselect 6748inoremap <buffer> < Space > < Space > <Space > 6749 6750let g: compl_iterations = -1 6751call feedkeys ( "Sfoo\<C-X>\<C-U> \<Esc>" , 'tx' ) 6752call assert_inrange ( 0 , 99 , g: compl_iterations ) 6753 6754bwipe ! 6755delfunc SlowComplete 6756unlet g: compl_iterations 6757endfunc 6758 6759" Test for the duplicate check when adding completion matches 6760func Test_ins_complete_dedup () 6761new 6762setl complete = . 6763 6764" a word that occurs several times only results in one match 6765call setline ( 1 , [ 'alpha beta alpha gamma' , 'beta alpha delta beta' , '' ]) 6766call cursor ( 3 , 1 ) 6767call feedkeys ( "Aal\<C-N>\<C-R>=GetCompleteInfo()\<CR>\<C-E>\<Esc>" , 'tx' ) 6768call assert_equal ([ 'alpha' ], g: compl_info . items -> mapnew ( 'v:val.word' )) 6769 6770" the duplicate check is case-sensitive 6771% delete _ 6772call setline ( 1 , [ 'Foo foo FOO fooBar Foo foo' , '' ]) 6773call cursor ( 2 , 1 ) 6774call feedkeys ( "Afo\<C-N>\<C-R>=GetCompleteInfo()\<CR>\<C-E>\<Esc>" , 'tx' ) 6775call assert_equal ([ 'foo' , 'fooBar' ], g: compl_info . items -> mapnew ( 'v:val.word' )) 6776 6777" with 'ignorecase' and 'infercase' case variants fold into one match 6778setl ignorecase infercase 6779% delete _ 6780call setline ( 1 , [ 'Word word WORD wordy Word' , '' ]) 6781call cursor ( 2 , 1 ) 6782call feedkeys ( "Awo\<C-N>\<C-R>=GetCompleteInfo()\<CR>\<C-E>\<Esc>" , 'tx' ) 6783call assert_equal ([ 'word' , 'wordy' ], g: compl_info . items -> mapnew ( 'v:val.word' )) 6784setl noignorecase noinfercase 6785 6786" duplicate dictionary entries only appear once; with 'ignorecase' case 6787" variants all match but stay separate matches 6788call writefile ([ 'apple' , 'apple' , 'Apple' , 'apricot' , 'apricot' , 'banana' ], 6789\ 'Xcompldict' , 'D' ) 6790setl dictionary = Xcompldict 6791set ignorecase 6792% delete _ 6793call feedkeys ( "Aap\<C-X>\<C-K>\<C-R>=GetCompleteInfo()\<CR>\<C-E>\<Esc>" , 'tx' ) 6794call assert_equal ([ 'apple' , 'Apple' , 'apricot' ], g: compl_info . items -> mapnew ( 'v:val.word' )) 6795set noignorecase 6796setl dictionary & 6797 6798" duplicate items passed to complete() are only added once 6799inoremap <buffer> < F5 > <Cmd> call complete ( 1 , [ 'dup' , 'dup' , 'uniq' , 'dup' ]) <CR> 6800% delete _ 6801call feedkeys ( "i\<F5>\<C-R>=GetCompleteInfo()\<CR>\<C-E>\<Esc>" , 'tx' ) 6802call assert_equal ([ 'dup' , 'uniq' ], g: compl_info . items -> mapnew ( 'v:val.word' )) 6803 6804" restarting a completion rebuilds the matches without duplicates 6805% delete _ 6806call setline ( 1 , [ 'echo edit eecho edit echo' , '' ]) 6807call cursor ( 2 , 1 ) 6808call feedkeys ( "Ae\<C-N>\<C-E>\<Esc>" , 'tx' ) 6809call feedkeys ( "A\<C-N>\<C-R>=GetCompleteInfo()\<CR>\<C-E>\<Esc>" , 'tx' ) 6810call assert_equal ([ 'echo' , 'edit' , 'eecho' ], g: compl_info . items -> mapnew ( 'v:val.word' )) 6811 6812" With "dup" matches from several sources, refreshing one source removes 6813" its duplicate but must not forget about the equal match of the other 6814" source: adding "dupword" again without "dup" is still a duplicate. 6815let g: dedup_calls = 0 6816func ! DedupSrcA ( findstart , base ) 6817if a: findstart 6818return 0 6819endif 6820let g: dedup_calls += 1 6821if g: dedup_calls == 1 6822return #{ words : [#{ word : 'dupword' , dup : 1 }], refresh : 'always' } 6823endif 6824return #{ words : [#{ word : 'dupword' }], refresh : 'always' } 6825endfunc 6826func ! DedupSrcB ( findstart , base ) 6827if a: findstart 6828return 0 6829endif 6830return #{ words : [#{ word : 'dupword' , dup : 1 }]} 6831endfunc 6832setl complete = FDedupSrcA,FDedupSrcB 6833% delete _ 6834call feedkeys ( "Sdup\<C-N>\<BS>\<C-R>=GetCompleteInfo()\<CR>\<C-E>\<Esc>" , 'tx' ) 6835call assert_equal ([ 'dupword' ], g: compl_info . items -> mapnew ( 'v:val.word' )) 6836setl complete & 6837delfunc DedupSrcA 6838delfunc DedupSrcB 6839unlet g: dedup_calls 6840 6841bwipe ! 6842unlet g: compl_info 6843endfunc 6844 6845func Test_complete_info_hlgroup () 6846new 6847set completeopt & 6848func DoComplete () 6849call complete ( 1 , [ 6850\ #{ word : 'aaa' , abbr_hlgroup : 'Title' , kind_hlgroup : 'SpecialKey' }, 6851\ #{ word : 'bbb' , abbr_hlgroup : 'MyAbbr' , kind_hlgroup : 'MyKind' }, 6852\ #{ word : 'ccc' }]) 6853return '' 6854endfunc 6855func ShownItems () 6856let g: items = complete_info ([ 'items' ]). items 6857return '' 6858endfunc 6859 6860call assert_false ( hlexists ( 'MyAbbr' )) 6861call feedkeys ( "i\<C-R>=DoComplete()\<CR>\<C-R>=ShownItems()\<CR>\<Esc>" , 'tx' ) 6862call assert_equal ([ 'Title' , 'MyAbbr' , '' ], 6863\ map ( copy ( g: items ), 'v:val.abbr_hlgroup' )) 6864call assert_equal ([ 'SpecialKey' , 'MyKind' , '' ], 6865\ map ( copy ( g: items ), 'v:val.kind_hlgroup' )) 6866call assert_true ( hlexists ( 'MyAbbr' )) 6867 6868unlet g: items 6869delfunc DoComplete 6870delfunc ShownItems 6871bwipe ! 6872endfunc 6873 6874" Test that complete_info() tells a completion Vim started by itself from one 6875" that a key asked for. 6876func Test_complete_info_auto () 6877call Ntest_override ( "char_avail" , 1 ) 6878new 6879func ! AutoOmni ( findstart , base ) 6880if a: findstart 6881call add ( g: auto , complete_info ([ 'auto' ]). auto ) 6882return 4 6883endif 6884return [ 'alpha' , 'alphabet' ] 6885endfunc 6886setlocal omnifunc = AutoOmni 6887setlocal complete = o 6888setlocal completeopt = menuone 6889call setline ( 1 , ' al' ) 6890 6891" 'autocomplete' starts it, nothing was asked for. 6892setlocal autocomplete 6893let g: auto = [] 6894call feedkeys ( "A\<Esc>" , 'tx' ) 6895call assert_equal ([ 1 ], g: auto ) 6896 6897" The second call is the one CTRL-X CTRL-O asked for. 6898let g: auto = [] 6899call feedkeys ( "A\<C-X>\<C-O>\<Esc>" , 'tx' ) 6900call assert_equal ([ 1 , 0 ], g: auto ) 6901 6902" CTRL-X CTRL-O is typed before 'autocompletedelay' expires. 6903set autocompletedelay = 100 6904let g: auto = [] 6905call feedkeys ( "A\<C-X>\<C-O>\<Esc>" , 'tx' ) 6906call assert_equal ([ 0 ], g: auto ) 6907 6908set autocompletedelay & 6909setlocal noautocomplete 6910let g: auto = [] 6911call feedkeys ( "A\<C-X>\<C-O>\<Esc>" , 'tx' ) 6912call assert_equal ([ 0 ], g: auto ) 6913 6914call Ntest_override ( "ALL" , 0 ) 6915unlet g: auto 6916delfunc AutoOmni 6917bwipe ! 6918endfunc 6919 6920" A completion asked for while 'autocompletedelay' runs is not given the 6921" implicit "noselect" of autocompletion. 6922func Test_complete_asked_for_during_delay () 6923call Ntest_override ( "char_avail" , 1 ) 6924new 6925func ! AutoOmni ( findstart , base ) 6926if a: findstart 6927return 4 6928endif 6929return [ 'alpha' , 'alphabet' ] 6930endfunc 6931func ! Selected () 6932let g: selected = complete_info ([ 'selected' ]). selected 6933return '' 6934endfunc 6935setlocal omnifunc = AutoOmni 6936setlocal complete = o 6937setlocal completeopt = menuone 6938setlocal autocomplete 6939call setline ( 1 , ' al' ) 6940 6941" CTRL-X CTRL-O before the delay expires: the first match is selected, 6942" and taken. 6943set autocompletedelay = 100 6944call feedkeys ( "A\<C-X>\<C-O>\<C-R>=Selected()\<CR>\<Esc>" , 'tx' ) 6945call assert_equal ( 0 , g: selected ) 6946call assert_equal ( ' alpha' , getline ( 1 )) 6947 6948" What autocompletion puts up on its own selects nothing, so the word is 6949" left as it was. 6950set autocompletedelay & 6951call setline ( 1 , ' al' ) 6952call feedkeys ( "A\<C-R>=Selected()\<CR>\<Esc>" , 'tx' ) 6953call assert_equal ( -1 , g: selected ) 6954call assert_equal ( ' al' , getline ( 1 )) 6955 6956call Ntest_override ( "ALL" , 0 ) 6957unlet g: selected 6958delfunc AutoOmni 6959delfunc Selected 6960bwipe ! 6961endfunc 6962 6963func Test_complete_fuzzy_resort () 6964new 6965set completeopt = menu,menuone,noselect,fuzzy 6966 6967inoremap <buffer> < F5 > <Cmd> call complete ( 1 , [ 'xxxb' , 'xb' , 'b' ]) <CR> 6968call feedkeys ( "i\<F5>b\<C-R>=string(map(complete_info(['items']).items, 'v:val.word'))\<CR>\<Esc>" , 'tx' ) 6969call assert_equal ( "b['b', 'xb', 'xxxb']" , getline ( 1 )) 6970 6971% d _ 6972call setline ( 1 , [ 'xxxbar' , 'xbar' , 'bar' ]) 6973call feedkeys ( "Go\<C-P>b\<C-R>=string(map(complete_info(['items']).items, 'v:val.word'))\<CR>\<Esc>" , 'tx' ) 6974call assert_equal ( "b['bar', 'xbar', 'xxxbar']" , getline ( '$' )) 6975 6976bwipe ! 6977set completeopt & 6978endfunc 6979 6980" Test for 'complete' F{func} callbacks when using ":setglobal" 6981func Test_complete_cpt_func_setglobal () 6982func ! CptSetglobalOne ( findstart , base ) 6983if a: findstart 6984return col ( '.' ) - 1 6985endif 6986return [ 'one' ] 6987endfunc 6988func ! CptSetglobalTwo ( findstart , base ) 6989if a: findstart 6990return col ( '.' ) - 1 6991endif 6992return [ 'two' ] 6993endfunc 6994 6995new 6996setlocal complete = FCptSetglobalOne 6997" ":setglobal" does not change the buffer-local value 6998setglobal complete=t,FCptSetglobalTwo 6999exe "normal! i\<C-N>\<Esc>" 7000call assert_equal ( 'one' , getline ( 1 )) 7001 7002" A new buffer uses the global value, the callbacks must match it 7003new 7004exe "normal! i\<C-N>\<Esc>" 7005call assert_equal ( 'two' , getline ( 1 )) 7006bwipe ! 7007bwipe ! 7008 7009" When the global value has more entries than the buffer-local one, the 7010" callback array used to be indexed out of bounds 7011new 7012setlocal complete = . 7013setglobal complete=FCptSetglobalTwo,FCptSetglobalTwo,FCptSetglobalTwo,FCptSetglobalTwo,FCptSetglobalTwo,FCptSetglobalTwo,FCptSetglobalTwo,FCptSetglobalTwo 7014new 7015exe "normal! i\<C-N>\<Esc>" 7016call assert_equal ( 'two' , getline ( 1 )) 7017bwipe ! 7018bwipe ! 7019 7020" ":setlocal" does not change the callbacks cached for the global value 7021new 7022setlocal complete = FCptSetglobalOne 7023new 7024exe "normal! i\<C-N>\<Esc>" 7025call assert_equal ( 'two' , getline ( 1 )) 7026bwipe ! 7027bwipe ! 7028 7029set complete & 7030delfunc CptSetglobalOne 7031delfunc CptSetglobalTwo 7032endfunc 7033 7034 7035" change the completion value while being triggered 7036func Test_complete_cpt_func_changes_complete () 7037func ! CptChange ( findstart , base ) 7038if a: findstart 7039set complete = . 7040return col ( '.' ) - 1 7041endif 7042return [ 'changed' ] 7043endfunc 7044 7045new 7046call setline ( 1 , [ 'alpha' , '' ]) 7047setlocal complete = FCptChange,FCptChange,FCptChange 7048exe "normal! Gi\<C-N>\<Esc>" 7049call assert_equal ( 'alpha' , getline ( 2 )) 7050 7051set complete & 7052delfunc CptChange 7053bwipe ! 7054endfunc 7055 7056" vim: shiftwidth=2 sts=2 expandtab nofoldenable