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 , after_each , setup , teardown , finally = 6t . describe , t . it , t . before_each , t . after_each , t . setup , t . teardown , t . finally 7local clear =n . clear 8local eq =t . eq 9local ok =t . ok 10local describe_lua_and_rpc =n . describe_lua_and_rpc ( describe ) 11local api =n . api 12local fn =n . fn 13local request =n . request 14local exec , exec_lua =n . exec , n . exec_lua 15local insert =n . insert 16local NIL =vim . NIL 17local command =n . command 18local feed =n . feed 19local pcall_err =t . pcall_err 20local assert_alive =n . assert_alive 21 22describe ( 'api/buf' , function () 23before_each ( clear ) 24 25-- access deprecated functions 26local function curbuf_depr ( method , ... ) 27return request ( 'buffer_' .. method , 0 , ... ) 28end 29 30describe ( 'nvim_buf_set_lines, nvim_buf_line_count' , function () 31it ( 'deprecated forms' , function () 32eq ( 1 , curbuf_depr ( 'line_count' )) 33curbuf_depr ( 'insert' , - 1 , { 'line' } ) 34eq ( 2 , curbuf_depr ( 'line_count' )) 35curbuf_depr ( 'insert' , - 1 , { 'line' } ) 36eq ( 3 , curbuf_depr ( 'line_count' )) 37curbuf_depr ( 'del_line' , - 1 ) 38eq ( 2 , curbuf_depr ( 'line_count' )) 39curbuf_depr ( 'del_line' , - 1 ) 40curbuf_depr ( 'del_line' , - 1 ) 41-- There's always at least one line 42eq ( 1 , curbuf_depr ( 'line_count' )) 43end ) 44 45it ( "doesn't crash just after set undolevels=1 #24894" , function () 46local buf =api . nvim_create_buf ( false , true ) 47api . nvim_set_option_value ( 'undolevels' , - 1 , { buf =buf } ) 48api . nvim_buf_set_lines ( buf , 0 , 1 , false , {} ) 49 50assert_alive () 51end ) 52 53it ( 'cursor position is maintained after lines are inserted #9961' , function () 54-- replace the buffer contents with these three lines. 55api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'line1' , 'line2' , 'line3' , 'line4' } ) 56-- Set the current cursor to {3, 2}. 57api . nvim_win_set_cursor ( 0 , { 3 , 2 } ) 58 59-- add 2 lines and delete 1 line above the current cursor position. 60api . nvim_buf_set_lines ( 0 , 1 , 2 , true , { 'line5' , 'line6' } ) 61-- check the current set of lines in the buffer. 62eq ( { 'line1' , 'line5' , 'line6' , 'line3' , 'line4' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 63-- cursor should be moved below by 1 line. 64eq ( { 4 , 2 } , api . nvim_win_get_cursor ( 0 )) 65 66-- add a line after the current cursor position. 67api . nvim_buf_set_lines ( 0 , 5 , 5 , true , { 'line7' } ) 68-- check the current set of lines in the buffer. 69eq ( 70{ 'line1' , 'line5' , 'line6' , 'line3' , 'line4' , 'line7' } , 71api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 72) 73-- cursor position is unchanged. 74eq ( { 4 , 2 } , api . nvim_win_get_cursor ( 0 )) 75 76-- overwrite current cursor line. 77api . nvim_buf_set_lines ( 0 , 3 , 5 , true , { 'line8' , 'line9' } ) 78-- check the current set of lines in the buffer. 79eq ( 80{ 'line1' , 'line5' , 'line6' , 'line8' , 'line9' , 'line7' } , 81api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 82) 83-- cursor position is unchanged. 84eq ( { 4 , 2 } , api . nvim_win_get_cursor ( 0 )) 85 86-- delete current cursor line. 87api . nvim_buf_set_lines ( 0 , 3 , 5 , true , {} ) 88-- check the current set of lines in the buffer. 89eq ( { 'line1' , 'line5' , 'line6' , 'line7' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 90-- cursor position is unchanged. 91eq ( { 4 , 2 } , api . nvim_win_get_cursor ( 0 )) 92end ) 93 94it ( 'cursor position is maintained in non-current window' , function () 95api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'line1' , 'line2' , 'line3' , 'line4' } ) 96api . nvim_win_set_cursor ( 0 , { 3 , 2 } ) 97local win =api . nvim_get_current_win () 98local buf =api . nvim_get_current_buf () 99 100command ( 'new' ) 101 102api . nvim_buf_set_lines ( buf , 1 , 2 , true , { 'line5' , 'line6' } ) 103eq ( { 'line1' , 'line5' , 'line6' , 'line3' , 'line4' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , true )) 104eq ( { 4 , 2 } , api . nvim_win_get_cursor ( win )) 105end ) 106 107it ( 'cursor position is maintained in TWO non-current windows' , function () 108api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'line1' , 'line2' , 'line3' , 'line4' } ) 109api . nvim_win_set_cursor ( 0 , { 3 , 2 } ) 110local win =api . nvim_get_current_win () 111local buf =api . nvim_get_current_buf () 112 113command ( 'split' ) 114api . nvim_win_set_cursor ( 0 , { 4 , 2 } ) 115local win2 =api . nvim_get_current_win () 116 117-- set current window to third one with another buffer 118command ( 'new' ) 119 120api . nvim_buf_set_lines ( buf , 1 , 2 , true , { 'line5' , 'line6' } ) 121eq ( { 'line1' , 'line5' , 'line6' , 'line3' , 'line4' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , true )) 122eq ( { 4 , 2 } , api . nvim_win_get_cursor ( win )) 123eq ( { 5 , 2 } , api . nvim_win_get_cursor ( win2 )) 124end ) 125 126it ( 'cursor position is maintained consistently with viewport' , function () 127local screen =Screen . new ( 20 , 12 ) 128 129local lines ={ 'line1' , 'line2' , 'line3' , 'line4' , 'line5' , 'line6' } 130local buf =api . nvim_get_current_buf () 131 132api . nvim_buf_set_lines ( buf , 0 , - 1 , true , lines ) 133 134command ( '6' ) 135command ( 'new' ) 136screen : expect { 137grid =[[ 138^ | 139{1:~ }|*4 140{3:[No Name] }| 141line5 | 142line6 | 143{1:~ }|*2 144{2:[No Name] [+] }| 145| 146]] , 147} 148 149lines [ 5 ] ='boogalo 5' 150api . nvim_buf_set_lines ( buf , 0 , - 1 , true , lines ) 151screen : expect { 152grid =[[ 153^ | 154{1:~ }|*4 155{3:[No Name] }| 156boogalo 5 | 157line6 | 158{1:~ }|*2 159{2:[No Name] [+] }| 160| 161]] , 162} 163 164command ( 'wincmd w' ) 165screen : expect { 166grid =[[ 167| 168{1:~ }|*4 169{2:[No Name] }| 170boogalo 5 | 171^line6 | 172{1:~ }|*2 173{3:[No Name] [+] }| 174| 175]] , 176} 177end ) 178 179it ( 'line_count has defined behaviour for unloaded buffers' , function () 180-- we'll need to know our bufnr for when it gets unloaded 181local bufnr =api . nvim_get_current_buf () 182-- replace the buffer contents with these three lines 183api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'line1' , 'line2' , 'line3' , 'line4' } ) 184-- check the line count is correct 185eq ( 4 , api . nvim_buf_line_count ( bufnr )) 186-- force unload the buffer (this will discard changes) 187command ( 'new' ) 188command ( 'bunload! ' .. bufnr ) 189-- line count for an unloaded buffer should always be 0 190eq ( 0 , api . nvim_buf_line_count ( bufnr )) 191end ) 192 193it ( 'get_lines and get_text has defined behaviour for unloaded buffers' , function () 194-- we'll need to know our bufnr for when it gets unloaded 195local bufnr =api . nvim_get_current_buf () 196-- replace the buffer contents with these three lines 197api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'line1' , 'line2' , 'line3' , 'line4' } ) 198-- confirm that getting lines works 199eq ( { 'line2' , 'line3' } , api . nvim_buf_get_lines ( bufnr , 1 , 3 , true )) 200-- force unload the buffer (this will discard changes) 201command ( 'new' ) 202command ( 'bunload! ' .. bufnr ) 203-- attempting to get lines now always gives empty list 204eq ( {} , api . nvim_buf_get_lines ( bufnr , 1 , 3 , true )) 205-- it's impossible to get out-of-bounds errors for an unloaded buffer 206eq ( {} , api . nvim_buf_get_lines ( bufnr , 8888 , 9999 , true )) 207eq ( {} , api . nvim_buf_get_text ( bufnr , 8888 , 1000 , 9999 , 2000 , {} )) 208-- The Lua path (vim.api) must also return an empty table, not nil. #39833 #39851 209eq ( 210{ 'table' , {} } , 211exec_lua ( function () 212local r =vim . api . nvim_buf_get_lines ( bufnr , 0 , - 1 , false ) 213return { type ( r ), r } 214end ) 215) 216eq ( 217{ 'table' , {} } , 218exec_lua ( function () 219local r =vim . api . nvim_buf_get_text ( bufnr , 0 , 0 , - 1 , 0 , {} ) 220return { type ( r ), r } 221end ) 222) 223end ) 224 225describe ( 'handles topline' , function () 226local screen 227before_each ( function () 228screen =Screen . new ( 20 , 12 ) 229api . nvim_buf_set_lines ( 2300 , 2310 , 232- 1 , 233true , 234{ 'aaa' , 'bbb' , 'ccc' , 'ddd' , 'www' , 'xxx' , 'yyy' , 'zzz' } 235) 236api . nvim_set_option_value ( 'modified' , false , {} ) 237end ) 238 239it ( 'of current window' , function () 240local win =api . nvim_get_current_win () 241local buf =api . nvim_get_current_buf () 242 243command ( 'new | wincmd w' ) 244api . nvim_win_set_cursor ( win , { 8 , 0 } ) 245 246screen : expect { 247grid =[[ 248| 249{1:~ }|*4 250{2:[No Name] }| 251www | 252xxx | 253yyy | 254^zzz | 255{3:[No Name] }| 256| 257]] , 258} 259 260api . nvim_buf_set_lines ( buf , 0 , 2 , true , { 'aaabbb' } ) 261screen : expect { 262grid =[[ 263| 264{1:~ }|*4 265{2:[No Name] }| 266www | 267xxx | 268yyy | 269^zzz | 270{3:[No Name] [+] }| 271| 272]] , 273} 274 275-- replacing topline keeps it the topline 276api . nvim_buf_set_lines ( buf , 3 , 4 , true , { 'wwweeee' } ) 277screen : expect { 278grid =[[ 279| 280{1:~ }|*4 281{2:[No Name] }| 282wwweeee | 283xxx | 284yyy | 285^zzz | 286{3:[No Name] [+] }| 287| 288]] , 289} 290 291-- inserting just before topline does not scroll up if cursor would be moved 292api . nvim_buf_set_lines ( buf , 3 , 3 , true , { 'mmm' } ) 293screen : expect { 294grid =[[ 295| 296{1:~ }|*4 297{2:[No Name] }| 298wwweeee | 299xxx | 300yyy | 301^zzz | 302{3:[No Name] [+] }| 303| 304]] , 305unchanged =true , 306} 307 308api . nvim_win_set_cursor ( 0 , { 7 , 0 } ) 309screen : expect { 310grid =[[ 311| 312{1:~ }|*4 313{2:[No Name] }| 314wwweeee | 315xxx | 316^yyy | 317zzz | 318{3:[No Name] [+] }| 319| 320]] , 321} 322 323api . nvim_buf_set_lines ( buf , 4 , 4 , true , { 'mmmeeeee' } ) 324screen : expect { 325grid =[[ 326| 327{1:~ }|*4 328{2:[No Name] }| 329mmmeeeee | 330wwweeee | 331xxx | 332^yyy | 333{3:[No Name] [+] }| 334| 335]] , 336} 337end ) 338 339it ( 'of non-current window' , function () 340local win =api . nvim_get_current_win () 341local buf =api . nvim_get_current_buf () 342 343command ( 'new' ) 344api . nvim_win_set_cursor ( win , { 8 , 0 } ) 345 346screen : expect { 347grid =[[ 348^ | 349{1:~ }|*4 350{3:[No Name] }| 351www | 352xxx | 353yyy | 354zzz | 355{2:[No Name] }| 356| 357]] , 358} 359 360api . nvim_buf_set_lines ( buf , 0 , 2 , true , { 'aaabbb' } ) 361screen : expect { 362grid =[[ 363^ | 364{1:~ }|*4 365{3:[No Name] }| 366www | 367xxx | 368yyy | 369zzz | 370{2:[No Name] [+] }| 371| 372]] , 373} 374 375-- replacing topline keeps it the topline 376api . nvim_buf_set_lines ( buf , 3 , 4 , true , { 'wwweeee' } ) 377screen : expect { 378grid =[[ 379^ | 380{1:~ }|*4 381{3:[No Name] }| 382wwweeee | 383xxx | 384yyy | 385zzz | 386{2:[No Name] [+] }| 387| 388]] , 389} 390 391api . nvim_buf_set_lines ( buf , 3 , 3 , true , { 'mmm' } ) 392screen : expect { 393grid =[[ 394^ | 395{1:~ }|*4 396{3:[No Name] }| 397wwweeee | 398xxx | 399yyy | 400zzz | 401{2:[No Name] [+] }| 402| 403]] , 404unchanged =true , 405} 406end ) 407 408it ( 'of split windows with same buffer' , function () 409local win =api . nvim_get_current_win () 410local buf =api . nvim_get_current_buf () 411 412command ( 'split' ) 413api . nvim_win_set_cursor ( win , { 8 , 0 } ) 414api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 415 416screen : expect { 417grid =[[ 418^aaa | 419bbb | 420ccc | 421ddd | 422www | 423{3:[No Name] }| 424www | 425xxx | 426yyy | 427zzz | 428{2:[No Name] }| 429| 430]] , 431} 432api . nvim_buf_set_lines ( buf , 0 , 2 , true , { 'aaabbb' } ) 433 434screen : expect { 435grid =[[ 436^aaabbb | 437ccc | 438ddd | 439www | 440xxx | 441{3:[No Name] [+] }| 442www | 443xxx | 444yyy | 445zzz | 446{2:[No Name] [+] }| 447| 448]] , 449} 450 451-- replacing topline keeps it the topline 452api . nvim_buf_set_lines ( buf , 3 , 4 , true , { 'wwweeee' } ) 453screen : expect { 454grid =[[ 455^aaabbb | 456ccc | 457ddd | 458wwweeee | 459xxx | 460{3:[No Name] [+] }| 461wwweeee | 462xxx | 463yyy | 464zzz | 465{2:[No Name] [+] }| 466| 467]] , 468} 469 470api . nvim_buf_set_lines ( buf , 3 , 3 , true , { 'mmm' } ) 471screen : expect { 472grid =[[ 473^aaabbb | 474ccc | 475ddd | 476mmm | 477wwweeee | 478{3:[No Name] [+] }| 479wwweeee | 480xxx | 481yyy | 482zzz | 483{2:[No Name] [+] }| 484| 485]] , 486} 487end ) 488 489describe ( 'of current window when' , function () 490before_each ( function () 491command ( 'new | wincmd w | setlocal modified' ) 492feed ( 'Gk' ) 493screen : expect ( [[ 494| 495{1:~ }|*4 496{2:[No Name] }| 497www | 498xxx | 499^yyy | 500zzz | 501{3:[No Name] [+] }| 502| 503]] ) 504end ) 505 506it ( 'deleting 3 lines around topline' , function () 507api . nvim_buf_set_lines ( 0 , 3 , 6 , true , {} ) 508screen : expect ( [[ 509| 510{1:~ }|*4 511{2:[No Name] }| 512ccc | 513^yyy | 514zzz | 515{1:~ }| 516{3:[No Name] [+] }| 517| 518]] ) 519eq ( 5 , api . nvim_buf_line_count ( 0 )) 520end ) 521 522it ( 'deleting 3 lines around the line just before topline' , function () 523api . nvim_buf_set_lines ( 0 , 2 , 5 , true , {} ) 524screen : expect ( [[ 525| 526{1:~ }|*4 527{2:[No Name] }| 528bbb | 529xxx | 530^yyy | 531zzz | 532{3:[No Name] [+] }| 533| 534]] ) 535eq ( 5 , api . nvim_buf_line_count ( 0 )) 536end ) 537 538for count =1 , 4 do 539it (( 'deleting %d lines just before topline' ): format ( count ), function () 540api . nvim_buf_set_lines ( 0 , 4 - count , 4 , true , {} ) 541screen : expect_unchanged () 542eq ( 8 - count , api . nvim_buf_line_count ( 0 )) 543end ) 544 545it (( 'replacing %d lines just before topline with 2 lines' ): format ( count ), function () 546api . nvim_buf_set_lines ( 0 , 4 - count , 4 , true , { 'eee' , 'fff' } ) 547screen : expect_unchanged () 548eq ( 8 - count + 2 , api . nvim_buf_line_count ( 0 )) 549end ) 550end 551 552for count =1 , 3 do 553it (( 'deleting %d lines far before topline' ): format ( count ), function () 554api . nvim_buf_set_lines ( 0 , 0 , count , true , {} ) 555screen : expect_unchanged () 556eq ( 8 - count , api . nvim_buf_line_count ( 0 )) 557end ) 558 559it (( 'replacing %d lines far before topline with 2 lines' ): format ( count ), function () 560api . nvim_buf_set_lines ( 0 , 0 , count , true , { 'eee' , 'fff' } ) 561screen : expect_unchanged () 562eq ( 8 - count + 2 , api . nvim_buf_line_count ( 0 )) 563end ) 564end 565end ) 566end ) 567 568it ( 'handles clearing out non-current buffer #24911' , function () 569local buf =api . nvim_get_current_buf () 570api . nvim_buf_set_lines ( buf , 0 , - 1 , true , { 'aaa' , 'bbb' , 'ccc' } ) 571command ( 'new' ) 572 573api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'xxx' , 'yyy' , 'zzz' } ) 574 575api . nvim_buf_set_lines ( buf , 0 , - 1 , true , {} ) 576eq ( { 'xxx' , 'yyy' , 'zzz' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 577eq ( { '' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , true )) 578end ) 579end ) 580 581describe ( 'deprecated: {get,set,del}_line' , function () 582it ( 'works' , function () 583eq ( '' , curbuf_depr ( 'get_line' , 0 )) 584curbuf_depr ( 'set_line' , 0 , 'line1' ) 585eq ( 'line1' , curbuf_depr ( 'get_line' , 0 )) 586curbuf_depr ( 'set_line' , 0 , 'line2' ) 587eq ( 'line2' , curbuf_depr ( 'get_line' , 0 )) 588curbuf_depr ( 'del_line' , 0 ) 589eq ( '' , curbuf_depr ( 'get_line' , 0 )) 590end ) 591 592it ( 'get_line: out-of-bounds is an error' , function () 593curbuf_depr ( 'set_line' , 0 , 'line1.a' ) 594eq ( 1 , curbuf_depr ( 'line_count' )) -- sanity 595eq ( false , pcall ( curbuf_depr , 'get_line' , 1 )) 596eq ( false , pcall ( curbuf_depr , 'get_line' , - 2 )) 597end ) 598 599it ( 'set_line, del_line: out-of-bounds is an error' , function () 600curbuf_depr ( 'set_line' , 0 , 'line1.a' ) 601eq ( false , pcall ( curbuf_depr , 'set_line' , 1 , 'line1.b' )) 602eq ( false , pcall ( curbuf_depr , 'set_line' , - 2 , 'line1.b' )) 603eq ( false , pcall ( curbuf_depr , 'del_line' , 2 )) 604eq ( false , pcall ( curbuf_depr , 'del_line' , - 3 )) 605end ) 606 607it ( 'can handle NULs' , function () 608curbuf_depr ( 'set_line' , 0 , 'ab\0cd' ) 609eq ( 'ab\0cd' , curbuf_depr ( 'get_line' , 0 )) 610end ) 611end ) 612 613describe ( 'deprecated: {get,set}_line_slice' , function () 614it ( 'get_line_slice: out-of-bounds returns empty array' , function () 615curbuf_depr ( 'set_line_slice' , 0 , 0 , true , true , { 'a' , 'b' , 'c' } ) 616eq ( { 'a' , 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , 0 , 2 , true , true )) --sanity 617 618eq ( {} , curbuf_depr ( 'get_line_slice' , 2 , 3 , false , true )) 619eq ( {} , curbuf_depr ( 'get_line_slice' , 3 , 9 , true , true )) 620eq ( {} , curbuf_depr ( 'get_line_slice' , 3 , - 1 , true , true )) 621eq ( {} , curbuf_depr ( 'get_line_slice' , - 3 , - 4 , false , true )) 622eq ( {} , curbuf_depr ( 'get_line_slice' , - 4 , - 5 , true , true )) 623end ) 624 625it ( 'set_line_slice: out-of-bounds extends past end' , function () 626curbuf_depr ( 'set_line_slice' , 0 , 0 , true , true , { 'a' , 'b' , 'c' } ) 627eq ( { 'a' , 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , 0 , 2 , true , true )) --sanity 628 629eq ( { 'c' } , curbuf_depr ( 'get_line_slice' , - 1 , 4 , true , true )) 630eq ( { 'a' , 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , 0 , 5 , true , true )) 631curbuf_depr ( 'set_line_slice' , 4 , 5 , true , true , { 'd' } ) 632eq ( { 'a' , 'b' , 'c' , 'd' } , curbuf_depr ( 'get_line_slice' , 0 , 5 , true , true )) 633curbuf_depr ( 'set_line_slice' , - 4 , - 5 , true , true , { 'e' } ) 634eq ( { 'e' , 'a' , 'b' , 'c' , 'd' } , curbuf_depr ( 'get_line_slice' , 0 , 5 , true , true )) 635end ) 636 637it ( 'works' , function () 638eq ( { '' } , curbuf_depr ( 'get_line_slice' , 0 , - 1 , true , true )) 639-- Replace buffer 640curbuf_depr ( 'set_line_slice' , 0 , - 1 , true , true , { 'a' , 'b' , 'c' } ) 641eq ( { 'a' , 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , 0 , - 1 , true , true )) 642eq ( { 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , 1 , - 1 , true , true )) 643eq ( { 'b' } , curbuf_depr ( 'get_line_slice' , 1 , 2 , true , false )) 644eq ( {} , curbuf_depr ( 'get_line_slice' , 1 , 1 , true , false )) 645eq ( { 'a' , 'b' } , curbuf_depr ( 'get_line_slice' , 0 , - 1 , true , false )) 646eq ( { 'b' } , curbuf_depr ( 'get_line_slice' , 1 , - 1 , true , false )) 647eq ( { 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , - 2 , - 1 , true , true )) 648curbuf_depr ( 'set_line_slice' , 1 , 2 , true , false , { 'a' , 'b' , 'c' } ) 649eq ( { 'a' , 'a' , 'b' , 'c' , 'c' } , curbuf_depr ( 'get_line_slice' , 0 , - 1 , true , true )) 650curbuf_depr ( 'set_line_slice' , - 1 , - 1 , true , true , { 'a' , 'b' , 'c' } ) 651eq ( { 'a' , 'a' , 'b' , 'c' , 'a' , 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , 0 , - 1 , true , true )) 652curbuf_depr ( 'set_line_slice' , 0 , - 3 , true , false , {} ) 653eq ( { 'a' , 'b' , 'c' } , curbuf_depr ( 'get_line_slice' , 0 , - 1 , true , true )) 654curbuf_depr ( 'set_line_slice' , 0 , - 1 , true , true , {} ) 655eq ( { '' } , curbuf_depr ( 'get_line_slice' , 0 , - 1 , true , true )) 656end ) 657end ) 658 659describe_lua_and_rpc ( 'nvim_buf_get_lines, nvim_buf_set_lines' , function ( lua_or_rpc ) 660local function get_lines ( ... ) 661return lua_or_rpc . nvim_buf_get_lines ( 0 , ... ) 662end 663 664local function set_lines ( ... ) 665return lua_or_rpc . nvim_buf_set_lines ( 0 , ... ) 666end 667 668local function line_count () 669return lua_or_rpc . nvim_buf_line_count ( 0 ) 670end 671 672it ( 'fails correctly when input is not valid' , function () 673eq ( 1 , lua_or_rpc . nvim_buf_get_number ( 0 )) 674eq ( 675[['replacement string' item contains newlines]] , 676pcall_err ( lua_or_rpc . nvim_buf_set_lines , 1 , 1 , 2 , false , { 'b\na' } ) 677) 678end ) 679 680it ( "fails if 'nomodifiable'" , function () 681command ( 'set nomodifiable' ) 682eq ( 683[[Buffer is not 'modifiable']] , 684pcall_err ( lua_or_rpc . nvim_buf_set_lines , 1 , 1 , 2 , false , { 'a' , 'b' } ) 685) 686end ) 687 688it ( 'has correct line_count when inserting and deleting' , function () 689eq ( 1 , line_count ()) 690set_lines ( - 1 , - 1 , true , { 'line' } ) 691eq ( 2 , line_count ()) 692set_lines ( - 1 , - 1 , true , { 'line' } ) 693eq ( 3 , line_count ()) 694set_lines ( - 2 , - 1 , true , {} ) 695eq ( 2 , line_count ()) 696set_lines ( - 2 , - 1 , true , {} ) 697set_lines ( - 2 , - 1 , true , {} ) 698-- There's always at least one line 699eq ( 1 , line_count ()) 700end ) 701 702it ( 'can get, set and delete a single line' , function () 703eq ( { '' } , get_lines ( 0 , 1 , true )) 704set_lines ( 0 , 1 , true , { 'line1' } ) 705eq ( { 'line1' } , get_lines ( 0 , 1 , true )) 706set_lines ( 0 , 1 , true , { 'line2' } ) 707eq ( { 'line2' } , get_lines ( 0 , 1 , true )) 708set_lines ( 0 , 1 , true , {} ) 709eq ( { '' } , get_lines ( 0 , 1 , true )) 710end ) 711 712it ( 'can get a single line with strict indexing' , function () 713set_lines ( 0 , 1 , true , { 'line1.a' } ) 714eq ( 1 , line_count ()) -- sanity 715eq ( 'Index out of bounds' , pcall_err ( get_lines , 1 , 2 , true )) 716eq ( 'Index out of bounds' , pcall_err ( get_lines , - 3 , - 2 , true )) 717end ) 718 719it ( 'can get a single line with non-strict indexing' , function () 720set_lines ( 0 , 1 , true , { 'line1.a' } ) 721eq ( 1 , line_count ()) -- sanity 722eq ( {} , get_lines ( 1 , 2 , false )) 723eq ( {} , get_lines ( - 3 , - 2 , false )) 724end ) 725 726it ( 'can set and delete a single line with strict indexing' , function () 727set_lines ( 0 , 1 , true , { 'line1.a' } ) 728eq ( 'Index out of bounds' , pcall_err ( set_lines , 1 , 2 , true , { 'line1.b' } )) 729eq ( 'Index out of bounds' , pcall_err ( set_lines , - 3 , - 2 , true , { 'line1.c' } )) 730eq ( { 'line1.a' } , get_lines ( 0 , - 1 , true )) 731eq ( 'Index out of bounds' , pcall_err ( set_lines , 1 , 2 , true , {} )) 732eq ( 'Index out of bounds' , pcall_err ( set_lines , - 3 , - 2 , true , {} )) 733eq ( { 'line1.a' } , get_lines ( 0 , - 1 , true )) 734end ) 735 736it ( 'can set and delete a single line with non-strict indexing' , function () 737set_lines ( 0 , 1 , true , { 'line1.a' } ) 738set_lines ( 1 , 2 , false , { 'line1.b' } ) 739set_lines ( - 4 , - 3 , false , { 'line1.c' } ) 740eq ( { 'line1.c' , 'line1.a' , 'line1.b' } , get_lines ( 0 , - 1 , true )) 741set_lines ( 3 , 4 , false , {} ) 742set_lines ( - 5 , - 4 , false , {} ) 743eq ( { 'line1.c' , 'line1.a' , 'line1.b' } , get_lines ( 0 , - 1 , true )) 744end ) 745 746it ( 'can handle NULs' , function () 747set_lines ( 0 , 1 , true , { 'ab\0cd' } ) 748eq ( { 'ab\0cd' } , get_lines ( 0 , - 1 , true )) 749end ) 750 751it ( 'works with multiple lines' , function () 752eq ( { '' } , get_lines ( 0 , - 1 , true )) 753-- Replace buffer 754for _ , mode in pairs ( { false , true } ) do 755set_lines ( 0 , - 1 , mode , { 'a' , 'b' , 'c' } ) 756eq ( { 'a' , 'b' , 'c' } , get_lines ( 0 , - 1 , mode )) 757eq ( { 'b' , 'c' } , get_lines ( 1 , - 1 , mode )) 758eq ( { 'b' } , get_lines ( 1 , 2 , mode )) 759eq ( {} , get_lines ( 1 , 1 , mode )) 760eq ( { 'a' , 'b' } , get_lines ( 0 , - 2 , mode )) 761eq ( { 'b' } , get_lines ( 1 , - 2 , mode )) 762eq ( { 'b' , 'c' } , get_lines ( - 3 , - 1 , mode )) 763set_lines ( 1 , 2 , mode , { 'a' , 'b' , 'c' } ) 764eq ( { 'a' , 'a' , 'b' , 'c' , 'c' } , get_lines ( 0 , - 1 , mode )) 765set_lines ( - 2 , - 1 , mode , { 'a' , 'b' , 'c' } ) 766eq ( { 'a' , 'a' , 'b' , 'c' , 'a' , 'b' , 'c' } , get_lines ( 0 , - 1 , mode )) 767set_lines ( 0 , - 4 , mode , {} ) 768eq ( { 'a' , 'b' , 'c' } , get_lines ( 0 , - 1 , mode )) 769set_lines ( 0 , - 1 , mode , {} ) 770eq ( { '' } , get_lines ( 0 , - 1 , mode )) 771end 772end ) 773 774it ( 'can get line ranges with non-strict indexing' , function () 775set_lines ( 0 , - 1 , true , { 'a' , 'b' , 'c' } ) 776eq ( { 'a' , 'b' , 'c' } , get_lines ( 0 , - 1 , true )) --sanity 777 778eq ( {} , get_lines ( 3 , 4 , false )) 779eq ( {} , get_lines ( 3 , 10 , false )) 780eq ( {} , get_lines ( - 5 , - 5 , false )) 781eq ( {} , get_lines ( 3 , - 1 , false )) 782eq ( {} , get_lines ( - 3 , - 4 , false )) 783end ) 784 785it ( 'can get line ranges with strict indexing' , function () 786set_lines ( 0 , - 1 , true , { 'a' , 'b' , 'c' } ) 787eq ( { 'a' , 'b' , 'c' } , get_lines ( 0 , - 1 , true )) --sanity 788 789eq ( 'Index out of bounds' , pcall_err ( get_lines , 3 , 4 , true )) 790eq ( 'Index out of bounds' , pcall_err ( get_lines , 3 , 10 , true )) 791eq ( 'Index out of bounds' , pcall_err ( get_lines , - 5 , - 5 , true )) 792-- empty or inverted ranges are not errors 793eq ( {} , get_lines ( 3 , - 1 , true )) 794eq ( {} , get_lines ( - 3 , - 4 , true )) 795end ) 796 797it ( 'set_lines: out-of-bounds can extend past end' , function () 798set_lines ( 0 , - 1 , true , { 'a' , 'b' , 'c' } ) 799eq ( { 'a' , 'b' , 'c' } , get_lines ( 0 , - 1 , true )) --sanity 800 801eq ( { 'c' } , get_lines ( - 2 , 5 , false )) 802eq ( { 'a' , 'b' , 'c' } , get_lines ( 0 , 6 , false )) 803eq ( 'Index out of bounds' , pcall_err ( set_lines , 4 , 6 , true , { 'd' } )) 804set_lines ( 4 , 6 , false , { 'd' } ) 805eq ( { 'a' , 'b' , 'c' , 'd' } , get_lines ( 0 , - 1 , true )) 806eq ( 'Index out of bounds' , pcall_err ( set_lines , - 6 , - 6 , true , { 'e' } )) 807set_lines ( - 6 , - 6 , false , { 'e' } ) 808eq ( { 'e' , 'a' , 'b' , 'c' , 'd' } , get_lines ( 0 , - 1 , true )) 809end ) 810 811it ( 'set_lines on alternate buffer does not access invalid line (E315)' , function () 812command ( 'set hidden' ) 813insert ( 'Initial file' ) 814command ( 'enew' ) 815insert ( [[ 816More 817Lines 818Than 819In 820The 821Other 822Buffer]] ) 823command ( '$' ) 824eq ( true , pcall ( api . nvim_buf_set_lines , 0 , 0 , 1 , false , { 'test' } )) 825end ) 826 827it ( "set_lines of invisible buffer doesn't move cursor in current window" , function () 828local screen =Screen . new ( 20 , 5 ) 829 830insert ( [[ 831Who would win? 832A real window 833with proper text]] ) 834local buf =lua_or_rpc . nvim_create_buf ( false , true ) 835screen : expect ( [[ 836Who would win? | 837A real window | 838with proper tex^t | 839{1:~ }| 840| 841]] ) 842 843lua_or_rpc . nvim_buf_set_lines ( buf , 0 , - 1 , true , { 'or some' , 'scratchy text' } ) 844feed ( 'i' ) -- provoke redraw 845screen : expect ( [[ 846Who would win? | 847A real window | 848with proper tex^t | 849{1:~ }| 850{5:-- INSERT --} | 851]] ) 852end ) 853 854it ( 'set_lines on hidden buffer preserves "previous window" #9741' , function () 855insert ( [[ 856visible buffer line 1 857line 2 858]] ) 859local hiddenbuf =lua_or_rpc . nvim_create_buf ( false , true ) 860command ( 'vsplit' ) 861command ( 'vsplit' ) 862feed ( '<c-w>l<c-w>l<c-w>l' ) 863eq ( 3 , fn . winnr ()) 864feed ( '<c-w>h' ) 865eq ( 2 , fn . winnr ()) 866lua_or_rpc . nvim_buf_set_lines ( hiddenbuf , 0 , - 1 , true , { 'hidden buffer line 1' , 'line 2' } ) 867feed ( '<c-w>p' ) 868eq ( 3 , fn . winnr ()) 869end ) 870 871it ( 'set_lines on unloaded buffer #8659 #22670' , function () 872local bufnr =api . nvim_get_current_buf () 873lua_or_rpc . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { 'a' , 'b' , 'c' } ) 874lua_or_rpc . nvim_buf_set_name ( bufnr , 'set_lines' ) 875finally ( function () 876os . remove ( 'set_lines' ) 877end ) 878command ( 'write!' ) 879command ( 'new' ) 880command ( 'bunload! ' .. bufnr ) 881local new_bufnr =fn . bufnr ( 'set_lines' , true ) 882lua_or_rpc . nvim_buf_set_lines ( new_bufnr , 0 , - 1 , false , {} ) 883eq ( { '' } , lua_or_rpc . nvim_buf_get_lines ( new_bufnr , 0 , - 1 , false )) 884eq ( true , api . nvim_buf_is_loaded ( new_bufnr )) 885end ) 886end ) 887 888describe ( 'nvim_buf_set_text' , function () 889local function get_lines ( ... ) 890return api . nvim_buf_get_lines ( 0 , ... ) 891end 892 893local function set_text ( ... ) 894return api . nvim_buf_set_text ( 0 , ... ) 895end 896 897it ( 'works' , function () 898insert ( [[ 899hello foo! 900text 901]] ) 902 903eq ( { 'hello foo!' } , get_lines ( 0 , 1 , true )) 904 905-- can replace a single word 906set_text ( 0 , 6 , 0 , 9 , { 'world' } ) 907eq ( { 'hello world!' , 'text' } , get_lines ( 0 , 2 , true )) 908 909-- can insert text 910set_text ( 0 , 0 , 0 , 0 , { 'well ' } ) 911eq ( { 'well hello world!' , 'text' } , get_lines ( 0 , 2 , true )) 912 913-- can delete text 914set_text ( 0 , 0 , 0 , 5 , { '' } ) 915eq ( { 'hello world!' , 'text' } , get_lines ( 0 , 2 , true )) 916 917-- can replace with multiple lines 918set_text ( 0 , 6 , 0 , 11 , { 'foo' , 'wo' , 'more' } ) 919eq ( { 'hello foo' , 'wo' , 'more!' , 'text' } , get_lines ( 0 , 4 , true )) 920 921-- will join multiple lines if needed 922set_text ( 0 , 6 , 3 , 4 , { 'bar' } ) 923eq ( { 'hello bar' } , get_lines ( 0 , 1 , true )) 924 925-- can use negative line numbers 926set_text ( - 2 , 0 , - 2 , 5 , { 'goodbye' } ) 927eq ( { 'goodbye bar' , '' } , get_lines ( 0 , - 1 , true )) 928 929set_text ( - 1 , 0 , - 1 , 0 , { 'text' } ) 930eq ( { 'goodbye bar' , 'text' } , get_lines ( 0 , 2 , true )) 931 932-- can append to a line 933set_text ( 1 , 4 , - 1 , 4 , { ' and' , 'more' } ) 934eq ( { 'goodbye bar' , 'text and' , 'more' } , get_lines ( 0 , 3 , true )) 935 936-- can use negative column numbers 937set_text ( 0 , - 5 , 0 , - 1 , { '!' } ) 938eq ( { 'goodbye!' } , get_lines ( 0 , 1 , true )) 939end ) 940 941it ( 'works with undo' , function () 942insert ( [[ 943hello world! 944foo bar 945]] ) 946 947-- setting text 948set_text ( 0 , 0 , 0 , 0 , { 'well ' } ) 949feed ( 'u' ) 950eq ( { 'hello world!' } , get_lines ( 0 , 1 , true )) 951 952-- deleting text 953set_text ( 0 , 0 , 0 , 6 , { '' } ) 954feed ( 'u' ) 955eq ( { 'hello world!' } , get_lines ( 0 , 1 , true )) 956 957-- inserting newlines 958set_text ( 0 , 0 , 0 , 0 , { 'hello' , 'mr ' } ) 959feed ( 'u' ) 960eq ( { 'hello world!' } , get_lines ( 0 , 1 , true )) 961 962-- deleting newlines 963set_text ( 0 , 0 , 1 , 4 , { 'hello' } ) 964feed ( 'u' ) 965eq ( { 'hello world!' } , get_lines ( 0 , 1 , true )) 966end ) 967 968it ( 'updates the cursor position' , function () 969insert ( [[ 970hello world! 971]] ) 972 973-- position the cursor on `!` 974api . nvim_win_set_cursor ( 0 , { 1 , 11 } ) 975-- replace 'world' with 'foo' 976set_text ( 0 , 6 , 0 , 11 , { 'foo' } ) 977eq ( 'hello foo!' , curbuf_depr ( 'get_line' , 0 )) 978-- cursor should be moved left by two columns (replacement is shorter by 2 chars) 979eq ( { 1 , 9 } , api . nvim_win_get_cursor ( 0 )) 980 981-- changelist entry reflects the edit column #28618 982local changes =fn . getchangelist ()[ 1 ] 983eq ( 6 , changes [ # changes ]. col ) 984end ) 985 986it ( 'updates the cursor position in non-current window' , function () 987insert ( [[ 988hello world!]] ) 989 990-- position the cursor on `!` 991api . nvim_win_set_cursor ( 0 , { 1 , 11 } ) 992 993local win =api . nvim_get_current_win () 994local buf =api . nvim_get_current_buf () 995 996command ( 'new' ) 997 998-- replace 'world' with 'foo' 999api . nvim_buf_set_text ( buf , 0 , 6 , 0 , 11 , { 'foo' } ) 1000eq ( { 'hello foo!' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , true )) 1001-- cursor should be moved left by two columns (replacement is shorter by 2 chars) 1002eq ( { 1 , 9 } , api . nvim_win_get_cursor ( win )) 1003end ) 1004 1005it ( 'updates the cursor position in TWO non-current windows' , function () 1006insert ( [[ 1007hello world!]] ) 1008 1009-- position the cursor on `!` 1010api . nvim_win_set_cursor ( 0 , { 1 , 11 } ) 1011local win =api . nvim_get_current_win () 1012local buf =api . nvim_get_current_buf () 1013 1014command ( 'split' ) 1015local win2 =api . nvim_get_current_win () 1016-- position the cursor on `w` 1017api . nvim_win_set_cursor ( 0 , { 1 , 6 } ) 1018 1019command ( 'new' ) 1020 1021-- replace 'hello' with 'foo' 1022api . nvim_buf_set_text ( buf , 0 , 0 , 0 , 5 , { 'foo' } ) 1023eq ( { 'foo world!' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , true )) 1024 1025-- both cursors should be moved left by two columns (replacement is shorter by 2 chars) 1026eq ( { 1 , 9 } , api . nvim_win_get_cursor ( win )) 1027eq ( { 1 , 4 } , api . nvim_win_get_cursor ( win2 )) 1028end ) 1029 1030it ( 'keep visual select position #29558' , function () 1031insert ( [[1234]] ) 1032api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1033feed ( 'vl' ) 1034exec_lua ( [[ 1035vim.defer_fn(function() vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { '0' }) end, 50) 1036vim.wait(80) 1037]] ) 1038local mode =api . nvim_get_mode (). mode 1039eq ( { '23' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1040eq ( { 'v' , { '01234' } } , { mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } ) 1041feed ( '<ESC>' ) 1042 1043api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '1' , '2' , '3' } ) 1044api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 1045feed ( 'v' ) 1046exec_lua ( [[ 1047vim.defer_fn(function() vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { '0', '' }) end, 50) 1048vim.wait(80) 1049]] ) 1050mode =api . nvim_get_mode (). mode 1051eq ( { '2' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1052eq ( { 'v' , { '0' , '1' , '2' , '3' } } , { mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } ) 1053feed ( '<ESC>' ) 1054 1055api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '1' , '2' } ) 1056api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1057feed ( 'vj' ) 1058exec_lua ( [[ 1059vim.defer_fn(function() vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { '0' }) end, 50) 1060vim.wait(80) 1061]] ) 1062mode =api . nvim_get_mode (). mode 1063eq ( { '1' , '2' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1064eq ( { 'v' , { '01' , '2' } } , { mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } ) 1065feed ( '<ESC>' ) 1066 1067api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '123' } ) 1068api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1069feed ( 'v' ) 1070exec_lua ( [[ 1071vim.defer_fn(function() vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { '', '' }) end, 50) 1072vim.wait(80) 1073]] ) 1074mode =api . nvim_get_mode (). mode 1075eq ( { '2' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1076eq ( { 'v' , { '' , '123' } } , { mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } ) 1077feed ( '<ESC>' ) 1078 1079-- Visual block mode 1080api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '123' , '456' } ) 1081api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1082feed ( '<C-v>jl' ) 1083exec_lua ( [[ 1084vim.defer_fn(function() vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { '0' }) end, 50) 1085vim.wait(80) 1086]] ) 1087mode =api . nvim_get_mode (). mode 1088eq ( { '01' , '45' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1089eq ( 1090{ string . char ( 0x16 ), { '0123' , '456' } } , 1091{ mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } 1092) 1093feed ( '<ESC>' ) 1094-- also test nvim_buf_set_lines inserts line above visual selection 1095api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '1' , '2' , '3' } ) 1096api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 1097feed ( 'v' ) 1098exec_lua ( [[ 1099vim.defer_fn(function() vim.api.nvim_buf_set_lines(0, 0, 0, false, { 'new' }) end, 50) 1100vim.wait(80) 1101]] ) 1102mode =api . nvim_get_mode (). mode 1103eq ( { '2' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1104eq ( { 'v' , { 'new' , '1' , '2' , '3' } } , { mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } ) 1105feed ( '<ESC>' ) 1106 1107api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '1234' } ) 1108api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1109feed ( 'vl' ) 1110exec_lua ( [[ 1111vim.defer_fn(function() 1112vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { '0', 'foo' }) 1113end, 50) 1114vim.wait(80) 1115]] ) 1116mode =api . nvim_get_mode (). mode 1117eq ( { '23' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1118eq ( { 'v' , { '0' , 'foo1234' } } , { mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } ) 1119feed ( '<ESC>' ) 1120 1121api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' } ) 1122api . nvim_win_set_cursor ( 0 , { 8 , 0 } ) 1123feed ( 'v' ) 1124exec_lua ( [[ 1125vim.defer_fn(function() vim.api.nvim_buf_set_lines(0, 0, 5, false, {}) end, 50) 1126vim.wait(80) 1127]] ) 1128mode =api . nvim_get_mode (). mode 1129eq ( { '8' } , fn . getregion ( fn . getpos ( '.' ), fn . getpos ( 'v' ), { type =mode } )) 1130eq ( { 'v' , { '6' , '7' , '8' , '9' , '10' } } , { mode , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) } ) 1131feed ( '<ESC>' ) 1132end ) 1133 1134describe ( 'when text is being added right at cursor position #22526' , function () 1135it ( 'updates the cursor position in NORMAL mode' , function () 1136insert ( [[ 1137abcd]] ) 1138 1139-- position the cursor on 'c' 1140api . nvim_win_set_cursor ( 0 , { 1 , 2 } ) 1141-- add 'xxx' before 'c' 1142set_text ( 0 , 2 , 0 , 2 , { 'xxx' } ) 1143eq ( { 'abxxxcd' } , get_lines ( 0 , - 1 , true )) 1144-- cursor should be on 'c' 1145eq ( { 1 , 5 } , api . nvim_win_get_cursor ( 0 )) 1146end ) 1147 1148it ( 'updates the cursor position only in non-current window when in INSERT mode' , function () 1149insert ( [[ 1150abcd]] ) 1151 1152-- position the cursor on 'c' 1153api . nvim_win_set_cursor ( 0 , { 1 , 2 } ) 1154-- open vertical split 1155feed ( '<c-w>v' ) 1156-- get into INSERT mode to treat cursor 1157-- as being after 'b', not on 'c' 1158feed ( 'i' ) 1159-- add 'xxx' between 'b' and 'c' 1160set_text ( 0 , 2 , 0 , 2 , { 'xxx' } ) 1161eq ( { 'abxxxcd' } , get_lines ( 0 , - 1 , true )) 1162-- in the current window cursor should stay after 'b' 1163eq ( { 1 , 2 } , api . nvim_win_get_cursor ( 0 )) 1164-- quit INSERT mode 1165feed ( '<esc>' ) 1166-- close current window 1167feed ( '<c-w>c' ) 1168-- in another window cursor should be on 'c' 1169eq ( { 1 , 5 } , api . nvim_win_get_cursor ( 0 )) 1170end ) 1171end ) 1172 1173describe ( 'when text is being deleted right at cursor position' , function () 1174it ( 'leaves cursor at the same position in NORMAL mode' , function () 1175insert ( [[ 1176abcd]] ) 1177 1178-- position the cursor on 'b' 1179api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1180-- delete 'b' 1181set_text ( 0 , 1 , 0 , 2 , {} ) 1182eq ( { 'acd' } , get_lines ( 0 , - 1 , true )) 1183-- cursor is now on 'c' 1184eq ( { 1 , 1 } , api . nvim_win_get_cursor ( 0 )) 1185end ) 1186 1187it ( 'maintains INSERT-mode cursor position current/non-current window' , function () 1188insert ( [[ 1189abcd]] ) 1190 1191-- position the cursor on 'b' 1192api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1193-- open vertical split 1194feed ( '<c-w>v' ) 1195-- get into INSERT mode to treat cursor 1196-- as being after 'a', not on 'b' 1197feed ( 'i' ) 1198-- delete 'b' 1199set_text ( 0 , 1 , 0 , 2 , {} ) 1200eq ( { 'acd' } , get_lines ( 0 , - 1 , true )) 1201-- cursor in the current window should stay after 'a' 1202eq ( { 1 , 1 } , api . nvim_win_get_cursor ( 0 )) 1203-- quit INSERT mode 1204feed ( '<esc>' ) 1205-- close current window 1206feed ( '<c-w>c' ) 1207-- cursor in non-current window should stay on 'c' 1208eq ( { 1 , 1 } , api . nvim_win_get_cursor ( 0 )) 1209end ) 1210end ) 1211 1212describe ( 'when cursor is inside replaced row range' , function () 1213it ( 'maintains cursor position if at start_row, but before start_col' , function () 1214insert ( [[ 1215This should be first 1216then there is a line we do not want 1217and finally the last one]] ) 1218 1219-- position the cursor on ' ' before 'first' 1220api . nvim_win_set_cursor ( 0 , { 1 , 14 } ) 1221 1222set_text ( 0 , 15 , 2 , 11 , { 1223'the line we do not want' , 1224'but hopefully' , 1225} ) 1226 1227eq ( { 1228'This should be the line we do not want' , 1229'but hopefully the last one' , 1230} , get_lines ( 0 , - 1 , true )) 1231-- cursor should stay at the same position 1232eq ( { 1 , 14 } , api . nvim_win_get_cursor ( 0 )) 1233end ) 1234 1235it ( 'maintains cursor position if at start_row and column is still valid' , function () 1236insert ( [[ 1237This should be first 1238then there is a line we do not want 1239and finally the last one]] ) 1240 1241-- position the cursor on 'f' in 'first' 1242api . nvim_win_set_cursor ( 0 , { 1 , 15 } ) 1243 1244set_text ( 0 , 15 , 2 , 11 , { 1245'the line we do not want' , 1246'but hopefully' , 1247} ) 1248 1249eq ( { 1250'This should be the line we do not want' , 1251'but hopefully the last one' , 1252} , get_lines ( 0 , - 1 , true )) 1253-- cursor should stay at the same position 1254eq ( { 1 , 15 } , api . nvim_win_get_cursor ( 0 )) 1255end ) 1256 1257it ( 'adjusts cursor column to keep it valid if start_row got smaller' , function () 1258insert ( [[ 1259This should be first 1260then there is a line we do not want 1261and finally the last one]] ) 1262 1263-- position the cursor on 't' in 'first' 1264api . nvim_win_set_cursor ( 0 , { 1 , 19 } ) 1265 1266local cursor =exec_lua ( [[ 1267vim.api.nvim_buf_set_text(0, 0, 15, 2, 24, {'last'}) 1268return vim.api.nvim_win_get_cursor(0) 1269]] ) 1270 1271eq ( { 'This should be last' } , get_lines ( 0 , - 1 , true )) 1272-- cursor should end up on 't' in 'last' 1273eq ( { 1 , 18 } , api . nvim_win_get_cursor ( 0 )) 1274-- immediate call to nvim_win_get_cursor should have returned the same position 1275eq ( { 1 , 18 } , cursor ) 1276end ) 1277 1278it ( 'adjusts cursor column to keep it valid if start_row decreased in INSERT mode' , function () 1279insert ( [[ 1280This should be first 1281then there is a line we do not want 1282and finally the last one]] ) 1283 1284-- position the cursor on 't' in 'first' 1285api . nvim_win_set_cursor ( 0 , { 1 , 19 } ) 1286-- enter INSERT mode to treat cursor as being after 't' 1287feed ( 'a' ) 1288 1289local cursor =exec_lua ( [[ 1290vim.api.nvim_buf_set_text(0, 0, 15, 2, 24, {'last'}) 1291return vim.api.nvim_win_get_cursor(0) 1292]] ) 1293 1294eq ( { 'This should be last' } , get_lines ( 0 , - 1 , true )) 1295-- cursor should end up after 't' in 'last' 1296eq ( { 1 , 19 } , api . nvim_win_get_cursor ( 0 )) 1297-- immediate call to nvim_win_get_cursor should have returned the same position 1298eq ( { 1 , 19 } , cursor ) 1299end ) 1300 1301it ( 'adjusts cursor to valid column in row after start_row if it got smaller' , function () 1302insert ( [[ 1303This should be first 1304then there is a line we do not want 1305and finally the last one]] ) 1306 1307-- position the cursor on 'w' in 'want' 1308api . nvim_win_set_cursor ( 0 , { 2 , 31 } ) 1309 1310local cursor =exec_lua ( [[ 1311vim.api.nvim_buf_set_text(0, 0, 15, 2, 11, { 1312'1', 1313'then 2', 1314'and then', 1315}) 1316return vim.api.nvim_win_get_cursor(0) 1317]] ) 1318 1319eq ( { 1320'This should be 1' , 1321'then 2' , 1322'and then the last one' , 1323} , get_lines ( 0 , - 1 , true )) 1324-- cursor column should end up at the end of a row 1325eq ( { 2 , 5 } , api . nvim_win_get_cursor ( 0 )) 1326-- immediate call to nvim_win_get_cursor should have returned the same position 1327eq ( { 2 , 5 } , cursor ) 1328end ) 1329 1330it ( 1331'adjusts cursor to valid column in row after start_row if it got smaller in INSERT mode' , 1332function () 1333insert ( [[ 1334This should be first 1335then there is a line we do not want 1336and finally the last one]] ) 1337 1338-- position the cursor on 'w' in 'want' 1339api . nvim_win_set_cursor ( 0 , { 2 , 31 } ) 1340-- enter INSERT mode 1341feed ( 'a' ) 1342 1343local cursor =exec_lua ( [[ 1344vim.api.nvim_buf_set_text(0, 0, 15, 2, 11, { 1345'1', 1346'then 2', 1347'and then', 1348}) 1349return vim.api.nvim_win_get_cursor(0) 1350]] ) 1351 1352eq ( { 1353'This should be 1' , 1354'then 2' , 1355'and then the last one' , 1356} , get_lines ( 0 , - 1 , true )) 1357-- cursor column should end up at the end of a row 1358eq ( { 2 , 6 } , api . nvim_win_get_cursor ( 0 )) 1359-- immediate call to nvim_win_get_cursor should have returned the same position 1360eq ( { 2 , 6 } , cursor ) 1361end 1362) 1363 1364it ( 'adjusts cursor line and column to keep it inside replacement range' , function () 1365insert ( [[ 1366This should be first 1367then there is a line we do not want 1368and finally the last one]] ) 1369 1370-- position the cursor on 'n' in 'finally' 1371api . nvim_win_set_cursor ( 0 , { 3 , 6 } ) 1372 1373local cursor =exec_lua ( [[ 1374vim.api.nvim_buf_set_text(0, 0, 15, 2, 11, { 1375'the line we do not want', 1376'but hopefully', 1377}) 1378return vim.api.nvim_win_get_cursor(0) 1379]] ) 1380 1381eq ( { 1382'This should be the line we do not want' , 1383'but hopefully the last one' , 1384} , get_lines ( 0 , - 1 , true )) 1385-- cursor should end up on 'y' in 'hopefully' 1386-- to stay in the range, because it got smaller 1387eq ( { 2 , 12 } , api . nvim_win_get_cursor ( 0 )) 1388-- immediate call to nvim_win_get_cursor should have returned the same position 1389eq ( { 2 , 12 } , cursor ) 1390end ) 1391 1392it ( 'adjusts cursor line and column if replacement is empty' , function () 1393insert ( [[ 1394This should be first 1395then there is a line we do not want 1396and finally the last one]] ) 1397 1398-- position the cursor on 'r' in 'there' 1399api . nvim_win_set_cursor ( 0 , { 2 , 8 } ) 1400 1401local cursor =exec_lua ( [[ 1402vim.api.nvim_buf_set_text(0, 0, 15, 2, 12, {}) 1403return vim.api.nvim_win_get_cursor(0) 1404]] ) 1405 1406eq ( { 'This should be the last one' } , get_lines ( 0 , - 1 , true )) 1407-- cursor should end up on the next column after deleted range 1408eq ( { 1 , 15 } , api . nvim_win_get_cursor ( 0 )) 1409-- immediate call to nvim_win_get_cursor should have returned the same position 1410eq ( { 1 , 15 } , cursor ) 1411end ) 1412 1413it ( 'adjusts cursor line and column if replacement is empty and start_col == 0' , function () 1414insert ( [[ 1415This should be first 1416then there is a line we do not want 1417and finally the last one]] ) 1418 1419-- position the cursor on 'r' in 'there' 1420api . nvim_win_set_cursor ( 0 , { 2 , 8 } ) 1421 1422local cursor =exec_lua ( [[ 1423vim.api.nvim_buf_set_text(0, 0, 0, 2, 4, {}) 1424return vim.api.nvim_win_get_cursor(0) 1425]] ) 1426 1427eq ( { 'finally the last one' } , get_lines ( 0 , - 1 , true )) 1428-- cursor should end up in column 0 1429eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 1430-- immediate call to nvim_win_get_cursor should have returned the same position 1431eq ( { 1 , 0 } , cursor ) 1432end ) 1433 1434it ( 'adjusts cursor column if replacement ends at cursor row, after cursor column' , function () 1435insert ( [[ 1436This should be first 1437then there is a line we do not want 1438and finally the last one]] ) 1439 1440-- position the cursor on 'y' in 'finally' 1441api . nvim_win_set_cursor ( 0 , { 3 , 10 } ) 1442set_text ( 0 , 15 , 2 , 11 , { '1' , 'this 2' , 'and then' } ) 1443 1444eq ( { 1445'This should be 1' , 1446'this 2' , 1447'and then the last one' , 1448} , get_lines ( 0 , - 1 , true )) 1449-- cursor should end up on 'n' in 'then' 1450eq ( { 3 , 7 } , api . nvim_win_get_cursor ( 0 )) 1451end ) 1452 1453it ( 1454'adjusts cursor column if replacement ends at cursor row, at cursor column in INSERT mode' , 1455function () 1456insert ( [[ 1457This should be first 1458then there is a line we do not want 1459and finally the last one]] ) 1460 1461-- position the cursor on 'y' at 'finally' 1462api . nvim_win_set_cursor ( 0 , { 3 , 10 } ) 1463-- enter INSERT mode to treat cursor as being between 'l' and 'y' 1464feed ( 'i' ) 1465set_text ( 0 , 15 , 2 , 11 , { '1' , 'this 2' , 'and then' } ) 1466 1467eq ( { 1468'This should be 1' , 1469'this 2' , 1470'and then the last one' , 1471} , get_lines ( 0 , - 1 , true )) 1472-- cursor should end up after 'n' in 'then' 1473eq ( { 3 , 8 } , api . nvim_win_get_cursor ( 0 )) 1474end 1475) 1476 1477it ( 'adjusts cursor column if replacement is inside of a single line' , function () 1478insert ( [[ 1479This should be first 1480then there is a line we do not want 1481and finally the last one]] ) 1482 1483-- position the cursor on 'y' in 'finally' 1484api . nvim_win_set_cursor ( 0 , { 3 , 10 } ) 1485set_text ( 2 , 4 , 2 , 11 , { 'then' } ) 1486 1487eq ( { 1488'This should be first' , 1489'then there is a line we do not want' , 1490'and then the last one' , 1491} , get_lines ( 0 , - 1 , true )) 1492-- cursor should end up on 'n' in 'then' 1493eq ( { 3 , 7 } , api . nvim_win_get_cursor ( 0 )) 1494end ) 1495 1496it ( 'does not move cursor column after end of a line' , function () 1497insert ( [[ 1498This should be the only line here 1499!!!]] ) 1500 1501-- position cursor on the last '1' 1502api . nvim_win_set_cursor ( 0 , { 2 , 2 } ) 1503 1504local cursor =exec_lua ( [[ 1505vim.api.nvim_buf_set_text(0, 0, 33, 1, 3, {}) 1506return vim.api.nvim_win_get_cursor(0) 1507]] ) 1508 1509eq ( { 'This should be the only line here' } , get_lines ( 0 , - 1 , true )) 1510-- cursor should end up on '!' 1511eq ( { 1 , 32 } , api . nvim_win_get_cursor ( 0 )) 1512-- immediate call to nvim_win_get_cursor should have returned the same position 1513eq ( { 1 , 32 } , cursor ) 1514end ) 1515 1516it ( 'does not move cursor column before start of a line' , function () 1517insert ( '\n!!!' ) 1518 1519-- position cursor on the last '1' 1520api . nvim_win_set_cursor ( 0 , { 2 , 2 } ) 1521 1522local cursor =exec_lua ( [[ 1523vim.api.nvim_buf_set_text(0, 0, 0, 1, 3, {}) 1524return vim.api.nvim_win_get_cursor(0) 1525]] ) 1526 1527eq ( { '' } , get_lines ( 0 , - 1 , true )) 1528-- cursor should end up on '!' 1529eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 1530-- immediate call to nvim_win_get_cursor should have returned the same position 1531eq ( { 1 , 0 } , cursor ) 1532end ) 1533 1534describe ( 'with virtualedit' , function () 1535it ( 'adjusts cursor line/col to keep inside replacement range if not after eol' , function () 1536insert ( [[ 1537This should be first 1538then there is a line we do not want 1539and finally the last one]] ) 1540 1541-- position cursor on 't' in 'want' 1542api . nvim_win_set_cursor ( 0 , { 2 , 34 } ) 1543-- turn on virtualedit 1544command ( 'set virtualedit=all' ) 1545 1546local cursor =exec_lua ( [[ 1547vim.api.nvim_buf_set_text(0, 0, 15, 2, 11, { 1548'the line we do not want', 1549'but hopefully', 1550}) 1551return vim.api.nvim_win_get_cursor(0) 1552]] ) 1553 1554eq ( { 1555'This should be the line we do not want' , 1556'but hopefully the last one' , 1557} , get_lines ( 0 , - 1 , true )) 1558-- cursor should end up on 'y' in 'hopefully' 1559-- to stay in the range 1560eq ( { 2 , 12 } , api . nvim_win_get_cursor ( 0 )) 1561-- immediate call to nvim_win_get_cursor should have returned the same position 1562eq ( { 2 , 12 } , cursor ) 1563-- coladd should be 0 1564eq ( 0 , fn . winsaveview (). coladd ) 1565end ) 1566 1567it ( 'does not change cursor screen column when cursor >EOL and row got shorter' , function () 1568insert ( [[ 1569This should be first 1570then there is a line we do not want 1571and finally the last one]] ) 1572 1573-- position cursor on 't' in 'want' 1574api . nvim_win_set_cursor ( 0 , { 2 , 34 } ) 1575-- turn on virtualedit 1576command ( 'set virtualedit=all' ) 1577-- move cursor after eol 1578fn . winrestview ( { coladd =5 } ) 1579 1580local cursor =exec_lua ( [[ 1581vim.api.nvim_buf_set_text(0, 0, 15, 2, 11, { 1582'the line we do not want', 1583'but hopefully', 1584}) 1585return vim.api.nvim_win_get_cursor(0) 1586]] ) 1587 1588eq ( { 1589'This should be the line we do not want' , 1590'but hopefully the last one' , 1591} , get_lines ( 0 , - 1 , true )) 1592-- cursor should end up at eol of a new row 1593eq ( { 2 , 26 } , api . nvim_win_get_cursor ( 0 )) 1594-- immediate call to nvim_win_get_cursor should have returned the same position 1595eq ( { 2 , 26 } , cursor ) 1596-- coladd should be increased so that cursor stays in the same screen column 1597eq ( 13 , fn . winsaveview (). coladd ) 1598end ) 1599 1600it ( 1601'does not change cursor screen column when cursor is after eol and row got longer' , 1602function () 1603insert ( [[ 1604This should be first 1605then there is a line we do not want 1606and finally the last one]] ) 1607 1608-- position cursor on 't' in 'first' 1609api . nvim_win_set_cursor ( 0 , { 1 , 19 } ) 1610-- turn on virtualedit 1611command ( 'set virtualedit=all' ) 1612-- move cursor after eol 1613fn . winrestview ( { coladd =21 } ) 1614 1615local cursor =exec_lua ( [[ 1616vim.api.nvim_buf_set_text(0, 0, 15, 2, 11, { 1617'the line we do not want', 1618'but hopefully', 1619}) 1620return vim.api.nvim_win_get_cursor(0) 1621]] ) 1622 1623eq ( { 1624'This should be the line we do not want' , 1625'but hopefully the last one' , 1626} , get_lines ( 0 , - 1 , true )) 1627-- cursor should end up at eol of a new row 1628eq ( { 1 , 38 } , api . nvim_win_get_cursor ( 0 )) 1629-- immediate call to nvim_win_get_cursor should have returned the same position 1630eq ( { 1 , 38 } , cursor ) 1631-- coladd should be increased so that cursor stays in the same screen column 1632eq ( 2 , fn . winsaveview (). coladd ) 1633end 1634) 1635 1636it ( 1637'does not change cursor screen column when cursor is after eol and row extended past cursor column' , 1638function () 1639insert ( [[ 1640This should be first 1641then there is a line we do not want 1642and finally the last one]] ) 1643 1644-- position cursor on 't' in 'first' 1645api . nvim_win_set_cursor ( 0 , { 1 , 19 } ) 1646-- turn on virtualedit 1647command ( 'set virtualedit=all' ) 1648-- move cursor after eol just a bit 1649fn . winrestview ( { coladd =3 } ) 1650 1651local cursor =exec_lua ( [[ 1652vim.api.nvim_buf_set_text(0, 0, 15, 2, 11, { 1653'the line we do not want', 1654'but hopefully', 1655}) 1656return vim.api.nvim_win_get_cursor(0) 1657]] ) 1658 1659eq ( { 1660'This should be the line we do not want' , 1661'but hopefully the last one' , 1662} , get_lines ( 0 , - 1 , true )) 1663-- cursor should stay at the same screen column 1664eq ( { 1 , 22 } , api . nvim_win_get_cursor ( 0 )) 1665-- immediate call to nvim_win_get_cursor should have returned the same position 1666eq ( { 1 , 22 } , cursor ) 1667-- coladd should become 0 1668eq ( 0 , fn . winsaveview (). coladd ) 1669end 1670) 1671 1672it ( 1673'does not change cursor screen column when cursor is after eol and row range decreased' , 1674function () 1675insert ( [[ 1676This should be first 1677then there is a line we do not want 1678and one more 1679and finally the last one]] ) 1680 1681-- position cursor on 'e' in 'more' 1682api . nvim_win_set_cursor ( 0 , { 3 , 11 } ) 1683-- turn on virtualedit 1684command ( 'set virtualedit=all' ) 1685-- move cursor after eol 1686fn . winrestview ( { coladd =28 } ) 1687 1688local cursor =exec_lua ( [[ 1689vim.api.nvim_buf_set_text(0, 0, 15, 3, 11, { 1690'the line we do not want', 1691'but hopefully', 1692}) 1693return vim.api.nvim_win_get_cursor(0) 1694]] ) 1695 1696eq ( { 1697'This should be the line we do not want' , 1698'but hopefully the last one' , 1699} , get_lines ( 0 , - 1 , true )) 1700-- cursor should end up at eol of a new row 1701eq ( { 2 , 26 } , api . nvim_win_get_cursor ( 0 )) 1702-- immediate call to nvim_win_get_cursor should have returned the same position 1703eq ( { 2 , 26 } , cursor ) 1704-- coladd should be increased so that cursor stays in the same screen column 1705eq ( 13 , fn . winsaveview (). coladd ) 1706end 1707) 1708end ) 1709end ) 1710 1711describe ( 'when cursor is at end_row and after end_col' , function () 1712it ( 'adjusts cursor column when only a newline is added or deleted' , function () 1713insert ( [[ 1714first line 1715second 1716line]] ) 1717 1718-- position the cursor on 'i' 1719api . nvim_win_set_cursor ( 0 , { 3 , 2 } ) 1720set_text ( 1 , 6 , 2 , 0 , {} ) 1721eq ( { 'first line' , 'second line' } , get_lines ( 0 , - 1 , true )) 1722-- cursor should stay on 'i' 1723eq ( { 2 , 8 } , api . nvim_win_get_cursor ( 0 )) 1724 1725-- add a newline back 1726set_text ( 1 , 6 , 1 , 6 , { '' , '' } ) 1727eq ( { 'first line' , 'second' , ' line' } , get_lines ( 0 , - 1 , true )) 1728-- cursor should return back to the original position 1729eq ( { 3 , 2 } , api . nvim_win_get_cursor ( 0 )) 1730end ) 1731 1732it ( 1733'adjusts cursor column if the range is not bound to either start or end of a line' , 1734function () 1735insert ( [[ 1736This should be first 1737then there is a line we do not want 1738and finally the last one]] ) 1739 1740-- position the cursor on 'h' in 'the' 1741api . nvim_win_set_cursor ( 0 , { 3 , 13 } ) 1742set_text ( 0 , 14 , 2 , 11 , {} ) 1743eq ( { 'This should be the last one' } , get_lines ( 0 , - 1 , true )) 1744-- cursor should stay on 'h' 1745eq ( { 1 , 16 } , api . nvim_win_get_cursor ( 0 )) 1746-- add deleted lines back 1747set_text ( 0 , 14 , 0 , 14 , { 1748' first' , 1749'then there is a line we do not want' , 1750'and finally' , 1751} ) 1752eq ( { 1753'This should be first' , 1754'then there is a line we do not want' , 1755'and finally the last one' , 1756} , get_lines ( 0 , - 1 , true )) 1757-- cursor should return back to the original position 1758eq ( { 3 , 13 } , api . nvim_win_get_cursor ( 0 )) 1759end 1760) 1761 1762it ( 1763'adjusts cursor column if replacing lines in range, not just deleting and adding' , 1764function () 1765insert ( [[ 1766This should be first 1767then there is a line we do not want 1768and finally the last one]] ) 1769 1770-- position the cursor on 's' in 'last' 1771api . nvim_win_set_cursor ( 0 , { 3 , 18 } ) 1772set_text ( 0 , 15 , 2 , 11 , { 1773'the line we do not want' , 1774'but hopefully' , 1775} ) 1776 1777eq ( { 1778'This should be the line we do not want' , 1779'but hopefully the last one' , 1780} , get_lines ( 0 , - 1 , true )) 1781-- cursor should stay on 's' 1782eq ( { 2 , 20 } , api . nvim_win_get_cursor ( 0 )) 1783 1784set_text ( 0 , 15 , 1 , 13 , { 1785'first' , 1786'then there is a line we do not want' , 1787'and finally' , 1788} ) 1789 1790eq ( { 1791'This should be first' , 1792'then there is a line we do not want' , 1793'and finally the last one' , 1794} , get_lines ( 0 , - 1 , true )) 1795-- cursor should return back to the original position 1796eq ( { 3 , 18 } , api . nvim_win_get_cursor ( 0 )) 1797end 1798) 1799 1800it ( 'does not move cursor column after end of a line' , function () 1801insert ( [[ 1802This should be the only line here 1803]] ) 1804 1805-- position cursor at the empty line 1806api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 1807 1808local cursor =exec_lua ( [[ 1809vim.api.nvim_buf_set_text(0, 0, 33, 1, 0, {'!'}) 1810return vim.api.nvim_win_get_cursor(0) 1811]] ) 1812 1813eq ( { 'This should be the only line here!' } , get_lines ( 0 , - 1 , true )) 1814-- cursor should end up on '!' 1815eq ( { 1 , 33 } , api . nvim_win_get_cursor ( 0 )) 1816-- immediate call to nvim_win_get_cursor should have returned the same position 1817eq ( { 1 , 33 } , cursor ) 1818end ) 1819 1820it ( 'does not move cursor column before start of a line' , function () 1821insert ( '\n' ) 1822 1823eq ( { '' , '' } , get_lines ( 0 , - 1 , true )) 1824 1825-- position cursor on the last '1' 1826api . nvim_win_set_cursor ( 0 , { 2 , 2 } ) 1827 1828local cursor =exec_lua ( [[ 1829vim.api.nvim_buf_set_text(0, 0, 0, 1, 0, {''}) 1830return vim.api.nvim_win_get_cursor(0) 1831]] ) 1832 1833eq ( { '' } , get_lines ( 0 , - 1 , true )) 1834-- cursor should end up on '!' 1835eq ( { 1 , 0 } , api . nvim_win_get_cursor ( 0 )) 1836-- immediate call to nvim_win_get_cursor should have returned the same position 1837eq ( { 1 , 0 } , cursor ) 1838end ) 1839end ) 1840 1841it ( 'can handle NULs' , function () 1842set_text ( 0 , 0 , 0 , 0 , { 'ab\0cd' } ) 1843eq ( 'ab\0cd' , curbuf_depr ( 'get_line' , 0 )) 1844end ) 1845 1846it ( 'adjusts extmarks' , function () 1847local ns =api . nvim_create_namespace ( 'my-fancy-plugin' ) 1848insert ( [[ 1849foo bar 1850baz 1851]] ) 1852local id1 =api . nvim_buf_set_extmark ( 0 , ns , 0 , 1 , {} ) 1853local id2 =api . nvim_buf_set_extmark ( 0 , ns , 0 , 7 , {} ) 1854local id3 =api . nvim_buf_set_extmark ( 0 , ns , 1 , 1 , {} ) 1855set_text ( 0 , 4 , 0 , 7 , { 'q' } ) 1856 1857eq ( { 'foo q' , 'baz' } , get_lines ( 0 , 2 , true )) 1858-- mark before replacement point is unaffected 1859eq ( { 0 , 1 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id1 , {} )) 1860-- mark gets shifted back because the replacement was shorter 1861eq ( { 0 , 5 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id2 , {} )) 1862-- mark on the next line is unaffected 1863eq ( { 1 , 1 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id3 , {} )) 1864 1865-- replacing the text spanning two lines will adjust the mark on the next line 1866set_text ( 0 , 3 , 1 , 3 , { 'qux' } ) 1867eq ( { 'fooqux' , '' } , get_lines ( 0 , 2 , true )) 1868eq ( { 0 , 6 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id3 , {} )) 1869-- but mark before replacement point is still unaffected 1870eq ( { 0 , 1 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id1 , {} )) 1871-- and the mark in the middle was shifted to the end of the insertion 1872eq ( { 0 , 6 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id2 , {} )) 1873 1874-- marks should be put back into the same place after undoing 1875set_text ( 0 , 0 , 0 , 2 , { '' } ) 1876feed ( 'u' ) 1877eq ( { 0 , 1 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id1 , {} )) 1878eq ( { 0 , 6 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id2 , {} )) 1879eq ( { 0 , 6 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id3 , {} )) 1880 1881-- marks should be shifted over by the correct number of bytes for multibyte 1882-- chars 1883set_text ( 0 , 0 , 0 , 0 , { 'Ø' } ) 1884eq ( { 0 , 3 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id1 , {} )) 1885eq ( { 0 , 8 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id2 , {} )) 1886eq ( { 0 , 8 } , api . nvim_buf_get_extmark_by_id ( 0 , ns , id3 , {} )) 1887end ) 1888 1889it ( 'correctly marks changed region for redraw #13890' , function () 1890local screen =Screen . new ( 20 , 5 ) 1891 1892insert ( [[ 1893AAA 1894BBB 1895]] ) 1896 1897api . nvim_buf_set_text ( 0 , 0 , 0 , 1 , 3 , { 'XXX' , 'YYY' } ) 1898 1899screen : expect ( [[ 1900XXX | 1901YYY | 1902^ | 1903{1:~ }| 1904| 1905]] ) 1906end ) 1907 1908it ( 'errors on out-of-range' , function () 1909insert ( [[ 1910hello foo! 1911text]] ) 1912eq ( "Invalid 'start_row': out of range" , pcall_err ( set_text , 2 , 0 , 3 , 0 , {} )) 1913eq ( "Invalid 'start_row': out of range" , pcall_err ( set_text , - 3 , 0 , 0 , 0 , {} )) 1914eq ( "Invalid 'end_row': out of range" , pcall_err ( set_text , 0 , 0 , 2 , 0 , {} )) 1915eq ( "Invalid 'end_row': out of range" , pcall_err ( set_text , 0 , 0 , - 3 , 0 , {} )) 1916eq ( "Invalid 'start_col': out of range" , pcall_err ( set_text , 1 , 5 , 1 , 5 , {} )) 1917eq ( "Invalid 'end_col': out of range" , pcall_err ( set_text , 1 , 0 , 1 , 5 , {} )) 1918end ) 1919 1920it ( 'errors when start is greater than end' , function () 1921insert ( [[ 1922hello foo! 1923text]] ) 1924eq ( "'start' is higher than 'end'" , pcall_err ( set_text , 1 , 0 , 0 , 0 , {} )) 1925eq ( "'start' is higher than 'end'" , pcall_err ( set_text , 0 , 1 , 0 , 0 , {} )) 1926end ) 1927 1928it ( 'no heap-use-after-free when called consecutively #19643' , function () 1929set_text ( 0 , 0 , 0 , 0 , { 'one' , '' , '' , 'two' } ) 1930eq ( { 'one' , '' , '' , 'two' } , get_lines ( 0 , 4 , true )) 1931api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1932exec_lua ( [[ 1933vim.api.nvim_buf_set_text(0, 0, 3, 1, 0, {''}) 1934vim.api.nvim_buf_set_text(0, 0, 3, 1, 0, {''}) 1935]] ) 1936eq ( { 'one' , 'two' } , get_lines ( 0 , 2 , true )) 1937end ) 1938 1939it ( 'auto-loads unloaded buffer' , function () 1940local new_bufnr =fn . bufnr ( 'set_text' , true ) 1941eq ( false , api . nvim_buf_is_loaded ( new_bufnr )) 1942api . nvim_buf_set_text ( new_bufnr , 0 , 0 , 0 , - 1 , { 'foo' } ) 1943eq ( true , api . nvim_buf_is_loaded ( new_bufnr )) 1944eq ( { 'foo' } , api . nvim_buf_get_lines ( new_bufnr , 0 , - 1 , false )) 1945end ) 1946 1947describe ( 'handles topline' , function () 1948local screen 1949before_each ( function () 1950screen =Screen . new ( 20 , 12 ) 1951api . nvim_buf_set_lines ( 19520 , 19530 , 1954- 1 , 1955true , 1956{ 'aaa' , 'bbb' , 'ccc' , 'ddd' , 'www' , 'xxx' , 'yyy' , 'zzz' } 1957) 1958api . nvim_set_option_value ( 'modified' , false , {} ) 1959end ) 1960 1961it ( 'of current window' , function () 1962local win =api . nvim_get_current_win () 1963local buf =api . nvim_get_current_buf () 1964 1965command ( 'new | wincmd w' ) 1966api . nvim_win_set_cursor ( win , { 8 , 0 } ) 1967 1968screen : expect { 1969grid =[[ 1970| 1971{1:~ }|*4 1972{2:[No Name] }| 1973www | 1974xxx | 1975yyy | 1976^zzz | 1977{3:[No Name] }| 1978| 1979]] , 1980} 1981api . nvim_buf_set_text ( buf , 0 , 3 , 1 , 0 , { 'X' } ) 1982 1983screen : expect { 1984grid =[[ 1985| 1986{1:~ }|*4 1987{2:[No Name] }| 1988www | 1989xxx | 1990yyy | 1991^zzz | 1992{3:[No Name] [+] }| 1993| 1994]] , 1995} 1996end ) 1997 1998it ( 'of non-current window' , function () 1999local win =api . nvim_get_current_win () 2000local buf =api . nvim_get_current_buf () 2001 2002command ( 'new' ) 2003api . nvim_win_set_cursor ( win , { 8 , 0 } ) 2004 2005screen : expect { 2006grid =[[ 2007^ | 2008{1:~ }|*4 2009{3:[No Name] }| 2010www | 2011xxx | 2012yyy | 2013zzz | 2014{2:[No Name] }| 2015| 2016]] , 2017} 2018 2019api . nvim_buf_set_text ( buf , 0 , 3 , 1 , 0 , { 'X' } ) 2020screen : expect { 2021grid =[[ 2022^ | 2023{1:~ }|*4 2024{3:[No Name] }| 2025www | 2026xxx | 2027yyy | 2028zzz | 2029{2:[No Name] [+] }| 2030| 2031]] , 2032} 2033end ) 2034 2035it ( 'of split windows with same buffer' , function () 2036local win =api . nvim_get_current_win () 2037local buf =api . nvim_get_current_buf () 2038 2039command ( 'split' ) 2040api . nvim_win_set_cursor ( win , { 8 , 0 } ) 2041api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 2042 2043screen : expect { 2044grid =[[ 2045a^aa | 2046bbb | 2047ccc | 2048ddd | 2049www | 2050{3:[No Name] }| 2051www | 2052xxx | 2053yyy | 2054zzz | 2055{2:[No Name] }| 2056| 2057]] , 2058} 2059api . nvim_buf_set_text ( buf , 0 , 3 , 1 , 0 , { 'X' } ) 2060 2061screen : expect { 2062grid =[[ 2063a^aaXbbb | 2064ccc | 2065ddd | 2066www | 2067xxx | 2068{3:[No Name] [+] }| 2069www | 2070xxx | 2071yyy | 2072zzz | 2073{2:[No Name] [+] }| 2074| 2075]] , 2076} 2077end ) 2078 2079describe ( 'of current window when' , function () 2080before_each ( function () 2081command ( 'new | wincmd w | setlocal modified' ) 2082feed ( 'Gk' ) 2083screen : expect ( [[ 2084| 2085{1:~ }|*4 2086{2:[No Name] }| 2087www | 2088xxx | 2089^yyy | 2090zzz | 2091{3:[No Name] [+] }| 2092| 2093]] ) 2094end ) 2095 2096it ( 'deleting 3 lines around topline' , function () 2097api . nvim_buf_set_text ( 0 , 3 , 0 , 6 , 0 , {} ) 2098screen : expect ( [[ 2099| 2100{1:~ }|*4 2101{2:[No Name] }| 2102ccc | 2103^yyy | 2104zzz | 2105{1:~ }| 2106{3:[No Name] [+] }| 2107| 2108]] ) 2109eq ( 5 , api . nvim_buf_line_count ( 0 )) 2110end ) 2111 2112it ( 'deleting 3 lines around the line just before topline' , function () 2113api . nvim_buf_set_text ( 0 , 2 , 0 , 5 , 0 , {} ) 2114screen : expect ( [[ 2115| 2116{1:~ }|*4 2117{2:[No Name] }| 2118bbb | 2119xxx | 2120^yyy | 2121zzz | 2122{3:[No Name] [+] }| 2123| 2124]] ) 2125eq ( 5 , api . nvim_buf_line_count ( 0 )) 2126end ) 2127 2128for count =1 , 4 do 2129it (( 'deleting %d lines just before topline' ): format ( count ), function () 2130api . nvim_buf_set_text ( 0 , 4 - count , 0 , 4 , 0 , {} ) 2131screen : expect_unchanged () 2132eq ( 8 - count , api . nvim_buf_line_count ( 0 )) 2133end ) 2134 2135describe (( 'replacing %d lines just before topline with 2 lines' ): format ( count ), function () 2136it ( 'including final newline' , function () 2137api . nvim_buf_set_text ( 0 , 4 - count , 0 , 4 , 0 , { 'eee' , 'fff' , '' } ) 2138screen : expect_unchanged () 2139eq ( 8 - count + 2 , api . nvim_buf_line_count ( 0 )) 2140end ) 2141 2142it ( 'excluding final newline' , function () 2143api . nvim_buf_set_text ( 0 , 4 - count , 0 , 3 , - 1 , { 'eee' , 'fff' } ) 2144screen : expect_unchanged () 2145eq ( 8 - count + 2 , api . nvim_buf_line_count ( 0 )) 2146end ) 2147end ) 2148end 2149 2150for count =1 , 3 do 2151it (( 'deleting %d lines far before topline' ): format ( count ), function () 2152api . nvim_buf_set_text ( 0 , 0 , 0 , count , 0 , {} ) 2153screen : expect_unchanged () 2154eq ( 8 - count , api . nvim_buf_line_count ( 0 )) 2155end ) 2156 2157describe (( 'replacing %d lines far before topline with 2 lines' ): format ( count ), function () 2158it ( 'including final newline' , function () 2159api . nvim_buf_set_text ( 0 , 0 , 0 , count , 0 , { 'eee' , 'fff' , '' } ) 2160screen : expect_unchanged () 2161eq ( 8 - count + 2 , api . nvim_buf_line_count ( 0 )) 2162end ) 2163 2164it ( 'excluding final newline' , function () 2165api . nvim_buf_set_text ( 0 , 0 , 0 , count - 1 , - 1 , { 'eee' , 'fff' } ) 2166screen : expect_unchanged () 2167eq ( 8 - count + 2 , api . nvim_buf_line_count ( 0 )) 2168end ) 2169end ) 2170end 2171 2172describe ( 'replacing topline' , function () 2173describe ( 'with 1 line' , function () 2174local s1 =[[ 2175| 2176{1:~ }|*4 2177{2:[No Name] }| 2178eee | 2179xxx | 2180^yyy | 2181zzz | 2182{3:[No Name] [+] }| 2183| 2184]] 2185it ( 'including final newline' , function () 2186api . nvim_buf_set_text ( 0 , 4 , 0 , 5 , 0 , { 'eee' , '' } ) 2187screen : expect ( s1 ) 2188end ) 2189it ( 'excluding final newline' , function () 2190api . nvim_buf_set_text ( 0 , 4 , 0 , 4 , - 1 , { 'eee' } ) 2191screen : expect ( s1 ) 2192end ) 2193end ) 2194 2195describe ( 'with 2 lines' , function () 2196local s2 =[[ 2197| 2198{1:~ }|*4 2199{2:[No Name] }| 2200eee | 2201fff | 2202xxx | 2203^yyy | 2204{3:[No Name] [+] }| 2205| 2206]] 2207it ( 'including final newline' , function () 2208api . nvim_buf_set_text ( 0 , 4 , 0 , 5 , 0 , { 'eee' , 'fff' , '' } ) 2209screen : expect ( s2 ) 2210end ) 2211it ( 'excluding final newline' , function () 2212api . nvim_buf_set_text ( 0 , 4 , 0 , 4 , - 1 , { 'eee' , 'fff' } ) 2213screen : expect ( s2 ) 2214end ) 2215end ) 2216end ) 2217 2218it ( 'inserting at start of topline' , function () 2219api . nvim_buf_set_text ( 0 , 4 , 0 , 4 , 0 , { 'X' , '' } ) 2220screen : expect ( [[ 2221| 2222{1:~ }|*4 2223{2:[No Name] }| 2224X | 2225www | 2226xxx | 2227^yyy | 2228{3:[No Name] [+] }| 2229| 2230]] ) 2231end ) 2232end ) 2233end ) 2234end ) 2235 2236describe_lua_and_rpc ( 'nvim_buf_get_text' , function ( lua_or_rpc ) 2237local get_text =lua_or_rpc . nvim_buf_get_text 2238before_each ( function () 2239insert ( [[ 2240hello foo! 2241text 2242more]] ) 2243end ) 2244 2245it ( 'works' , function () 2246eq ( { 'hello' } , get_text ( 0 , 0 , 0 , 0 , 5 , {} )) 2247eq ( { 'hello foo!' } , get_text ( 0 , 0 , 0 , 0 , 42 , {} )) 2248eq ( { 'foo!' } , get_text ( 0 , 0 , 6 , 0 , 10 , {} )) 2249eq ( { 'foo!' , 'tex' } , get_text ( 0 , 0 , 6 , 1 , 3 , {} )) 2250eq ( { 'foo!' , 'tex' } , get_text ( 0 , - 3 , 6 , - 2 , 3 , {} )) 2251eq ( { '' } , get_text ( 0 , 0 , 18 , 0 , 20 , {} )) 2252eq ( { 'ext' } , get_text ( 0 , - 2 , 1 , - 2 , 4 , {} )) 2253eq ( { 'hello foo!' , 'text' , 'm' } , get_text ( 0 , 0 , 0 , 2 , 1 , {} )) 2254eq ( { 'hello foo!' } , get_text ( 0 , 0 , - 987654321 , 0 , 987654321 , {} )) 2255eq ( { '' } , get_text ( 0 , 0 , - 15 , 0 , - 20 , {} )) 2256end ) 2257 2258it ( 'errors on out-of-range' , function () 2259eq ( 'Index out of bounds' , pcall_err ( get_text , 0 , 2 , 0 , 4 , 0 , {} )) 2260eq ( 'Index out of bounds' , pcall_err ( get_text , 0 , - 4 , 0 , 0 , 0 , {} )) 2261eq ( 'Index out of bounds' , pcall_err ( get_text , 0 , 0 , 0 , 3 , 0 , {} )) 2262eq ( 'Index out of bounds' , pcall_err ( get_text , 0 , 0 , 0 , - 4 , 0 , {} )) 2263-- no ml_get errors should happen #19017 2264eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 2265end ) 2266 2267it ( 'errors when start is greater than end' , function () 2268eq ( "'start' is higher than 'end'" , pcall_err ( get_text , 0 , 1 , 0 , 0 , 0 , {} )) 2269eq ( 'start_col must be less than or equal to end_col' , pcall_err ( get_text , 0 , 0 , 1 , 0 , 0 , {} )) 2270end ) 2271end ) 2272 2273describe ( 'nvim_buf_get_offset' , function () 2274local get_offset =api . nvim_buf_get_offset 2275it ( 'works' , function () 2276api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'Some\r' , 'exa\000mple' , '' , 'buf\rfer' , 'text' } ) 2277eq ( 5 , api . nvim_buf_line_count ( 0 )) 2278eq ( 0 , get_offset ( 0 , 0 )) 2279eq ( 6 , get_offset ( 0 , 1 )) 2280eq ( 15 , get_offset ( 0 , 2 )) 2281eq ( 16 , get_offset ( 0 , 3 )) 2282eq ( 24 , get_offset ( 0 , 4 )) 2283eq ( 29 , get_offset ( 0 , 5 )) 2284eq ( 'Index out of bounds' , pcall_err ( get_offset , 0 , 6 )) 2285eq ( 'Index out of bounds' , pcall_err ( get_offset , 0 , - 1 )) 2286 2287api . nvim_set_option_value ( 'eol' , false , {} ) 2288api . nvim_set_option_value ( 'fixeol' , false , {} ) 2289eq ( 28 , get_offset ( 0 , 5 )) 2290 2291-- fileformat is ignored 2292api . nvim_set_option_value ( 'fileformat' , 'dos' , {} ) 2293eq ( 0 , get_offset ( 0 , 0 )) 2294eq ( 6 , get_offset ( 0 , 1 )) 2295eq ( 15 , get_offset ( 0 , 2 )) 2296eq ( 16 , get_offset ( 0 , 3 )) 2297eq ( 24 , get_offset ( 0 , 4 )) 2298eq ( 28 , get_offset ( 0 , 5 )) 2299api . nvim_set_option_value ( 'eol' , true , {} ) 2300eq ( 29 , get_offset ( 0 , 5 )) 2301 2302command ( 'set hidden' ) 2303command ( 'enew' ) 2304eq ( 6 , api . nvim_buf_get_offset ( 1 , 1 )) 2305command ( 'bunload! 1' ) 2306eq ( - 1 , api . nvim_buf_get_offset ( 1 , 1 )) 2307eq ( - 1 , api . nvim_buf_get_offset ( 1 , 0 )) 2308end ) 2309 2310it ( 'works in empty buffer' , function () 2311eq ( 0 , get_offset ( 0 , 0 )) 2312eq ( 1 , get_offset ( 0 , 1 )) 2313eq ( - 1 , fn . line2byte ( '$' )) 2314end ) 2315 2316it ( 'works in buffer with one line inserted' , function () 2317feed ( 'itext' ) 2318eq ( 0 , get_offset ( 0 , 0 )) 2319eq ( 5 , get_offset ( 0 , 1 )) 2320end ) 2321end ) 2322 2323describe ( 'nvim_buf_get_var, nvim_buf_set_var, nvim_buf_del_var' , function () 2324it ( 'works' , function () 2325api . nvim_buf_set_var ( 0 , 'lua' , { 1 , 2 , { [ '3' ] =1 } } ) 2326eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_buf_get_var ( 0 , 'lua' )) 2327eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_eval ( 'b:lua' )) 2328eq ( 1 , fn . exists ( 'b:lua' )) 2329api . nvim_buf_del_var ( 0 , 'lua' ) 2330eq ( 0 , fn . exists ( 'b:lua' )) 2331eq ( 'Key not found: lua' , pcall_err ( api . nvim_buf_del_var , 0 , 'lua' )) 2332api . nvim_buf_set_var ( 0 , 'lua' , 1 ) 2333command ( 'lockvar b:lua' ) 2334eq ( 'Key is locked: lua' , pcall_err ( api . nvim_buf_del_var , 0 , 'lua' )) 2335eq ( 'Key is locked: lua' , pcall_err ( api . nvim_buf_set_var , 0 , 'lua' , 1 )) 2336eq ( 'Key is read-only: changedtick' , pcall_err ( api . nvim_buf_del_var , 0 , 'changedtick' )) 2337eq ( 'Key is read-only: changedtick' , pcall_err ( api . nvim_buf_set_var , 0 , 'changedtick' , 1 )) 2338end ) 2339end ) 2340 2341describe ( 'nvim_buf_get_changedtick' , function () 2342it ( 'works' , function () 2343eq ( 2 , api . nvim_buf_get_changedtick ( 0 )) 2344api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'abc\0' , '\0def' , 'ghi' } ) 2345eq ( 3 , api . nvim_buf_get_changedtick ( 0 )) 2346eq ( 3 , api . nvim_buf_get_var ( 0 , 'changedtick' )) 2347end ) 2348 2349it ( 'buffer_set_var returns the old value' , function () 2350local val1 ={ 1 , 2 , { [ '3' ] =1 } } 2351local val2 ={ 4 , 7 } 2352eq ( NIL , request ( 'buffer_set_var' , 0 , 'lua' , val1 )) 2353eq ( val1 , request ( 'buffer_set_var' , 0 , 'lua' , val2 )) 2354end ) 2355 2356it ( 'buffer_del_var returns the old value' , function () 2357local val1 ={ 1 , 2 , { [ '3' ] =1 } } 2358local val2 ={ 4 , 7 } 2359eq ( NIL , request ( 'buffer_set_var' , 0 , 'lua' , val1 )) 2360eq ( val1 , request ( 'buffer_set_var' , 0 , 'lua' , val2 )) 2361eq ( val2 , request ( 'buffer_del_var' , 0 , 'lua' )) 2362end ) 2363end ) 2364 2365describe ( 'nvim_get_option_value, nvim_set_option_value' , function () 2366it ( 'works' , function () 2367eq ( 8 , api . nvim_get_option_value ( 'shiftwidth' , {} )) 2368api . nvim_set_option_value ( 'shiftwidth' , 4 , {} ) 2369eq ( 4 , api . nvim_get_option_value ( 'shiftwidth' , {} )) 2370-- global-local option 2371api . nvim_set_option_value ( 'define' , 'test' , { buf =0 } ) 2372eq ( 'test' , api . nvim_get_option_value ( 'define' , { buf =0 } )) 2373-- Doesn't change the global value 2374eq ( '' , api . nvim_get_option_value ( 'define' , { scope ='global' } )) 2375end ) 2376 2377it ( 'returns values for unset local options' , function () 2378-- 'undolevels' is only set to its "unset" value when a new buffer is 2379-- created 2380command ( 'enew' ) 2381eq ( - 123456 , api . nvim_get_option_value ( 'undolevels' , { buf =0 } )) 2382end ) 2383end ) 2384 2385describe ( 'nvim_buf_get_name, nvim_buf_set_name' , function () 2386it ( 'works' , function () 2387command ( 'new' ) 2388eq ( '' , api . nvim_buf_get_name ( 0 )) 2389local new_name =api . nvim_eval ( 'resolve(tempname())' ) 2390api . nvim_buf_set_name ( 0 , new_name ) 2391eq ( new_name , api . nvim_buf_get_name ( 0 )) 2392command ( 'w!' ) 2393eq ( 1 , fn . filereadable ( new_name )) 2394os . remove ( new_name ) 2395end ) 2396 2397it ( 'directory buffer-name always ends with slash' , function () 2398local cwd =t . fix_slashes ( fn . getcwd ()) 2399local dir ='Xtest_dir_name' 2400t . mkdir ( dir ) 2401finally ( function () 2402n . rmdir ( dir ) 2403end ) 2404api . nvim_buf_set_name ( 0 , dir ) 2405eq ( cwd .. '/' .. dir .. '/' , t . fix_slashes ( api . nvim_buf_get_name ( 0 ))) 2406end ) 2407 2408it ( 'directory buffer-name preserves symbolic link path' , function () 2409t . skip ( t . is_os ( 'win' ), 'N/A for Windows' ) 2410local cwd =t . fix_slashes ( fn . getcwd ()) 2411local target ='Xtest_dir_target' 2412local link ='Xtest_dir_link' 2413t . mkdir ( target ) 2414assert ( vim . uv . fs_symlink ( assert ( vim . uv . fs_realpath ( target )), link , { dir =true } )) 2415finally ( function () 2416os . remove ( link ) 2417n . rmdir ( target ) 2418end ) 2419api . nvim_buf_set_name ( 0 , link .. '/' ) 2420eq ( cwd .. '/' .. link .. '/' , t . fix_slashes ( api . nvim_buf_get_name ( 0 ))) 2421end ) 2422 2423it ( 'allows URI buffer names that differ by a trailing slash' , function () 2424api . nvim_buf_set_name ( 0 , 'test://example' ) 2425local trailing_slash =api . nvim_create_buf ( true , false ) 2426api . nvim_buf_set_name ( trailing_slash , 'test://example/' ) 2427 2428eq ( 'test://example' , api . nvim_buf_get_name ( 0 )) 2429eq ( 'test://example/' , api . nvim_buf_get_name ( trailing_slash )) 2430end ) 2431 2432describe ( "with 'autochdir'" , function () 2433local topdir 2434local oldbuf 2435local newbuf 2436 2437before_each ( function () 2438command ( 'set shellslash' ) 2439topdir =fn . getcwd () 2440t . mkdir ( topdir .. '/Xacd' ) 2441 2442oldbuf =api . nvim_get_current_buf () 2443command ( 'vnew' ) 2444newbuf =api . nvim_get_current_buf () 2445command ( 'set autochdir' ) 2446end ) 2447 2448after_each ( function () 2449n . rmdir ( topdir .. '/Xacd' ) 2450end ) 2451 2452it ( 'does not change cwd with non-current buffer' , function () 2453api . nvim_buf_set_name ( oldbuf , topdir .. '/Xacd/foo.txt' ) 2454eq ( topdir , fn . getcwd ()) 2455end ) 2456 2457it ( 'changes cwd with current buffer' , function () 2458api . nvim_buf_set_name ( newbuf , topdir .. '/Xacd/foo.txt' ) 2459eq ( topdir .. '/Xacd' , fn . getcwd ()) 2460end ) 2461end ) 2462end ) 2463 2464describe ( 'nvim_buf_is_loaded' , function () 2465it ( 'works' , function () 2466-- record our buffer number for when we unload it 2467local bufnr =api . nvim_get_current_buf () 2468-- api should report that the buffer is loaded 2469ok ( api . nvim_buf_is_loaded ( bufnr )) 2470-- hide the current buffer by switching to a new empty buffer 2471-- Careful! we need to modify the buffer first or vim will just reuse it 2472api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'line1' } ) 2473command ( 'hide enew' ) 2474-- confirm the buffer is hidden, but still loaded 2475local infolist =api . nvim_eval ( 'getbufinfo(' .. bufnr .. ')' ) 2476eq ( 1 , # infolist ) 2477eq ( 1 , infolist [ 1 ]. hidden ) 2478eq ( 1 , infolist [ 1 ]. loaded ) 2479-- now force unload the buffer 2480command ( 'bunload! ' .. bufnr ) 2481-- confirm the buffer is unloaded 2482infolist =api . nvim_eval ( 'getbufinfo(' .. bufnr .. ')' ) 2483eq ( 0 , infolist [ 1 ]. loaded ) 2484-- nvim_buf_is_loaded() should also report the buffer as unloaded 2485eq ( false , api . nvim_buf_is_loaded ( bufnr )) 2486end ) 2487end ) 2488 2489describe ( 'nvim_buf_is_valid' , function () 2490it ( 'works' , function () 2491command ( 'new' ) 2492local b =api . nvim_get_current_buf () 2493ok ( api . nvim_buf_is_valid ( b )) 2494command ( 'bw!' ) 2495ok ( not api . nvim_buf_is_valid ( b )) 2496end ) 2497end ) 2498 2499describe ( 'nvim_buf_delete' , function () 2500it ( 'allows for just deleting' , function () 2501command ( 'new' ) 2502local b =api . nvim_get_current_buf () 2503ok ( api . nvim_buf_is_valid ( b )) 2504api . nvim_buf_delete ( b , {} ) 2505ok ( not api . nvim_buf_is_loaded ( b )) 2506ok ( not api . nvim_buf_is_valid ( b )) 2507end ) 2508 2509it ( 'allows for just unloading' , function () 2510command ( 'new' ) 2511local b =api . nvim_get_current_buf () 2512ok ( api . nvim_buf_is_valid ( b )) 2513api . nvim_buf_delete ( b , { unload =true } ) 2514ok ( not api . nvim_buf_is_loaded ( b )) 2515ok ( api . nvim_buf_is_valid ( b )) 2516end ) 2517 2518it ( 'does not crash if WinClosed closes the next tabpage #40852' , function () 2519exec_lua ( function () 2520vim . cmd . tabnew () 2521vim . api . nvim_create_autocmd ( 'WinClosed' , { 2522callback =function () 2523pcall ( vim . api . nvim_win_close , 0 , true ) 2524end , 2525} ) 2526local bufs =vim . api . nvim_list_bufs () 2527vim . api . nvim_buf_delete ( bufs [ # bufs - 1 ], { force =true } ) 2528end ) 2529assert_alive () 2530end ) 2531end ) 2532 2533describe ( 'nvim_buf_get_mark' , function () 2534it ( 'works' , function () 2535api . nvim_buf_set_lines ( 0 , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 2536api . nvim_win_set_cursor ( 0 , { 3 , 4 } ) 2537command ( 'mark v' ) 2538eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , 'v' )) 2539end ) 2540end ) 2541 2542describe ( 'nvim_buf_set_mark' , function () 2543it ( 'works with buffer local marks' , function () 2544api . nvim_buf_set_lines ( 0 , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 2545eq ( true , api . nvim_buf_set_mark ( 0 , 'z' , 1 , 1 , {} )) 2546eq ( { 1 , 1 } , api . nvim_buf_get_mark ( 0 , 'z' )) 2547eq ( { 0 , 1 , 2 , 0 } , fn . getpos ( "'z" )) 2548end ) 2549it ( 'works with file/uppercase marks' , function () 2550api . nvim_buf_set_lines ( 0 , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 2551eq ( true , api . nvim_buf_set_mark ( 0 , 'Z' , 3 , 2 , {} )) 2552eq ( { 3 , 2 } , api . nvim_buf_get_mark ( 0 , 'Z' )) 2553eq ( { api . nvim_get_current_buf (), 3 , 3 , 0 } , fn . getpos ( "'Z" )) 2554end ) 2555it ( 'fails when invalid marks names are used' , function () 2556eq ( false , pcall ( api . nvim_buf_set_mark , 0 , '!' , 1 , 0 , {} )) 2557eq ( false , pcall ( api . nvim_buf_set_mark , 0 , 'fail' , 1 , 0 , {} )) 2558end ) 2559it ( 'fails when invalid buffer number is used' , function () 2560eq ( false , pcall ( api . nvim_buf_set_mark , 99 , 'a' , 1 , 1 , {} )) 2561end ) 2562it ( 'auto-loads unloaded buffer' , function () 2563local new_bufnr =fn . bufnr ( 'set_mark' , true ) 2564eq ( false , api . nvim_buf_is_loaded ( new_bufnr )) 2565eq ( true , api . nvim_buf_set_mark ( new_bufnr , 'A' , 0 , 0 , {} )) 2566eq ( true , api . nvim_buf_is_loaded ( new_bufnr )) 2567eq ( { 0 , 0 } , api . nvim_buf_get_mark ( new_bufnr , 'A' )) 2568end ) 2569end ) 2570 2571describe ( 'nvim_buf_del_mark' , function () 2572it ( 'works with buffer local marks' , function () 2573api . nvim_buf_set_lines ( 0 , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 2574api . nvim_buf_set_mark ( 0 , 'z' , 3 , 1 , {} ) 2575eq ( true , api . nvim_buf_del_mark ( 0 , 'z' )) 2576eq ( { 0 , 0 } , api . nvim_buf_get_mark ( 0 , 'z' )) 2577end ) 2578it ( 'works with file/uppercase marks' , function () 2579api . nvim_buf_set_lines ( 0 , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 2580api . nvim_buf_set_mark ( 0 , 'Z' , 3 , 3 , {} ) 2581eq ( true , api . nvim_buf_del_mark ( 0 , 'Z' )) 2582eq ( { 0 , 0 } , api . nvim_buf_get_mark ( 0 , 'Z' )) 2583end ) 2584it ( 'returns false in marks not set in this buffer' , function () 2585local abuf =api . nvim_create_buf ( false , true ) 2586api . nvim_buf_set_lines ( abuf , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 2587api . nvim_buf_set_mark ( abuf , 'A' , 2 , 2 , {} ) 2588eq ( false , api . nvim_buf_del_mark ( 0 , 'A' )) 2589eq ( { 2 , 2 } , api . nvim_buf_get_mark ( abuf , 'A' )) 2590end ) 2591it ( 'returns false if mark was not deleted' , function () 2592api . nvim_buf_set_lines ( 0 , - 1 , - 1 , true , { 'a' , 'bit of' , 'text' } ) 2593api . nvim_buf_set_mark ( 0 , 'z' , 3 , 1 , {} ) 2594eq ( true , api . nvim_buf_del_mark ( 0 , 'z' )) 2595eq ( false , api . nvim_buf_del_mark ( 0 , 'z' )) -- Mark was already deleted 2596end ) 2597it ( 'fails when invalid marks names are used' , function () 2598eq ( false , pcall ( api . nvim_buf_del_mark , 0 , '!' )) 2599eq ( false , pcall ( api . nvim_buf_del_mark , 0 , 'fail' )) 2600end ) 2601it ( 'fails when invalid buffer number is used' , function () 2602eq ( false , pcall ( api . nvim_buf_del_mark , 99 , 'a' )) 2603end ) 2604end ) 2605 2606describe ( 'nvim_buf_call' , function () 2607it ( 'window keeps the buffer when the callback closes the original window' , function () 2608local origin =api . nvim_get_current_win () 2609local other_buf =api . nvim_create_buf ( true , false ) 2610local other_win =api . nvim_open_win ( other_buf , false , { split ='right' } ) 2611exec_lua ( function () 2612vim . api . nvim_buf_call ( other_buf , function () 2613vim . api . nvim_win_close ( origin , true ) 2614vim . cmd ( 'enew' ) -- switch the context window away from other_buf 2615end ) 2616end ) 2617-- Original window is gone: stay in the nvim_buf_call window, but buffer is restored. 2618eq ( other_win , api . nvim_get_current_win ()) 2619eq ( other_buf , api . nvim_win_get_buf ( other_win )) 2620end ) 2621 2622it ( 'preserves visual-mode, unless the callback ended it' , function () 2623-- Same-buffer: Visual survives untouched. 2624command ( 'normal! v' ) 2625exec_lua ( 'vim.api.nvim_buf_call(0, function() end)' ) 2626eq ( 'v' , fn . mode ()) 2627-- The callback ends Visual mode: it must STAY ended. 2628exec_lua ( [[vim.api.nvim_buf_call(0, function() vim.cmd('normal! \27') end)]] ) 2629eq ( 'n' , fn . mode ()) 2630end ) 2631 2632it ( 'supports multiple returns' , function () 2633local curbuf =api . nvim_get_current_buf () 2634local other =api . nvim_create_buf ( false , true ) 2635exec_lua ( function () 2636function with_len ( ... ) 2637return select ( '#' , ... ), { ... } 2638end 2639function test ( fn ) 2640local len , res =with_len ( vim . api . nvim_buf_call ( other , fn )) 2641-- convert to serializable vim.NIL 2642for i =1 , len do 2643if res [ i ] == nil then 2644res [ i ] =vim . NIL 2645end 2646end 2647return res 2648end 2649end ) 2650 2651eq ( 2652{ other } , 2653exec_lua ( function () 2654return test ( function () 2655return vim . api . nvim_get_current_buf () 2656end ) 2657end ) 2658) 2659eq ( curbuf , api . nvim_get_current_buf ()) 2660eq ( 2661{ other , vim . NIL } , 2662exec_lua ( function () 2663return test ( function () 2664return vim . api . nvim_get_current_buf (), nil 2665end ) 2666end ) 2667) 2668 2669eq ( 2670{ 6 , 7 } , 2671exec_lua ( function () 2672return test ( function () 2673return 6 , 7 2674end ) 2675end ) 2676) 2677eq ( 2678{ 6 , vim . NIL , 7 } , 2679exec_lua ( function () 2680return test ( function () 2681return 6 , nil , 7 2682end ) 2683end ) 2684) 2685eq ( 2686{} , 2687exec_lua ( function () 2688return test ( function () end ) 2689end ) 2690) 2691eq ( 2692{ vim . NIL } , 2693exec_lua ( function () 2694return test ( function () 2695return nil 2696end ) 2697end ) 2698) 2699end ) 2700 2701it ( 'propagates return values when called from a coroutine #39834' , function () 2702local other =api . nvim_create_buf ( false , true ) 2703eq ( 2704{ other , vim . NIL , 42 } , 2705exec_lua ( function () 2706local out 2707local co =coroutine . create ( function () 2708local function pack ( ... ) 2709local r ={ ... } 2710for i =1 , select ( '#' , ... ) do 2711if r [ i ] == nil then 2712r [ i ] =vim . NIL 2713end 2714end 2715return r 2716end 2717out =pack ( vim . api . nvim_buf_call ( other , function () 2718return vim . api . nvim_get_current_buf (), nil , 42 2719end )) 2720end ) 2721assert ( coroutine . resume ( co )) 2722return out 2723end ) 2724) 2725end ) 2726 2727it ( 'can access buf options' , function () 2728local buf1 =api . nvim_get_current_buf () 2729local buf2 =exec_lua [[ 2730buf2 = vim.api.nvim_create_buf(false, true) 2731return buf2 2732]] 2733 2734eq ( false , api . nvim_get_option_value ( 'autoindent' , { buf =buf1 } )) 2735eq ( false , api . nvim_get_option_value ( 'autoindent' , { buf =buf2 } )) 2736 2737local val =exec_lua [[ 2738return vim.api.nvim_buf_call(buf2, function() 2739vim.cmd "set autoindent" 2740return vim.api.nvim_get_current_buf() 2741end) 2742]] 2743 2744eq ( false , api . nvim_get_option_value ( 'autoindent' , { buf =buf1 } )) 2745eq ( true , api . nvim_get_option_value ( 'autoindent' , { buf =buf2 } )) 2746eq ( buf1 , api . nvim_get_current_buf ()) 2747eq ( buf2 , val ) 2748end ) 2749 2750it ( 'does not cause ml_get errors with invalid visual selection' , function () 2751-- Should be fixed by vim-patch:8.2.4028. 2752exec_lua [[ 2753local api = vim.api 2754local t = function(s) return api.nvim_replace_termcodes(s, true, true, true) end 2755api.nvim_buf_set_lines(0, 0, -1, true, {"a", "b", "c"}) 2756api.nvim_feedkeys(t "G<C-V>", "txn", false) 2757api.nvim_buf_call(api.nvim_create_buf(false, true), function() vim.cmd "redraw" end) 2758]] 2759end ) 2760 2761it ( 'can be nested crazily with hidden buffers' , function () 2762eq ( 2763true , 2764exec_lua ( [[ 2765local function scratch_buf_call(fn) 2766local buf = vim.api.nvim_create_buf(false, true) 2767vim.api.nvim_set_option_value('cindent', true, {buf = buf}) 2768return vim.api.nvim_buf_call(buf, function() 2769return vim.api.nvim_get_current_buf() == buf 2770and vim.api.nvim_get_option_value('cindent', {buf = buf}) 2771and fn() 2772end) and vim.api.nvim_buf_delete(buf, {}) == nil 2773end 2774 2775return scratch_buf_call(function() 2776return scratch_buf_call(function() 2777return scratch_buf_call(function() 2778return scratch_buf_call(function() 2779return scratch_buf_call(function() 2780return scratch_buf_call(function() 2781return scratch_buf_call(function() 2782return scratch_buf_call(function() 2783return scratch_buf_call(function() 2784return scratch_buf_call(function() 2785return scratch_buf_call(function() 2786return scratch_buf_call(function() 2787return true 2788end) 2789end) 2790end) 2791end) 2792end) 2793end) 2794end) 2795end) 2796end) 2797end) 2798end) 2799end) 2800]] ) 2801) 2802end ) 2803 2804it ( 'can return values by reference' , function () 2805eq ( 2806{ 4 , 7 } , 2807exec_lua [[ 2808local val = {4, 10} 2809local ref = vim.api.nvim_buf_call(0, function() return val end) 2810ref[2] = 7 2811return val 2812]] 2813) 2814end ) 2815 2816it ( 'can get Visual selection in current buffer #34162' , function () 2817insert ( 'foo bar baz' ) 2818feed ( 'gg0fbvtb' ) 2819local text =exec_lua ( [[ 2820return vim.api.nvim_buf_call(0, function() 2821return vim.fn.getregion(vim.fn.getpos('.'), vim.fn.getpos('v')) 2822end) 2823]] ) 2824eq ( { 'bar ' } , text ) 2825end ) 2826end ) 2827end ) 2828 2829describe ( 'path sep in ffname/sfname #39382,' , function () 2830if not t . is_os ( 'win' ) then 2831return 2832end 2833 2834local cwd , files =vim . uv . cwd (), {} 2835local expected_cwd =vim . fs . normalize ( cwd ) 2836for i =1 , 3 do 2837files [ i ] =( [[%s\Xtest\file%s]] ): format ( cwd , i ) 2838end 2839 2840local sfname_eq =function ( expected , nr ) 2841command (( 'buffer %s' ): format ( nr or '' )) 2842eq ( expected , api . nvim_eval_statusline ( '%f' , {} ). str ) 2843end 2844 2845setup ( function () 2846t . mkdir ( 'Xtest' ) 2847end ) 2848 2849teardown ( function () 2850n . rmdir ( 'Xtest' ) 2851end ) 2852 2853before_each ( function () 2854clear () 2855end ) 2856 2857it ( 'normalizes startup file arguments' , function () 2858clear { args =files } 2859for i =1 , 3 do 2860eq (( '%s/Xtest/file%s' ): format ( expected_cwd , i ), api . nvim_buf_get_name ( i )) 2861sfname_eq (( 'Xtest/file%s' ): format ( i ), i ) 2862end 2863end ) 2864 2865it ( 'normalizes :next arguments (FILES flag)' , function () 2866command (( 'next %s' ): format ( table . concat ( files , ' ' ))) 2867for i =1 , 3 do 2868eq (( '%s/Xtest/file%s' ): format ( expected_cwd , i ), api . nvim_buf_get_name ( i )) 2869sfname_eq (( '%s/Xtest/file%s' ): format ( expected_cwd , i ), i ) 2870end 2871end ) 2872 2873it ( 'normalizes :edit arguments (FILE1 flag)' , function () 2874-- tilde prefix 2875command ( [[edit ~\foo\bar]] ) 2876eq (( '%s/foo/bar' ): format ( fn . getenv ( 'HOME' )), api . nvim_buf_get_name ( 0 )) 2877sfname_eq ( '~/foo/bar' ) 2878 2879-- absolute path 2880command (( 'edit %s' ): format ( files [ 1 ])) 2881eq (( '%s/Xtest/file1' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2882sfname_eq (( '%s/Xtest/file1' ): format ( expected_cwd )) 2883end ) 2884 2885it ( 'normalizes bufadd() arguments' , function () 2886local n =fn . bufadd ( files [ 1 ]) 2887eq (( '%s/Xtest/file1' ): format ( expected_cwd ), api . nvim_buf_get_name ( n )) 2888sfname_eq (( '%s/Xtest/file1' ): format ( expected_cwd ), n ) 2889end ) 2890 2891it ( 'normalizes bufnr() arguments' , function () 2892local n =fn . bufnr ( files [ 1 ], 1 ) 2893eq (( '%s/Xtest/file1' ): format ( expected_cwd ), api . nvim_buf_get_name ( n )) 2894sfname_eq (( '%s/Xtest/file1' ): format ( expected_cwd ), n ) 2895end ) 2896 2897it ( 'normalizes temp paths, <afile> and <amatch> for :diffpatch' , function () 2898t . write_file ( 'Xtest/patch' , [[diff file]] ) 2899t . write_file ( 'Xtest/test.txt' , [[input file]] ) 2900command ( 'edit Xtest/test.txt' ) 2901exec ( [[ 2902fun MyPatch() 2903call writefile(['output file'], v:fname_out) 2904endfunc 2905set patchexpr=MyPatch() 2906]] ) 2907exec_lua ( [[ 2908vim.api.nvim_create_autocmd('BufAdd', { 2909pattern = '*', 2910callback = function(ev) 2911vim.g.test_file = ev.file 2912vim.g.test_match = ev.match 2913end 2914}) 2915]] ) 2916command ( 'diffpatch Xtest/patch' ) 2917eq (( '%s/Xtest/test.txt.new' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2918sfname_eq ( 'Xtest/test.txt.new' ) 2919local tmp =fn . getenv ( 'TMPDIR' ) 2920local afile =fn . eval ( 'g:test_file' ) ---@type string 2921local amatch =fn . eval ( 'g:test_match' ) ---@type string 2922t . matches ( tmp , afile , true ) 2923t . not_matches ( '\\' , afile , true ) 2924t . matches ( tmp , amatch , true ) 2925t . not_matches ( '\\' , amatch , true ) 2926end ) 2927 2928it ( 'normalizes :find results' , function () 2929t . write_file ( 'Xtest/file1' , '' ) 2930command ( [[find Xtest\file1]] ) 2931eq (( '%s/Xtest/file1' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2932sfname_eq ( 'Xtest/file1' ) 2933 2934-- 'findfunc' 2935exec (( [[ 2936func FindFile(cmdarg, cmdcomplete) 2937return ['%s'] 2938endfunc 2939set findfunc=FindFile 2940]] ): format ( files [ 2 ])) 2941command ( [[find file2]] ) 2942eq (( '%s/Xtest/file2' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2943sfname_eq (( '%s/Xtest/file2' ): format ( expected_cwd )) 2944end ) 2945 2946it ( 'normalizes :scriptnames arguments' , function () 2947command (( 'scriptnames %s' ): format ( files [ 1 ])) 2948eq (( '%s/Xtest/file1' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2949sfname_eq (( '%s/Xtest/file1' ): format ( expected_cwd )) 2950end ) 2951 2952it ( 'normalizes gf targets' , function () 2953t . write_file ( 'Xtest/file1' , '' ) 2954feed (( 'i%s<Esc>0gf' ): format ( files [ 1 ])) 2955eq (( '%s/Xtest/file1' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2956sfname_eq (( '%s/Xtest/file1' ): format ( expected_cwd )) 2957command ( 'bw' ) 2958feed ( '<C-W>f' ) 2959eq (( '%s/Xtest/file1' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2960sfname_eq (( '%s/Xtest/file1' ): format ( expected_cwd )) 2961end ) 2962 2963it ( 'normalizes macro definition targets and completions' , function () 2964t . mkdir ( 'Xtest/sub' ) 2965t . write_file ( 2966'Xtest/sub/bar.h' , 2967[[ 2968#define MIN 0; 2969#define MAX 10; 2970]] 2971) 2972t . write_file ( 2973'Xtest/foo.c' , 2974[[ 2975#include "sub\bar.h"; 2976void main() { 2977int i = MIN; 2978} 2979]] 2980) 2981local screen =Screen . new ( 30 , 10 ) 2982command ( [[set include=^\\s*#\\s*include]] ) 2983command ( [[set define=^\\s*#\\s*define]] ) 2984command ( 'e Xtest/foo.c' ) 2985command ( 'djump MIN' ) 2986eq (( '%s/Xtest/sub/bar.h' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 2987sfname_eq ( 'Xtest/sub/bar.h' ) 2988command ( 'bw' ) 2989feed ( '/MIN<CR>lxxi<C-x><C-d>' ) 2990screen : expect ( { 2991any ={ 2992'MIN Xtest/sub/bar.h' , 2993'MAX Xtest/sub/bar.h' , 2994} , 2995} ) 2996end ) 2997 2998it ( 'normalizes tag targets' , function () 2999t . write_file ( 3000'Xtest/tags' , 3001[[ 3002main .\\foo.c /^void main() /;" f typeref:typename:void 3003main2 $DIR\foo.c /^void main2() /;" f typeref:typename:void 3004main3 ./foo.c /^void main3() /;" f typeref:typename:void 3005]] 3006) 3007t . write_file ( 3008'Xtest/foo.c' , 3009[[ 3010void main() {} 3011void main2() {}; 3012void main3() {}; 3013]] 3014) 3015 3016command ( 'cd Xtest' ) 3017command ( 'tjump main' ) 3018eq (( '%s/Xtest/foo.c' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 3019sfname_eq ( './/foo.c' ) 3020 3021command ( 'bw' ) 3022fn . setenv ( 'DIR' , ( [[%s\Xtest]] ): format ( cwd )) 3023command ( 'tjump main2' ) 3024eq (( '%s/Xtest/foo.c' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 3025sfname_eq (( '%s/Xtest/foo.c' ): format ( expected_cwd )) 3026 3027command ( 'bw' ) 3028command ( 'tjump main3' ) 3029eq (( '%s/Xtest/foo.c' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 3030sfname_eq ( './foo.c' ) 3031end ) 3032 3033it ( 'normalizes quickfix targets' , function () 3034t . write_file ( 3035'Xtest/error' , 3036[[ 3037foo.c 3038.\Xtest\foo.c(3): error C2143: syntax error: missing ';' before 'return' 3039]] 3040) 3041local screen =Screen . new ( 40 , 10 ) 3042command ( 'cg Xtest/error' ) 3043command ( 'copen' ) 3044screen : expect ( { 3045any ='./Xtest/foo.c' , 3046} ) 3047feed ( '<CR>' ) 3048eq (( '%s/Xtest/foo.c' ): format ( expected_cwd ), api . nvim_buf_get_name ( 0 )) 3049sfname_eq ( './Xtest/foo.c' ) 3050end ) 3051 3052it ( 'preserves backslashes in URIs' , function () 3053local exe_path =fn . exepath ( 'cmd' ) 3054t . not_matches ( '/' , exe_path , true ) 3055command (( 'e term://%s' ): format ( exe_path )) 3056t . matches ( exe_path , api . nvim_buf_get_name ( 0 ), true ) 3057t . matches ( exe_path , api . nvim_eval_statusline ( '%f' , {} ). str , true ) 3058end ) 3059end )