robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
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 =t . describe , t . it , t . before_each , t . after_each 6local clear =n . clear 7local command =n . command 8local dedent =t . dedent 9local eq =t . eq 10local fn =n . fn 11local feed =n . feed 12local exec_capture =n . exec_capture 13local write_file =t . write_file 14local api =n . api 15 16describe ( 'jumplist' , function () 17local fname1 ='Xtest-functional-normal-jump' 18local fname2 =fname1 .. '2' 19before_each ( clear ) 20after_each ( function () 21os . remove ( fname1 ) 22os . remove ( fname2 ) 23end ) 24 25it ( 'does not add a new entry on startup' , function () 26eq ( '\n jump line col file/text\n>' , fn . execute ( 'jumps' )) 27end ) 28 29it ( 'does not require two <C-O> strokes to jump back' , function () 30write_file ( fname1 , 'first file contents' ) 31write_file ( fname2 , 'second file contents' ) 32 33command ( 'args ' .. fname1 .. ' ' .. fname2 ) 34local buf1 =fn . bufnr ( fname1 ) 35local buf2 =fn . bufnr ( fname2 ) 36 37command ( 'next' ) 38feed ( '<C-O>' ) 39eq ( buf1 , fn . bufnr ( '%' )) 40 41command ( 'first' ) 42command ( 'snext' ) 43feed ( '<C-O>' ) 44eq ( buf1 , fn . bufnr ( '%' )) 45feed ( '<C-I>' ) 46eq ( buf2 , fn . bufnr ( '%' )) 47feed ( '<C-O>' ) 48eq ( buf1 , fn . bufnr ( '%' )) 49 50command ( 'drop ' .. fname2 ) 51feed ( '<C-O>' ) 52eq ( buf1 , fn . bufnr ( '%' )) 53end ) 54 55it ( '<C-O> scrolls cursor halfway when switching buffer #25763' , function () 56write_file ( fname1 , ( 'foobar\n' ): rep ( 100 )) 57write_file ( fname2 , 'baz' ) 58 59local screen =Screen . new ( 12 , 25 ) 60command ( 'set number' ) 61command ( 'edit ' .. fname1 ) 62feed ( '35gg' ) 63command ( 'edit ' .. fname2 ) 64feed ( '<C-O>' ) 65screen : expect ( [[ 66{8: 24 }foobar | 67{8: 25 }foobar | 68{8: 26 }foobar | 69{8: 27 }foobar | 70{8: 28 }foobar | 71{8: 29 }foobar | 72{8: 30 }foobar | 73{8: 31 }foobar | 74{8: 32 }foobar | 75{8: 33 }foobar | 76{8: 34 }foobar | 77{8: 35 }^foobar | 78{8: 36 }foobar | 79{8: 37 }foobar | 80{8: 38 }foobar | 81{8: 39 }foobar | 82{8: 40 }foobar | 83{8: 41 }foobar | 84{8: 42 }foobar | 85{8: 43 }foobar | 86{8: 44 }foobar | 87{8: 45 }foobar | 88{8: 46 }foobar | 89{8: 47 }foobar | 90| 91]] ) 92end ) 93end ) 94 95describe ( "jumpoptions=stack behaves like 'tagstack'" , function () 96before_each ( function () 97clear () 98feed ( ':clearjumps<cr>' ) 99 100-- Add lines so that we have locations to jump to. 101for i =1 , 101 , 1 do 102feed ( 'iLine ' .. i .. '<cr><esc>' ) 103end 104 105-- Jump around to add some locations to the jump list. 106feed ( '0gg' ) 107feed ( '10gg' ) 108feed ( '20gg' ) 109feed ( '30gg' ) 110feed ( '40gg' ) 111feed ( '50gg' ) 112 113feed ( ':set jumpoptions=stack<cr>' ) 114end ) 115 116after_each ( function () 117feed ( 'set jumpoptions=' ) 118end ) 119 120it ( 'discards the tail when navigating from the middle' , function () 121feed ( '<C-O>' ) 122feed ( '<C-O>' ) 123 124eq ( 125'' 126.. ' jump line col file/text\n' 127.. ' 4 102 0 \n' 128.. ' 3 1 0 Line 1\n' 129.. ' 2 10 0 Line 10\n' 130.. ' 1 20 0 Line 20\n' 131.. '> 0 30 0 Line 30\n' 132.. ' 1 40 0 Line 40\n' 133.. ' 2 50 0 Line 50' , 134exec_capture ( 'jumps' ) 135) 136 137feed ( '90gg' ) 138 139eq ( 140'' 141.. ' jump line col file/text\n' 142.. ' 5 102 0 \n' 143.. ' 4 1 0 Line 1\n' 144.. ' 3 10 0 Line 10\n' 145.. ' 2 20 0 Line 20\n' 146.. ' 1 30 0 Line 30\n' 147.. '>' , 148exec_capture ( 'jumps' ) 149) 150end ) 151 152it ( 'does not add the same location twice adjacently' , function () 153feed ( '60gg' ) 154feed ( '60gg' ) 155 156eq ( 157'' 158.. ' jump line col file/text\n' 159.. ' 7 102 0 \n' 160.. ' 6 1 0 Line 1\n' 161.. ' 5 10 0 Line 10\n' 162.. ' 4 20 0 Line 20\n' 163.. ' 3 30 0 Line 30\n' 164.. ' 2 40 0 Line 40\n' 165.. ' 1 50 0 Line 50\n' 166.. '>' , 167exec_capture ( 'jumps' ) 168) 169end ) 170 171it ( 'does add the same location twice nonadjacently' , function () 172feed ( '10gg' ) 173feed ( '20gg' ) 174 175eq ( 176'' 177.. ' jump line col file/text\n' 178.. ' 8 102 0 \n' 179.. ' 7 1 0 Line 1\n' 180.. ' 6 10 0 Line 10\n' 181.. ' 5 20 0 Line 20\n' 182.. ' 4 30 0 Line 30\n' 183.. ' 3 40 0 Line 40\n' 184.. ' 2 50 0 Line 50\n' 185.. ' 1 10 0 Line 10\n' 186.. '>' , 187exec_capture ( 'jumps' ) 188) 189end ) 190end ) 191 192describe ( 'buffer deletion with jumpoptions+=clean' , function () 193local base_file ='Xtest-functional-buffer-deletion' 194local file1 =base_file .. '1' 195local file2 =base_file .. '2' 196local file3 =base_file .. '3' 197local base_content ='text' 198local content1 =base_content .. '1' 199local content2 =base_content .. '2' 200local content3 =base_content .. '3' 201 202local function format_jumplist ( input ) 203return dedent ( input ) 204: gsub ( '%{file1%}' , file1 ) 205: gsub ( '%{file2%}' , file2 ) 206: gsub ( '%{file3%}' , file3 ) 207: gsub ( '%{content1%}' , content1 ) 208: gsub ( '%{content2%}' , content2 ) 209: gsub ( '%{content3%}' , content3 ) 210end 211 212before_each ( function () 213clear () 214command ( 'clearjumps' ) 215 216write_file ( file1 , content1 , false , false ) 217write_file ( file2 , content2 , false , false ) 218write_file ( file3 , content3 , false , false ) 219 220command ( 'edit ' .. file1 ) 221command ( 'edit ' .. file2 ) 222command ( 'edit ' .. file3 ) 223end ) 224 225after_each ( function () 226os . remove ( file1 ) 227os . remove ( file2 ) 228os . remove ( file3 ) 229end ) 230 231it ( 'deletes jump list entries when the current buffer is deleted' , function () 232command ( 'edit ' .. file1 ) 233 234eq ( 235format_jumplist ( [[ 236jump line col file/text 2373 1 0 {content1} 2382 1 0 {file2} 2391 1 0 {file3} 240>]] ), 241exec_capture ( 'jumps' ) 242) 243 244command ( 'bwipeout' ) 245 246eq ( 247format_jumplist ( [[ 248jump line col file/text 2491 1 0 {file2} 250> 0 1 0 {content3}]] ), 251exec_capture ( 'jumps' ) 252) 253end ) 254 255it ( 'deletes jump list entries when another buffer is deleted' , function () 256eq ( 257format_jumplist ( [[ 258jump line col file/text 2592 1 0 {file1} 2601 1 0 {file2} 261>]] ), 262exec_capture ( 'jumps' ) 263) 264 265command ( 'bwipeout ' .. file2 ) 266 267eq ( 268format_jumplist ( [[ 269jump line col file/text 2701 1 0 {file1} 271>]] ), 272exec_capture ( 'jumps' ) 273) 274end ) 275 276it ( 'sets the correct jump index when the current buffer is deleted' , function () 277feed ( '<C-O>' ) 278 279eq ( 280format_jumplist ( [[ 281jump line col file/text 2821 1 0 {file1} 283> 0 1 0 {content2} 2841 1 0 {file3}]] ), 285exec_capture ( 'jumps' ) 286) 287 288command ( 'bw' ) 289 290eq ( 291format_jumplist ( [[ 292jump line col file/text 2931 1 0 {file1} 294> 0 1 0 {content3}]] ), 295exec_capture ( 'jumps' ) 296) 297end ) 298 299it ( 'sets the correct jump index when the another buffer is deleted' , function () 300feed ( '<C-O>' ) 301 302eq ( 303format_jumplist ( [[ 304jump line col file/text 3051 1 0 {file1} 306> 0 1 0 {content2} 3071 1 0 {file3}]] ), 308exec_capture ( 'jumps' ) 309) 310 311command ( 'bwipeout ' .. file1 ) 312 313eq ( 314format_jumplist ( [[ 315jump line col file/text 316> 0 1 0 {content2} 3171 1 0 {file3}]] ), 318exec_capture ( 'jumps' ) 319) 320end ) 321end ) 322 323describe ( 'buffer deletion with jumpoptions-=clean' , function () 324local base_file ='Xtest-functional-buffer-deletion' 325local file1 =base_file .. '1' 326local file2 =base_file .. '2' 327local base_content ='text' 328local content1 =base_content .. '1' 329local content2 =base_content .. '2' 330 331before_each ( function () 332clear () 333command ( 'clearjumps' ) 334command ( 'set jumpoptions-=clean' ) 335 336write_file ( file1 , content1 , false , false ) 337write_file ( file2 , content2 , false , false ) 338 339command ( 'edit ' .. file1 ) 340command ( 'edit ' .. file2 ) 341end ) 342 343after_each ( function () 344os . remove ( file1 ) 345os . remove ( file2 ) 346end ) 347 348it ( 'Ctrl-O reopens previous buffer with :bunload or :bdelete #28968' , function () 349eq ( file2 , fn . bufname ( '' )) 350command ( 'bunload' ) 351eq ( file1 , fn . bufname ( '' )) 352feed ( '<C-O>' ) 353eq ( file2 , fn . bufname ( '' )) 354command ( 'bdelete' ) 355eq ( file1 , fn . bufname ( '' )) 356feed ( '<C-O>' ) 357eq ( file2 , fn . bufname ( '' )) 358end ) 359end ) 360 361describe ( 'jumpoptions=view' , function () 362local file1 ='Xtestfile-functional-editor-jumps' 363local file2 ='Xtestfile-functional-editor-jumps-2' 364local function content () 365local c ={} 366for i =1 , 30 do 367c [ i ] =i .. ' line' 368end 369return table . concat ( c , '\n' ) 370end 371before_each ( function () 372clear () 373write_file ( file1 , content (), false , false ) 374write_file ( file2 , content (), false , false ) 375command ( 'set jumpoptions=view' ) 376end ) 377after_each ( function () 378os . remove ( file1 ) 379os . remove ( file2 ) 380end ) 381 382it ( 'restores the view' , function () 383local screen =Screen . new ( 12 , 8 ) 384command ( 'edit ' .. file1 ) 385feed ( '12Gztj' ) 386feed ( 'gg<C-o>' ) 387screen : expect ( [[ 38812 line | 389^13 line | 39014 line | 39115 line | 39216 line | 39317 line | 39418 line | 395| 396]] ) 397end ) 398 399it ( "restores the view with 'smoothscroll'" , function () 400local screen =Screen . new ( 12 , 8 ) 401command ( 'edit ' .. file1 ) 402command ( 'setlocal smoothscroll' ) 403command ( [[call setline(24, repeat('a', 55))]] ) 404feed ( '12Gzz' ) 405local s1 =[[ 4069 line | 40710 line | 40811 line | 409^12 line | 41013 line | 41114 line | 41215 line | 413| 414]] 415screen : expect ( s1 ) 416feed ( 'G' ) 417local s2 =[[ 418{1:<<<}aaaa | 41925 line | 42026 line | 42127 line | 42228 line | 42329 line | 424^30 line | 425| 426]] 427screen : expect ( s2 ) 428feed ( '<C-o>' ) 429screen : expect ( s1 ) 430feed ( '<C-i>' ) 431screen : expect ( s2 ) 432feed ( '<C-o>' ) 433screen : expect ( s1 ) 434command ( [[call setline(10, repeat('a', 55))]] ) 435feed ( 'zz' ) 436local s3 =[[ 437{1:<<<}aaaaaaaaa| 438aaaaaaa | 43911 line | 440^12 line | 44113 line | 44214 line | 44315 line | 444| 445]] 446screen : expect ( s3 ) 447feed ( 'G' ) 448screen : expect ( s2 ) 449feed ( '<C-o>' ) 450screen : expect ( s3 ) 451feed ( '<C-i>' ) 452screen : expect ( s2 ) 453command ( [[call setline(10, '10 line')]] ) 454feed ( '<C-o>' ) 455-- Topline should still be line 10, but skipcol should be reset. 456screen : expect ( [[ 45710 line | 45811 line | 459^12 line | 46013 line | 46114 line | 46215 line | 46316 line | 464| 465]] ) 466end ) 467 468it ( 'restores the view across files' , function () 469local screen =Screen . new ( 12 , 5 ) 470command ( 'args ' .. file1 .. ' ' .. file2 ) 471feed ( '12Gzt' ) 472command ( 'next' ) 473feed ( 'G' ) 474screen : expect ( [[ 47527 line | 47628 line | 47729 line | 478^30 line | 479| 480]] ) 481feed ( '<C-o><C-o>' ) 482screen : expect ( [[ 483^12 line | 48413 line | 48514 line | 48615 line | 487| 488]] ) 489end ) 490 491it ( 'restores the view across files with <C-^>/:bprevious/:bnext' , function () 492local screen =Screen . new ( 12 , 5 ) 493command ( 'args ' .. file1 .. ' ' .. file2 ) 494feed ( '12Gzt' ) 495local s1 =[[ 496^12 line | 49713 line | 49814 line | 49915 line | 500| 501]] 502screen : expect ( s1 ) 503command ( 'next' ) 504feed ( 'G' ) 505local s2 =[[ 50627 line | 50728 line | 50829 line | 509^30 line | 510| 511]] 512screen : expect ( s2 ) 513feed ( '<C-^>' ) 514screen : expect ( s1 ) 515feed ( '<C-^>' ) 516screen : expect ( s2 ) 517command ( 'bprevious' ) 518screen : expect ( s1 ) 519command ( 'bnext' ) 520screen : expect ( s2 ) 521end ) 522 523it ( "falls back to standard behavior when view can't be recovered" , function () 524local screen =Screen . new ( 12 , 8 ) 525command ( 'edit ' .. file1 ) 526feed ( '7GzbG' ) 527api . nvim_buf_set_lines ( 0 , 0 , 2 , true , {} ) 528-- Move to line 7, and set it as the last line visible on the view with zb, meaning to recover 529-- the view it needs to put the cursor 7 lines from the top line. Then go to the end of the 530-- file, delete 2 lines before line 7, meaning the jump/mark is moved 2 lines up to line 5. 531-- Therefore when trying to jump back to it it's not possible to set a 7 line offset from the 532-- mark position to the top line, since there's only 5 lines from the mark position to line 0. 533-- Therefore falls back to standard behavior which is centering the view/line. 534feed ( '<C-o>' ) 535screen : expect ( [[ 5364 line | 5375 line | 5386 line | 539^7 line | 5408 line | 5419 line | 54210 line | 543| 544]] ) 545end ) 546 547it ( 'falls back to standard behavior for a mark without a view' , function () 548local screen =Screen . new ( 12 , 8 ) 549command ( 'edit ' .. file1 ) 550feed ( '10ggzzvwy' ) 551screen : expect ( [[ 5527 line | 5538 line | 5549 line | 555^10 line | 55611 line | 55712 line | 55813 line | 559| 560]] ) 561feed ( '`]' ) 562screen : expect ( [[ 5637 line | 5648 line | 5659 line | 56610 ^line | 56711 line | 56812 line | 56913 line | 570| 571]] ) 572end ) 573 574describe ( 'tagstack popping' , function () 575local tags_file ='Xtestfile-functional-editor-jumps-tags' 576before_each ( function () 577write_file ( 578tags_file , 579'!_TAG_FILE_ENCODING\tutf-8\t//\n' 580.. ( '10\t%s\t2\n' ): format ( file1 ) 581.. ( '30\t%s\t20\n' ): format ( file2 ), 582false , 583false 584) 585command ( 'set tags=' .. tags_file ) 586end ) 587after_each ( function () 588os . remove ( tags_file ) 589end ) 590 591it ( 'restores the view' , function () 592local screen =Screen . new ( 12 , 6 ) 593command ( 'set laststatus=2 | set statusline=%f | edit ' .. file1 ) 594feed ( '10Gzb<C-]>30Gzt<C-]>' ) 595screen : expect ( [[ 59619 line | 597^20 line | 59821 line | 59922 line | 600{3:<tor-jumps-2}| 601| 602]] ) 603feed ( '<C-T>' ) 604screen : expect ( [[ 605^30 line | 606{1:~ }|*3 607{3:<ditor-jumps}| 608| 609]] ) 610feed ( '<C-T>' ) 611screen : expect ( [[ 6127 line | 6138 line | 6149 line | 615^10 line | 616{3:<ditor-jumps}| 617| 618]] ) 619 620local tagstack =' # TO tag FROM line in file/text\n' 621.. '> 1 1 10 10 \n' 622.. ' 2 1 30 30 ' 623eq ( tagstack , exec_capture ( 'tags' )) 624-- Un-pop via `:tag`; like `:tag 10` it should go to L2. (restoring cursor/view doesn't apply) 625-- However, after a `:pop`, it should restore the view from after the single `<C-E>` below. 626feed ( '<C-E>' ) 627command ( 'tag' ) 628screen : expect ( [[ 6291 line | 630^2 line | 6313 line | 6324 line | 633{3:<ditor-jumps}| 634| 635]] ) 636command ( 'pop' ) 637screen : expect ( [[ 6388 line | 6399 line | 640^10 line | 64111 line | 642{3:<ditor-jumps}| 643| 644]] ) 645eq ( tagstack , exec_capture ( 'tags' )) 646 647-- No view information associated with tags set via settagstack(). 648-- (specifically, replacing tag "10" shouldn't continue to use it's now unrelated view) 649fn . settagstack ( fn . win_getid (), { 650items ={ { from ={ fn . bufnr (), 11 , 1 , 0 , 1 } , tagname ='settagstack!!!' } } , 651} , 'r' ) 652tagstack =' # TO tag FROM line in file/text\n' 653.. ' 1 1 settagstack!!! 11 \n' 654.. '>' 655eq ( tagstack , exec_capture ( 'tags' )) 656feed ( 'G<C-T>' ) 657screen : expect ( [[ 65810 line | 659^11 line | 66012 line | 66113 line | 662{3:<ditor-jumps}| 663| 664]] ) 665end ) 666end ) 667end )