robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local t_lsp =require ( 'test.functional.plugin.lsp.testutil' ) 5 6local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 7local dedent =t . dedent 8local eq =t . eq 9local retry =t . retry 10 11local clear_notrace =t_lsp . clear_notrace 12local create_server_definition =t_lsp . create_server_definition 13 14local api =n . api 15local exec_lua =n . exec_lua 16local insert =n . insert 17local command =n . command 18local feed =n . feed 19 20describe ( 'vim.lsp.folding_range' , function () 21local text =[[// foldLevel() {{{2 22/// @return fold level at line number "lnum" in the current window. 23static int foldLevel(linenr_T lnum) 24{ 25// While updating the folds lines between invalid_top and invalid_bot have 26// an undefined fold level. Otherwise update the folds first. 27if (invalid_top == 0) { 28checkupdate(curwin); 29} else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { 30return prev_lnum_lvl; 31} else if (lnum >= invalid_top && lnum <= invalid_bot) { 32return -1; 33} 34 35// Return quickly when there is no folding at all in this window. 36if (!hasAnyFolding(curwin)) { 37return 0; 38} 39 40return foldLevelWin(curwin, lnum); 41}]] 42 43local result ={ 44{ 45endLine =19 , 46kind ='region' , 47startCharacter =1 , 48startLine =3 , 49} , 50{ 51endCharacter =2 , 52endLine =7 , 53kind ='region' , 54startCharacter =25 , 55startLine =6 , 56} , 57{ 58endCharacter =2 , 59endLine =9 , 60kind ='region' , 61startCharacter =55 , 62startLine =8 , 63} , 64{ 65endCharacter =2 , 66endLine =11 , 67kind ='region' , 68startCharacter =58 , 69startLine =10 , 70} , 71{ 72endCharacter =2 , 73endLine =16 , 74kind ='region' , 75startCharacter =31 , 76startLine =15 , 77} , 78{ 79endCharacter =68 , 80endLine =1 , 81kind ='comment' , 82startCharacter =2 , 83startLine =0 , 84} , 85{ 86endCharacter =64 , 87endLine =5 , 88kind ='comment' , 89startCharacter =4 , 90startLine =4 , 91} , 92} 93 94local bufnr ---@type integer 95local client_id ---@type integer 96 97clear_notrace () 98before_each ( function () 99clear_notrace () 100 101exec_lua ( create_server_definition ) 102bufnr =n . api . nvim_get_current_buf () 103client_id =exec_lua ( function () 104_G . folding_ranges =result 105_G . server =_G . _create_server ( { 106capabilities ={ 107foldingRangeProvider =true , 108} , 109handlers ={ 110[ 'textDocument/foldingRange' ] =function ( _ , _ , callback ) 111callback ( nil , _G . folding_ranges ) 112end , 113} , 114} ) 115 116vim . api . nvim_win_set_buf ( 0 , bufnr ) 117 118return vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 119end ) 120command ( 'set foldmethod=expr foldcolumn=1 foldlevel=999' ) 121insert ( text ) 122end ) 123after_each ( function () 124api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 125end ) 126 127describe ( 'expr()' , function () 128--- @type test.functional.ui.screen 129local screen 130before_each ( function () 131screen =Screen . new ( 80 , 45 ) 132command ( [[set foldexpr=v:lua.vim.lsp.foldexpr()]] ) 133command ( [[split]] ) 134end ) 135 136it ( 'controls whether folding range is enabled' , function () 137eq ( 138true , 139exec_lua ( function () 140return vim . lsp . _capability . is_enabled ( 'folding_range' , { bufnr =0 } ) 141end ) 142) 143command [[setlocal foldexpr=]] 144eq ( 145false , 146exec_lua ( function () 147return vim . lsp . _capability . is_enabled ( 'folding_range' , { bufnr =0 } ) 148end ) 149) 150command ( [[set foldexpr=v:lua.vim.lsp.foldexpr()]] ) 151eq ( 152true , 153exec_lua ( function () 154return vim . lsp . _capability . is_enabled ( 'folding_range' , { bufnr =0 } ) 155end ) 156) 157end ) 158 159it ( 'can compute fold levels' , function () 160---@type table<integer, string> 161local foldlevels ={} 162for i =1 , 21 do 163foldlevels [ i ] =exec_lua ( 'return vim.lsp.foldexpr(' .. i .. ')' ) 164end 165eq ( { 166[ 1 ] ='>1' , 167[ 2 ] ='<1' , 168[ 3 ] ='0' , 169[ 4 ] ='>1' , 170[ 5 ] ='>2' , 171[ 6 ] ='<2' , 172[ 7 ] ='>2' , 173[ 8 ] ='<2' , 174[ 9 ] ='>2' , 175[ 10 ] ='<2' , 176[ 11 ] ='>2' , 177[ 12 ] ='<2' , 178[ 13 ] ='1' , 179[ 14 ] ='1' , 180[ 15 ] ='1' , 181[ 16 ] ='>2' , 182[ 17 ] ='<2' , 183[ 18 ] ='1' , 184[ 19 ] ='1' , 185[ 20 ] ='<1' , 186[ 21 ] ='0' , 187} , foldlevels ) 188end ) 189 190it ( 'treats unknown folding range kinds like an absent kind #41765' , function () 191exec_lua ( function () 192_G . folding_ranges ={ 193{ startLine =0 , endLine =1 , kind ='function' } , 194{ startLine =3 , endLine =4 } , 195{ startLine =6 , endLine =7 , kind ='' } , 196{ startLine =9 , endLine =10 , kind ='comment' } , 197{ startLine =12 , endLine =13 , kind ='imports' } , 198{ startLine =15 , endLine =16 , kind ='region' } , 199} 200vim . lsp . _folding_range . on_refresh ( 201nil , 202nil , 203{ method ='workspace/foldingRange/refresh' , client_id =client_id } 204) 205end ) 206 207retry ( nil , nil , function () 208eq ( 209{ 210'>1' , 211'<1' , 212'0' , 213'>1' , 214'<1' , 215'0' , 216'>1' , 217'<1' , 218'0' , 219'>1' , 220'<1' , 221'0' , 222'>1' , 223'<1' , 224'0' , 225'>1' , 226'<1' , 227} , 228exec_lua ( function () 229local levels ={} 230for lnum =1 , 17 do 231levels [ lnum ] =vim . lsp . foldexpr ( lnum ) 232end 233return levels 234end ) 235) 236end ) 237 238exec_lua ( function () 239for _ , kind in ipairs ( { 'comment' , 'imports' , 'region' , 'function' , '' } ) do 240vim . lsp . foldclose ( kind ) 241end 242end ) 243-- Unknown and absent kinds are not selected by kind-specific folding. 244eq ( 245{ - 1 , - 1 , - 1 , 10 , 13 , 16 } , 246exec_lua ( function () 247return vim . tbl_map ( vim . fn . foldclosed , { 1 , 4 , 7 , 10 , 13 , 16 } ) 248end ) 249) 250 251command ( 'normal! zM' ) 252eq ( 253{ 1 , 4 , 7 , 10 , 13 , 16 } , 254exec_lua ( function () 255return vim . tbl_map ( vim . fn . foldclosed , { 1 , 4 , 7 , 10 , 13 , 16 } ) 256end ) 257) 258end ) 259 260it ( 'refreshes folding ranges on request' , function () 261local function foldlevels () 262return exec_lua ( function () 263return { vim . lsp . foldexpr ( 1 ), vim . lsp . foldexpr ( 2 ), vim . lsp . foldexpr ( 3 ) } 264end ) 265end 266 267retry ( nil , nil , function () 268eq ( { '>1' , '<1' , '0' } , foldlevels ()) 269end ) 270 271exec_lua ( function () 272_G . folding_ranges ={ { startLine =1 , endLine =2 } } 273vim . lsp . _folding_range . on_refresh ( 274nil , 275nil , 276{ method ='workspace/foldingRange/refresh' , client_id =client_id } 277) 278end ) 279 280retry ( nil , nil , function () 281eq ( { '0' , '>1' , '<1' } , foldlevels ()) 282end ) 283end ) 284 285it ( 'updates folds in all windows' , function () 286screen : expect ( { 287grid =[[ 288{7:-}// foldLevel() {{{2 | 289{7:│}/// @return fold level at line number "lnum" in the current window. | 290{7: }static int foldLevel(linenr_T lnum) | 291{7:-}{ | 292{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 293{7:2} // an undefined fold level. Otherwise update the folds first. | 294{7:-} if (invalid_top == 0) { | 295{7:2} checkupdate(curwin); | 296{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 297{7:2} return prev_lnum_lvl; | 298{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 299{7:2} return -1; | 300{7:│} } | 301{7:│} | 302{7:│} // Return quickly when there is no folding at all in this window. | 303{7:-} if (!hasAnyFolding(curwin)) { | 304{7:2} return 0; | 305{7:│} } | 306{7:│} | 307{7:│} return foldLevelWin(curwin, lnum); | 308{7: }^} | 309{3:[No Name] [+] }| 310{7:-}// foldLevel() {{{2 | 311{7:│}/// @return fold level at line number "lnum" in the current window. | 312{7: }static int foldLevel(linenr_T lnum) | 313{7:-}{ | 314{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 315{7:2} // an undefined fold level. Otherwise update the folds first. | 316{7:-} if (invalid_top == 0) { | 317{7:2} checkupdate(curwin); | 318{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 319{7:2} return prev_lnum_lvl; | 320{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 321{7:2} return -1; | 322{7:│} } | 323{7:│} | 324{7:│} // Return quickly when there is no folding at all in this window. | 325{7:-} if (!hasAnyFolding(curwin)) { | 326{7:2} return 0; | 327{7:│} } | 328{7:│} | 329{7:│} return foldLevelWin(curwin, lnum); | 330{7: }} | 331{2:[No Name] [+] }| 332| 333]] , 334} ) 335end ) 336 337it ( 'persists wherever foldexpr is set' , function () 338command ( [[setlocal foldexpr=]] ) 339feed ( '<C-w><C-w>zx' ) 340screen : expect ( { 341grid =[[ 342{7: }// foldLevel() {{{2 | 343{7: }/// @return fold level at line number "lnum" in the current window. | 344{7: }static int foldLevel(linenr_T lnum) | 345{7: }{ | 346{7: } // While updating the folds lines between invalid_top and invalid_bot have | 347{7: } // an undefined fold level. Otherwise update the folds first. | 348{7: } if (invalid_top == 0) { | 349{7: } checkupdate(curwin); | 350{7: } } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 351{7: } return prev_lnum_lvl; | 352{7: } } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 353{7: } return -1; | 354{7: } } | 355{7: } | 356{7: } // Return quickly when there is no folding at all in this window. | 357{7: } if (!hasAnyFolding(curwin)) { | 358{7: } return 0; | 359{7: } } | 360{7: } | 361{7: } return foldLevelWin(curwin, lnum); | 362{7: }} | 363{2:[No Name] [+] }| 364{7:-}// foldLevel() {{{2 | 365{7:│}/// @return fold level at line number "lnum" in the current window. | 366{7: }static int foldLevel(linenr_T lnum) | 367{7:-}{ | 368{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 369{7:2} // an undefined fold level. Otherwise update the folds first. | 370{7:-} if (invalid_top == 0) { | 371{7:2} checkupdate(curwin); | 372{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 373{7:2} return prev_lnum_lvl; | 374{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 375{7:2} return -1; | 376{7:│} } | 377{7:│} | 378{7:│} // Return quickly when there is no folding at all in this window. | 379{7:-} if (!hasAnyFolding(curwin)) { | 380{7:2} return 0; | 381{7:│} } | 382{7:│} | 383{7:│} return foldLevelWin(curwin, lnum); | 384{7: }^} | 385{3:[No Name] [+] }| 386| 387]] , 388} ) 389end ) 390 391it ( 'synchronizes changed rows with their previous foldlevels' , function () 392command ( '1,2d' ) 393screen : expect ( { 394grid =[[ 395{7: }^static int foldLevel(linenr_T lnum) | 396{7:-}{ | 397{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 398{7:2} // an undefined fold level. Otherwise update the folds first. | 399{7:-} if (invalid_top == 0) { | 400{7:2} checkupdate(curwin); | 401{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 402{7:2} return prev_lnum_lvl; | 403{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 404{7:2} return -1; | 405{7:│} } | 406{7:│} | 407{7:│} // Return quickly when there is no folding at all in this window. | 408{7:-} if (!hasAnyFolding(curwin)) { | 409{7:2} return 0; | 410{7:│} } | 411{7:│} | 412{7:│} return foldLevelWin(curwin, lnum); | 413{7: }} | 414{1:~ }|*2 415{3:[No Name] [+] }| 416{7: }static int foldLevel(linenr_T lnum) | 417{7:-}{ | 418{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 419{7:2} // an undefined fold level. Otherwise update the folds first. | 420{7:-} if (invalid_top == 0) { | 421{7:2} checkupdate(curwin); | 422{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 423{7:2} return prev_lnum_lvl; | 424{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 425{7:2} return -1; | 426{7:│} } | 427{7:│} | 428{7:│} // Return quickly when there is no folding at all in this window. | 429{7:-} if (!hasAnyFolding(curwin)) { | 430{7:2} return 0; | 431{7:│} } | 432{7:│} | 433{7:│} return foldLevelWin(curwin, lnum); | 434{7: }} | 435{1:~ }|*2 436{2:[No Name] [+] }| 437| 438]] , 439} ) 440end ) 441 442it ( 'clears folds when sole client detaches' , function () 443exec_lua ( function () 444vim . lsp . buf_detach_client ( bufnr , client_id ) 445end ) 446screen : expect ( { 447grid =[[ 448{7: }// foldLevel() {{{2 | 449{7: }/// @return fold level at line number "lnum" in the current window. | 450{7: }static int foldLevel(linenr_T lnum) | 451{7: }{ | 452{7: } // While updating the folds lines between invalid_top and invalid_bot have | 453{7: } // an undefined fold level. Otherwise update the folds first. | 454{7: } if (invalid_top == 0) { | 455{7: } checkupdate(curwin); | 456{7: } } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 457{7: } return prev_lnum_lvl; | 458{7: } } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 459{7: } return -1; | 460{7: } } | 461{7: } | 462{7: } // Return quickly when there is no folding at all in this window. | 463{7: } if (!hasAnyFolding(curwin)) { | 464{7: } return 0; | 465{7: } } | 466{7: } | 467{7: } return foldLevelWin(curwin, lnum); | 468{7: }^} | 469{3:[No Name] [+] }| 470{7: }// foldLevel() {{{2 | 471{7: }/// @return fold level at line number "lnum" in the current window. | 472{7: }static int foldLevel(linenr_T lnum) | 473{7: }{ | 474{7: } // While updating the folds lines between invalid_top and invalid_bot have | 475{7: } // an undefined fold level. Otherwise update the folds first. | 476{7: } if (invalid_top == 0) { | 477{7: } checkupdate(curwin); | 478{7: } } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 479{7: } return prev_lnum_lvl; | 480{7: } } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 481{7: } return -1; | 482{7: } } | 483{7: } | 484{7: } // Return quickly when there is no folding at all in this window. | 485{7: } if (!hasAnyFolding(curwin)) { | 486{7: } return 0; | 487{7: } } | 488{7: } | 489{7: } return foldLevelWin(curwin, lnum); | 490{7: }} | 491{2:[No Name] [+] }| 492| 493]] , 494} ) 495end ) 496 497it ( 'remains valid after the client re-attaches.' , function () 498exec_lua ( function () 499vim . lsp . buf_detach_client ( bufnr , client_id ) 500vim . lsp . buf_attach_client ( bufnr , client_id ) 501end ) 502screen : expect ( { 503grid =[[ 504{7:-}// foldLevel() {{{2 | 505{7:│}/// @return fold level at line number "lnum" in the current window. | 506{7: }static int foldLevel(linenr_T lnum) | 507{7:-}{ | 508{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 509{7:2} // an undefined fold level. Otherwise update the folds first. | 510{7:-} if (invalid_top == 0) { | 511{7:2} checkupdate(curwin); | 512{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 513{7:2} return prev_lnum_lvl; | 514{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 515{7:2} return -1; | 516{7:│} } | 517{7:│} | 518{7:│} // Return quickly when there is no folding at all in this window. | 519{7:-} if (!hasAnyFolding(curwin)) { | 520{7:2} return 0; | 521{7:│} } | 522{7:│} | 523{7:│} return foldLevelWin(curwin, lnum); | 524{7: }^} | 525{3:[No Name] [+] }| 526{7:-}// foldLevel() {{{2 | 527{7:│}/// @return fold level at line number "lnum" in the current window. | 528{7: }static int foldLevel(linenr_T lnum) | 529{7:-}{ | 530{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 531{7:2} // an undefined fold level. Otherwise update the folds first. | 532{7:-} if (invalid_top == 0) { | 533{7:2} checkupdate(curwin); | 534{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 535{7:2} return prev_lnum_lvl; | 536{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 537{7:2} return -1; | 538{7:│} } | 539{7:│} | 540{7:│} // Return quickly when there is no folding at all in this window. | 541{7:-} if (!hasAnyFolding(curwin)) { | 542{7:2} return 0; | 543{7:│} } | 544{7:│} | 545{7:│} return foldLevelWin(curwin, lnum); | 546{7: }} | 547{2:[No Name] [+] }| 548| 549]] , 550} ) 551end ) 552end ) 553 554describe ( 'foldtext()' , function () 555--- @type test.functional.ui.screen 556local screen 557before_each ( function () 558screen =Screen . new ( 80 , 23 ) 559command ( 560[[set foldexpr=v:lua.vim.lsp.foldexpr() foldtext=v:lua.vim.lsp.foldtext() foldlevel=1]] 561) 562end ) 563 564it ( 'shows the first folded line if `collapsedText` does not exist' , function () 565screen : expect ( { 566grid =[[ 567{7:-}// foldLevel() {{{2 | 568{7:│}/// @return fold level at line number "lnum" in the current window. | 569{7: }static int foldLevel(linenr_T lnum) | 570{7:-}{ | 571{7:+}{13: // While updating the folds lines between invalid_top and invalid_bot have···}| 572{7:+}{13: if (invalid_top == 0) {······················································}| 573{7:+}{13: } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {························}| 574{7:+}{13: } else if (lnum >= invalid_top && lnum <= invalid_bot) {·····················}| 575{7:│} } | 576{7:│} | 577{7:│} // Return quickly when there is no folding at all in this window. | 578{7:+}{13: if (!hasAnyFolding(curwin)) {················································}| 579{7:│} } | 580{7:│} | 581{7:│} return foldLevelWin(curwin, lnum); | 582{7: }^} | 583{1:~ }|*6 584| 585]] , 586} ) 587end ) 588 589it ( 'shows the foldtext by virt line' , function () 590command ( [[set filetype=c]] ) 591eq ( 592{ 593{ ' ' } , 594{ 'if' , { '@keyword.conditional.c' } } , 595{ ' ' } , 596{ '(' , { '@punctuation.bracket.c' } } , 597{ '!' , { '@operator.c' } } , 598{ 'hasAnyFolding' , { '@variable.c' , '@function.call.c' } } , 599{ '(' , { '@punctuation.bracket.c' } } , 600{ 'curwin' , { '@variable.c' } } , 601{ ')' , { '@punctuation.bracket.c' } } , 602{ ')' , { '@punctuation.bracket.c' } } , 603{ ' ' } , 604{ '{' , { '@punctuation.bracket.c' } } , 605} , 606exec_lua ( function () 607return vim . lsp . foldtext ( 16 ) 608end ) 609) 610end ) 611end ) 612 613describe ( 'foldclose()' , function () 614--- @type test.functional.ui.screen 615local screen 616before_each ( function () 617screen =Screen . new ( 80 , 23 ) 618command ( [[set foldexpr=v:lua.vim.lsp.foldexpr()]] ) 619end ) 620 621it ( 'closes all folds of one kind immediately' , function () 622exec_lua ( function () 623vim . lsp . foldclose ( 'comment' ) 624end ) 625screen : expect ( { 626grid =[[ 627{7:+}{13:+-- 2 lines: foldLevel()······················································}| 628{7: }static int foldLevel(linenr_T lnum) | 629{7:-}{ | 630{7:+}{13:+--- 2 lines: While updating the folds lines between invalid_top and invalid_b}| 631{7:-} if (invalid_top == 0) { | 632{7:2} checkupdate(curwin); | 633{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 634{7:2} return prev_lnum_lvl; | 635{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 636{7:2} return -1; | 637{7:│} } | 638{7:│} | 639{7:│} // Return quickly when there is no folding at all in this window. | 640{7:-} if (!hasAnyFolding(curwin)) { | 641{7:2} return 0; | 642{7:│} } | 643{7:│} | 644{7:│} return foldLevelWin(curwin, lnum); | 645{7: }^} | 646{1:~ }|*3 647| 648]] , 649} ) 650end ) 651 652it ( 'closes the smallest fold first' , function () 653exec_lua ( function () 654vim . lsp . foldclose ( 'region' ) 655end ) 656screen : expect ( { 657grid =[[ 658{7:-}// foldLevel() {{{2 | 659{7:│}/// @return fold level at line number "lnum" in the current window. | 660{7: }static int foldLevel(linenr_T lnum) | 661{7:+}{13:+-- 17 lines: {································································}| 662{7: }^} | 663{1:~ }|*17 664| 665]] , 666} ) 667command ( '4foldopen' ) 668screen : expect ( { 669grid =[[ 670{7:-}// foldLevel() {{{2 | 671{7:│}/// @return fold level at line number "lnum" in the current window. | 672{7: }static int foldLevel(linenr_T lnum) | 673{7:-}{ | 674{7:-} // While updating the folds lines between invalid_top and invalid_bot have | 675{7:2} // an undefined fold level. Otherwise update the folds first. | 676{7:+}{13:+--- 2 lines: if (invalid_top == 0) {·········································}| 677{7:+}{13:+--- 2 lines: } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) {···········}| 678{7:+}{13:+--- 2 lines: } else if (lnum >= invalid_top && lnum <= invalid_bot) {········}| 679{7:│} } | 680{7:│} | 681{7:│} // Return quickly when there is no folding at all in this window. | 682{7:+}{13:+--- 2 lines: if (!hasAnyFolding(curwin)) {···································}| 683{7:│} } | 684{7:│} | 685{7:│} return foldLevelWin(curwin, lnum); | 686{7: }^} | 687{1:~ }|*5 688| 689]] , 690} ) 691end ) 692 693it ( 'is deferred when the buffer is not up-to-date' , function () 694exec_lua ( function () 695vim . lsp . foldclose ( 'comment' ) 696vim . lsp . util . buf_versions [ bufnr ] =0 697end ) 698screen : expect ( { 699grid =[[ 700{7:+}{13:+-- 2 lines: foldLevel()······················································}| 701{7: }static int foldLevel(linenr_T lnum) | 702{7:-}{ | 703{7:+}{13:+--- 2 lines: While updating the folds lines between invalid_top and invalid_b}| 704{7:-} if (invalid_top == 0) { | 705{7:2} checkupdate(curwin); | 706{7:-} } else if (lnum == prev_lnum && prev_lnum_lvl >= 0) { | 707{7:2} return prev_lnum_lvl; | 708{7:-} } else if (lnum >= invalid_top && lnum <= invalid_bot) { | 709{7:2} return -1; | 710{7:│} } | 711{7:│} | 712{7:│} // Return quickly when there is no folding at all in this window. | 713{7:-} if (!hasAnyFolding(curwin)) { | 714{7:2} return 0; | 715{7:│} } | 716{7:│} | 717{7:│} return foldLevelWin(curwin, lnum); | 718{7: }^} | 719{1:~ }|*3 720| 721]] , 722} ) 723end ) 724end ) 725end ) 726 727describe ( 'vim.lsp nested folding ranges' , function () 728local bufnr ---@type integer 729 730local function start_server ( text , ranges ) 731insert ( text ) 732exec_lua ( function ( ranges ) 733_G . server =_G . _create_server ( { 734capabilities ={ 735foldingRangeProvider =true , 736} , 737handlers ={ 738[ 'textDocument/foldingRange' ] =function ( _ , _ , callback ) 739callback ( nil , ranges ) 740end , 741} , 742} ) 743 744vim . api . nvim_win_set_buf ( 0 , bufnr ) 745vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 746end , ranges ) 747command ( 748[[set foldmethod=expr foldexpr=v:lua.vim.lsp.foldexpr() foldtext=v:lua.vim.lsp.foldtext() foldminlines=0]] 749) 750end 751 752before_each ( function () 753clear_notrace () 754exec_lua ( create_server_definition ) 755bufnr =api . nvim_get_current_buf () 756end ) 757after_each ( function () 758api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 759end ) 760 761it ( 'uses the outermost level when nested ranges end on the same row' , function () 762start_server ( 763dedent ( [=[ 764local function first() 765return { 766child = { 767value = true, 768} } end; local function second() return { 769child = { 770value = false, 771}, 772} 773end 774]=] ), 775{ 776{ startLine =0 , endLine =3 , kind ='region' } , 777{ startLine =4 , endLine =8 , kind ='region' } , 778{ startLine =1 , endLine =3 , kind ='region' } , 779{ startLine =4 , endLine =7 , kind ='region' } , 780{ startLine =2 , endLine =3 , kind ='region' } , 781{ startLine =5 , endLine =6 , kind ='region' } , 782} 783) 784 785retry ( nil , nil , function () 786eq ( 787{ '>1' , '>2' , '>3' , '<1' , '>2' , '>3' , '<3' , '<2' , '<1' , '0' } , 788exec_lua ( function () 789local levels ={} 790for lnum =1 , 10 do 791levels [ lnum ] =vim . lsp . foldexpr ( lnum ) 792end 793return levels 794end ) 795) 796end ) 797 798exec_lua ( function () 799vim . _foldupdate ( vim . api . nvim_get_current_win (), 0 , vim . api . nvim_buf_line_count ( 0 )) 800end ) 801command ( 'normal! zM' ) 802eq ( 803{ 1 , 4 , 5 , 9 } , 804exec_lua ( function () 805return { 806vim . fn . foldclosed ( 1 ), 807vim . fn . foldclosedend ( 1 ), 808vim . fn . foldclosed ( 5 ), 809vim . fn . foldclosedend ( 5 ), 810} 811end ) 812) 813end ) 814 815it ( 'prefers a fold start when ranges end and start on the same row' , function () 816start_server ( 'one\ntwo\nthree\nfour\nfive' , { 817{ startLine =0 , endLine =4 } , 818{ startLine =2 , endLine =4 } , 819{ startLine =0 , endLine =2 } , 820} ) 821 822retry ( nil , nil , function () 823eq ( 824{ '>2' , '2' , '>3' , '2' , '<1' } , 825exec_lua ( function () 826local levels ={} 827for lnum =1 , 5 do 828levels [ lnum ] =vim . lsp . foldexpr ( lnum ) 829end 830return levels 831end ) 832) 833end ) 834end ) 835end )