robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local feed =n . feed 7local fn =n . call 8local source =n . source 9local clear =n . clear 10local command =n . command 11local expect =n . expect 12local poke_eventloop =n . poke_eventloop 13local api =n . api 14local eq =t . eq 15local pcall_err =t . pcall_err 16local neq =t . neq 17local exec_lua =n . exec_lua 18 19describe ( 'prompt buffer' , function () 20local screen 21 22before_each ( function () 23clear () 24screen =Screen . new ( 25 , 10 ) 25command ( 'set laststatus=0 nohidden' ) 26end ) 27 28local function source_script () 29source ( [[ 30func TextEntered(text) 31if a:text == "exit" 32stopinsert 33close 34else 35" Add the output above the current prompt. 36call prompt_appendbuf(bufnr(''), split('Command: "' . a:text . '"', '\n')) 37" Reset &modified to allow the buffer to be closed. 38set nomodified 39call timer_start(20, {id -> TimerFunc(a:text)}) 40endif 41endfunc 42 43func TimerFunc(text) 44" Add the output above the current prompt. 45call prompt_appendbuf(bufnr(''), split('Result: "' . a:text .'"', '\n')) 46" Reset &modified to allow the buffer to be closed. 47set nomodified 48endfunc 49 50func SwitchWindows() 51call timer_start(0, {-> execute("wincmd p", "")}) 52endfunc 53 54call setline(1, "other buffer") 55set nomodified 56new 57set buftype=prompt 58call prompt_setcallback(bufnr(''), function("TextEntered")) 59eval bufnr("")->prompt_setprompt("cmd: ") 60startinsert 61]] ) 62screen : expect ( [[ 63cmd: ^ | 64{1:~ }|*3 65{3:[Prompt] }| 66other buffer | 67{1:~ }|*3 68{5:-- INSERT --} | 69]] ) 70end 71 72-- oldtest: Test_prompt_basic() 73it ( 'works' , function () 74source_script () 75feed ( 'hello\n' ) 76screen : expect ( [[ 77cmd: hello | 78Command: "hello" | 79Result: "hello" | 80cmd: ^ | 81{3:[Prompt] }| 82other buffer | 83{1:~ }|*3 84{5:-- INSERT --} | 85]] ) 86feed ( 'exit\n' ) 87screen : expect ( [[ 88^other buffer | 89{1:~ }|*8 90| 91]] ) 92 93command ( 'new' ) 94command ( 'set buftype=prompt' ) 95feed ( 'iabc<BS><BS>' ) 96eq ( 'a' , fn ( 'prompt_getinput' , fn ( 'bufnr' ))) 97command ( 'quit' ) 98eq ( 1 , # api . nvim_list_wins ()) 99 100command ( 'new' ) 101command ( 'set buftype=prompt modified' ) 102eq ( 103'Vim(quit):E37: No write since last change (add ! to override)' , 104t . pcall_err ( command , 'quit' ) 105) 106eq ( 2 , # api . nvim_list_wins ()) 107end ) 108 109-- oldtest: Test_prompt_editing() 110it ( 'editing' , function () 111source_script () 112feed ( 'hello<BS><BS>' ) 113screen : expect ( [[ 114cmd: hel^ | 115{1:~ }|*3 116{3:[Prompt] }| 117other buffer | 118{1:~ }|*3 119{5:-- INSERT --} | 120]] ) 121feed ( '<Left><Left><Left><BS>-' ) 122screen : expect ( [[ 123cmd: -^hel | 124{1:~ }|*3 125{3:[Prompt] }| 126other buffer | 127{1:~ }|*3 128{5:-- INSERT --} | 129]] ) 130feed ( '<C-O>lz' ) 131screen : expect ( [[ 132cmd: -hz^el | 133{1:~ }|*3 134{3:[Prompt] }| 135other buffer | 136{1:~ }|*3 137{5:-- INSERT --} | 138]] ) 139feed ( '<End>x' ) 140screen : expect ( [[ 141cmd: -hzelx^ | 142{1:~ }|*3 143{3:[Prompt] }| 144other buffer | 145{1:~ }|*3 146{5:-- INSERT --} | 147]] ) 148 149-- :edit doesn't apply on prompt buffer 150eq ( 'Vim(edit):cannot :edit a prompt buffer' , t . pcall_err ( api . nvim_command , 'edit' )) 151 152feed ( '<C-U>exit\n' ) 153screen : expect ( [[ 154^other buffer | 155{1:~ }|*8 156| 157]] ) 158end ) 159 160-- oldtest: Test_prompt_switch_windows() 161it ( 'switch windows' , function () 162source_script () 163feed ( '<C-O>:call SwitchWindows()<CR>' ) 164screen : expect ( [[ 165cmd: | 166{1:~ }|*3 167{2:[Prompt] }| 168^other buffer | 169{1:~ }|*3 170| 171]] ) 172feed ( '<C-O>:call SwitchWindows()<CR>' ) 173screen : expect ( [[ 174cmd: ^ | 175{1:~ }|*3 176{3:[Prompt] }| 177other buffer | 178{1:~ }|*3 179{5:-- INSERT --} | 180]] ) 181feed ( '<Esc>' ) 182screen : expect ( [[ 183cmd:^ | 184{1:~ }|*3 185{3:[Prompt] }| 186other buffer | 187{1:~ }|*3 188| 189]] ) 190end ) 191 192-- oldtest: Test_prompt_while_writing_to_hidden_buffer() 193it ( 'keeps insert mode after aucmd_restbuf in callback' , function () 194source_script () 195source [[ 196let s:buf = nvim_create_buf(1, 1) 197call timer_start(0, {-> nvim_buf_set_lines(s:buf, -1, -1, 0, ['walrus'])}) 198]] 199poke_eventloop () 200eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 201end ) 202 203-- oldtest: Test_prompt_appending_while_hidden() 204it ( 'accessing hidden prompt buffer does not start insert mode' , function () 205local prev_win =api . nvim_get_current_win () 206source ( [[ 207new prompt 208set buftype=prompt 209set bufhidden=hide 210 211func s:TextEntered(text) 212if a:text == 'exit' 213close 214endif 215let g:entered = a:text 216endfunc 217call prompt_setcallback(bufnr(), function('s:TextEntered')) 218 219func DoAppend(cmd_before = '') 220exe a:cmd_before 221call appendbufline('prompt', '$', 'Test') 222return '' 223endfunc 224 225autocmd User SwitchTabPages tabprevious | tabnext 226func DoAutoAll(cmd_before = '') 227exe a:cmd_before 228doautoall User SwitchTabPages 229return '' 230endfunc 231]] ) 232feed ( 'asomething<CR>' ) 233eq ( 'something' , api . nvim_get_var ( 'entered' )) 234neq ( prev_win , api . nvim_get_current_win ()) 235feed ( 'exit<CR>' ) 236eq ( prev_win , api . nvim_get_current_win ()) 237eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 238command ( 'call DoAppend()' ) 239eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 240feed ( 'i' ) 241eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 242command ( 'call DoAppend()' ) 243eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 244command ( "call DoAppend('stopinsert')" ) 245eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 246command ( "call DoAppend('startreplace')" ) 247eq ( { mode ='R' , blocking =false } , api . nvim_get_mode ()) 248feed ( '<Esc>' ) 249command ( 'tabnew' ) 250eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 251command ( "call DoAutoAll('startinsert')" ) 252eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 253command ( "call DoAutoAll('stopinsert')" ) 254eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 255end ) 256 257-- oldtest: Test_prompt_leave_modify_hidden() 258it ( 'modifying hidden buffer does not prevent prompt buffer mode change' , function () 259source ( [[ 260file hidden 261set bufhidden=hide 262enew 263new prompt 264set buftype=prompt 265 266inoremap <buffer> w <Cmd>wincmd w<CR> 267inoremap <buffer> q <Cmd>bwipe!<CR> 268autocmd BufLeave prompt call appendbufline('hidden', '$', 'Leave') 269autocmd BufEnter prompt call appendbufline('hidden', '$', 'Enter') 270autocmd BufWinLeave prompt call appendbufline('hidden', '$', 'Close') 271]] ) 272feed ( 'a' ) 273eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 274feed ( 'w' ) 275eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 276feed ( '<C-W>w' ) 277eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 278feed ( 'q' ) 279eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 280command ( 'bwipe!' ) 281expect ( [[ 282 283Leave 284Enter 285Leave 286Close]] ) 287end ) 288 289it ( 'can insert multiline text' , function () 290source_script () 291local buf =api . nvim_get_current_buf () 292 293feed ( 'line 1<s-cr>line 2<s-cr>line 3' ) 294screen : expect ( [[ 295cmd: line 1 | 296line 2 | 297line 3^ | 298{1:~ }| 299{3:[Prompt] }| 300other buffer | 301{1:~ }|*3 302{5:-- INSERT --} | 303]] ) 304 305-- prompt_getinput works with multiline input 306eq ( 'line 1\nline 2\nline 3' , fn ( 'prompt_getinput' , buf )) 307 308feed ( '<cr>' ) 309-- submitting multiline text works 310screen : expect ( [[ 311Result: "line 1 | 312line 2 | 313line 3" | 314cmd: ^ | 315{3:[Prompt] }| 316other buffer | 317{1:~ }|*3 318{5:-- INSERT --} | 319]] ) 320 321eq ( '' , fn ( 'prompt_getinput' , buf )) 322 323-- % prompt is not repeated with formatoptions+=r 324source ( [[ 325bwipeout! 326set formatoptions+=r 327call prompt_setprompt(bufnr(), "% ") 328set buftype=prompt 329]] ) 330feed ( 'iline1<s-cr>line2' ) 331screen : expect ( [[ 332other buffer | 333% line1 | 334line2^ | 335{1:~ }|*6 336{5:-- INSERT --} | 337]] ) 338 339-- ensure cursor gets placed on first line of user input. 340-- when insert mode is entered from read-only region of prompt buffer. 341local prompt_pos =api . nvim_buf_get_mark ( 0 , ':' ) 342feed ( '<esc>' ) 343-- works before prompt 344api . nvim_win_set_cursor ( 0 , { prompt_pos [ 1 ] - 1 , 0 } ) 345screen : expect ( [[ 346^other buffer | 347% line1 | 348line2 | 349{1:~ }|*6 350| 351]] ) 352feed ( 'a' ) 353feed ( '<esc>' ) 354screen : expect ( [[ 355other buffer | 356%^ line1 | 357line2 | 358{1:~ }|*6 359| 360]] ) 361-- works on prompt 362api . nvim_win_set_cursor ( 0 , { prompt_pos [ 1 ], 0 } ) 363screen : expect ( [[ 364other buffer | 365^% line1 | 366line2 | 367{1:~ }|*6 368| 369]] ) 370feed ( 'a' ) 371feed ( '<esc>' ) 372screen : expect ( [[ 373other buffer | 374%^ line1 | 375line2 | 376{1:~ }|*6 377| 378]] ) 379 380-- i_<Left> i_<C-Left> i_<Home> i_<End> keys on prompt-line doesn't put cursor 381-- at end of text 382feed ( 'a<Left><C-Left>' ) 383screen : expect ( [[ 384other buffer | 385% ^line1 | 386line2 | 387{1:~ }|*6 388{5:-- INSERT --} | 389]] ) 390 391feed ( '<End>' ) 392screen : expect ( [[ 393other buffer | 394% line1^ | 395line2 | 396{1:~ }|*6 397{5:-- INSERT --} | 398]] ) 399 400feed ( '<Home>' ) 401screen : expect ( [[ 402other buffer | 403% ^line1 | 404line2 | 405{1:~ }|*6 406{5:-- INSERT --} | 407]] ) 408end ) 409 410it ( 'can put (p) multiline text' , function () 411source_script () 412local buf =api . nvim_get_current_buf () 413 414fn ( 'setreg' , 'a' , 'line 1\nline 2\nline 3' ) 415feed ( '<esc>"ap' ) 416screen : expect ( [[ 417cmd: ^line 1 | 418line 2 | 419line 3 | 420{1:~ }| 421{3:[Prompt] }| 422other buffer | 423{1:~ }|*3 424| 425]] ) 426 427-- prompt_getinput works with pasted input 428eq ( 'line 1\nline 2\nline 3' , fn ( 'prompt_getinput' , buf )) 429 430feed ( 'i<cr>' ) 431screen : expect ( [[ 432Result: "line 1 | 433line 2 | 434line 3" | 435cmd: ^ | 436{3:[Prompt] }| 437other buffer | 438{1:~ }|*3 439{5:-- INSERT --} | 440]] ) 441end ) 442 443it ( 'can put multiline text with nvim_paste' , function () 444source_script () 445api . nvim_paste ( 'line 1\nline 2\nline 3' , false , - 1 ) 446screen : expect ( [[ 447cmd: line 1 | 448line 2 | 449line 3^ | 450{1:~ }| 451{3:[Prompt] }| 452other buffer | 453{1:~ }|*3 454{5:-- INSERT --} | 455]] ) 456end ) 457 458it ( 'can undo current prompt' , function () 459source_script () 460local buf =api . nvim_get_current_buf () 461 462-- text editing allowed in current prompt 463feed ( 'tests-initial<esc>' ) 464feed ( 'bimiddle-<esc>' ) 465screen : expect ( [[ 466cmd: tests-middle^-initial| 467{1:~ }|*3 468{3:[Prompt] }| 469other buffer | 470{1:~ }|*3 471| 472]] ) 473 474feed ( 'Fdx' ) 475screen : expect ( [[ 476cmd: tests-mid^le-initial | 477{1:~ }|*3 478{3:[Prompt] }| 479other buffer | 480{1:~ }|*3 481| 482]] ) 483 484-- can undo edits until prompt has been submitted 485feed ( 'u' ) 486screen : expect ( [[ 487cmd: tests-mid^dle-initial| 488{1:~ }|*3 489{3:[Prompt] }| 490other buffer | 491{1:~ }|*3 4921 change; {MATCH:.*} | 493]] ) 494 495-- undo is reflected in prompt_getinput 496eq ( 'tests-middle-initial' , fn ( 'prompt_getinput' , buf )) 497 498feed ( 'u' ) 499screen : expect ( [[ 500cmd: tests-^initial | 501{1:~ }|*3 502{3:[Prompt] }| 503other buffer | 504{1:~ }|*3 5051 change; {MATCH:.*} | 506]] ) 507 508feed ( 'i<cr><esc>' ) 509screen : expect ( [[ 510cmd: tests-initial | 511Command: "tests-initial" | 512Result: "tests-initial" | 513cmd:^ | 514{3:[Prompt] }| 515other buffer | 516{1:~ }|*3 517| 518]] ) 519 520-- after submit undo does nothing 521feed ( 'u' ) 522screen : expect ( [[ 523cmd: tests-initial | 524Command: "tests-initial" | 525cmd:^ | 526{1:~ }| 527{3:[Prompt] }| 528other buffer | 529{1:~ }|*3 5301 line {MATCH:.*} | 531]] ) 532 533-- "S" does not clear undo 534feed ( 'ihello<Esc>S' ) 535screen : expect ( [[ 536cmd: tests-initial | 537Command: "tests-initial" | 538cmd: ^ | 539{1:~ }| 540{3:[Prompt] }| 541other buffer | 542{1:~ }|*3 543{5:-- INSERT --} | 544]] ) 545feed ( '<Esc>u' ) 546-- Nvim: the cursor returns to its pre-"S" position (|restore-undo-cursor|). 547screen : expect ( [[ 548cmd: tests-initial | 549Command: "tests-initial" | 550cmd: hell^o | 551{1:~ }| 552{3:[Prompt] }| 553other buffer | 554{1:~ }|*3 5551 change; {MATCH:.*} | 556]] ) 557 558-- undo cleared if prompt changes 559-- (otherwise undoing would abort it and append a new prompt, which isn't useful) 560fn ( 'prompt_setprompt' , '' , 'cmd > ' ) 561feed ( 'u' ) 562screen : expect ( [[ 563cmd: tests-initial | 564Command: "tests-initial" | 565cmd > hell^o | 566{1:~ }| 567{3:[Prompt] }| 568other buffer | 569{1:~ }|*3 570Already at oldest change | 571]] ) 572 573-- new prompt line appended to fix missing prompt also clears undo 574feed ( 'A there' ) 575fn ( 'setpos' , "':" , { 0 , fn ( 'line' , '.' ), 99 , 0 } ) 576feed ( '<Esc>u' ) 577screen : expect ( [[ 578cmd: tests-initial | 579Command: "tests-initial" | 580cmd > hello there | 581cmd >^ | 582{3:[Prompt] }| 583other buffer | 584{1:~ }|*3 585Already at oldest change | 586]] ) 587end ) 588 589it ( 'o/O can create new lines' , function () 590source_script () 591local buf =api . nvim_get_current_buf () 592 593feed ( 'line 1<s-cr>line 2<s-cr>line 3' ) 594screen : expect ( [[ 595cmd: line 1 | 596line 2 | 597line 3^ | 598{1:~ }| 599{3:[Prompt] }| 600other buffer | 601{1:~ }|*3 602{5:-- INSERT --} | 603]] ) 604 605feed ( '<esc>koafter' ) 606screen : expect ( [[ 607cmd: line 1 | 608line 2 | 609after^ | 610line 3 | 611{3:[Prompt] }| 612other buffer | 613{1:~ }|*3 614{5:-- INSERT --} | 615]] ) 616 617-- newline created with o is reflected in prompt_getinput 618eq ( 'line 1\nline 2\nafter\nline 3' , fn ( 'prompt_getinput' , buf )) 619 620feed ( '<esc>kObefore' ) 621 622screen : expect ( [[ 623cmd: line 1 | 624before^ | 625line 2 | 626after | 627{3:[Prompt] }| 628other buffer | 629{1:~ }|*3 630{5:-- INSERT --} | 631]] ) 632 633-- newline created with O is reflected in prompt_getinput 634eq ( 'line 1\nbefore\nline 2\nafter\nline 3' , fn ( 'prompt_getinput' , buf )) 635 636feed ( '<cr>' ) 637vim . uv . sleep ( 20 ) 638eq ( '' , fn ( 'prompt_getinput' , buf )) 639screen : expect ( [[ 640line 2 | 641after | 642line 3" | 643cmd: ^ | 644{3:[Prompt] }| 645other buffer | 646{1:~ }|*3 647{5:-- INSERT --} | 648]] ) 649 650feed ( 'line 4<s-cr>line 5' ) 651screen : expect ( [[ 652after | 653line 3" | 654cmd: line 4 | 655line 5^ | 656{3:[Prompt] }| 657other buffer | 658{1:~ }|*3 659{5:-- INSERT --} | 660]] ) 661 662feed ( '<esc>k0oafter prompt' ) 663screen : expect ( [[ 664after | 665line 3" | 666cmd: line 4 | 667after prompt^ | 668{3:[Prompt] }| 669other buffer | 670{1:~ }|*3 671{5:-- INSERT --} | 672]] ) 673 674feed ( '<esc>k0Oat prompt' ) 675screen : expect ( [[ 676after | 677line 3" | 678cmd: at prompt^ | 679line 4 | 680{3:[Prompt] }| 681other buffer | 682{1:~ }|*3 683{5:-- INSERT --} | 684]] ) 685 686feed ( '<cr>' ) 687vim . uv . sleep ( 20 ) 688eq ( '' , fn ( 'prompt_getinput' , buf )) 689screen : expect ( [[ 690line 4 | 691after prompt | 692line 5" | 693cmd: ^ | 694{3:[Prompt] }| 695other buffer | 696{1:~ }|*3 697{5:-- INSERT --} | 698]] ) 699end ) 700 701it ( 'deleting prompt adds it back on insert' , function () 702source_script () 703feed ( 'asdf' ) 704screen : expect ( [[ 705cmd: asdf^ | 706{1:~ }|*3 707{3:[Prompt] }| 708other buffer | 709{1:~ }|*3 710{5:-- INSERT --} | 711]] ) 712 713feed ( '<esc>ddi' ) 714screen : expect ( [[ 715cmd: ^ | 716{1:~ }|*3 717{3:[Prompt] }| 718other buffer | 719{1:~ }|*3 720{5:-- INSERT --} | 721]] ) 722 723feed ( 'asdf' ) 724screen : expect ( [[ 725cmd: asdf^ | 726{1:~ }|*3 727{3:[Prompt] }| 728other buffer | 729{1:~ }|*3 730{5:-- INSERT --} | 731]] ) 732 733feed ( '<esc>cc' ) 734screen : expect ( [[ 735cmd: ^ | 736{1:~ }|*3 737{3:[Prompt] }| 738other buffer | 739{1:~ }|*3 740{5:-- INSERT --} | 741]] ) 742end ) 743 744it ( "sets the ': mark" , function () 745api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =0 } ) 746exec_lua ( function () 747local buf =vim . api . nvim_get_current_buf () 748vim . fn . prompt_setprompt ( buf , 'cmd > ' ) 749vim . fn . prompt_setcallback ( buf , function ( str ) 750local last_line =vim . api . nvim_buf_line_count ( buf ) 751vim . api . nvim_buf_set_lines ( buf , last_line - 1 , last_line - 1 , true , vim . split ( str , '\n' )) 752end ) 753end ) 754 755feed ( 'asdf' ) 756eq ( { 1 , 6 } , api . nvim_buf_get_mark ( 0 , ':' )) 757feed ( '<cr>' ) 758eq ( { 3 , 6 } , api . nvim_buf_get_mark ( 0 , ':' )) 759-- Multiline prompt. 760feed ( '<s-cr>line1<s-cr>line2<s-cr>line3<cr>' ) 761eq ( { 11 , 6 } , api . nvim_buf_get_mark ( 0 , ':' )) 762 763-- ': mark is only available in prompt buffer. 764api . nvim_set_option_value ( 'buftype' , '' , { buf =0 } ) 765eq ( "Invalid mark name: ':'" , t . pcall_err ( api . nvim_buf_get_mark , 0 , ':' )) 766 767-- mark can be moved 768api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =0 } ) 769local last_line =api . nvim_buf_line_count ( 0 ) 770eq ( { last_line , 6 } , api . nvim_buf_get_mark ( 0 , ':' )) 771eq ( true , api . nvim_buf_set_mark ( 0 , ':' , 1 , 5 , {} )) 772eq ( { 1 , 5 } , api . nvim_buf_get_mark ( 0 , ':' )) 773 774-- No crash from invalid col. 775eq ( true , api . nvim_buf_set_mark ( 0 , ':' , fn ( 'line' , '.' ), 999 , {} )) 776eq ( { 12 , 6 } , api . nvim_buf_get_mark ( 0 , ':' )) 777 778-- Clamps lnum to at least 1. Do in one event to repro the leak. 779exec_lua ( function () 780vim . fn . setpos ( "':" , { 0 , 0 , 0 , 0 } ) 781vim . fn . prompt_setprompt ( '' , 'bar > ' ) 782end ) 783eq ( { 1 , 6 } , api . nvim_buf_get_mark ( 0 , ':' )) 784 785-- No ml_get error from invalid lnum. 786command ( 'set messagesopt+=wait:0 messagesopt-=hit-enter' ) 787fn ( 'setpos' , "':" , { 0 , 999 , 7 , 0 } ) 788eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 789command ( 'set messagesopt&' ) 790eq ( { 13 , 6 } , api . nvim_buf_get_mark ( 0 , ':' )) 791end ) 792 793describe ( 'prompt_getinput' , function () 794it ( 'returns current prompts text' , function () 795command ( 'new' ) 796local bufnr =fn ( 'bufnr' ) 797api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =0 } ) 798eq ( '' , fn ( 'prompt_getinput' , bufnr )) 799feed ( 'iasdf' ) 800eq ( 'asdf' , fn ( 'prompt_getinput' , bufnr )) 801feed ( '<esc>dd' ) 802eq ( '' , fn ( 'prompt_getinput' , bufnr )) 803feed ( 'iasdf2' ) 804eq ( 'asdf2' , fn ( 'prompt_getinput' , bufnr )) 805 806-- returns empty string when called from non prompt buffer 807api . nvim_set_option_value ( 'buftype' , '' , { buf =0 } ) 808eq ( '' , fn ( 'prompt_getinput' , bufnr )) 809end ) 810end ) 811 812it ( 'programmatic (non-user) edits' , function () 813api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =0 } ) 814 815-- with nvim_buf_set_lines 816exec_lua ( [[ 817local buf = vim.api.nvim_get_current_buf() 818vim.fn.prompt_setcallback(buf, function(text) 819vim.api.nvim_buf_set_lines(buf, -2, -2, true, vim.split(text, '\n')) 820end) 821]] ) 822feed ( 'iset_lines<cr>' ) 823feed ( 'set_lines2<cr>' ) 824screen : expect ( [[ 825% set_lines | 826set_lines | 827% set_lines2 | 828set_lines2 | 829% ^ | 830{1:~ }|*4 831{5:-- INSERT --} | 832]] ) 833 834feed ( 'set_lines3(multi-1)<s-cr>set_lines3(multi-2)<cr>' ) 835screen : expect ( [[ 836% set_lines | 837set_lines | 838% set_lines2 | 839set_lines2 | 840% set_lines3(multi-1) | 841set_lines3(multi-2) | 842set_lines3(multi-1) | 843set_lines3(multi-2) | 844% ^ | 845{5:-- INSERT --} | 846]] ) 847-- with nvim_buf_set_text 848source ( 'bwipeout!' ) 849api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =0 } ) 850exec_lua ( [[ 851local buf = vim.api.nvim_get_current_buf() 852vim.fn.prompt_setcallback(buf, function(text) 853local lines = vim.split(text, '\n') 854if lines[#lines] ~= '' then 855table.insert(lines, '') 856end 857vim.api.nvim_buf_set_text(buf, -1, 0, -1, 0, lines) 858end) 859]] ) 860feed ( 'set_text<cr>' ) 861feed ( 'set_text2<cr>' ) 862screen : expect ( [[ 863% set_text | 864set_text | 865% set_text2 | 866set_text2 | 867% ^ | 868{1:~ }|*4 869{5:-- INSERT --} | 870]] ) 871 872feed ( 'set_text3(multi-1)<s-cr>set_text3(multi-2)<cr>' ) 873screen : expect ( [[ 874% set_text | 875set_text | 876% set_text2 | 877set_text2 | 878% set_text3(multi-1) | 879set_text3(multi-2) | 880set_text3(multi-1) | 881set_text3(multi-2) | 882% ^ | 883{5:-- INSERT --} | 884]] ) 885end ) 886 887it ( 'works correctly with empty string as prompt' , function () 888api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =0 } ) 889exec_lua ( function () 890local buf =vim . api . nvim_get_current_buf () 891vim . fn . prompt_setprompt ( buf , '' ) 892end ) 893 894source ( 'startinsert' ) 895 896-- mark correctly set 897eq ( { 1 , 0 } , api . nvim_buf_get_mark ( 0 , ':' )) 898 899feed ( 'asdf' ) 900screen : expect ( [[ 901asdf^ | 902{1:~ }|*8 903{5:-- INSERT --} | 904]] ) 905 906-- can clear all of it 907feed ( '<backspace><backspace><backspace><backspace>' ) 908screen : expect ( [[ 909^ | 910{1:~ }|*8 911{5:-- INSERT --} | 912]] ) 913feed ( '<cr>' ) 914 915eq ( { 2 , 0 } , api . nvim_buf_get_mark ( 0 , ':' )) 916end ) 917 918it ( 'prompt can be changed without interrupting user input' , function () 919api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =0 } ) 920local buf =api . nvim_get_current_buf () 921 922local function set_prompt ( prompt , b ) 923fn ( 'prompt_setprompt' , b or buf , prompt ) 924end 925 926set_prompt ( '> ' ) 927 928source ( 'startinsert' ) 929 930feed ( 'user input' ) 931-- Move the cursor a bit to check cursor maintaining position 932feed ( '<esc>hhi' ) 933 934screen : expect ( [[ 935> user in^put | 936{1:~ }|*8 937{5:-- INSERT --} | 938]] ) 939 940eq ( { 1 , 2 } , api . nvim_buf_get_mark ( 0 , ':' )) 941 942set_prompt ( 'new-prompt > ' ) 943 944screen : expect ( [[ 945new-prompt > user in^put | 946{1:~ }|*8 947{5:-- INSERT --} | 948]] ) 949 950eq ( { 1 , 13 } , api . nvim_buf_get_mark ( 0 , ':' )) 951 952set_prompt ( 'new-prompt(status) > ' ) 953 954screen : expect ( [[ 955new-prompt(status) > user| 956in^put | 957{1:~ }|*7 958{5:-- INSERT --} | 959]] ) 960eq ( { 1 , 21 } , api . nvim_buf_get_mark ( 0 , ':' )) 961 962set_prompt ( 'new-prompt > ' ) 963 964screen : expect ( [[ 965new-prompt > user in^put | 966{1:~ }|*8 967{5:-- INSERT --} | 968]] ) 969eq ( { 1 , 13 } , api . nvim_buf_get_mark ( 0 , ':' )) 970 971-- Cursor not moved when not on the prompt line. 972feed ( '<CR>user input<Esc>k' ) 973screen : expect ( [[ 974new-prompt > user inpu^t | 975new-prompt > user input | 976{1:~ }|*7 977| 978]] ) 979set_prompt ( '<>< ' ) 980screen : expect ( [[ 981new-prompt > user inpu^t | 982<>< user input | 983{1:~ }|*7 984| 985]] ) 986-- Correct col when prompt has multi-cell chars. 987feed ( 'i<Left><Left>' ) 988screen : expect ( [[ 989new-prompt > user input | 990<>< user inp^ut | 991{1:~ }|*7 992{5:-- INSERT --} | 993]] ) 994set_prompt ( '\t > ' ) 995screen : expect ( [[ 996new-prompt > user input | 997> user inp^ut | 998{1:~ }|*7 999{5:-- INSERT --} | 1000]] ) 1001-- Works with 'virtualedit': coladd remains sensible. Cursor is redrawn correctly. 1002-- Tab size visually changes due to multiples of 'tabstop'. 1003command ( 'set virtualedit=all' ) 1004feed ( '<C-O>Sa<Tab>b<C-O>3h' ) 1005screen : expect ( [[ 1006new-prompt > user input | 1007> a ^ b | 1008{1:~ }|*7 1009{5:-- INSERT --} | 1010]] ) 1011set_prompt ( '😊 > ' ) 1012screen : expect ( [[ 1013new-prompt > user input | 1014😊 > a ^ b | 1015{1:~ }|*7 1016{5:-- INSERT --} | 1017]] ) 1018-- Minimum col should be 1. Same event to avoid corrections from the state loop. 1019feed ( '<Esc>0' ) 1020local colnr =exec_lua ( function () 1021vim . fn . prompt_setprompt ( '' , '' ) 1022return vim . fn . col ( '.' ) 1023end ) 1024eq ( 1 , colnr ) 1025-- Correct cursor adjustment when old ': col and old prompt length differs. 1026set_prompt ( 'foo > ' ) 1027fn ( 'setpos' , "':" , { 0 , fn ( 'line' , '.' ), 10 , 0 } ) 1028fn ( 'setline' , '.' , ' foo > hello' ) 1029feed ( 'fh' ) 1030screen : expect ( [[ 1031new-prompt > user input | 1032foo > ^hello | 1033{1:~ }|*7 1034| 1035]] ) 1036set_prompt ( 'bar > ' ) 1037screen : expect ( [[ 1038new-prompt > user input | 1039bar > ^hello | 1040{1:~ }|*7 1041| 1042]] ) 1043 1044-- No crash when setting shorter prompt than curbuf's in other buffer. 1045feed ( 'ztA' ) 1046command ( 'set virtualedit& | new | setlocal buftype=prompt' ) 1047set_prompt ( 'looooooooooooooooooooooooooooooooooooooooooooong > ' , '' ) -- curbuf 1048set_prompt ( 'foo > ' ) 1049screen : expect ( [[ 1050loooooooooooooooooooooooo| 1051ooooooooooooooooooooong >| 1052^ | 1053{1:~ }| 1054{3:[Prompt] }| 1055foo > hello | 1056{1:~ }|*3 1057{5:-- INSERT --} | 1058]] ) 1059 1060-- No prompt_setprompt crash from invalid ': col. Must happen in the same event. 1061exec_lua ( function () 1062vim . cmd 'bwipeout!' 1063vim . api . nvim_buf_set_mark ( 0 , ':' , vim . fn . line ( '.' ), 999 , {} ) 1064vim . fn . prompt_setprompt ( '' , 'new-prompt > ' ) 1065end ) 1066screen : expect ( [[ 1067new-prompt > ^ | 1068{1:~ }|*8 1069{5:-- INSERT --} | 1070]] ) 1071 1072-- No leak if prompt_setprompt called for unloaded prompt buffer. 1073local unloaded_buf =fn ( 'bufadd' , '' ) 1074api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =unloaded_buf } ) 1075fn ( 'prompt_setprompt' , unloaded_buf , 'hello unloaded! > ' ) 1076eq ( 'hello unloaded! > ' , fn ( 'prompt_getprompt' , unloaded_buf )) 1077end ) 1078 1079it ( 'prompt_appendbuf with multi-element list and singleline prompt' , function () 1080command ( 'new' ) 1081local buf =api . nvim_get_current_buf () 1082api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =buf } ) 1083fn ( 'prompt_setprompt' , buf , 'cmd: ' ) 1084 1085-- Single element list: appends before prompt 1086fn ( 'prompt_appendbuf' , buf , { 'line1' } ) 1087eq ( { 'line1' , 'cmd: ' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , false )) 1088 1089-- Multi-element list: first element appended, rest inserted as new lines before prompt 1090fn ( 'prompt_appendbuf' , buf , { '-append' , 'line2' } ) 1091eq ( { 'line1-append' , 'line2' , 'cmd: ' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , false )) 1092 1093-- Multi-element list after multi-element list 1094fn ( 'prompt_appendbuf' , buf , { '' , 'line3' , 'line4' , 'line5' , 'line6' } ) 1095eq ( 1096{ 'line1-append' , 'line2' , 'line3' , 'line4' , 'line5' , 'line6' , 'cmd: ' } , 1097api . nvim_buf_get_lines ( buf , 0 , - 1 , false ) 1098) 1099end ) 1100 1101it ( 'prompt_appendbuf with multi-element list and multiline prompt' , function () 1102command ( 'new' ) 1103local buf =api . nvim_get_current_buf () 1104api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =buf } ) 1105fn ( 'prompt_setprompt' , buf , 'cmd: ' ) 1106source ( 'startinsert' ) 1107 1108-- User types multiline input in the prompt 1109feed ( 'input1<s-cr>input2' ) 1110eq ( { 'cmd: input1' , 'input2' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , false )) 1111 1112-- Single element list: appends before prompt 1113fn ( 'prompt_appendbuf' , buf , { 'line1' } ) 1114eq ( { 'line1' , 'cmd: input1' , 'input2' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , false )) 1115 1116-- Multi-element list: first element appended, rest inserted as new lines before prompt 1117fn ( 'prompt_appendbuf' , buf , { '-append' , 'line2' } ) 1118eq ( 1119{ 'line1-append' , 'line2' , 'cmd: input1' , 'input2' } , 1120api . nvim_buf_get_lines ( buf , 0 , - 1 , false ) 1121) 1122 1123-- Multi-element list after multi-element list 1124fn ( 'prompt_appendbuf' , buf , { '' , 'line3' , 'line4' , 'line5' , 'line6' } ) 1125eq ( 1126{ 'line1-append' , 'line2' , 'line3' , 'line4' , 'line5' , 'line6' , 'cmd: input1' , 'input2' } , 1127api . nvim_buf_get_lines ( buf , 0 , - 1 , false ) 1128) 1129end ) 1130 1131it ( "respects 'scrollback'" , function () 1132exec_lua ( function () 1133local buf =vim . api . nvim_create_buf ( false , true ) 1134vim . api . nvim_set_option_value ( 'buftype' , 'prompt' , { buf =buf } ) 1135vim . api . nvim_set_option_value ( 'scrollback' , 3 , { buf =buf } ) 1136vim . fn . prompt_setcallback ( buf , function ( _ ) 1137vim . fn . prompt_setprompt ( buf , '> ' ) 1138end ) 1139vim . fn . prompt_setprompt ( buf , '> ' ) 1140vim . api . nvim_set_current_buf ( buf ) 1141vim . cmd ( 'startinsert' ) 1142end ) 1143local buf =fn ( 'bufnr' ) 1144 1145-- Submit 5 times, each creates a new prompt line as history 1146feed ( 'a<cr>' ) 1147feed ( 'b<cr>' ) 1148feed ( 'c<cr>' ) 1149feed ( 'd<cr>' ) 1150feed ( 'e<cr>' ) 1151 1152-- Should only keep 3 lines above the prompt (scrollback limit = 3) 1153local lines =api . nvim_buf_get_lines ( buf , 0 , - 1 , false ) 1154eq ( { '> c' , '> d' , '> e' , '> ' } , lines ) 1155end ) 1156end )