robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- ShaDa marks saving/reading support 2local t =require ( 'test.testutil' ) 3local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 4local pcall_err =t . pcall_err 5local n =require ( 'test.functional.testnvim' )() 6local t_shada =require ( 'test.functional.shada.testutil' ) 7 8local api , nvim_command , fn , eq =n . api , n . command , n . fn , t . eq 9local feed =n . feed 10local exec_capture =n . exec_capture 11local expect_exit =n . expect_exit 12local command =n . command 13 14local reset , clear =t_shada . reset , t_shada . clear 15 16local nvim_current_line =function () 17return api . nvim_win_get_cursor ( 0 )[ 1 ] 18end 19 20describe ( 'ShaDa support code' , function () 21local testfilename ='Xtestfile-functional-shada-marks' 22local testfilename_2 ='Xtestfile-functional-shada-marks-2' 23local non_existent_testfilename =testfilename .. '.nonexistent' 24before_each ( function () 25reset () 26os . remove ( non_existent_testfilename ) 27local fd =io . open ( testfilename , 'w' ) 28fd : write ( 'test\n' ) 29fd : write ( 'test2\n' ) 30fd : close () 31fd =io . open ( testfilename_2 , 'w' ) 32fd : write ( 'test3\n' ) 33fd : write ( 'test4\n' ) 34fd : close () 35end ) 36after_each ( function () 37clear () 38os . remove ( testfilename ) 39os . remove ( testfilename_2 ) 40end ) 41 42it ( 'is able to dump and read back global mark' , function () 43nvim_command ( 'edit ' .. testfilename ) 44nvim_command ( 'mark A' ) 45nvim_command ( '2' ) 46nvim_command ( 'kB' ) 47nvim_command ( 'wshada' ) 48reset () 49nvim_command ( 'rshada' ) 50nvim_command ( 'normal! `A' ) 51eq ( testfilename , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 52eq ( 1 , nvim_current_line ()) 53nvim_command ( 'normal! `B' ) 54eq ( 2 , nvim_current_line ()) 55end ) 56 57it ( 'can dump and read back numbered marks' , function () 58local function move ( cmd ) 59feed ( cmd ) 60nvim_command ( 'wshada' ) 61end 62nvim_command ( 'edit ' .. testfilename ) 63move ( 'l' ) 64move ( 'l' ) 65move ( 'j' ) 66move ( 'l' ) 67move ( 'l' ) 68nvim_command ( 'edit ' .. testfilename_2 ) 69move ( 'l' ) 70move ( 'l' ) 71move ( 'j' ) 72move ( 'l' ) 73move ( 'l' ) 74-- we have now populated marks 0 through 9 75nvim_command ( 'edit ' .. testfilename ) 76feed ( 'gg0' ) 77-- during shada save on exit, mark 0 will become the current position, 78-- 9 will be removed, and all other marks shifted 79expect_exit ( nvim_command , 'qall' ) 80reset () 81nvim_command ( 'edit ' .. testfilename ) 82nvim_command ( 'edit ' .. testfilename_2 ) 83local marklist =fn . getmarklist () 84for _ , mark in ipairs ( marklist ) do 85mark . file =fn . fnamemodify ( mark . file , ':t' ) 86end 87eq ( { 88{ 89file =testfilename , 90mark ="'0" , 91pos ={ 1 , 1 , 1 , 0 } , 92} , 93{ 94file =testfilename_2 , 95mark ="'1" , 96pos ={ 2 , 2 , 5 , 0 } , 97} , 98{ 99file =testfilename_2 , 100mark ="'2" , 101pos ={ 2 , 2 , 4 , 0 } , 102} , 103{ 104file =testfilename_2 , 105mark ="'3" , 106pos ={ 2 , 2 , 3 , 0 } , 107} , 108{ 109file =testfilename_2 , 110mark ="'4" , 111pos ={ 2 , 1 , 3 , 0 } , 112} , 113{ 114file =testfilename_2 , 115mark ="'5" , 116pos ={ 2 , 1 , 2 , 0 } , 117} , 118{ 119file =testfilename , 120mark ="'6" , 121pos ={ 1 , 2 , 5 , 0 } , 122} , 123{ 124file =testfilename , 125mark ="'7" , 126pos ={ 1 , 2 , 4 , 0 } , 127} , 128{ 129file =testfilename , 130mark ="'8" , 131pos ={ 1 , 2 , 3 , 0 } , 132} , 133{ 134file =testfilename , 135mark ="'9" , 136pos ={ 1 , 1 , 3 , 0 } , 137} , 138} , marklist ) 139end ) 140 141it ( 'does not dump global or numbered marks with `f0` in shada' , function () 142nvim_command ( 'set shada+=f0' ) 143nvim_command ( 'edit ' .. testfilename ) 144nvim_command ( 'mark A' ) 145nvim_command ( '2' ) 146nvim_command ( 'wshada' ) 147reset () 148nvim_command ( 'language C' ) 149eq ( 'Vim(normal):E20: Mark not set' , pcall_err ( command , 'normal! `A' )) 150eq ( 'Vim(normal):E20: Mark not set' , pcall_err ( command , 'normal! `0' )) 151end ) 152 153it ( "restores global and numbered marks even with `'0` and `f0` in shada" , function () 154nvim_command ( 'edit ' .. testfilename ) 155nvim_command ( 'mark A' ) 156nvim_command ( '2' ) 157nvim_command ( 'wshada' ) 158reset ( "set shada='0,f0" ) 159nvim_command ( 'language C' ) 160nvim_command ( 'normal! `A' ) 161eq ( testfilename , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 162eq ( 1 , nvim_current_line ()) 163nvim_command ( 'normal! `0' ) 164eq ( testfilename , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 165eq ( 2 , nvim_current_line ()) 166end ) 167 168it ( 'is able to dump and read back local mark' , function () 169nvim_command ( 'edit ' .. testfilename ) 170nvim_command ( 'mark a' ) 171nvim_command ( '2' ) 172nvim_command ( 'kb' ) 173expect_exit ( nvim_command , 'qall' ) 174reset () 175nvim_command ( 'edit ' .. testfilename ) 176nvim_command ( 'normal! `a' ) 177eq ( testfilename , fn . fnamemodify ( api . nvim_buf_get_name ( 0 ), ':t' )) 178eq ( 1 , nvim_current_line ()) 179nvim_command ( 'normal! `b' ) 180eq ( 2 , nvim_current_line ()) 181end ) 182 183it ( 'is able to dump and read back mark "' , function () 184nvim_command ( 'edit ' .. testfilename ) 185nvim_command ( '2' ) 186expect_exit ( nvim_command , 'qall' ) 187reset () 188nvim_command ( 'edit ' .. testfilename ) 189nvim_command ( 'normal! `"' ) 190eq ( 2 , nvim_current_line ()) 191end ) 192 193it ( "is able to dump, read back local mark and use `]'`" , function () 194nvim_command ( 'edit ' .. testfilename ) 195nvim_command ( '2' ) 196nvim_command ( 'mark a' ) 197expect_exit ( nvim_command , 'qall' ) 198reset ( { args ={ testfilename } } ) 199eq ( 1 , nvim_current_line ()) 200nvim_command ( "normal! ]'" ) 201eq ( 2 , nvim_current_line ()) 202end ) 203 204it ( 'is able to dump and read back mark " from a closed tab' , function () 205nvim_command ( 'edit ' .. testfilename ) 206nvim_command ( 'tabedit ' .. testfilename_2 ) 207nvim_command ( '2' ) 208nvim_command ( 'q!' ) 209expect_exit ( nvim_command , 'qall' ) 210reset () 211nvim_command ( 'edit ' .. testfilename_2 ) 212nvim_command ( 'normal! `"' ) 213eq ( 2 , nvim_current_line ()) 214end ) 215 216it ( 'is able to populate v:oldfiles' , function () 217nvim_command ( 'edit ' .. testfilename ) 218local tf_full =api . nvim_buf_get_name ( 0 ) 219nvim_command ( 'edit ' .. testfilename_2 ) 220local tf_full_2 =api . nvim_buf_get_name ( 0 ) 221expect_exit ( nvim_command , 'qall' ) 222reset () 223local oldfiles =api . nvim_get_vvar ( 'oldfiles' ) 224table . sort ( oldfiles ) 225eq ( 2 , # oldfiles ) 226eq ( testfilename , oldfiles [ 1 ]: sub ( -# testfilename )) 227eq ( testfilename_2 , oldfiles [ 2 ]: sub ( -# testfilename_2 )) 228eq ( tf_full , oldfiles [ 1 ]) 229eq ( tf_full_2 , oldfiles [ 2 ]) 230nvim_command ( 'rshada!' ) 231oldfiles =api . nvim_get_vvar ( 'oldfiles' ) 232table . sort ( oldfiles ) 233eq ( 2 , # oldfiles ) 234eq ( testfilename , oldfiles [ 1 ]: sub ( -# testfilename )) 235eq ( testfilename_2 , oldfiles [ 2 ]: sub ( -# testfilename_2 )) 236eq ( tf_full , oldfiles [ 1 ]) 237eq ( tf_full_2 , oldfiles [ 2 ]) 238end ) 239 240it ( 'is able to dump and restore jump list' , function () 241nvim_command ( 'edit ' .. testfilename_2 ) 242nvim_command ( 'normal! G' ) 243nvim_command ( 'normal! gg' ) 244nvim_command ( 'edit ' .. testfilename ) 245nvim_command ( 'normal! G' ) 246nvim_command ( 'normal! gg' ) 247nvim_command ( 'enew' ) 248nvim_command ( 'normal! gg' ) 249local saved =exec_capture ( 'jumps' ) 250expect_exit ( nvim_command , 'qall' ) 251reset () 252eq ( saved , exec_capture ( 'jumps' )) 253end ) 254 255it ( "does not dump jumplist if `'0` in shada" , function () 256local empty_jumps =exec_capture ( 'jumps' ) 257nvim_command ( "set shada='0" ) 258nvim_command ( 'edit ' .. testfilename_2 ) 259nvim_command ( 'normal! G' ) 260nvim_command ( 'normal! gg' ) 261nvim_command ( 'edit ' .. testfilename ) 262nvim_command ( 'normal! G' ) 263nvim_command ( 'normal! gg' ) 264nvim_command ( 'enew' ) 265nvim_command ( 'normal! gg' ) 266expect_exit ( nvim_command , 'qall' ) 267reset () 268eq ( empty_jumps , exec_capture ( 'jumps' )) 269end ) 270 271it ( "does read back jumplist even with `'0` in shada" , function () 272nvim_command ( 'edit ' .. testfilename_2 ) 273nvim_command ( 'normal! G' ) 274nvim_command ( 'normal! gg' ) 275nvim_command ( 'edit ' .. testfilename ) 276nvim_command ( 'normal! G' ) 277nvim_command ( 'normal! gg' ) 278nvim_command ( 'enew' ) 279nvim_command ( 'normal! gg' ) 280local saved =exec_capture ( 'jumps' ) 281expect_exit ( nvim_command , 'qall' ) 282reset ( "set shada='0" ) 283eq ( saved , exec_capture ( 'jumps' )) 284end ) 285 286it ( 'when dumping jump list also dumps current position' , function () 287nvim_command ( 'edit ' .. testfilename ) 288nvim_command ( 'normal! G' ) 289nvim_command ( 'split ' .. testfilename_2 ) 290nvim_command ( 'normal! G' ) 291nvim_command ( 'wshada' ) 292nvim_command ( 'quit' ) 293nvim_command ( 'rshada' ) 294nvim_command ( 'normal! \15' ) -- <C-o> 295eq ( testfilename_2 , fn . bufname ( '%' )) 296eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 297end ) 298 299it ( 'is able to dump and restore jump list with different times' , function () 300nvim_command ( 'edit ' .. testfilename_2 ) 301nvim_command ( 'sleep 10m' ) 302nvim_command ( 'normal! G' ) 303nvim_command ( 'sleep 10m' ) 304nvim_command ( 'normal! gg' ) 305nvim_command ( 'sleep 10m' ) 306nvim_command ( 'edit ' .. testfilename ) 307nvim_command ( 'sleep 10m' ) 308nvim_command ( 'normal! G' ) 309nvim_command ( 'sleep 10m' ) 310nvim_command ( 'normal! gg' ) 311expect_exit ( nvim_command , 'qall' ) 312reset () 313nvim_command ( 'redraw' ) 314nvim_command ( 'edit ' .. testfilename ) 315eq ( testfilename , fn . bufname ( '%' )) 316eq ( 1 , nvim_current_line ()) 317nvim_command ( 'execute "normal! \\<C-o>"' ) 318eq ( testfilename , fn . bufname ( '%' )) 319eq ( 2 , nvim_current_line ()) 320nvim_command ( 'execute "normal! \\<C-o>"' ) 321eq ( testfilename_2 , fn . bufname ( '%' )) 322eq ( 1 , nvim_current_line ()) 323nvim_command ( 'execute "normal! \\<C-o>"' ) 324eq ( testfilename_2 , fn . bufname ( '%' )) 325eq ( 2 , nvim_current_line ()) 326nvim_command ( 'execute "normal! \\<C-o>"' ) 327eq ( testfilename_2 , fn . bufname ( '%' )) 328eq ( 2 , nvim_current_line ()) 329end ) 330 331it ( 'is able to dump and restore change list' , function () 332nvim_command ( 'edit ' .. testfilename ) 333nvim_command ( 'normal! Gra' ) 334nvim_command ( 'normal! ggrb' ) 335expect_exit ( nvim_command , 'qall!' ) 336reset () 337nvim_command ( 'edit ' .. testfilename ) 338nvim_command ( 'normal! Gg;' ) 339-- Note: without “sync” “commands” test has good changes to fail for unknown 340-- reason (in first eq expected 1 is compared with 2). Any command inserted 341-- causes this to work properly. 342nvim_command ( '" sync' ) 343eq ( 1 , nvim_current_line ()) 344nvim_command ( 'normal! g;' ) 345nvim_command ( '" sync 2' ) 346eq ( 2 , nvim_current_line ()) 347end ) 348 349-- -c temporary sets lnum to zero to make `+/pat` work, so calling setpcmark() 350-- during -c used to add item with zero lnum to jump list. 351it ( 'does not create incorrect file for non-existent buffers when writing from -c' , function () 352local p =n . spawn_wait { 353args_rm ={ 354'-i' , 355'--embed' , -- no --embed 356} , 357args ={ 358'-i' , 359api . nvim_get_var ( 'tmpname' ), -- Use same shada file as parent. 360'--cmd' , 361'silent edit ' .. non_existent_testfilename , 362'-c' , 363'qall' , 364} , 365} 366eq ( '' , p : output ()) 367eq ( 0 , p . status ) 368command ( 'rshada' ) 369end ) 370 371it ( 'does not create incorrect file for non-existent buffers opened from -c' , function () 372local p =n . spawn_wait { 373args_rm ={ 374'-i' , 375'--embed' , -- no --embed 376} , 377args ={ 378'-i' , 379api . nvim_get_var ( 'tmpname' ), -- Use same shada file as parent. 380'-c' , 381'silent edit ' .. non_existent_testfilename , 382'-c' , 383'autocmd VimEnter * qall' , 384} , 385} 386eq ( '' , p : output ()) 387eq ( 0 , p . status ) 388command ( 'rshada' ) 389end ) 390 391it ( 'updates deleted marks with :delmarks' , function () 392nvim_command ( 'edit ' .. testfilename ) 393 394nvim_command ( 'mark A' ) 395nvim_command ( 'mark a' ) 396-- create a change to set the '.' mark, 397-- since it can't be set via :mark 398feed ( 'ggifoobar<esc>' ) 399nvim_command ( 'wshada' ) 400 401reset () 402nvim_command ( 'edit ' .. testfilename ) 403nvim_command ( 'normal! `A`a`.' ) 404nvim_command ( 'delmarks A a .' ) 405nvim_command ( 'wshada' ) 406 407reset () 408nvim_command ( 'edit ' .. testfilename ) 409eq ( 'Vim(normal):E20: Mark not set' , pcall_err ( command , 'normal! `A' )) 410eq ( 'Vim(normal):E20: Mark not set' , pcall_err ( command , 'normal! `a' )) 411eq ( 'Vim(normal):E20: Mark not set' , pcall_err ( command , 'normal! `.' )) 412end ) 413 414it ( 'updates deleted marks with :delmarks!' , function () 415nvim_command ( 'edit ' .. testfilename ) 416 417nvim_command ( 'mark A' ) 418nvim_command ( 'mark a' ) 419feed ( 'ggifoobar<esc>' ) 420nvim_command ( 'wshada' ) 421 422reset () 423nvim_command ( 'edit ' .. testfilename ) 424nvim_command ( 'normal! `A`a`.' ) 425nvim_command ( 'delmarks!' ) 426nvim_command ( 'wshada' ) 427 428reset () 429nvim_command ( 'edit ' .. testfilename ) 430eq ( 'Vim(normal):E20: Mark not set' , pcall_err ( command , 'normal! `a' )) 431eq ( 'Vim(normal):E20: Mark not set' , pcall_err ( command , 'normal! `.' )) 432-- Make sure that uppercase marks aren't deleted. 433nvim_command ( 'normal! `A' ) 434end ) 435 436it ( 'preserves marks after :bdelete #35770' , function () 437nvim_command ( 'edit foo' ) 438nvim_command ( 'mark X' ) 439nvim_command ( 'bdelete' ) 440nvim_command ( 'wshada' ) 441 442reset () 443local mX =api . nvim_get_mark ( 'X' , {} ) 444t . matches ( vim . pesc ( 'foo' ), mX [ 4 ]) 445 446nvim_command ( 'delmarks X' ) 447nvim_command ( 'edit foo' ) 448nvim_command ( 'mark X' ) 449nvim_command ( 'edit bar' ) 450nvim_command ( 'mark Y' ) 451nvim_command ( 'bufdo bdelete' ) 452nvim_command ( 'edit foo' ) 453nvim_command ( 'wshada' ) 454 455reset () 456mX =api . nvim_get_mark ( 'X' , {} ) 457local mY =api . nvim_get_mark ( 'Y' , {} ) 458t . matches ( vim . pesc ( 'foo' ), mX [ 4 ]) 459t . matches ( vim . pesc ( 'bar' ), mY [ 4 ]) 460end ) 461end )