robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
20ff82d9fc
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 =t . describe , t . it , t . before_each 6local clear =n . clear 7local eq =t . eq 8local insert =n . insert 9local write_file =t . write_file 10local exec_lua =n . exec_lua 11local command =n . command 12local feed =n . feed 13local poke_eventloop =n . poke_eventloop 14 15before_each ( clear ) 16 17describe ( 'treesitter foldexpr' , function () 18before_each ( function () 19-- open folds to avoid deleting entire folded region 20exec_lua ( [[vim.opt.foldlevel = 9]] ) 21end ) 22 23local test_text =[[ 24void ui_refresh(void) 25{ 26int width = INT_MAX, height = INT_MAX; 27bool ext_widgets[kUIExtCount]; 28for (UIExtension i = 0; (int)i < kUIExtCount; i++) { 29ext_widgets[i] = true; 30} 31 32bool inclusive = ui_override(); 33for (size_t i = 0; i < ui_count; i++) { 34UI *ui = uis[i]; 35width = MIN(ui->width, width); 36height = MIN(ui->height, height); 37foo = BAR(ui->bazaar, bazaar); 38for (UIExtension j = 0; (int)j < kUIExtCount; j++) { 39ext_widgets[j] &= (ui->ui_ext[j] || inclusive); 40} 41} 42}]] 43 44local function parse ( lang ) 45exec_lua ( 46( [[vim.treesitter.get_parser(0, %s):parse()]] ): format ( lang and '"' .. lang .. '"' or 'nil' ) 47) 48end 49 50local function get_fold_levels () 51return exec_lua ( function () 52local res ={} 53for i =1 , vim . api . nvim_buf_line_count ( 0 ) do 54res [ i ] =vim . treesitter . foldexpr ( i ) 55end 56return res 57end ) 58end 59 60it ( 'can compute fold levels' , function () 61insert ( test_text ) 62 63parse ( 'c' ) 64 65eq ( { 66[ 1 ] ='>1' , 67[ 2 ] ='1' , 68[ 3 ] ='1' , 69[ 4 ] ='1' , 70[ 5 ] ='>2' , 71[ 6 ] ='2' , 72[ 7 ] ='2' , 73[ 8 ] ='1' , 74[ 9 ] ='1' , 75[ 10 ] ='>2' , 76[ 11 ] ='2' , 77[ 12 ] ='2' , 78[ 13 ] ='2' , 79[ 14 ] ='2' , 80[ 15 ] ='>3' , 81[ 16 ] ='3' , 82[ 17 ] ='3' , 83[ 18 ] ='2' , 84[ 19 ] ='1' , 85} , get_fold_levels ()) 86end ) 87 88it ( 'recomputes fold levels after lines are added/removed' , function () 89insert ( test_text ) 90 91parse ( 'c' ) 92 93command ( '1,2d' ) 94poke_eventloop () 95 96eq ( { 97[ 1 ] ='0' , 98[ 2 ] ='0' , 99[ 3 ] ='>1' , 100[ 4 ] ='1' , 101[ 5 ] ='1' , 102[ 6 ] ='0' , 103[ 7 ] ='0' , 104[ 8 ] ='>1' , 105[ 9 ] ='1' , 106[ 10 ] ='1' , 107[ 11 ] ='1' , 108[ 12 ] ='1' , 109[ 13 ] ='>2' , 110[ 14 ] ='2' , 111[ 15 ] ='2' , 112[ 16 ] ='1' , 113[ 17 ] ='0' , 114} , get_fold_levels ()) 115 116command ( '1put!' ) 117poke_eventloop () 118 119eq ( { 120[ 1 ] ='>1' , 121[ 2 ] ='1' , 122[ 3 ] ='1' , 123[ 4 ] ='1' , 124[ 5 ] ='>2' , 125[ 6 ] ='2' , 126[ 7 ] ='2' , 127[ 8 ] ='1' , 128[ 9 ] ='1' , 129[ 10 ] ='>2' , 130[ 11 ] ='2' , 131[ 12 ] ='2' , 132[ 13 ] ='2' , 133[ 14 ] ='2' , 134[ 15 ] ='>3' , 135[ 16 ] ='3' , 136[ 17 ] ='3' , 137[ 18 ] ='2' , 138[ 19 ] ='1' , 139} , get_fold_levels ()) 140end ) 141 142it ( 'handles changes close to start/end of folds' , function () 143insert ( [[ 144# h1 145t1 146# h2 147t2]] ) 148 149exec_lua ( [[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]] ) 150parse ( 'markdown' ) 151 152eq ( { 153[ 1 ] ='>1' , 154[ 2 ] ='1' , 155[ 3 ] ='>1' , 156[ 4 ] ='1' , 157} , get_fold_levels ()) 158 159feed ( '2ggo<Esc>' ) 160poke_eventloop () 161 162eq ( { 163[ 1 ] ='>1' , 164[ 2 ] ='1' , 165[ 3 ] ='1' , 166[ 4 ] ='>1' , 167[ 5 ] ='1' , 168} , get_fold_levels ()) 169 170feed ( 'dd' ) 171poke_eventloop () 172 173eq ( { 174[ 1 ] ='>1' , 175[ 2 ] ='1' , 176[ 3 ] ='>1' , 177[ 4 ] ='1' , 178} , get_fold_levels ()) 179 180feed ( '2ggdd' ) 181poke_eventloop () 182 183eq ( { 184[ 1 ] ='0' , 185[ 2 ] ='>1' , 186[ 3 ] ='1' , 187} , get_fold_levels ()) 188 189feed ( 'u' ) 190poke_eventloop () 191 192eq ( { 193[ 1 ] ='>1' , 194[ 2 ] ='1' , 195[ 3 ] ='>1' , 196[ 4 ] ='1' , 197} , get_fold_levels ()) 198 199feed ( '3ggdd' ) 200poke_eventloop () 201 202eq ( { 203[ 1 ] ='>1' , 204[ 2 ] ='1' , 205[ 3 ] ='1' , 206} , get_fold_levels ()) 207 208feed ( 'u' ) 209poke_eventloop () 210 211eq ( { 212[ 1 ] ='>1' , 213[ 2 ] ='1' , 214[ 3 ] ='>1' , 215[ 4 ] ='1' , 216} , get_fold_levels ()) 217 218feed ( '3ggI#<Esc>' ) 219parse () 220poke_eventloop () 221 222eq ( { 223[ 1 ] ='>1' , 224[ 2 ] ='1' , 225[ 3 ] ='>2' , 226[ 4 ] ='2' , 227} , get_fold_levels ()) 228 229feed ( 'x' ) 230parse () 231poke_eventloop () 232 233eq ( { 234[ 1 ] ='>1' , 235[ 2 ] ='1' , 236[ 3 ] ='>1' , 237[ 4 ] ='1' , 238} , get_fold_levels ()) 239end ) 240 241it ( 'handles changes that trigger multiple on_bytes' , function () 242insert ( [[ 243function f() 244asdf() 245asdf() 246end 247-- comment]] ) 248 249exec_lua ( function () 250vim . treesitter . query . set ( 251'lua' , 252'folds' , 253'[(function_declaration) (parameters) (arguments)] @fold' 254) 255end ) 256parse ( 'lua' ) 257 258eq ( { 259[ 1 ] ='>1' , 260[ 2 ] ='1' , 261[ 3 ] ='1' , 262[ 4 ] ='1' , 263[ 5 ] ='0' , 264} , get_fold_levels ()) 265 266command ( '1,4join' ) 267poke_eventloop () 268 269eq ( { 270[ 1 ] ='0' , 271[ 2 ] ='0' , 272} , get_fold_levels ()) 273 274feed ( 'u' ) 275poke_eventloop () 276 277eq ( { 278[ 1 ] ='>1' , 279[ 2 ] ='1' , 280[ 3 ] ='1' , 281[ 4 ] ='1' , 282[ 5 ] ='0' , 283} , get_fold_levels ()) 284end ) 285 286it ( 'handles multiple folds that overlap at the end and start' , function () 287insert ( [[ 288function f() 289g( 290function() 291asdf() 292end, function() 293end 294) 295end]] ) 296 297exec_lua ( function () 298vim . treesitter . query . set ( 299'lua' , 300'folds' , 301'[(function_declaration) (function_definition) (parameters) (arguments)] @fold' 302) 303end ) 304parse ( 'lua' ) 305 306-- If fold1.stop = fold2.start, then move fold1's stop up so that fold2.start gets proper level. 307eq ( { 308[ 1 ] ='>1' , 309[ 2 ] ='>2' , 310[ 3 ] ='>3' , 311[ 4 ] ='3' , 312[ 5 ] ='>3' , 313[ 6 ] ='3' , 314[ 7 ] ='2' , 315[ 8 ] ='1' , 316} , get_fold_levels ()) 317 318command ( '1,8join' ) 319feed ( 'u' ) 320poke_eventloop () 321 322eq ( { 323[ 1 ] ='>1' , 324[ 2 ] ='>2' , 325[ 3 ] ='>3' , 326[ 4 ] ='3' , 327[ 5 ] ='>3' , 328[ 6 ] ='3' , 329[ 7 ] ='2' , 330[ 8 ] ='1' , 331} , get_fold_levels ()) 332end ) 333 334it ( 'handles multiple folds that start at the same line' , function () 335insert ( [[ 336function f(a) 337if #(g({ 338k = v, 339})) > 0 then 340return 341end 342end]] ) 343 344exec_lua ( function () 345vim . treesitter . query . set ( 346'lua' , 347'folds' , 348'[(if_statement) (function_declaration) (parameters) (arguments) (table_constructor)] @fold' 349) 350end ) 351parse ( 'lua' ) 352 353eq ( { 354[ 1 ] ='>1' , 355[ 2 ] ='>3' , 356[ 3 ] ='3' , 357[ 4 ] ='3' , 358[ 5 ] ='2' , 359[ 6 ] ='2' , 360[ 7 ] ='1' , 361} , get_fold_levels ()) 362 363command ( '2,6join' ) 364poke_eventloop () 365 366eq ( { 367[ 1 ] ='>1' , 368[ 2 ] ='1' , 369[ 3 ] ='1' , 370} , get_fold_levels ()) 371 372feed ( 'u' ) 373poke_eventloop () 374 375eq ( { 376[ 1 ] ='>1' , 377[ 2 ] ='>3' , 378[ 3 ] ='3' , 379[ 4 ] ='3' , 380[ 5 ] ='2' , 381[ 6 ] ='2' , 382[ 7 ] ='1' , 383} , get_fold_levels ()) 384end ) 385 386it ( 'takes account of relevant options' , function () 387insert ( [[ 388# h1 389t1 390## h2 391t2 392### h3 393t3]] ) 394 395exec_lua ( [[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]] ) 396parse ( 'markdown' ) 397 398command ( [[set foldminlines=2]] ) 399 400eq ( { 401[ 1 ] ='>1' , 402[ 2 ] ='1' , 403[ 3 ] ='>2' , 404[ 4 ] ='2' , 405[ 5 ] ='2' , 406[ 6 ] ='2' , 407} , get_fold_levels ()) 408 409command ( [[set foldminlines=1 foldnestmax=1]] ) 410 411eq ( { 412[ 1 ] ='>1' , 413[ 2 ] ='1' , 414[ 3 ] ='1' , 415[ 4 ] ='1' , 416[ 5 ] ='1' , 417[ 6 ] ='1' , 418} , get_fold_levels ()) 419end ) 420 421it ( 'handles quantified patterns' , function () 422insert ( [[ 423-- hello 424-- hello 425-- hello 426-- hello 427-- hello 428-- hello]] ) 429 430exec_lua ( [[vim.treesitter.query.set('lua', 'folds', '(comment)+ @fold')]] ) 431parse ( 'lua' ) 432 433eq ( { 434[ 1 ] ='>1' , 435[ 2 ] ='1' , 436[ 3 ] ='1' , 437[ 4 ] ='1' , 438[ 5 ] ='1' , 439[ 6 ] ='1' , 440} , get_fold_levels ()) 441end ) 442 443it ( 'updates folds in all windows' , function () 444local screen =Screen . new ( 60 , 48 ) 445screen : set_default_attr_ids ( { 446[ 1 ] ={ background =Screen . colors . Grey , foreground =Screen . colors . DarkBlue } , 447[ 2 ] ={ bold =true , foreground =Screen . colors . Blue1 } , 448[ 3 ] ={ bold =true , reverse =true } , 449[ 4 ] ={ reverse =true } , 450} ) 451 452parse ( 'c' ) 453command ( [[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1]] ) 454command ( 'split' ) 455 456insert ( test_text ) 457 458screen : expect { 459grid =[[ 460{1:-}void ui_refresh(void) | 461{1:│}{ | 462{1:│} int width = INT_MAX, height = INT_MAX; | 463{1:│} bool ext_widgets[kUIExtCount]; | 464{1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { | 465{1:2} ext_widgets[i] = true; | 466{1:2} } | 467{1:│} | 468{1:│} bool inclusive = ui_override(); | 469{1:-} for (size_t i = 0; i < ui_count; i++) { | 470{1:2} UI *ui = uis[i]; | 471{1:2} width = MIN(ui->width, width); | 472{1:2} height = MIN(ui->height, height); | 473{1:2} foo = BAR(ui->bazaar, bazaar); | 474{1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 475{1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 476{1:3} } | 477{1:2} } | 478{1:│}^} | 479{2:~ }|*4 480{3:[No Name] [+] }| 481{1:-}void ui_refresh(void) | 482{1:│}{ | 483{1:│} int width = INT_MAX, height = INT_MAX; | 484{1:│} bool ext_widgets[kUIExtCount]; | 485{1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { | 486{1:2} ext_widgets[i] = true; | 487{1:2} } | 488{1:│} | 489{1:│} bool inclusive = ui_override(); | 490{1:-} for (size_t i = 0; i < ui_count; i++) { | 491{1:2} UI *ui = uis[i]; | 492{1:2} width = MIN(ui->width, width); | 493{1:2} height = MIN(ui->height, height); | 494{1:2} foo = BAR(ui->bazaar, bazaar); | 495{1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 496{1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 497{1:3} } | 498{1:2} } | 499{1:│}} | 500{2:~ }|*3 501{4:[No Name] [+] }| 502| 503]] , 504} 505 506command ( '1,2d' ) 507 508screen : expect { 509grid =[[ 510{1: } ^int width = INT_MAX, height = INT_MAX; | 511{1: } bool ext_widgets[kUIExtCount]; | 512{1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { | 513{1:│} ext_widgets[i] = true; | 514{1:│} } | 515{1: } | 516{1: } bool inclusive = ui_override(); | 517{1:-} for (size_t i = 0; i < ui_count; i++) { | 518{1:│} UI *ui = uis[i]; | 519{1:│} width = MIN(ui->width, width); | 520{1:│} height = MIN(ui->height, height); | 521{1:│} foo = BAR(ui->bazaar, bazaar); | 522{1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 523{1:2} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 524{1:2} } | 525{1:│} } | 526{1: }} | 527{2:~ }|*6 528{3:[No Name] [+] }| 529{1: } int width = INT_MAX, height = INT_MAX; | 530{1: } bool ext_widgets[kUIExtCount]; | 531{1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { | 532{1:│} ext_widgets[i] = true; | 533{1:│} } | 534{1: } | 535{1: } bool inclusive = ui_override(); | 536{1:-} for (size_t i = 0; i < ui_count; i++) { | 537{1:│} UI *ui = uis[i]; | 538{1:│} width = MIN(ui->width, width); | 539{1:│} height = MIN(ui->height, height); | 540{1:│} foo = BAR(ui->bazaar, bazaar); | 541{1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 542{1:2} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 543{1:2} } | 544{1:│} } | 545{1: }} | 546{2:~ }|*5 547{4:[No Name] [+] }| 548| 549]] , 550} 551 552feed ( [[O<C-u><C-r>"<BS><Esc>]] ) 553 554screen : expect { 555grid =[[ 556{1:-}void ui_refresh(void) | 557{1:│}^{ | 558{1:│} int width = INT_MAX, height = INT_MAX; | 559{1:│} bool ext_widgets[kUIExtCount]; | 560{1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { | 561{1:2} ext_widgets[i] = true; | 562{1:2} } | 563{1:│} | 564{1:│} bool inclusive = ui_override(); | 565{1:-} for (size_t i = 0; i < ui_count; i++) { | 566{1:2} UI *ui = uis[i]; | 567{1:2} width = MIN(ui->width, width); | 568{1:2} height = MIN(ui->height, height); | 569{1:2} foo = BAR(ui->bazaar, bazaar); | 570{1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 571{1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 572{1:3} } | 573{1:2} } | 574{1:│}} | 575{2:~ }|*4 576{3:[No Name] [+] }| 577{1:-}void ui_refresh(void) | 578{1:│}{ | 579{1:│} int width = INT_MAX, height = INT_MAX; | 580{1:│} bool ext_widgets[kUIExtCount]; | 581{1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { | 582{1:2} ext_widgets[i] = true; | 583{1:2} } | 584{1:│} | 585{1:│} bool inclusive = ui_override(); | 586{1:-} for (size_t i = 0; i < ui_count; i++) { | 587{1:2} UI *ui = uis[i]; | 588{1:2} width = MIN(ui->width, width); | 589{1:2} height = MIN(ui->height, height); | 590{1:2} foo = BAR(ui->bazaar, bazaar); | 591{1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 592{1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 593{1:3} } | 594{1:2} } | 595{1:│}} | 596{2:~ }|*3 597{4:[No Name] [+] }| 598| 599]] , 600} 601end ) 602 603it ( "doesn't open folds in diff mode" , function () 604local screen =Screen . new ( 60 , 36 ) 605 606parse ( 'c' ) 607command ( 608[[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=9]] 609) 610insert ( test_text ) 611command ( '16d' ) 612 613command ( 'new' ) 614insert ( test_text ) 615 616command ( 'windo diffthis' ) 617feed ( 'do' ) 618 619screen : expect ( [[ 620{7:+ }{13:+-- 19 lines: void ui_refresh(void)·······················}| 621{1:~ }|*16 622{2:[No Name] [+] }| 623{7:+ }{13:^+-- 19 lines: void ui_refresh(void)·······················}| 624{1:~ }|*15 625{3:[No Name] [+] }| 626| 627]] ) 628end ) 629 630it ( 'does not extend closed fold with `o`/`O`' , function () 631local screen =Screen . new ( 60 , 24 ) 632 633insert ( test_text ) 634parse ( 'c' ) 635command ( [[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1]] ) 636 637feed ( '5ggzco' ) 638screen : expect ( { 639grid =[[ 640{7:-}void ui_refresh(void) | 641{7:│}{ | 642{7:│} int width = INT_MAX, height = INT_MAX; | 643{7:│} bool ext_widgets[kUIExtCount]; | 644{7:+}{13:+--- 3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}| 645{7:│}^ | 646{7:│} | 647{7:│} bool inclusive = ui_override(); | 648{7:-} for (size_t i = 0; i < ui_count; i++) { | 649{7:2} UI *ui = uis[i]; | 650{7:2} width = MIN(ui->width, width); | 651{7:2} height = MIN(ui->height, height); | 652{7:2} foo = BAR(ui->bazaar, bazaar); | 653{7:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 654{7:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 655{7:3} } | 656{7:2} } | 657{7:│}} | 658{1:~ }|*5 659{5:-- INSERT --} | 660]] , 661} ) 662 663feed ( '<Esc>O' ) 664screen : expect ( { 665grid =[[ 666{7:-}void ui_refresh(void) | 667{7:│}{ | 668{7:│} int width = INT_MAX, height = INT_MAX; | 669{7:│} bool ext_widgets[kUIExtCount]; | 670{7:+}{13:+--- 3 lines: for (UIExtension i = 0; (int)i < kUIExtCount}| 671{7:│}^ | 672{7:│} |*2 673{7:│} bool inclusive = ui_override(); | 674{7:-} for (size_t i = 0; i < ui_count; i++) { | 675{7:2} UI *ui = uis[i]; | 676{7:2} width = MIN(ui->width, width); | 677{7:2} height = MIN(ui->height, height); | 678{7:2} foo = BAR(ui->bazaar, bazaar); | 679{7:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { | 680{7:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); | 681{7:3} } | 682{7:2} } | 683{7:│}} | 684{1:~ }|*4 685{5:-- INSERT --} | 686]] , 687} ) 688end ) 689 690it ( "doesn't open folds that are not touched" , function () 691local screen =Screen . new ( 40 , 8 ) 692screen : set_default_attr_ids ( { 693[ 1 ] ={ foreground =Screen . colors . DarkBlue , background =Screen . colors . Gray } , 694[ 2 ] ={ foreground =Screen . colors . DarkBlue , background =Screen . colors . LightGray } , 695[ 3 ] ={ foreground =Screen . colors . Blue1 , bold =true } , 696[ 4 ] ={ bold =true } , 697} ) 698 699insert ( [[ 700# h1 701t1 702# h2 703t2]] ) 704exec_lua ( [[vim.treesitter.query.set('markdown', 'folds', '(section) @fold')]] ) 705parse ( 'markdown' ) 706command ( 707[[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=0]] 708) 709 710feed ( 'ggzojo' ) 711screen : expect ( [[ 712{1:-}# h1 | 713{1:│}t1 | 714{1:-}^ | 715{1:+}{2:+-- 2 lines: # h2·····················}| 716{3:~ }|*3 717{4:-- INSERT --} | 718]] ) 719 720-- TODO(tomtomjhj): `u` spuriously opens the fold (#26499). 721feed ( '<Esc>uzMggzodd' ) 722screen : expect ( [[ 723{1:-}^t1 | 724{1:-}# h2 | 725{1:│}t2 | 726{3:~ }|*4 7271 line less; before #2 {MATCH:.*}| 728]] ) 729end ) 730 731it ( "doesn't call get_parser too often when parser is not available" , function () 732-- spy on vim.treesitter.get_parser() to keep track of how many times it is called 733exec_lua ( function () 734_G . count =0 735vim . treesitter . get_parser =( function ( wrapped ) 736return function ( ... ) 737_G . count =_G . count + 1 738return wrapped ( ... ) 739end 740end )( vim . treesitter . get_parser ) 741end ) 742 743insert ( test_text ) 744command [[ 745set filetype=some_filetype_without_treesitter_parser 746set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=0 747]] 748 749-- foldexpr will return '0' for all lines 750local levels =get_fold_levels () ---@type integer[] 751eq ( 19 , # levels ) 752for lnum , level in ipairs ( levels ) do 753eq ( '0' , level , string . format ( "foldlevel[%d] == %s; expected '0'" , lnum , level )) 754end 755 756eq ( 7571 , 758exec_lua [[ return _G.count ]] , 759'count should not be as high as the # of lines; actually only once for the buffer.' 760) 761end ) 762 763it ( 'can detect a new parser and refresh folds accordingly' , function () 764local name =t . tmpname () 765write_file ( name , test_text ) 766command ( 'edit ' .. name ) 767command [[ 768set filetype=some_filetype_without_treesitter_parser 769set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=0 770]] 771 772-- foldexpr will return '0' for all lines 773local function expect_no_folds () 774local levels =get_fold_levels () ---@type integer[] 775eq ( 19 , # levels ) 776for lnum , level in ipairs ( levels ) do 777eq ( '0' , level , string . format ( "foldlevel[%d] == %s; expected '0'" , lnum , level )) 778end 779end 780expect_no_folds () 781 782-- reload buffer as c filetype to simulate new parser being found 783feed ( 'GA// vim: ft=c<Esc>' ) 784command ( [[write | edit]] ) 785local foldlevels ={ 786[ 1 ] ='>1' , 787[ 2 ] ='1' , 788[ 3 ] ='1' , 789[ 4 ] ='1' , 790[ 5 ] ='>2' , 791[ 6 ] ='2' , 792[ 7 ] ='2' , 793[ 8 ] ='1' , 794[ 9 ] ='1' , 795[ 10 ] ='>2' , 796[ 11 ] ='2' , 797[ 12 ] ='2' , 798[ 13 ] ='2' , 799[ 14 ] ='2' , 800[ 15 ] ='>3' , 801[ 16 ] ='3' , 802[ 17 ] ='3' , 803[ 18 ] ='2' , 804[ 19 ] ='1' , 805} 806eq ( foldlevels , get_fold_levels ()) 807 808-- only changing filetype should change the parser again 809command ( 'set ft=some_filetype_without_treesitter_parser' ) 810expect_no_folds () 811 812command ( 'set ft=c' ) 813eq ( foldlevels , get_fold_levels ()) 814end ) 815 816it ( 'no error when deleting lines at end of buffer with fml=0' , function () 817local screen =Screen . new ( 40 , 2 ) 818insert ( 'hello' ) 819parse ( 'markdown' ) 820command ( 'set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldminlines=0' ) 821feed ( 'o<Esc>dd' ) 822screen : expect ( [[ 823^hello | 824| 825]] ) 826end ) 827 828it ( 'clamps unbounded changed ranges on 32-bit platforms' , function () 829insert ( [[ 830one 831two]] ) 832 833exec_lua ( function () 834local parser ={} 835 836function parser : parse ( _ , callback ) 837if callback then 838callback ( nil , {} ) 839end 840end 841 842function parser : for_each_tree () end 843 844function parser : register_cbs ( callbacks ) 845self . callbacks =callbacks 846end 847 848vim . treesitter . get_parser =function () 849return parser 850end 851 852vim . treesitter . foldexpr () 853vim . wo . foldmethod ='expr' 854vim . _foldupdate =function ( _ , first , last ) 855_G . foldupdate_range ={ first , last } 856end 857 858-- UINT32_MAX is represented as -1 by Lua integers on 32-bit platforms. 859parser . callbacks . on_changedtree ( { { 0 , 0 , - 1 , - 1 } } ) 860end ) 861poke_eventloop () 862 863eq ( { 0 , 2 } , exec_lua ( 'return _G.foldupdate_range' )) 864end ) 865end )