robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each , setup = 5t . describe , t . it , t . before_each , t . after_each , t . setup 6local clear =n . clear 7local insert =n . insert 8local exec =n . exec 9local feed =n . feed 10local expect =n . expect 11local command =n . command 12local fn =n . fn 13local eq =t . eq 14local neq =t . neq 15 16describe ( 'Folding' , function () 17local tempfname ='Xtest-fold.txt' 18 19setup ( clear ) 20before_each ( function () 21command ( 'bwipe! | new' ) 22end ) 23after_each ( function () 24os . remove ( tempfname ) 25end ) 26 27it ( 'manual folding adjusts with filter' , function () 28insert ( [[ 291 302 313 324 335 346 357 368 379 3810 3911 4012 4113 4214 4315 4416 4517 4618 4719 4820]] ) 49command ( '4,$fold' ) 50command ( '%foldopen' ) 51command ( '10,$fold' ) 52command ( '%foldopen' ) 53command ( '1,8! cat' ) 54feed ( '5ggzdzMGdd' ) 55expect ( [[ 561 572 583 594 605 616 627 638 649]] ) 65end ) 66 67describe ( 'adjusting folds after :move' , function () 68local function manually_fold_indent () 69-- setting foldmethod twice is a trick to get vim to set the folds for me 70command ( 'setlocal foldmethod=indent' ) 71command ( 'setlocal foldmethod=manual' ) 72-- Ensure that all folds will get closed (makes it easier to test the 73-- length of folds). 74command ( 'setlocal foldminlines=0' ) 75-- Start with all folds open (so :move ranges aren't affected by closed 76-- folds). 77command ( '%foldopen!' ) 78end 79 80local function get_folds () 81local rettab ={} 82for i =1 , fn . line ( '$' ) do 83table . insert ( rettab , fn . foldlevel ( i )) 84end 85return rettab 86end 87 88local function test_move_indent ( insert_string , move_command ) 89-- This test is easy because we just need to ensure that the resulting 90-- fold is the same as calculated when creating folds from scratch. 91insert ( insert_string ) 92command ( move_command ) 93local after_move_folds =get_folds () 94-- Doesn't change anything, but does call foldUpdateAll() 95command ( 'setlocal foldminlines=0' ) 96eq ( after_move_folds , get_folds ()) 97-- Set up the buffer with insert_string for the manual fold testing. 98command ( 'enew!' ) 99insert ( insert_string ) 100manually_fold_indent () 101command ( move_command ) 102end 103 104it ( 'neither closes nor corrupts folds' , function () 105test_move_indent ( 106[[ 107a 108a 109a 110a 111a 112a 113a 114a 115a 116a 117a 118a 119a 120a 121a 122a 123a 124a]] , 125'7,12m0' 126) 127expect ( [[ 128a 129a 130a 131a 132a 133a 134a 135a 136a 137a 138a 139a 140a 141a 142a 143a 144a 145a]] ) 146-- lines are not closed, folds are correct 147for i =1 , fn . line ( '$' ) do 148eq ( - 1 , fn . foldclosed ( i )) 149if i == 1 or i == 7 or i == 13 then 150eq ( 0 , fn . foldlevel ( i )) 151elseif i == 4 then 152eq ( 2 , fn . foldlevel ( i )) 153else 154eq ( 1 , fn . foldlevel ( i )) 155end 156end 157-- folds are not corrupted 158feed ( 'zM' ) 159eq ( 6 , fn . foldclosedend ( 2 )) 160eq ( 12 , fn . foldclosedend ( 8 )) 161eq ( 18 , fn . foldclosedend ( 14 )) 162end ) 163 164it ( "doesn't split a fold when the move is within it" , function () 165test_move_indent ( 166[[ 167a 168a 169a 170a 171a 172a 173a 174a 175a 176a]] , 177'5m6' 178) 179eq ( { 0 , 1 , 1 , 2 , 2 , 2 , 2 , 1 , 1 , 0 } , get_folds ()) 180end ) 181 182it ( 'truncates folds that end in the moved range' , function () 183test_move_indent ( 184[[ 185a 186a 187a 188a 189a 190a 191a]] , 192'4,5m6' 193) 194eq ( { 0 , 1 , 2 , 0 , 0 , 0 , 0 } , get_folds ()) 195end ) 196 197it ( 'moves folds that start between moved range and destination' , function () 198test_move_indent ( 199[[ 200a 201a 202a 203a 204a 205a 206a 207a 208a 209a 210a 211a 212a]] , 213'3,4m$' 214) 215eq ( { 0 , 1 , 1 , 0 , 0 , 1 , 2 , 1 , 0 , 0 , 1 , 0 , 0 } , get_folds ()) 216end ) 217 218it ( 'does not affect folds outside changed lines' , function () 219test_move_indent ( 220[[ 221a 222a 223a 224a 225a 226a 227a 228a 229a]] , 230'4m5' 231) 232eq ( { 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 } , get_folds ()) 233end ) 234 235it ( 'moves and truncates folds that start in moved range' , function () 236test_move_indent ( 237[[ 238a 239a 240a 241a 242a 243a 244a 245a 246a 247a]] , 248'1,3m7' 249) 250eq ( { 0 , 0 , 0 , 0 , 0 , 1 , 2 , 0 , 0 , 0 } , get_folds ()) 251end ) 252 253it ( 'breaks a fold when moving text into it' , function () 254test_move_indent ( 255[[ 256a 257a 258a 259a 260a 261a 262a]] , 263'$m4' 264) 265eq ( { 0 , 1 , 2 , 2 , 0 , 0 , 0 } , get_folds ()) 266end ) 267 268it ( 'adjusts correctly when moving a range backwards' , function () 269test_move_indent ( 270[[ 271a 272a 273a 274a 275a]] , 276'2,3m0' 277) 278eq ( { 1 , 2 , 0 , 0 , 0 } , get_folds ()) 279end ) 280 281it ( 'handles shifting all remaining folds' , function () 282test_move_indent ( 283[[ 284a 285a 286a 287a 288a 289a 290a 291a 292a 293a 294a 295a 296a 297a 298a]] , 299'13m7' 300) 301eq ( { 1 , 2 , 2 , 2 , 1 , 2 , 2 , 1 , 1 , 1 , 2 , 2 , 2 , 1 , 0 } , get_folds ()) 302end ) 303end ) 304 305it ( 'updates correctly on :read' , function () 306-- luacheck: ignore 621 307t . write_file ( 308tempfname , 309[[ 310a 311 312 313a]] 314) 315insert ( [[ 316a 317a 318a 319a 320]] ) 321command ( 'setlocal foldmethod=indent' ) 322command ( '2' ) 323command ( '%foldopen' ) 324command ( 'read ' .. tempfname ) 325-- Just to check we have the correct file text. 326expect ( [[ 327a 328a 329a 330 331 332a 333a 334a 335]] ) 336for i =1 , 2 do 337eq ( 1 , fn . foldlevel ( i )) 338end 339for i =3 , 5 do 340eq ( 0 , fn . foldlevel ( i )) 341end 342for i =6 , 8 do 343eq ( 1 , fn . foldlevel ( i )) 344end 345end ) 346 347it ( 'combines folds when removing separating space' , function () 348-- luacheck: ignore 621 349insert ( [[ 350a 351a 352a 353a 354a 355a 356a 357a 358]] ) 359command ( 'setlocal foldmethod=indent' ) 360command ( '3,5d' ) 361eq ( 5 , fn . foldclosedend ( 1 )) 362end ) 363 364it ( "doesn't combine folds that have a specified end" , function () 365insert ( [[ 366{{{ 367}}} 368 369 370 371{{{ 372 373}}} 374]] ) 375command ( 'setlocal foldmethod=marker' ) 376command ( '3,5d' ) 377command ( '%foldclose' ) 378eq ( 2 , fn . foldclosedend ( 1 )) 379end ) 380 381it ( 'splits folds according to >N and <N with foldexpr' , function () 382n . source ( [[ 383function TestFoldExpr(lnum) 384let thisline = getline(a:lnum) 385if thisline == 'a' 386return 1 387elseif thisline == 'b' 388return 0 389elseif thisline == 'c' 390return '<1' 391elseif thisline == 'd' 392return '>1' 393endif 394return 0 395endfunction 396]] ) 397t . write_file ( 398tempfname , 399[[ 400b 401b 402a 403a 404d 405a 406a 407c]] 408) 409insert ( [[ 410a 411a 412a 413a 414a 415a 416]] ) 417command ( 'setlocal foldmethod=expr foldexpr=TestFoldExpr(v:lnum)' ) 418command ( '2' ) 419command ( 'foldopen' ) 420command ( 'read ' .. tempfname ) 421command ( '%foldclose' ) 422eq ( 2 , fn . foldclosedend ( 1 )) 423eq ( 0 , fn . foldlevel ( 3 )) 424eq ( 0 , fn . foldlevel ( 4 )) 425eq ( 6 , fn . foldclosedend ( 5 )) 426eq ( 10 , fn . foldclosedend ( 7 )) 427eq ( 14 , fn . foldclosedend ( 11 )) 428end ) 429 430it ( 'fdm=expr works correctly with :move #18668' , function () 431exec ( [[ 432set foldmethod=expr foldexpr=indent(v:lnum) 433let content = ['', '', 'Line1', ' Line2', ' Line3', 434\ 'Line4', ' Line5', ' Line6', 435\ 'Line7', ' Line8', ' Line9'] 436call append(0, content) 437normal! zM 438call cursor(4, 1) 439move 2 440move 1 441]] ) 442expect ( [[ 443 444Line2 445Line3 446 447Line1 448Line4 449Line5 450Line6 451Line7 452Line8 453Line9 454]] ) 455eq ( 0 , fn . foldlevel ( 1 )) 456eq ( 3 , fn . foldclosedend ( 2 )) 457eq ( 0 , fn . foldlevel ( 4 )) 458eq ( 0 , fn . foldlevel ( 5 )) 459eq ( 0 , fn . foldlevel ( 6 )) 460eq ( 8 , fn . foldclosedend ( 7 )) 461eq ( 0 , fn . foldlevel ( 9 )) 462eq ( 11 , fn . foldclosedend ( 10 )) 463eq ( 0 , fn . foldlevel ( 12 )) 464end ) 465 466it ( 'no folds remain if :delete makes buffer empty #19671' , function () 467command ( 'setlocal foldmethod=manual' ) 468fn . setline ( 1 , { 'foo' , 'bar' , 'baz' } ) 469command ( '2,3fold' ) 470command ( '%delete' ) 471eq ( 0 , fn . foldlevel ( 1 )) 472end ) 473 474it ( 'multibyte fold markers work #20438' , function () 475command ( 'setlocal foldmethod=marker foldmarker=«,» commentstring=/*%s*/' ) 476insert ( [[ 477bbbbb 478bbbbb 479bbbbb]] ) 480feed ( 'zfgg' ) 481expect ( [[ 482bbbbb/*«*/ 483bbbbb 484bbbbb/*»*/]] ) 485eq ( 1 , fn . foldlevel ( 1 )) 486end ) 487 488it ( 'fdm=indent updates correctly with visual blockwise insertion #22898' , function () 489insert ( [[ 490a 491b 492]] ) 493command ( 'setlocal foldmethod=indent shiftwidth=2' ) 494feed ( 'gg0<C-v>jI <Esc>' ) -- indent both lines using visual blockwise mode 495eq ( 1 , fn . foldlevel ( 1 )) 496eq ( 1 , fn . foldlevel ( 2 )) 497end ) 498 499it ( "fdm=indent doesn't open folds when inserting lower foldlevel line" , function () 500insert ( [[ 501insert an unindented line under this line 502keep the lines under this line folded 503keep this line folded 1 504keep this line folded 2 505 506. 507]] ) 508command ( 'set foldmethod=indent shiftwidth=2 noautoindent' ) 509eq ( 1 , fn . foldlevel ( 1 )) 510eq ( 1 , fn . foldlevel ( 2 )) 511eq ( 2 , fn . foldlevel ( 3 )) 512eq ( 2 , fn . foldlevel ( 4 )) 513 514feed ( 'zo' ) -- open the outer fold 515neq ( - 1 , fn . foldclosed ( 3 )) -- make sure the inner fold is not open 516 517feed ( 'gg0oa<Esc>' ) -- insert unindented line 518 519eq ( 1 , fn . foldlevel ( 1 )) --| insert an unindented line under this line 520eq ( 0 , fn . foldlevel ( 2 )) --|a 521eq ( 1 , fn . foldlevel ( 3 )) --| keep the lines under this line folded 522eq ( 2 , fn . foldlevel ( 4 )) --| keep this line folded 1 523eq ( 2 , fn . foldlevel ( 5 )) --| keep this line folded 2 524 525neq ( - 1 , fn . foldclosed ( 4 )) -- make sure the inner fold is still not open 526end ) 527end )