robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
37c670e682
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 , finally = 6t . describe , t . it , t . before_each , t . after_each , t . finally 7local api =n . api 8local clear =n . clear 9local command =n . command 10local fn =n . fn 11local eq =t . eq 12local feed =n . feed 13local write_file =t . write_file 14local pcall_err =t . pcall_err 15local cursor =function () 16return n . api . nvim_win_get_cursor ( 0 ) 17end 18 19describe ( 'named marks' , function () 20local file1 ='Xtestfile-functional-editor-marks' 21local file2 ='Xtestfile-functional-editor-marks-2' 22before_each ( function () 23clear () 24write_file ( file1 , '1test1\n1test2\n1test3\n1test4' , false , false ) 25write_file ( file2 , '2test1\n2test2\n2test3\n2test4' , false , false ) 26end ) 27after_each ( function () 28os . remove ( file1 ) 29os . remove ( file2 ) 30end ) 31 32it ( 'can be set' , function () 33command ( 'edit ' .. file1 ) 34command ( 'mark a' ) 35eq ( { 1 , 0 } , api . nvim_buf_get_mark ( 0 , 'a' )) 36feed ( 'jmb' ) 37eq ( { 2 , 0 } , api . nvim_buf_get_mark ( 0 , 'b' )) 38feed ( 'jmB' ) 39eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , 'B' )) 40command ( '4kc' ) 41eq ( { 4 , 0 } , api . nvim_buf_get_mark ( 0 , 'c' )) 42end ) 43 44it ( 'moved after a change follow their text through undo #5754' , function () 45command ( 'edit ' .. file1 ) 46command ( '1mark d' ) 47command ( '$' ) 48feed ( 'dw' ) 49command ( '2mark d' ) 50eq ( { 2 , 0 } , api . nvim_buf_get_mark ( 0 , 'd' )) 51command ( 'undo' ) 52eq ( { 2 , 0 } , api . nvim_buf_get_mark ( 0 , 'd' )) 53command ( 'redo' ) 54eq ( { 2 , 0 } , api . nvim_buf_get_mark ( 0 , 'd' )) 55 56-- A mark on a line the change deletes is cleared by the change; undo restores it. 57api . nvim_buf_set_mark ( 0 , 'e' , 2 , 2 , {} ) 58command ( '2delete' ) 59eq ( { 0 , 2 } , api . nvim_buf_get_mark ( 0 , 'e' )) 60command ( 'undo' ) 61eq ( { 2 , 2 } , api . nvim_buf_get_mark ( 0 , 'e' )) 62eq ( '1test2' , fn . getline ( 2 )) 63 64-- A mark moved after a line-count change shifts with the text it was moved to. 65command ( '2delete' ) 66eq ( '1test3' , fn . getline ( 2 )) 67command ( '2mark f' ) 68command ( 'undo' ) 69eq ( { 3 , 0 } , api . nvim_buf_get_mark ( 0 , 'f' )) 70eq ( '1test3' , fn . getline ( 3 )) 71end ) 72 73it ( 'errors when set out of range with :mark' , function () 74command ( 'edit ' .. file1 ) 75local err =pcall_err ( n . exec_capture , '1000mark x' ) 76eq ( 'nvim_exec2(), line 1: Vim(mark):E16: Invalid range: 1000mark x' , err ) 77end ) 78 79it ( 'errors when set out of range with :k' , function () 80command ( 'edit ' .. file1 ) 81local err =pcall_err ( n . exec_capture , '1000kx' ) 82eq ( 'nvim_exec2(), line 1: Vim(k):E16: Invalid range: 1000kx' , err ) 83end ) 84 85it ( 'errors on unknown mark name with :mark' , function () 86command ( 'edit ' .. file1 ) 87local err =pcall_err ( n . exec_capture , 'mark #' ) 88eq ( 89'nvim_exec2(), line 1: Vim(mark):E191: Argument must be a letter or forward/backward quote' , 90err 91) 92end ) 93 94it ( "errors on unknown mark name with '" , function () 95command ( 'edit ' .. file1 ) 96local err =pcall_err ( n . exec_capture , "normal! '#" ) 97eq ( 'nvim_exec2(), line 1: Vim(normal):E78: Unknown mark' , err ) 98end ) 99 100it ( 'errors on unknown mark name with `' , function () 101command ( 'edit ' .. file1 ) 102local err =pcall_err ( n . exec_capture , 'normal! `#' ) 103eq ( 'nvim_exec2(), line 1: Vim(normal):E78: Unknown mark' , err ) 104end ) 105 106it ( "errors when moving to a mark that is not set with '" , function () 107command ( 'edit ' .. file1 ) 108local err =pcall_err ( n . exec_capture , "normal! 'z" ) 109eq ( 'nvim_exec2(), line 1: Vim(normal):E20: Mark not set' , err ) 110err =pcall_err ( n . exec_capture , "normal! '." ) 111eq ( 'nvim_exec2(), line 1: Vim(normal):E20: Mark not set' , err ) 112end ) 113 114it ( 'errors when moving to a mark that is not set with `' , function () 115command ( 'edit ' .. file1 ) 116local err =pcall_err ( n . exec_capture , 'normal! `z' ) 117eq ( 'nvim_exec2(), line 1: Vim(normal):E20: Mark not set' , err ) 118err =pcall_err ( n . exec_capture , 'normal! `>' ) 119eq ( 'nvim_exec2(), line 1: Vim(normal):E20: Mark not set' , err ) 120end ) 121 122it ( "errors when moving to a global mark that is not set with '" , function () 123command ( 'edit ' .. file1 ) 124local err =pcall_err ( n . exec_capture , "normal! 'Z" ) 125eq ( 'nvim_exec2(), line 1: Vim(normal):E20: Mark not set' , err ) 126end ) 127 128it ( 'errors when moving to a global mark that is not set with `' , function () 129command ( 'edit ' .. file1 ) 130local err =pcall_err ( n . exec_capture , 'normal! `Z' ) 131eq ( 'nvim_exec2(), line 1: Vim(normal):E20: Mark not set' , err ) 132end ) 133 134it ( "can move to them using '" , function () 135command ( 'args ' .. file1 .. ' ' .. file2 ) 136feed ( 'j' ) 137feed ( 'ma' ) 138feed ( "G'a" ) 139eq ( { 2 , 0 } , cursor ()) 140feed ( 'mA' ) 141command ( 'next' ) 142feed ( "'A" ) 143eq ( 1 , api . nvim_get_current_buf ()) 144eq ( { 2 , 0 } , cursor ()) 145end ) 146 147it ( 'can move to them using `' , function () 148command ( 'args ' .. file1 .. ' ' .. file2 ) 149feed ( 'jll' ) 150feed ( 'ma' ) 151feed ( 'G`a' ) 152eq ( { 2 , 2 } , cursor ()) 153feed ( 'mA' ) 154command ( 'next' ) 155feed ( '`A' ) 156eq ( 1 , api . nvim_get_current_buf ()) 157eq ( { 2 , 2 } , cursor ()) 158end ) 159 160it ( "can move to them using g'" , function () 161command ( 'args ' .. file1 .. ' ' .. file2 ) 162feed ( 'jll' ) 163feed ( 'ma' ) 164feed ( "Gg'a" ) 165eq ( { 2 , 0 } , cursor ()) 166feed ( 'mA' ) 167command ( 'next' ) 168feed ( "g'A" ) 169eq ( 1 , api . nvim_get_current_buf ()) 170eq ( { 2 , 0 } , cursor ()) 171end ) 172 173it ( 'can move to them using g`' , function () 174command ( 'args ' .. file1 .. ' ' .. file2 ) 175feed ( 'jll' ) 176feed ( 'ma' ) 177feed ( 'Gg`a' ) 178eq ( { 2 , 2 } , cursor ()) 179feed ( 'mA' ) 180command ( 'next' ) 181feed ( 'g`A' ) 182eq ( 1 , api . nvim_get_current_buf ()) 183eq ( { 2 , 2 } , cursor ()) 184end ) 185 186it ( "can move to them using :'" , function () 187command ( 'args ' .. file1 .. ' ' .. file2 ) 188feed ( 'j' ) 189feed ( 'ma' ) 190feed ( 'G' ) 191command ( "'a" ) 192eq ( { 2 , 0 } , cursor ()) 193feed ( 'mA' ) 194command ( 'next' ) 195command ( "'A" ) 196eq ( 1 , api . nvim_get_current_buf ()) 197eq ( { 2 , 0 } , cursor ()) 198end ) 199 200it ( "errors when it can't find the buffer" , function () 201command ( 'args ' .. file1 .. ' ' .. file2 ) 202feed ( 'mA' ) 203command ( 'next' ) 204command ( 'bw! ' .. file1 ) 205local err =pcall_err ( n . exec_capture , "normal! 'A" ) 206eq ( 'nvim_exec2(), line 1: Vim(normal):E92: Buffer 1 not found' , err ) 207os . remove ( file1 ) 208end ) 209 210it ( 'errors when using a mark in another buffer in command range' , function () 211feed ( 'ifoo<Esc>mA' ) 212command ( 'enew' ) 213feed ( 'ibar<Esc>' ) 214eq ( "Vim(print):E20: Mark not set: 'Aprint" , pcall_err ( command , [['Aprint]] )) 215end ) 216 217it ( "leave a context mark when moving with '" , function () 218command ( 'edit ' .. file1 ) 219feed ( 'llmamA' ) 220feed ( '10j0' ) -- first col, last line 221local pos =cursor () 222feed ( "'a" ) 223feed ( '<C-o>' ) 224eq ( pos , cursor ()) 225feed ( "'A" ) 226feed ( '<C-o>' ) 227eq ( pos , cursor ()) 228end ) 229 230it ( 'leave a context mark when moving with `' , function () 231command ( 'edit ' .. file1 ) 232feed ( 'llmamA' ) 233feed ( '10j0' ) -- first col, last line 234local pos =cursor () 235feed ( '`a' ) 236feed ( '<C-o>' ) 237eq ( pos , cursor ()) 238feed ( '`A' ) 239feed ( '<C-o>' ) 240eq ( pos , cursor ()) 241end ) 242 243it ( "leave a context mark when the mark changes buffer with g'" , function () 244command ( 'args ' .. file1 .. ' ' .. file2 ) 245local pos 246feed ( 'GmA' ) 247command ( 'next' ) 248pos =cursor () 249command ( 'clearjumps' ) 250feed ( "g'A" ) -- since the mark is in another buffer, it leaves a context mark 251feed ( '<C-o>' ) 252eq ( pos , cursor ()) 253end ) 254 255it ( 'leave a context mark when the mark changes buffer with g`' , function () 256command ( 'args ' .. file1 .. ' ' .. file2 ) 257local pos 258feed ( 'GmA' ) 259command ( 'next' ) 260pos =cursor () 261command ( 'clearjumps' ) 262feed ( 'g`A' ) -- since the mark is in another buffer, it leaves a context mark 263feed ( '<C-o>' ) 264eq ( pos , cursor ()) 265end ) 266 267it ( "do not leave a context mark when moving with g'" , function () 268command ( 'edit ' .. file1 ) 269local pos 270feed ( 'ma' ) 271pos =cursor () -- Mark pos 272feed ( '10j0' ) -- first col, last line 273feed ( "g'a" ) 274feed ( '<C-o>' ) -- should do nothing 275eq ( pos , cursor ()) 276feed ( 'mA' ) 277pos =cursor () -- Mark pos 278feed ( '10j0' ) -- first col, last line 279feed ( "g'a" ) 280feed ( '<C-o>' ) -- should do nothing 281eq ( pos , cursor ()) 282end ) 283 284it ( 'do not leave a context mark when moving with g`' , function () 285command ( 'edit ' .. file1 ) 286local pos 287feed ( 'ma' ) 288pos =cursor () -- Mark pos 289feed ( '10j0' ) -- first col, last line 290feed ( 'g`a' ) 291feed ( '<C-o>' ) -- should do nothing 292eq ( pos , cursor ()) 293feed ( 'mA' ) 294pos =cursor () -- Mark pos 295feed ( '10j0' ) -- first col, last line 296feed ( "g'a" ) 297feed ( '<C-o>' ) -- should do nothing 298eq ( pos , cursor ()) 299end ) 300 301it ( 'open folds when moving to them' , function () 302command ( 'edit ' .. file1 ) 303feed ( 'jzfG' ) -- Fold from the second line to the end 304command ( '3mark a' ) 305feed ( 'G' ) -- On top of the fold 306assert ( fn . foldclosed ( '.' ) ~= - 1 ) -- folded 307feed ( "'a" ) 308eq ( - 1 , fn . foldclosed ( '.' )) 309 310feed ( 'zc' ) 311assert ( fn . foldclosed ( '.' ) ~= - 1 ) -- folded 312-- TODO: remove this workaround after fixing #15873 313feed ( 'k`a' ) 314eq ( - 1 , fn . foldclosed ( '.' )) 315 316feed ( 'zc' ) 317assert ( fn . foldclosed ( '.' ) ~= - 1 ) -- folded 318feed ( "kg'a" ) 319eq ( - 1 , fn . foldclosed ( '.' )) 320 321feed ( 'zc' ) 322assert ( fn . foldclosed ( '.' ) ~= - 1 ) -- folded 323feed ( 'kg`a' ) 324eq ( - 1 , fn . foldclosed ( '.' )) 325end ) 326 327it ( "do not open folds when moving to them doesn't move the cursor" , function () 328command ( 'edit ' .. file1 ) 329feed ( 'jzfG' ) -- Fold from the second line to the end 330assert ( fn . foldclosed ( '.' ) == 2 ) -- folded 331feed ( 'ma' ) 332feed ( "'a" ) 333feed ( '`a' ) 334feed ( "g'a" ) 335feed ( 'g`a' ) 336-- should still be folded 337eq ( 2 , fn . foldclosed ( '.' )) 338end ) 339 340it ( "getting '{ '} '( ') does not move cursor" , function () 341api . nvim_buf_set_lines ( 0 , 0 , 0 , true , { 'aaaaa' , 'bbbbb' , 'ccccc' , 'ddddd' , 'eeeee' } ) 342api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 343fn . getpos ( "'{" ) 344eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 345fn . getpos ( "'}" ) 346eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 347fn . getpos ( "'(" ) 348eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 349fn . getpos ( "')" ) 350eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 351end ) 352 353it ( 'in command range does not move cursor #19248' , function () 354api . nvim_create_user_command ( 'Test' , ':' , { range =true } ) 355api . nvim_buf_set_lines ( 0 , 0 , 0 , true , { 'aaaaa' , 'bbbbb' , 'ccccc' , 'ddddd' , 'eeeee' } ) 356api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 357command ( [['{,'}Test]] ) 358eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 359end ) 360end ) 361 362describe ( 'named marks view' , function () 363local file1 ='Xtestfile-functional-editor-marks' 364local file2 ='Xtestfile-functional-editor-marks-2' 365local function content () 366local c ={} 367for i =1 , 30 do 368c [ i ] =i .. ' line' 369end 370return table . concat ( c , '\n' ) 371end 372before_each ( function () 373clear () 374write_file ( file1 , content (), false , false ) 375write_file ( file2 , content (), false , false ) 376command ( 'set jumpoptions+=view' ) 377end ) 378after_each ( function () 379os . remove ( file1 ) 380os . remove ( file2 ) 381end ) 382 383it ( 'is restored in normal mode but not op-pending mode' , function () 384local screen =Screen . new ( 12 , 8 ) 385command ( 'edit ' .. file1 ) 386feed ( '<C-e>jWma' ) 387feed ( "G'a" ) 388local expected =[[ 3892 line | 390^3 line | 3914 line | 3925 line | 3936 line | 3947 line | 3958 line | 396| 397]] 398screen : expect ( { grid =expected } ) 399feed ( 'G`a' ) 400screen : expect ( [[ 4012 line | 4023 ^line | 4034 line | 4045 line | 4056 line | 4067 line | 4078 line | 408| 409]] ) 410-- not in op-pending mode #20886 411feed ( 'ggj=`a' ) 412screen : expect ( [[ 4131 line | 414^2 line | 4153 line | 4164 line | 4175 line | 4186 line | 4197 line | 420| 421]] ) 422end ) 423 424it ( 'is restored across files' , function () 425local screen =Screen . new ( 12 , 5 ) 426command ( 'args ' .. file1 .. ' ' .. file2 ) 427feed ( '<C-e>mA' ) 428local mark_view =[[ 429^2 line | 4303 line | 4314 line | 4325 line | 433| 434]] 435screen : expect ( mark_view ) 436command ( 'next' ) 437screen : expect ( [[ 438^1 line | 4392 line | 4403 line | 4414 line | 442| 443]] ) 444feed ( "'A" ) 445screen : expect ( mark_view ) 446end ) 447 448it ( "fallback to standard behavior when view can't be recovered" , function () 449local screen =Screen . new ( 12 , 10 ) 450command ( 'edit ' .. file1 ) 451feed ( '7GzbmaG' ) -- Seven lines from the top 452command ( 'new' ) -- Screen size for window is now half the height can't be restored 453feed ( "<C-w>p'a" ) 454screen : expect ( [[ 455| 456{1:~ }|*3 457{2:[No Name] }| 4586 line | 459^7 line | 4608 line | 461{3:<itor-marks }| 462| 463]] ) 464end ) 465 466it ( 'fallback to standard behavior when mark is loaded from shada' , function () 467local screen =Screen . new ( 12 , 6 ) 468command ( 'edit ' .. file1 ) 469feed ( 'G' ) 470feed ( 'mA' ) 471screen : expect ( [[ 47226 line | 47327 line | 47428 line | 47529 line | 476^30 line | 477| 478]] ) 479command ( 'set shadafile=Xtestfile-functional-editor-marks-shada' ) 480finally ( function () 481command ( 'set shadafile=NONE' ) 482os . remove ( 'Xtestfile-functional-editor-marks-shada' ) 483end ) 484command ( 'wshada!' ) 485command ( 'bwipe!' ) 486screen : expect ( [[ 487^ | 488{1:~ }|*4 489| 490]] ) 491command ( 'rshada!' ) 492command ( 'edit ' .. file1 ) 493feed ( '`"' ) 494screen : expect ( [[ 49526 line | 49627 line | 49728 line | 49829 line | 499^30 line | 500| 501]] ) 502feed ( '`A' ) 503screen : expect_unchanged () 504end ) 505end )