robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
9a4b38584c
master
1" Test for folding 2 3source check.vim 4source view_util.vim 5source screendump.vim 6 7func PrepIndent ( arg ) 8return [ a: arg ] + repeat ([ "\t" . a: arg ], 5 ) 9endfu 10 11func Test_address_fold_new_default_commentstring () 12" Test with the new commentstring defaults, that includes padding after v9.1.464 13new 14call setline ( 1 , [ 'int FuncName() {/* {{{ */' , 1 , 2 , 3 , 4 , 5 , '}/* }}} */' , 15 \'after fold 1' , 'after fold 2' , 'after fold 3' ]) 16setl fen fdm = marker 17" The next commands should all copy the same part of the buffer, 18" regardless of the addressing type, since the part to be copied 19" is folded away 20: 1 y 21call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 22: .y 23call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 24: .+ y 25call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 26: ., .y 27call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 28: sil .1 , .y 29call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 30" use silent to make E493 go away 31: sil .+ , .y 32call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 33:, y 34call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 35:, + y 36call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' , 'after fold 1' ], getreg ( 0 , 1 , 1 )) 37" using .+3 as second address should c opy the whole folded line + the next 3 38" lines 39: ., +3 y 40call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' , 41 \'after fold 1' , 'after fold 2' , 'after fold 3' ], getreg ( 0 , 1 , 1 )) 42: sil ., -2 y 43call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' , '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 44 45" now test again with folding disabled 46set nofoldenable 47: 1 y 48call assert_equal ([ 'int FuncName() {/* {{{ */' ], getreg ( 0 , 1 , 1 )) 49: .y 50call assert_equal ([ 'int FuncName() {/* {{{ */' ], getreg ( 0 , 1 , 1 )) 51: .+ y 52call assert_equal ([ '1' ], getreg ( 0 , 1 , 1 ) ) 53: ., .y 54call assert_equal ([ 'int FuncName() {/* {{{ */' ], getreg ( 0 , 1 , 1 )) 55" use silent to make E493 go away 56: sil .1 , .y 57call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' ], getreg ( 0 , 1 , 1 )) 58" use silent to make E493 go away 59: sil .+ , .y 60call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' ], getreg ( 0 , 1 , 1 )) 61:, y 62call assert_equal ([ 'int FuncName() {/* {{{ */' ], getreg ( 0 , 1 , 1 )) 63:, + y 64call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' ], getreg ( 0 , 1 , 1 )) 65" using .+3 as second address should c opy the whole folded line + the next 3 66" lines 67: ., +3 y 68call assert_equal ([ 'int FuncName() {/* {{{ */' , '1' , '2' , '3' ], getreg ( 0 , 1 , 1 )) 69: 7 70: sil ., -2 y 71call assert_equal ([ '4' , '5' , '}/* }}} */' ], getreg ( 0 , 1 , 1 )) 72 73quit ! 74endfunc 75 76func Test_address_fold_old_default_commentstring () 77" Test with the old commentstring defaults, before v9.1.464 78new 79call setline ( 1 , [ 'int FuncName() {/*{{{*/' , 1 , 2 , 3 , 4 , 5 , '}/*}}}*/' , 80 \'after fold 1' , 'after fold 2' , 'after fold 3' ]) 81setl fen fdm = marker 82" The next commands should all copy the same part of the buffer, 83" regardless of the addressing type, since the part to be copied 84" is folded away 85: 1 y 86call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 87: .y 88call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 89: .+ y 90call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 91: ., .y 92call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 93: sil .1 , .y 94call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 95" use silent to make E493 go away 96: sil .+ , .y 97call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 98:, y 99call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 100:, + y 101call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' , 'after fold 1' ], getreg ( 0 , 1 , 1 )) 102" using .+3 as second address should copy the whole folded line + the next 3 103" lines 104: ., +3 y 105call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' , 106 \'after fold 1' , 'after fold 2' , 'after fold 3' ], getreg ( 0 , 1 , 1 )) 107: sil ., -2 y 108call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' , '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 109 110" now test again with folding disabled 111set nofoldenable 112: 1 y 113call assert_equal ([ 'int FuncName() {/*{{{*/' ], getreg ( 0 , 1 , 1 )) 114: .y 115call assert_equal ([ 'int FuncName() {/*{{{*/' ], getreg ( 0 , 1 , 1 )) 116: .+ y 117call assert_equal ([ '1' ], getreg ( 0 , 1 , 1 )) 118: ., .y 119call assert_equal ([ 'int FuncName() {/*{{{*/' ], getreg ( 0 , 1 , 1 )) 120" use silent to make E493 go away 121: sil .1 , .y 122call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' ], getreg ( 0 , 1 , 1 )) 123" use silent to make E493 go away 124: sil .+ , .y 125call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' ], getreg ( 0 , 1 , 1 )) 126:, y 127call assert_equal ([ 'int FuncName() {/*{{{*/' ], getreg ( 0 , 1 , 1 )) 128:, + y 129call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' ], getreg ( 0 , 1 , 1 )) 130" using .+3 as second address should copy the whole folded line + the next 3 131" lines 132: ., +3 y 133call assert_equal ([ 'int FuncName() {/*{{{*/' , '1' , '2' , '3' ], getreg ( 0 , 1 , 1 )) 134: 7 135: sil ., -2 y 136call assert_equal ([ '4' , '5' , '}/*}}}*/' ], getreg ( 0 , 1 , 1 )) 137 138quit ! 139endfunc 140 141func Test_address_offsets () 142" check the help for :range-closed-fold 143enew 144call setline ( 1 , [ 145 \'1 one' , 146 \'2 two' , 147 \'3 three' , 148 \'4 four FOLDED' , 149 \'5 five FOLDED' , 150 \'6 six' , 151 \'7 seven' , 152 \'8 eight' , 153 \]) 154set foldmethod = manual 155normal 4Gvjzf 1563 , 4+2 yank 157call assert_equal ([ 158 \'3 three' , 159 \'4 four FOLDED' , 160 \'5 five FOLDED' , 161 \'6 six' , 162 \'7 seven' , 163 \], getreg ( 0 , 1 , 1 )) 164 165enew ! 166call setline ( 1 , [ 167 \'1 one' , 168 \'2 two' , 169 \'3 three FOLDED' , 170 \'4 four FOLDED' , 171 \'5 five FOLDED' , 172 \'6 six FOLDED' , 173 \'7 seven' , 174 \'8 eight' , 175 \]) 176normal 3Gv3jzf 1772 , 4-1 yank 178call assert_equal ([ 179 \'2 two' , 180 \'3 three FOLDED' , 181 \'4 four FOLDED' , 182 \'5 five FOLDED' , 183 \'6 six FOLDED' , 184 \], getreg ( 0 , 1 , 1 )) 185 186bwipe ! 187endfunc 188 189func Test_indent_fold () 190new 191call setline ( 1 , [ '' , 'a' , ' b' , ' c' ]) 192setl fen fdm = indent 1932 194norm ! >> 195let a = map ( range ( 1 , 4 ), 'foldclosed(v:val)' ) 196call assert_equal ([ -1 , -1 , -1 , -1 ], a ) 197bw ! 198endfunc 199 200func Test_indent_fold2 () 201new 202call setline ( 1 , [ '' , '{{{' , '}}}' , '{{{' , '}}}' ]) 203setl fen fdm = marker 2042 205norm ! >> 206let a = map ( range ( 1 , 5 ), 'v:val->foldclosed()' ) 207call assert_equal ([ -1 , -1 , -1 , 4 , 4 ], a ) 208bw ! 209endfunc 210 211" Test for fold indent with indents greater than 'foldnestmax' 212func Test_indent_fold_max () 213new 214setlocal foldmethod = indent 215setlocal shiftwidth = 2 216" 'foldnestmax' default value is 20 217call setline ( 1 , "\t\t\t\t\t\ta" ) 218call assert_equal ( 20 , foldlevel ( 1 )) 219setlocal foldnestmax = 10 220call assert_equal ( 10 , foldlevel ( 1 )) 221setlocal foldnestmax = -1 222call assert_equal ( 0 , foldlevel ( 1 )) 223bw ! 224endfunc 225 226func Test_indent_fold_tabstop () 227call setline ( 1 , [ '0' , ' 1' , ' 1' , "\t2" , "\t2" ]) 228setlocal shiftwidth = 4 229setlocal foldcolumn = 1 230setlocal foldlevel = 2 231setlocal foldmethod = indent 232redraw 233call assert_equal ( '2 2' , ScreenLines ( 5 , 10 )[ 0 ]) 234vsplit 235windo diffthis 236botright new 237" This 'tabstop' value should not be used for folding in other buffers. 238setlocal tabstop = 4 239diffoff ! 240redraw 241call assert_equal ( '2 2' , ScreenLines ( 5 , 10 )[ 0 ]) 242 243bwipe ! 244bwipe ! 245endfunc 246 247func Test_manual_fold_with_filter () 248CheckExecutable cat 249for type in [ 'manual' , 'marker' ] 250exe 'set foldmethod=' . type 251new 252call setline ( 1 , range ( 1 , 20 )) 2534 , $ fold 254 %foldopen 25510 , $ fold 256 %foldopen 257" This filter command should not have an effect 2581 , 8 ! cat 259call feedkeys ( '5ggzdzMGdd' , 'xt' ) 260call assert_equal ([ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ], getline ( 1 , '$' )) 261 262bwipe ! 263set foldmethod & 264endfor 265endfunc 266 267func Test_indent_fold_with_read () 268new 269set foldmethod = indent 270call setline ( 1 , repeat ([ "\<Tab>a" ], 4 )) 271for n in range ( 1 , 4 ) 272call assert_equal ( 1 , foldlevel ( n )) 273endfor 274 275call writefile ([ "a" , "" , "\<Tab>a" ], 'Xfile' ) 276foldopen 2772 read Xfile 278 %foldclose 279call assert_equal ( 1 , foldlevel ( 1 )) 280call assert_equal ( 2 , foldclosedend ( 1 )) 281call assert_equal ( 0 , foldlevel ( 3 )) 282call assert_equal ( 0 , foldlevel ( 4 )) 283call assert_equal ( 1 , foldlevel ( 5 )) 284call assert_equal ( 7 , 5 ->foldclosedend ()) 285 286bwipe ! 287set foldmethod & 288call delete ( 'Xfile' ) 289endfunc 290 291func Test_combining_folds_indent () 292new 293let one = "\<Tab>a" 294let zero = 'a' 295call setline ( 1 , [ one , one , zero , zero , zero , one , one , one ]) 296set foldmethod = indent 2973 , 5 d 298 %foldclose 299call assert_equal ( 5 , foldclosedend ( 1 )) 300 301set foldmethod & 302bwipe ! 303endfunc 304 305func Test_combining_folds_marker () 306new 307call setline ( 1 , [ '{{{' , '}}}' , '' , '' , '' , '{{{' , '' , '}}}' ]) 308set foldmethod = marker 3093 , 5 d 310 %foldclose 311call assert_equal ( 2 , foldclosedend ( 1 )) 312 313set foldmethod & 314bwipe ! 315endfunc 316 317func Test_folds_marker_in_comment () 318new 319call setline ( 1 , [ '" foo' , 'bar' , 'baz' ]) 320setl fen fdm = marker 321setl com = sO:\"\ -,mO:\"\ \ ,eO:\"\",:\"cms = \"%s 322norm ! zf2j 323setl nofen 324: 1 y 325call assert_equal ([ '" foo{{{' ], getreg ( 0 , 1 , 1 )) 326: +2 y 327call assert_equal ([ 'baz"}}}' ], getreg ( 0 , 1 , 1 )) 328 329set foldmethod & 330bwipe ! 331endfunc 332 333func s: TestFoldExpr ( lnum ) 334let thisline = getline ( a: lnum ) 335if thisline == 'a' 336return 1 337elseif thisline == 'b' 338return 0 339elseif thisline == 'c' 340return '<1' 341elseif thisline == 'd' 342return '>1' 343endif 344return 0 345endfunction 346 347func Test_update_folds_expr_read () 348new 349call setline ( 1 , [ 'a' , 'a' , 'a' , 'a' , 'a' , 'a' ]) 350set foldmethod = expr 351set foldexpr = s :TestFoldExpr(v:lnum) 3522 353foldopen 354call writefile ([ 'b' , 'b' , 'a' , 'a' , 'd' , 'a' , 'a' , 'c' ], 'Xfile' ) 355read Xfile 356 %foldclose 357call assert_equal ( 2 , foldclosedend ( 1 )) 358call assert_equal ( 0 , foldlevel ( 3 )) 359call assert_equal ( 0 , 4 ->foldlevel ()) 360call assert_equal ( 6 , foldclosedend ( 5 )) 361call assert_equal ( 10 , foldclosedend ( 7 )) 362call assert_equal ( 14 , foldclosedend ( 11 )) 363 364call delete ( 'Xfile' ) 365bwipe ! 366set foldmethod & foldexpr & 367endfunc 368 369" Test for what patch 8.1.0535 fixes. 370func Test_foldexpr_no_interrupt_addsub () 371new 372func ! FoldFunc () 373call setpos ( '.' , getcurpos ()) 374return '=' 375endfunc 376 377set foldmethod = expr 378set foldexpr = FoldFunc () 379call setline ( 1 , '1.2' ) 380 381exe "norm! $\<C-A>" 382call assert_equal ( '1.3' , getline ( 1 )) 383 384bwipe ! 385delfunc FoldFunc 386set foldmethod & foldexpr & 387endfunc 388 389" Fold function defined in another script 390func Test_foldexpr_compiled () 391throw 'Skipped: Vim9 script is N/A' 392new 393let lines =<< trim END 394vim9script 395def FoldFunc(): number 396return v:lnum 397enddef 398 399set foldmethod=expr 400set foldexpr=s:FoldFunc() 401END 402call writefile ( lines , 'XfoldExpr' , 'D' ) 403source XfoldExpr 404 405call setline ( 1 , [ 'one' , 'two' , 'three' ]) 406redraw 407call assert_equal ( 1 , foldlevel ( 1 )) 408call assert_equal ( 2 , foldlevel ( 2 )) 409call assert_equal ( 3 , foldlevel ( 3 )) 410 411bwipe ! 412set foldmethod & foldexpr & 413endfunc 414 415func Check_foldlevels ( expected ) 416call assert_equal ( a: expected , map ( range ( 1 , line ( '$' )), 'foldlevel(v:val)' )) 417endfunc 418 419func Test_move_folds_around_manual () 420new 421let input = PrepIndent ( "a" ) + PrepIndent ( "b" ) + PrepIndent ( "c" ) 422call setline ( 1 , PrepIndent ( "a" ) + PrepIndent ( "b" ) + PrepIndent ( "c" )) 423let folds = [ -1 , 2 , 2 , 2 , 2 , 2 , -1 , 8 , 8 , 8 , 8 , 8 , -1 , 14 , 14 , 14 , 14 , 14 ] 424" all folds closed 425set foldenable foldlevel = 0 fdm = indent 426" needs a forced redraw 427redraw ! 428set fdm = manual 429call assert_equal ( folds , map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 430call assert_equal ( input , getline ( 1 , '$' )) 4317 , 12 m 0 432call assert_equal ( PrepIndent ( "b" ) + PrepIndent ( "a" ) + PrepIndent ( "c" ), getline ( 1 , '$' )) 433call assert_equal ( folds , map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 43410 , 12 m 0 435call assert_equal ( PrepIndent ( "a" )[ 1 :] + PrepIndent ( "b" ) + [ "a" ] + PrepIndent ( "c" ), getline ( 1 , '$' )) 436call assert_equal ([ 1 , 1 , 1 , 1 , 1 , -1 , 7 , 7 , 7 , 7 , 7 , -1 , -1 , 14 , 14 , 14 , 14 , 14 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 437" moving should not close the folds 438 %d 439call setline ( 1 , PrepIndent ( "a" ) + PrepIndent ( "b" ) + PrepIndent ( "c" )) 440set fdm = indent 441redraw ! 442set fdm = manual 443call cursor ( 2 , 1 ) 444 %foldopen 4457 , 12 m 0 446let folds = repeat ([ -1 ], 18 ) 447call assert_equal ( PrepIndent ( "b" ) + PrepIndent ( "a" ) + PrepIndent ( "c" ), getline ( 1 , '$' )) 448call assert_equal ( folds , map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 449norm ! zM 450" folds are not corrupted and all have been closed 451call assert_equal ([ -1 , 2 , 2 , 2 , 2 , 2 , -1 , 8 , 8 , 8 , 8 , 8 , -1 , 14 , 14 , 14 , 14 , 14 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 452 %d 453call setline ( 1 , [ "a" , "\tb" , "\tc" , "\td" , "\te" ]) 454set fdm = indent 455redraw ! 456set fdm = manual 457 %foldopen 4583 m 4 459 %foldclose 460call assert_equal ([ "a" , "\tb" , "\td" , "\tc" , "\te" ], getline ( 1 , '$' )) 461call assert_equal ([ -1 , 5 , 5 , 5 , 5 ], map ( range ( 1 , line ( '$' )), 'foldclosedend(v:val)' )) 462 %d 463call setline ( 1 , [ "a" , "\tb" , "\tc" , "\td" , "\te" , "z" , "\ty" , "\tx" , "\tw" , "\tv" ]) 464set fdm = indentfoldlevel = 0 465set fdm = manual 466 %foldopen 4673 m 1 468 %foldclose 469call assert_equal ([ "a" , "\tc" , "\tb" , "\td" , "\te" , "z" , "\ty" , "\tx" , "\tw" , "\tv" ], getline ( 1 , '$' )) 470call assert_equal ( 0 , foldlevel ( 2 )) 471call assert_equal ( 5 , foldclosedend ( 3 )) 472call assert_equal ([ -1 , -1 , 3 , 3 , 3 , -1 , 7 , 7 , 7 , 7 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 4732 , 6 m $ 474 %foldclose 475call assert_equal ( 5 , foldclosedend ( 2 )) 476call assert_equal ( 0 , foldlevel ( 6 )) 477call assert_equal ( 9 , foldclosedend ( 7 )) 478call assert_equal ([ -1 , 2 , 2 , 2 , 2 , -1 , 7 , 7 , 7 , -1 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 479 480 %d 481" Ensure moving around the edges still works. 482call setline ( 1 , PrepIndent ( "a" ) + repeat ([ "a" ], 3 ) + [ "\ta" ]) 483set fdm = indentfoldlevel = 0 484set fdm = manual 485 %foldopen 4866 m $ 487" The first fold has been truncated to the 5'th line. 488" Second fold has been moved up because the moved line is now below it. 489call Check_foldlevels ([ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 ]) 490 491 %delete 492set fdm = indentfoldlevel = 0 493call setline ( 1 , [ 494 \"a" , 495 \"\ta" , 496 \"\t\ta" , 497 \"\t\ta" , 498 \"\t\ta" , 499 \"a" , 500 \"a" ]) 501set fdm = manual 502 %foldopen ! 5034 , 5 m 6 504call Check_foldlevels ([ 0 , 1 , 2 , 0 , 0 , 0 , 0 ]) 505 506 %delete 507set fdm = indent 508call setline ( 1 , [ 509 \"\ta" , 510 \"\t\ta" , 511 \"\t\ta" , 512 \"\t\ta" , 513 \"\ta" , 514 \"\t\ta" , 515 \"\t\ta" , 516 \"\t\ta" , 517 \"\ta" , 518 \"\t\ta" , 519 \"\t\ta" , 520 \"\t\ta" , 521 \"\t\ta" , 522 \"\ta" , 523 \"a" ]) 524set fdm = manual 525 %foldopen ! 52613 m 7 527call Check_foldlevels ([ 1 , 2 , 2 , 2 , 1 , 2 , 2 , 1 , 1 , 1 , 2 , 2 , 2 , 1 , 0 ]) 528 529bw ! 530endfunc 531 532func Test_move_folds_around_indent () 533new 534let input = PrepIndent ( "a" ) + PrepIndent ( "b" ) + PrepIndent ( "c" ) 535call setline ( 1 , PrepIndent ( "a" ) + PrepIndent ( "b" ) + PrepIndent ( "c" )) 536let folds = [ -1 , 2 , 2 , 2 , 2 , 2 , -1 , 8 , 8 , 8 , 8 , 8 , -1 , 14 , 14 , 14 , 14 , 14 ] 537" all folds closed 538set fdm = indent 539call assert_equal ( folds , map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 540call assert_equal ( input , getline ( 1 , '$' )) 5417 , 12 m 0 542call assert_equal ( PrepIndent ( "b" ) + PrepIndent ( "a" ) + PrepIndent ( "c" ), getline ( 1 , '$' )) 543call assert_equal ( folds , map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 54410 , 12 m 0 545call assert_equal ( PrepIndent ( "a" )[ 1 :] + PrepIndent ( "b" ) + [ "a" ] + PrepIndent ( "c" ), getline ( 1 , '$' )) 546call assert_equal ([ 1 , 1 , 1 , 1 , 1 , -1 , 7 , 7 , 7 , 7 , 7 , -1 , -1 , 14 , 14 , 14 , 14 , 14 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 547" moving should not close the folds 548 %d 549call setline ( 1 , PrepIndent ( "a" ) + PrepIndent ( "b" ) + PrepIndent ( "c" )) 550set fdm = indent 551call cursor ( 2 , 1 ) 552 %foldopen 5537 , 12 m 0 554let folds = repeat ([ -1 ], 18 ) 555call assert_equal ( PrepIndent ( "b" ) + PrepIndent ( "a" ) + PrepIndent ( "c" ), getline ( 1 , '$' )) 556call assert_equal ( folds , map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 557norm ! zM 558" folds are not corrupted and all have been closed 559call assert_equal ([ -1 , 2 , 2 , 2 , 2 , 2 , -1 , 8 , 8 , 8 , 8 , 8 , -1 , 14 , 14 , 14 , 14 , 14 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 560 %d 561call setline ( 1 , [ "a" , "\tb" , "\tc" , "\td" , "\te" ]) 562set fdm = indent 563 %foldopen 5643 m 4 565 %foldclose 566call assert_equal ([ "a" , "\tb" , "\td" , "\tc" , "\te" ], getline ( 1 , '$' )) 567call assert_equal ([ -1 , 5 , 5 , 5 , 5 ], map ( range ( 1 , line ( '$' )), 'foldclosedend(v:val)' )) 568 %d 569call setline ( 1 , [ "a" , "\tb" , "\tc" , "\td" , "\te" , "z" , "\ty" , "\tx" , "\tw" , "\tv" ]) 570set fdm = indentfoldlevel = 0 571 %foldopen 5723 m 1 573 %foldclose 574call assert_equal ([ "a" , "\tc" , "\tb" , "\td" , "\te" , "z" , "\ty" , "\tx" , "\tw" , "\tv" ], getline ( 1 , '$' )) 575call assert_equal ( 1 , foldlevel ( 2 )) 576call assert_equal ( 5 , foldclosedend ( 3 )) 577call assert_equal ([ -1 , 2 , 2 , 2 , 2 , -1 , 7 , 7 , 7 , 7 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 5782 , 6 m $ 579 %foldclose 580call assert_equal ( 9 , foldclosedend ( 2 )) 581call assert_equal ( 1 , foldlevel ( 6 )) 582call assert_equal ( 9 , foldclosedend ( 7 )) 583call assert_equal ([ -1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , -1 ], map ( range ( 1 , line ( '$' )), 'foldclosed(v:val)' )) 584" Ensure moving around the edges still works. 585 %d 586call setline ( 1 , PrepIndent ( "a" ) + repeat ([ "a" ], 3 ) + [ "\ta" ]) 587set fdm = indentfoldlevel = 0 588 %foldopen 5896 m $ 590" The first fold has been truncated to the 5'th line. 591" Second fold has been moved up because the moved line is now below it. 592call Check_foldlevels ([ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 ]) 593bw ! 594endfunc 595 596func Test_folddoopen_folddoclosed () 597new 598call setline ( 1 , range ( 1 , 9 )) 599set foldmethod = manual 6001 , 3 fold 6016 , 8 fold 602 603" Test without range. 604folddoopen s/$/o/ 605folddoclosed s/$/c/ 606call assert_equal ([ '1c' , '2c' , '3c' , 607 \'4o' , '5o' , 608 \'6c' , '7c' , '8c' , 609 \'9o' ], getline ( 1 , '$' )) 610 611" Test with range. 612call setline ( 1 , range ( 1 , 9 )) 6131 , 8 folddoopen s/$/o/ 6144 , $ folddoclosed s/$/c/ 615call assert_equal ([ '1' , '2' , '3' , 616 \'4o' , '5o' , 617 \'6c' , '7c' , '8c' , 618 \'9' ], getline ( 1 , '$' )) 619 620set foldmethod & 621bw ! 622endfunc 623 624func Test_fold_error () 625new 626call setline ( 1 , [ 1 , 2 ]) 627 628for fm in [ 'indent' , 'expr' , 'syntax' , 'diff' ] 629exe 'set foldmethod=' . fm 630call assert_fails ( 'norm zf' , 'E350:' ) 631call assert_fails ( 'norm zd' , 'E351:' ) 632call assert_fails ( 'norm zE' , 'E352:' ) 633endfor 634 635set foldmethod = manual 636call assert_fails ( 'norm zd' , 'E490:' ) 637call assert_fails ( 'norm zo' , 'E490:' ) 638call assert_fails ( '3fold' , 'E16:' ) 639 640set foldmethod = marker 641set nomodifiable 642call assert_fails ( '1,2fold' , 'E21:' ) 643 644set modifiable & 645set foldmethod & 646bw ! 647endfunc 648 649func Test_foldtext_recursive () 650new 651call setline ( 1 , [ '{{{' , 'some text' , '}}}' ]) 652setlocal foldenable foldmethod = markerfoldtext = foldtextresult (v\:foldstart) 653" This was crashing because of endless recursion. 6542 foldclose 655redraw 656call assert_equal ( 1 , foldlevel ( 2 )) 657call assert_equal ( 1 , foldclosed ( 2 )) 658call assert_equal ( 3 , foldclosedend ( 2 )) 659bwipe ! 660endfunc 661 662" Various fold related tests 663 664" Basic test if a fold can be created, opened, moving to the end and closed 665func Test_fold_manual () 666new 667set fdm = manual 668 669let content = [ '1 aa' , '2 bb' , '3 cc' ] 670call append ( 0 , content ) 671call cursor ( 1 , 1 ) 672normal zf2j 673call assert_equal ( '1 aa' , getline ( foldclosed ( '.' ))) 674normal zo 675call assert_equal ( -1 , foldclosed ( '.' )) 676normal ]z 677call assert_equal ( '3 cc' , getline ( '.' )) 678normal zc 679call assert_equal ( '1 aa' , getline ( foldclosed ( '.' ))) 680 681" Create a fold inside a closed fold after setting 'foldlevel' 682 %d _ 683call setline ( 1 , range ( 1 , 5 )) 6841 , 5 fold 685normal zR 6862 , 4 fold 687set foldlevel = 1 6883 fold 689call assert_equal ([ 1 , 3 , 3 , 3 , 1 ], map ( range ( 1 , 5 ), { ->foldlevel ( v: val )})) 690set foldlevel & 691 692" Create overlapping folds (at the start and at the end) 693normal zE 6942 , 3 fold 695normal zR 6963 , 4 fold 697call assert_equal ([ 0 , 2 , 2 , 1 , 0 ], map ( range ( 1 , 5 ), { ->foldlevel ( v: val )})) 698normal zE 6993 , 4 fold 700normal zR 7012 , 3 fold 702call assert_equal ([ 0 , 1 , 2 , 2 , 0 ], map ( range ( 1 , 5 ), { ->foldlevel ( v: val )})) 703 704" Create a nested fold across two non-adjoining folds 705 %d _ 706call setline ( 1 , range ( 1 , 7 )) 7071 , 2 fold 708normal zR 7094 , 5 fold 710normal zR 7116 , 7 fold 712normal zR 7131 , 5 fold 714call assert_equal ([ 2 , 2 , 1 , 2 , 2 , 1 , 1 ], 715 \map ( range ( 1 , 7 ), { ->foldlevel ( v: val )})) 716 717" A newly created nested fold should be closed 718 %d _ 719call setline ( 1 , range ( 1 , 6 )) 7201 , 6 fold 721normal zR 7223 , 4 fold 723normal zR 7242 , 5 fold 725call assert_equal ([ 1 , 2 , 3 , 3 , 2 , 1 ], map ( range ( 1 , 6 ), { ->foldlevel ( v: val )})) 726call assert_equal ( 2 , foldclosed ( 4 )) 727call assert_equal ( 5 , foldclosedend ( 4 )) 728 729" Test zO, zC and zA on a line with no folds. 730normal zE 731call assert_fails ( 'normal zO' , 'E490:' ) 732call assert_fails ( 'normal zC' , 'E490:' ) 733call assert_fails ( 'normal zA' , 'E490:' ) 734 735set fdm & 736bw ! 737endfunc 738 739" test folding with markers. 740func Test_fold_marker () 741new 742set fdm = markerfdl = 1 fdc = 3 743 744let content = [ '4 dd {{{' , '5 ee {{{ }}}' , '6 ff }}}' ] 745call append ( 0 , content ) 746call cursor ( 2 , 1 ) 747call assert_equal ( 2 , foldlevel ( '.' )) 748normal [z 749call assert_equal ( 1 , foldlevel ( '.' )) 750exe "normal jo{{ \<Esc>r{jj" 751call assert_equal ( 1 , foldlevel ( '.' )) 752normal kYpj 753call assert_equal ( 0 , foldlevel ( '.' )) 754 755" Use only closing fold marker (without and with a count) 756set fdl & 757 %d _ 758call setline ( 1 , [ 'one }}}' , 'two' ]) 759call assert_equal ([ 0 , 0 ], [ foldlevel ( 1 ), foldlevel ( 2 )]) 760 %d _ 761call setline ( 1 , [ 'one }}}4' , 'two' ]) 762call assert_equal ([ 4 , 3 ], [ foldlevel ( 1 ), foldlevel ( 2 )]) 763 764set fdm & fdl & fdc & 765bw ! 766endfunc 767 768" test create fold markers with C filetype 769func Test_fold_create_marker_in_C () 770bw ! 771set fdm = markerfdl = 9 772set filetype = c 773 774let content =<< trim [CODE] 775/* 776* comment 777* 778* 779*/ 780int f(int* p) { 781*p = 3; 782return 0; 783} 784[CODE] 785 786for c in range ( len ( content ) - 1 ) 787bw ! 788call append ( 0 , content ) 789call cursor ( c + 1 , 1 ) 790norm ! zfG 791call assert_equal ( content [ c ] . ( c < 4 ? '{{{' : '/* {{{ */' ), getline ( c + 1 )) 792endfor 793 794set fdm & fdl & 795bw ! 796endfunc 797 798" test folding with indent 799func Test_fold_indent () 800new 801set fdm = indentsw = 2 802 803let content = [ '1 aa' , '2 bb' , '3 cc' ] 804call append ( 0 , content ) 805call cursor ( 2 , 1 ) 806exe "normal i \<Esc>jI " 807call assert_equal ( 2 , foldlevel ( '.' )) 808normal k 809call assert_equal ( 1 , foldlevel ( '.' )) 810 811set fdm & sw & 812bw ! 813endfunc 814 815" test syntax folding 816func Test_fold_syntax () 817CheckFeature syntax 818 819new 820set fdm = syntaxfdl = 0 821 822syn region Hup start = "dd "end = "ii "fold contains = Fd1 , Fd2 , Fd3 823syn region Fd1 start = "ee "end = "ff "fold contained 824syn region Fd2 start = "gg "end = "hh "fold contained 825syn region Fd3 start = "commentstart "end = "commentend "fold contained 826let content = [ '3 cc' , '4 dd {{{' , '5 ee {{{ }}}' , '{{{{' , '6 ff }}}' , 827 \'6 ff }}}' , '7 gg' , '8 hh' , '9 ii' ] 828call append ( 0 , content ) 829normal Gzk 830call assert_equal ( '9 ii' , getline ( '.' )) 831normal k 832call assert_equal ( '3 cc' , getline ( '.' )) 833exe "normal jAcommentstart \<Esc>Acommentend" 834set fdl = 1 835normal 3j 836call assert_equal ( '7 gg' , getline ( '.' )) 837set fdl = 0 838exe "normal zO\<C-L>j" 839call assert_equal ( '8 hh' , getline ( '.' )) 840syn clear Fd1 Fd2Fd3 Hup 841 842set fdm & fdl & 843bw ! 844endfunc 845 846func Flvl () 847let l = getline ( v: lnum ) 848if l =~ "bb$" 849return 2 850elseif l =~ "gg$" 851return "s1" 852elseif l =~ "ii$" 853return ">2" 854elseif l =~ "kk$" 855return "0" 856endif 857return "=" 858endfun 859 860" test expression folding 861func Test_fold_expr () 862new 863set fdm = exprfde = Flvl () 864 865let content = [ '1 aa' , 866 \'2 bb' , 867 \'3 cc' , 868 \'4 dd {{{commentstart commentend' , 869 \'5 ee {{{ }}}' , 870 \'{{{' , 871 \'6 ff }}}' , 872 \'6 ff }}}' , 873 \' 7 gg' , 874 \' 8 hh' , 875 \'9 ii' , 876 \'a jj' , 877 \'b kk' ] 878call append ( 0 , content ) 879call cursor ( 1 , 1 ) 880exe "normal /bb$\<CR>" 881call assert_equal ( 2 , foldlevel ( '.' )) 882exe "normal /hh$\<CR>" 883call assert_equal ( 1 , foldlevel ( '.' )) 884exe "normal /ii$\<CR>" 885call assert_equal ( 2 , foldlevel ( '.' )) 886exe "normal /kk$\<CR>" 887call assert_equal ( 0 , foldlevel ( '.' )) 888 889set fdm & fde & 890bw ! 891endfunc 892 893" Bug with fdm=indent and moving folds 894" Moving a fold a few times, messes up the folds below the moved fold. 895" Fixed by 7.4.700 896func Test_fold_move () 897new 898set fdm = indentsw = 2 fdl = 0 899 900let content = [ '' , '' , 'Line1' , ' Line2' , ' Line3' , 901 \'Line4' , ' Line5' , ' Line6' , 902 \'Line7' , ' Line8' , ' Line9' ] 903call append ( 0 , content ) 904normal zM 905call cursor ( 4 , 1 ) 906move 2 907move 1 908call assert_equal ( 7 , foldclosed ( 7 )) 909call assert_equal ( 8 , foldclosedend ( 7 )) 910call assert_equal ( 0 , foldlevel ( 9 )) 911call assert_equal ( 10 , foldclosed ( 10 )) 912call assert_equal ( 11 , foldclosedend ( 10 )) 913call assert_equal ( '+-- 2 lines: Line2' , foldtextresult ( 2 )) 914call assert_equal ( '+-- 2 lines: Line8' , 10 ->foldtextresult ()) 915 916set fdm & sw & fdl & 917bw ! 918endfunc 919 920" test for patch 7.3.637 921" Cannot catch the error caused by a foldopen when there is no fold. 922func Test_foldopen_exception () 923new 924let a = 'No error caught' 925try 926foldopen 927catch 928let a = matchstr ( v: exception , '^[^ ]*' ) 929endtry 930call assert_equal ( 'Vim(foldopen):E490:' , a ) 931 932let a = 'No error caught' 933try 934foobar 935catch 936let a = matchstr ( v: exception , '^[^ ]*' ) 937endtry 938call assert_match ( 'E492:' , a ) 939bw ! 940endfunc 941 942func Test_fold_last_line_with_pagedown () 943new 944set fdm = manual 945 946let expect = '+-- 11 lines: 9---' 947let content = range ( 1 , 19 ) 948call append ( 0 , content ) 949normal dd9G 950normal zfG 951normal zt 952call assert_equal ( '9' , getline ( foldclosed ( '.' ))) 953call assert_equal ( '19' , getline ( foldclosedend ( '.' ))) 954call assert_equal ( expect , ScreenLines ( 1 , len ( expect ))[ 0 ]) 955call feedkeys ( "\<C-F>" , 'xt' ) 956call assert_equal ( expect , ScreenLines ( 1 , len ( expect ))[ 0 ]) 957call feedkeys ( "\<C-F>" , 'xt' ) 958call assert_equal ( expect , ScreenLines ( 1 , len ( expect ))[ 0 ]) 959call feedkeys ( "\<C-B>\<C-F>\<C-F>" , 'xt' ) 960call assert_equal ( expect , ScreenLines ( 1 , len ( expect ))[ 0 ]) 961 962set fdm & 963bw ! 964endfunc 965 966func Test_folds_with_rnu () 967CheckScreendump 968 969call writefile ([ 970 \'set fdm=marker rnu foldcolumn=2' , 971 \'call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])' , 972 \], 'Xtest_folds_with_rnu' ) 973let buf = RunVimInTerminal ( '-S Xtest_folds_with_rnu' , {}) 974 975call VerifyScreenDump ( buf , 'Test_folds_with_rnu_01' , {}) 976call term_sendkeys ( buf , "j" ) 977call VerifyScreenDump ( buf , 'Test_folds_with_rnu_02' , {}) 978 979" clean up 980call StopVimInTerminal ( buf ) 981call delete ( 'Xtest_folds_with_rnu' ) 982endfunc 983 984func Test_folds_marker_in_comment2 () 985new 986call setline ( 1 , [ 'Lorem ipsum dolor sit' , 'Lorem ipsum dolor sit' , 'Lorem ipsum dolor sit' ]) 987setl fen fdm = marker 988setl commentstring = <!--%s--> 989setl comments = s:<!--,m:\ \ \ \ ,e:--> 990norm ! zf2j 991setl nofen 992: 1 y 993call assert_equal ([ 'Lorem ipsum dolor sit<!--{{{-->' ], getreg ( 0 , 1 , 1 )) 994: +2 y 995call assert_equal ([ 'Lorem ipsum dolor sit<!--}}}-->' ], getreg ( 0 , 1 , 1 )) 996 997set foldmethod & 998bwipe ! 999endfunc 1000 1001func Test_fold_delete_with_marker () 1002new 1003call setline ( 1 , [ 'func Func() {{{1' , 'endfunc' ]) 10041 , 2 yank 1005new 1006set fdm = marker 1007call setline ( 1 , 'x' ) 1008normal ! Vp 1009normal ! zd 1010call assert_equal ([ 'func Func() ' , 'endfunc' ], getline ( 1 , '$' )) 1011 1012set fdm & 1013bwipe ! 1014bwipe ! 1015endfunc 1016 1017func Test_fold_delete_with_marker_and_whichwrap () 1018new 1019let content1 = [ '' ] 1020let content2 = [ 'folded line 1 "{{{1' , ' test' , ' test2' , ' test3' , '' , 'folded line 2 "{{{1' , ' test' , ' test2' , ' test3' ] 1021call setline ( 1 , content1 + content2 ) 1022set fdm = markerww += l 1023normal ! x 1024call assert_equal ( content2 , getline ( 1 , '$' )) 1025set fdm & ww & 1026bwipe ! 1027endfunc 1028 1029func Test_fold_delete_first_line () 1030new 1031call setline ( 1 , [ 1032 \'" x {{{1' , 1033 \'" a' , 1034 \'" aa' , 1035 \'" x {{{1' , 1036 \'" b' , 1037 \'" bb' , 1038 \'" x {{{1' , 1039 \'" c' , 1040 \'" cc' , 1041 \]) 1042set foldmethod = marker 10431 1044normal dj 1045call assert_equal ([ 1046 \'" x {{{1' , 1047 \'" c' , 1048 \'" cc' , 1049 \], getline ( 1 , '$' )) 1050bwipe ! 1051set foldmethod & 1052endfunc 1053 1054" Add a test for deleting the outer fold of a nested fold and promoting the 1055" inner folds to one level up with already a fold at that level following the 1056" nested fold. 1057func Test_fold_delete_recursive_fold () 1058new 1059call setline ( 1 , range ( 1 , 7 )) 10602 , 3 fold 1061normal zR 10624 , 5 fold 1063normal zR 10641 , 5 fold 1065normal zR 10666 , 7 fold 1067normal zR 1068normal 1Gzd 1069normal 1Gzj 1070call assert_equal ( 2 , line ( '.' )) 1071normal zj 1072call assert_equal ( 4 , line ( '.' )) 1073normal zj 1074call assert_equal ( 6 , line ( '.' )) 1075bw ! 1076endfunc 1077 1078" Test for errors in 'foldexpr' 1079func Test_fold_expr_error () 1080new 1081call setline ( 1 , [ 'one' , 'two' , 'three' ]) 1082" In a window with no folds, foldlevel() should return 0 1083call assert_equal ( 0 , foldlevel ( 1 )) 1084 1085" Return a list from the expression 1086set foldexpr = [] 1087set foldmethod = expr 1088for i in range ( 3 ) 1089call assert_equal ( 0 , foldlevel ( i )) 1090endfor 1091 1092" expression error 1093set foldexpr = [{] 1094set foldmethod = expr 1095for i in range ( 3 ) 1096call assert_equal ( 0 , foldlevel ( i )) 1097endfor 1098 1099set foldmethod & foldexpr & 1100bw ! 1101endfunc 1102 1103func Test_undo_fold_deletion () 1104new 1105set fdm = marker 1106let lines =<< trim END 1107" {{{ 1108" }}}1 1109" {{{ 1110END 1111call setline ( 1 , lines ) 11123 d 1113g /" /d 1114undo 1115redo 1116eval getline ( 1 , '$' ) ->assert_equal ([ '' ]) 1117 1118set fdm & vim 1119bwipe ! 1120endfunc 1121 1122" this was crashing 1123func Test_move_no_folds () 1124new 1125fold 1126setlocal fdm = expr 1127normal zj 1128bwipe ! 1129endfunc 1130 1131" this was crashing 1132func Test_fold_create_delete_create () 1133new 1134fold 1135fold 1136normal zd 1137fold 1138bwipe ! 1139endfunc 1140 1141" this was crashing 1142func Test_fold_create_delete () 1143new 1144norm zFzFzdzj 1145bwipe ! 1146endfunc 1147 1148func Test_fold_relative_move () 1149new 1150set fdm = indentsw = 2 wrap tw = 80 1151 1152let longtext = repeat ( 'x' , & columns + 1 ) 1153let content = [ ' foo' , ' ' .. longtext , ' baz' , 1154 \longtext , 1155 \' foo' , ' ' .. longtext , ' baz' 1156 \] 1157call append ( 0 , content ) 1158 1159normal zM 1160 1161for lnum in range ( 1 , 3 ) 1162call cursor ( lnum , 1 ) 1163call assert_true ( foldclosed ( line ( '.' ))) 1164normal gj 1165call assert_equal ( 2 , winline ()) 1166endfor 1167 1168call cursor ( 2 , 1 ) 1169call assert_true ( foldclosed ( line ( '.' ))) 1170normal 2gj 1171call assert_equal ( 3 , winline ()) 1172 1173for lnum in range ( 5 , 7 ) 1174call cursor ( lnum , 1 ) 1175call assert_true ( foldclosed ( line ( '.' ))) 1176normal gk 1177call assert_equal ( 3 , winline ()) 1178endfor 1179 1180call cursor ( 6 , 1 ) 1181call assert_true ( foldclosed ( line ( '.' ))) 1182normal 2gk 1183call assert_equal ( 2 , winline ()) 1184 1185set fdm & sw & wrap & tw & 1186bw ! 1187endfunc 1188 1189" Test for calling foldlevel() from a fold expression 1190let g: FoldLevels = [] 1191func FoldExpr1 ( lnum ) 1192let f = [ a: lnum ] 1193for i in range ( 1 , line ( '$' )) 1194call add ( f , foldlevel ( i )) 1195endfor 1196call add ( g: FoldLevels , f ) 1197return getline ( a: lnum )[ 0 ] == "\t" 1198endfunc 1199 1200func Test_foldexpr_foldlevel () 1201new 1202call setline ( 1 , [ 'one' , "\ttwo" , "\tthree" ]) 1203setlocal foldmethod = expr 1204setlocal foldexpr = FoldExpr1 (v:lnum) 1205setlocal foldenable 1206setlocal foldcolumn = 3 1207redraw ! 1208call assert_equal ([[ 1 , -1 , -1 , -1 ], [ 2 , -1 , -1 , -1 ], [ 3 , 0 , 1 , -1 ]], 1209 \g: FoldLevels ) 1210set foldmethod & foldexpr & foldenable & foldcolumn & 1211bw ! 1212endfunc 1213 1214" Test for returning different values from a fold expression 1215func FoldExpr2 ( lnum ) 1216if a: lnum == 1 || a: lnum == 4 1217return -2 1218elseif a: lnum == 2 1219return 'a1' 1220elseif a: lnum == 3 1221return 's4' 1222endif 1223return '=' 1224endfunc 1225 1226func Test_foldexpr_2 () 1227new 1228call setline ( 1 , [ 'one' , 'two' , 'three' , 'four' ]) 1229setlocal foldexpr = FoldExpr2 (v:lnum) 1230setlocal foldmethod = expr 1231call assert_equal ([ 0 , 1 , 1 , 0 ], [ foldlevel ( 1 ), foldlevel ( 2 ), foldlevel ( 3 ), 1232 \foldlevel ( 4 )]) 1233bw ! 1234endfunc 1235 1236" Test for the 'foldclose' option 1237func Test_foldclose_opt () 1238CheckScreendump 1239 1240let lines =<< trim END 1241set foldmethod=manual foldclose=all foldopen=all 1242call setline(1, ['one', 'two', 'three', 'four']) 12432,3fold 1244func XsaveFoldLevels() 1245redraw! 1246call writefile([json_encode([foldclosed(1), foldclosed(2), foldclosed(3), 1247\ foldclosed(4)])], 'Xoutput', 'a') 1248endfunc 1249END 1250call writefile ( lines , 'Xscript' ) 1251let rows = 10 1252let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : rows }) 1253call term_wait ( buf ) 1254call term_sendkeys ( buf , ":set noruler\n" ) 1255call term_wait ( buf ) 1256call term_sendkeys ( buf , ":call XsaveFoldLevels()\n" ) 1257call term_sendkeys ( buf , "2G" ) 1258call WaitForAssert ({ ->assert_equal ( 'two' , term_getline ( buf , 2 ))}) 1259call term_sendkeys ( buf , ":call XsaveFoldLevels()\n" ) 1260call term_sendkeys ( buf , "4G" ) 1261call WaitForAssert ({ ->assert_equal ( 'four' , term_getline ( buf , 3 ))}) 1262call term_sendkeys ( buf , ":call XsaveFoldLevels()\n" ) 1263call term_sendkeys ( buf , "3G" ) 1264call WaitForAssert ({ ->assert_equal ( 'three' , term_getline ( buf , 3 ))}) 1265call term_sendkeys ( buf , ":call XsaveFoldLevels()\n" ) 1266call term_sendkeys ( buf , "1G" ) 1267call WaitForAssert ({ ->assert_equal ( 'four' , term_getline ( buf , 3 ))}) 1268call term_sendkeys ( buf , ":call XsaveFoldLevels()\n" ) 1269call term_sendkeys ( buf , "2G" ) 1270call WaitForAssert ({ ->assert_equal ( 'two' , term_getline ( buf , 2 ))}) 1271call term_sendkeys ( buf , "k" ) 1272call WaitForAssert ({ ->assert_equal ( 'four' , term_getline ( buf , 3 ))}) 1273 1274" clean up 1275call StopVimInTerminal ( buf ) 1276 1277call assert_equal ([ '[-1,2,2,-1]' , '[-1,-1,-1,-1]' , '[-1,2,2,-1]' , 1278 \'[-1,-1,-1,-1]' , '[-1,2,2,-1]' ], readfile ( 'Xoutput' )) 1279call delete ( 'Xscript' ) 1280call delete ( 'Xoutput' ) 1281endfunc 1282 1283" Test for foldtextresult() 1284func Test_foldtextresult () 1285new 1286call assert_equal ( '' , foldtextresult ( -1 )) 1287call assert_equal ( '' , foldtextresult ( 0 )) 1288call assert_equal ( '' , foldtextresult ( 1 )) 1289call setline ( 1 , [ 'one' , 'two' , 'three' , 'four' ]) 12902 , 3 fold 1291call assert_equal ( '' , foldtextresult ( 1 )) 1292call assert_equal ( '+-- 2 lines: two' , foldtextresult ( 2 )) 1293setlocal foldtext = 1294call assert_equal ( '+-- 2 lines folded ' , foldtextresult ( 2 )) 1295 1296" Fold text for a C comment fold 1297 %d _ 1298setlocal foldtext & 1299call setline ( 1 , [ '' , '/*' , ' * Comment' , ' */' , '' ]) 13002 , 4 fold 1301call assert_equal ( '+-- 3 lines: Comment' , foldtextresult ( 2 )) 1302 1303bw ! 1304endfunc 1305 1306" Test for foldtext and fillchars with 'rightleft' enabled 1307func Test_foldtext_and_fillchars_rightleft () 1308CheckFeature rightleft 1309CheckScreendump 1310CheckRunVimInTerminal 1311 1312let script_lines =<< trim END 1313let longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing' 1314let g:multibyte = 'Lorem ipsum dolor sit amet' 1315 1316call setline(1, [longtext, longtext, longtext]) 13171,2fold 1318 1319setlocal rightleft 1320set noshowmode noshowcmd 1321END 1322call writefile ( script_lines , 'XTest_foldtext_and_fillchars_rightleft' , 'D' ) 1323let buf = RunVimInTerminal ( '-S XTest_foldtext_and_fillchars_rightleft' , { 'rows' : 5 , 'cols' : 70 }) 1324 1325call VerifyScreenDump ( buf , 'Test_foldtext_and_fillchars_rightleft_01' , {}) 1326call term_sendkeys ( buf , ":call setline(1, [g:multibyte, g:multibyte, g:multibyte])\<CR>" ) 1327call VerifyScreenDump ( buf , 'Test_foldtext_and_fillchars_rightleft_02' , {}) 1328 1329" clean up 1330call StopVimInTerminal ( buf ) 1331endfunc 1332 1333" Test for foldtextresult() with 'rightleft' enabled 1334func Test_foldtextresult_rightleft () 1335CheckFeature rightleft 1336 1337let save_columns = & columns 1338new 1339set columns = 70 1340setlocal rightleft 1341 1342let longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing' 1343let multibyte = 'Lorem ipsum dolor sit amet' 1344 1345call setline ( 1 , [ longtext , longtext , longtext ]) 13461 , 2 fold 1347call assert_equal ( '+-- 2 lines: ' .. longtext , foldtextresult ( 1 )) 1348 1349call setline ( 1 , [ multibyte , multibyte , multibyte ]) 1350call assert_equal ( '+-- 2 lines: ' .. multibyte , foldtextresult ( 1 )) 1351 1352bw ! 1353let & columns = save_columns 1354endfunc 1355 1356" Test for merging two recursive folds when an intermediate line with no fold 1357" is removed 1358func Test_fold_merge_recursive () 1359new 1360call setline ( 1 , [ ' one' , ' two' , 'xxxx' , ' three' , 1361 \' four' , "\tfive" ]) 1362setlocal foldmethod = indentshiftwidth = 2 13633 d _ 1364 %foldclose 1365call assert_equal ([ 1 , 5 ], [ foldclosed ( 5 ), foldclosedend ( 1 )]) 1366bw ! 1367endfunc 1368 1369" Test for moving a line which is the start of a fold from a recursive fold to 1370" outside. The fold length should reduce. 1371func Test_fold_move_foldlevel () 1372new 1373call setline ( 1 , [ 'a{{{' , 'b{{{' , 'c{{{' , 'd}}}' , 'e}}}' , 'f}}}' , 'g' ]) 1374setlocal foldmethod = marker 1375normal zR 1376call assert_equal ([ 3 , 2 , 1 ], [ foldlevel ( 4 ), foldlevel ( 5 ), foldlevel ( 6 )]) 13773 move 7 1378call assert_equal ([ 2 , 1 , 0 ], [ foldlevel ( 3 ), foldlevel ( 4 ), foldlevel ( 5 )]) 1379call assert_equal ( 1 , foldlevel ( 7 )) 1380 1381" Move a line from outside a fold to inside the fold. 1382 %d _ 1383call setline ( 1 , [ 'a' , 'b{{{' , 'c}}}' ]) 1384normal zR 13851 move 2 1386call assert_equal ([ 1 , 1 , 1 ], [ foldlevel ( 1 ), foldlevel ( 2 ), foldlevel ( 3 )]) 1387 1388" Move the start of one fold to inside another fold 1389 %d _ 1390call setline ( 1 , [ 'a' , 'b{{{' , 'c}}}' , 'd{{{' , 'e}}}' ]) 1391normal zR 1392call assert_equal ([ 0 , 1 , 1 , 1 , 1 ], [ foldlevel ( 1 ), foldlevel ( 2 ), 1393 \foldlevel ( 3 ), foldlevel ( 4 ), foldlevel ( 5 )]) 13941 , 2 move 4 1395call assert_equal ([ 0 , 1 , 1 , 2 , 2 ], [ foldlevel ( 1 ), foldlevel ( 2 ), 1396 \foldlevel ( 3 ), foldlevel ( 4 ), foldlevel ( 5 )]) 1397 1398bw ! 1399endfunc 1400 1401" Test for using zj and zk to move downwards and upwards to the start and end 1402" of the next fold. 1403" Test for using [z and ]z in a closed fold to jump to the beginning and end 1404" of the fold. 1405func Test_fold_jump () 1406new 1407call setline ( 1 , [ "\t1" , "\t2" , "\t\t3" , "\t\t4" , "\t\t\t5" , "\t\t\t6" , "\t\t7" , "\t\t8" , "\t9" , "\t10" ]) 1408setlocal foldmethod = indent 1409normal zR 1410normal zj 1411call assert_equal ( 3 , line ( '.' )) 1412normal zj 1413call assert_equal ( 5 , line ( '.' )) 1414call assert_beeps ( 'normal zj' ) 1415call assert_equal ( 5 , line ( '.' )) 1416call assert_beeps ( 'normal 9Gzj' ) 1417call assert_equal ( 9 , line ( '.' )) 1418normal Gzk 1419call assert_equal ( 8 , line ( '.' )) 1420normal zk 1421call assert_equal ( 6 , line ( '.' )) 1422call assert_beeps ( 'normal zk' ) 1423call assert_equal ( 6 , line ( '.' )) 1424call assert_beeps ( 'normal 2Gzk' ) 1425call assert_equal ( 2 , line ( '.' )) 1426 1427" Using [z or ]z in a closed fold should not move the cursor 1428 %d _ 1429call setline ( 1 , [ "1" , "\t2" , "\t3" , "\t4" , "\t5" , "\t6" , "7" ]) 1430normal zR4Gzc 1431call assert_equal ( 4 , line ( '.' )) 1432call assert_beeps ( 'normal [z' ) 1433call assert_equal ( 4 , line ( '.' )) 1434call assert_beeps ( 'normal ]z' ) 1435call assert_equal ( 4 , line ( '.' )) 1436bw ! 1437endfunc 1438 1439" Test for using a script-local function for 'foldexpr' 1440func Test_foldexpr_scriptlocal_func () 1441func ! s: FoldFunc () 1442let g: FoldLnum = v: lnum 1443endfunc 1444new |only 1445call setline ( 1 , 'abc' ) 1446let g: FoldLnum = 0 1447set foldmethod = exprfoldexpr = s :FoldFunc() 1448redraw ! 1449call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & foldexpr ) 1450call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & g: foldexpr ) 1451call assert_equal ( 1 , g: FoldLnum ) 1452set foldmethod & foldexpr = 1453bw ! 1454new |only 1455call setline ( 1 , 'abc' ) 1456let g: FoldLnum = 0 1457set foldmethod = exprfoldexpr = <SID>FoldFunc() 1458redraw ! 1459call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & foldexpr ) 1460call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & g: foldexpr ) 1461call assert_equal ( 1 , g: FoldLnum ) 1462bw ! 1463call setline ( 1 , 'abc' ) 1464setlocal foldmethod & foldexpr & 1465setglobal foldmethod=expr foldexpr=s:FoldFunc() 1466call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & g: foldexpr ) 1467call assert_equal ( '0' , & foldexpr ) 1468enew ! 1469call setline ( 1 , 'abc' ) 1470redraw ! 1471call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & foldexpr ) 1472call assert_equal ( 1 , g: FoldLnum ) 1473bw ! 1474call setline ( 1 , 'abc' ) 1475setlocal foldmethod & foldexpr & 1476setglobal foldmethod=expr foldexpr=<SID>FoldFunc() 1477call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & g: foldexpr ) 1478call assert_equal ( '0' , & foldexpr ) 1479enew ! 1480call setline ( 1 , 'abc' ) 1481redraw ! 1482call assert_equal ( expand ( '<SID>' ) .. 'FoldFunc()' , & foldexpr ) 1483call assert_equal ( 1 , g: FoldLnum ) 1484set foldmethod & foldexpr & 1485delfunc s:FoldFunc 1486bw ! 1487endfunc 1488 1489" Test for using a script-local function for 'foldtext' 1490func Test_foldtext_scriptlocal_func () 1491func ! s: FoldText () 1492let g: FoldTextArgs = [ v: foldstart , v: foldend ] 1493return foldtext () 1494endfunc 1495new |only 1496call setline ( 1 , range ( 50 )) 1497let g: FoldTextArgs = [] 1498set foldtext = s :FoldText() 1499norm ! 4Gzf4j 1500redraw ! 1501call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & foldtext ) 1502call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & g: foldtext ) 1503call assert_equal ([ 4 , 8 ], g: FoldTextArgs ) 1504set foldtext & 1505bw ! 1506new |only 1507call setline ( 1 , range ( 50 )) 1508let g: FoldTextArgs = [] 1509set foldtext = <SID>FoldText() 1510norm ! 8Gzf4j 1511redraw ! 1512call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & foldtext ) 1513call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & g: foldtext ) 1514call assert_equal ([ 8 , 12 ], g: FoldTextArgs ) 1515set foldtext & 1516bw ! 1517call setline ( 1 , range ( 50 )) 1518let g: FoldTextArgs = [] 1519setlocal foldtext & 1520setglobal foldtext=s:FoldText() 1521call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & g: foldtext ) 1522call assert_equal ( 'foldtext()' , & foldtext ) 1523enew ! 1524call setline ( 1 , range ( 50 )) 1525norm ! 12Gzf4j 1526redraw ! 1527call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & foldtext ) 1528call assert_equal ([ 12 , 16 ], g: FoldTextArgs ) 1529set foldtext & 1530bw ! 1531call setline ( 1 , range ( 50 )) 1532let g: FoldTextArgs = [] 1533setlocal foldtext & 1534setglobal foldtext=<SID>FoldText() 1535call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & g: foldtext ) 1536call assert_equal ( 'foldtext()' , & foldtext ) 1537enew ! 1538call setline ( 1 , range ( 50 )) 1539norm ! 16Gzf4j 1540redraw ! 1541call assert_equal ( expand ( '<SID>' ) .. 'FoldText()' , & foldtext ) 1542call assert_equal ([ 16 , 20 ], g: FoldTextArgs ) 1543set foldtext & 1544bw ! 1545delfunc s:FoldText 1546endfunc 1547 1548" Test for setting 'foldtext' from the modeline and executing the expression 1549" in a sandbox 1550func Test_foldtext_in_modeline () 1551func ModelineFoldText () 1552call writefile ([ 'after' ], 'Xmodelinefoldtext_write' ) 1553return "folded text" 1554endfunc 1555let lines =<< trim END 1556func T() 1557let i = 1 1558endfunc 1559" vim: foldenable foldtext=ModelineFoldText() 1560END 1561call writefile ( lines , 'Xmodelinefoldtext' , 'D' ) 1562 1563func Check_foldtext_in_modeline ( set_cmd ) 1564call writefile ([ 'before' ], 'Xmodelinefoldtext_write' , 'D' ) 1565split Xmodelinefoldtext 1566call cursor ( 1 , 1 ) 1567normal ! zf3j 1568call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1569call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldtext_write' )) 1570 1571split 1572call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1573call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldtext_write' )) 1574close 1575 1576setglobal foldtext=ModelineFoldText() 1577call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1578call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldtext_write' )) 1579 1580setglobal foldtext& 1581call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1582call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldtext_write' )) 1583 1584exe a: set_cmd 'foldtext=ModelineFoldText()' 1585call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1586call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldtext_write' )) 1587 1588call writefile ([ 'before' ], 'Xmodelinefoldtext_write' ) 1589exe 'sandbox' a: set_cmd 'foldtext=ModelineFoldText()' 1590call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1591call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldtext_write' )) 1592 1593exe a: set_cmd 'foldtext=ModelineFoldText()' 1594call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1595call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldtext_write' )) 1596bw ! 1597endfunc 1598 1599set modeline modelineexpr 1600call Check_foldtext_in_modeline ( 'setlocal' ) 1601call Check_foldtext_in_modeline ( 'set' ) 1602 1603new Xa 1604sandbox setglobal foldenable foldtext=ModelineFoldText() 1605setlocal bufhidden = wipe 1606call writefile ([ 'before' ], 'Xmodelinefoldtext_write' , 'D' ) 1607edit ! Xb 1608call setline ( 1 , [ 'func T()' , ' let i = 1' , 'endfunc' ]) | %fold 1609call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1610call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldtext_write' )) 1611setglobal foldtext=ModelineFoldText() 1612call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1613call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldtext_write' )) 1614setlocal foldtext = ModelineFoldText () 1615call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1616call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldtext_write' )) 1617setlocal bufhidden = wipe 1618call writefile ([ 'before' ], 'Xmodelinefoldtext_write' , 'D' ) 1619edit ! Xc 1620call setline ( 1 , [ 'func T()' , ' let i = 1' , 'endfunc' ]) | %fold 1621call assert_equal ( 'folded text' , foldtextresult ( 1 )) 1622call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldtext_write' )) 1623bwipe ! 1624 1625set modeline & modelineexpr & 1626delfunc ModelineFoldText 1627delfunc Check_foldtext_in_modeline 1628endfunc 1629 1630" Test for setting 'foldexpr' from the modeline and executing the expression 1631" in a sandbox 1632func Test_foldexpr_in_modeline () 1633func ModelineFoldExpr () 1634call writefile ([ 'after' ], 'Xmodelinefoldexpr_write' ) 1635return strlen ( matchstr ( getline ( v: lnum ), '^\s*' )) 1636endfunc 1637let lines =<< trim END 1638aaa 1639bbb 1640ccc 1641ccc 1642bbb 1643aaa 1644" vim: foldenable foldmethod=expr foldexpr=ModelineFoldExpr() 1645END 1646call writefile ( lines , 'Xmodelinefoldexpr' , 'D' ) 1647 1648func Check_foldexpr_in_modeline ( set_cmd ) 1649call writefile ([ 'before' ], 'Xmodelinefoldexpr_write' , 'D' ) 1650split Xmodelinefoldexpr 1651call assert_equal ( 2 , foldlevel ( 3 )) 1652call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldexpr_write' )) 1653 1654split 1655call assert_equal ( 2 , foldlevel ( 3 )) 1656call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldexpr_write' )) 1657close 1658 1659setglobal foldexpr=ModelineFoldExpr() 1660call assert_equal ( 2 , foldlevel ( 3 )) 1661call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldexpr_write' )) 1662 1663setglobal foldexpr& 1664call assert_equal ( 2 , foldlevel ( 3 )) 1665call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldexpr_write' )) 1666 1667exe a: set_cmd 'foldexpr=ModelineFoldExpr()' 1668call assert_equal ( 2 , foldlevel ( 3 )) 1669call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldexpr_write' )) 1670 1671call writefile ([ 'before' ], 'Xmodelinefoldexpr_write' ) 1672exe 'sandbox' a: set_cmd 'foldexpr=ModelineFoldExpr()' 1673call assert_equal ( 2 , foldlevel ( 3 )) 1674call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldexpr_write' )) 1675 1676exe a: set_cmd 'foldexpr=ModelineFoldExpr()' 1677call assert_equal ( 2 , foldlevel ( 3 )) 1678call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldexpr_write' )) 1679bw ! 1680endfunc 1681 1682set modeline modelineexpr 1683call Check_foldexpr_in_modeline ( 'setlocal' ) 1684call Check_foldexpr_in_modeline ( 'set' ) 1685 1686new Xa 1687sandbox setglobal foldenable foldmethod=expr foldexpr=ModelineFoldExpr() 1688setlocal bufhidden = wipe 1689call writefile ([ 'before' ], 'Xmodelinefoldexpr_write' , 'D' ) 1690edit ! Xb 1691call setline ( 1 , lines [ 0 : 5 ]) 1692call assert_equal ( 2 , foldlevel ( 3 )) 1693call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldexpr_write' )) 1694setglobal foldexpr=ModelineFoldExpr() 1695call assert_equal ( 2 , foldlevel ( 3 )) 1696call assert_equal ([ 'before' ], readfile ( 'Xmodelinefoldexpr_write' )) 1697setlocal foldexpr = ModelineFoldExpr () 1698call assert_equal ( 2 , foldlevel ( 3 )) 1699call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldexpr_write' )) 1700setlocal bufhidden = wipe 1701call writefile ([ 'before' ], 'Xmodelinefoldexpr_write' , 'D' ) 1702edit ! Xc 1703call setline ( 1 , lines [ 0 : 5 ]) 1704call assert_equal ( 2 , foldlevel ( 3 )) 1705call assert_equal ([ 'after' ], readfile ( 'Xmodelinefoldexpr_write' )) 1706bwipe ! 1707 1708set modeline & modelineexpr & 1709delfunc ModelineFoldExpr 1710delfunc Check_foldexpr_in_modeline 1711endfunc 1712 1713" Make sure a fold containing a nested fold is split correctly when using 1714" foldmethod=indent 1715func Test_fold_split () 1716new 1717let lines =<< trim END 1718line 1 1719line 2 1720line 3 1721line 4 1722line 5 1723END 1724call setline ( 1 , lines ) 1725setlocal sw = 2 1726setlocal foldmethod = indentfoldenable 1727call assert_equal ([ 0 , 1 , 1 , 2 , 2 ], range ( 1 , 5 ) ->map ( 'foldlevel(v:val)' )) 1728call append ( 2 , 'line 2.5' ) 1729call assert_equal ([ 0 , 1 , 0 , 1 , 2 , 2 ], range ( 1 , 6 ) ->map ( 'foldlevel(v:val)' )) 17303 d 1731call assert_equal ([ 0 , 1 , 1 , 2 , 2 ], range ( 1 , 5 ) ->map ( 'foldlevel(v:val)' )) 1732bw ! 1733endfunc 1734 1735" Make sure that when you append under a blank line that is under a fold with 1736" the same indent level as your appended line, the fold expands across the 1737" blank line 1738func Test_indent_append_under_blank_line () 1739new 1740let lines =<< trim END 1741line 1 1742line 2 1743line 3 1744END 1745call setline ( 1 , lines ) 1746setlocal sw = 2 1747setlocal foldmethod = indentfoldenable 1748call assert_equal ([ 0 , 1 , 1 ], range ( 1 , 3 ) ->map ( 'foldlevel(v:val)' )) 1749call append ( 3 , '' ) 1750call append ( 4 , ' line 5' ) 1751call assert_equal ([ 0 , 1 , 1 , 1 , 1 ], range ( 1 , 5 ) ->map ( 'foldlevel(v:val)' )) 1752bw ! 1753endfunc 1754 1755" Make sure that when you delete 1 line of a fold whose length is 2 lines, the 1756" fold can't be closed since its length (1) is now less than foldminlines. 1757func Test_indent_one_line_fold_close () 1758let lines =<< trim END 1759line 1 1760line 2 1761line 3 1762END 1763 1764new 1765setlocal sw = 2 foldmethod = indent 1766call setline ( 1 , lines ) 1767" open all folds, delete line, then close all folds 1768normal zR 17693 delete 1770normal zM 1771call assert_equal ( -1 , foldclosed ( 2 )) " the fold should not be closed 1772 1773" Now do the same, but delete line 2 this time; this covers different code. 1774" (Combining this code with the above code doesn't expose both bugs.) 17751 , $ delete 1776call setline ( 1 , lines ) 1777normal zR 17782 delete 1779normal zM 1780call assert_equal ( -1 , foldclosed ( 2 )) 1781bw ! 1782endfunc 1783 1784" Make sure that when appending [an indented line then a blank line] right 1785" before a single indented line, the resulting extended fold can be closed 1786func Test_indent_append_blank_small_fold_close () 1787new 1788setlocal sw = 2 foldmethod = indent 1789" at first, the fold at the second line can't be closed since it's smaller 1790" than foldminlines 1791let lines =<< trim END 1792line 1 1793line 4 1794END 1795call setline ( 1 , lines ) 1796call append ( 1 , [ ' line 2' , '' ]) 1797" close all folds 1798normal zM 1799call assert_notequal ( -1 , foldclosed ( 2 )) " the fold should be closed now 1800bw ! 1801endfunc 1802 1803func Test_sort_closed_fold () 1804CheckExecutable sort 1805 1806call setline ( 1 , [ 1807 \'Section 1' , 1808 \' how' , 1809 \' now' , 1810 \' brown' , 1811 \' cow' , 1812 \'Section 2' , 1813 \' how' , 1814 \' now' , 1815 \' brown' , 1816 \' cow' , 1817 \]) 1818setlocal foldmethod = indentsw = 3 1819normal 2G 1820 1821" The "!!" expands to ".,.+3" and must only sort four lines 1822call feedkeys ( "!!sort\<CR>" , 'xt' ) 1823call assert_equal ([ 1824 \'Section 1' , 1825 \' brown' , 1826 \' cow' , 1827 \' how' , 1828 \' now' , 1829 \'Section 2' , 1830 \' how' , 1831 \' now' , 1832 \' brown' , 1833 \' cow' , 1834 \], getline ( 1 , 10 )) 1835 1836bwipe ! 1837endfunc 1838 1839func Test_indent_with_L_command () 1840" The "L" command moved the cursor to line zero, causing the text saved for 1841" undo to use line number -1, which caused trouble for undo later. 1842new 1843sil ! norm 8R V{zf8=Lu 1844bwipe ! 1845endfunc 1846 1847" Make sure that when there is a fold at the bottom of the buffer and a newline 1848" character is appended to the line, the fold gets expanded (instead of the new 1849" line not being part of the fold). 1850func Test_expand_fold_at_bottom_of_buffer () 1851new 1852" create a fold on the only line 1853fold 1854execute "normal A\<CR>" 1855call assert_equal ([ 1 , 1 ], range ( 1 , 2 ) ->map ( 'foldlevel(v:val)' )) 1856 1857bwipe ! 1858endfunc 1859 1860func Test_fold_screenrow_motion () 1861call setline ( 1 , repeat ([ 'aaaa' ], 5 )) 18621 , 4 fold 1863norm Ggkzo 1864call assert_equal ( 1 , line ( '.' )) 1865endfunc 1866 1867" This was using freed memory 1868func Test_foldcolumn_linebreak_control_char () 1869CheckFeature linebreak 1870 18715 vnew 1872setlocal foldcolumn = 1 linebreak 1873call setline ( 1 , "aaa\<C-A>b" ) 1874redraw 1875call assert_equal ([ ' aaa^' , ' Ab ' ], ScreenLines ([ 1 , 2 ], 5 )) 1876call assert_equal ( screenattr ( 1 , 5 ), screenattr ( 2 , 2 )) 1877 1878bwipe ! 1879endfunc 1880 1881" This used to cause invalid memory access 1882func Test_foldexpr_return_empty_string () 1883new 1884setlocal foldexpr = ''foldmethod = expr 1885redraw 1886 1887bwipe ! 1888endfunc 1889 1890" Make sure that when ending a fold that hasn't been started, it does not 1891" start a new fold. 1892func Test_foldexpr_end_fold () 1893new 1894setlocal foldmethod = expr 1895let & l: foldexpr = 'v:lnum == 2 ? "<2" : "="' 1896call setline ( 1 , range ( 1 , 3 )) 1897redraw 1898call assert_equal ([ 0 , 0 , 0 ], range ( 1 , 3 ) ->map ( 'foldlevel(v:val)' )) 1899 1900bwipe ! 1901endfunc 1902 1903" Test moving cursor down to or beyond start of folded end of buffer. 1904func Test_cursor_down_fold_eob () 1905call setline ( 1 , range ( 1 , 4 )) 1906norm Gzf2kj 1907call assert_equal ( 2 , line ( '.' )) 1908norm zojzc 1909call assert_equal ( 3 , line ( '.' )) 1910norm j 1911call assert_equal ( 3 , line ( '.' )) 1912norm k2j 1913call assert_equal ( 4 , line ( '.' )) 1914bwipe ! 1915endfunc 1916 1917" issue: #15455 1918func Test_cursor_fold_marker_undo () 1919new 1920call setline ( 1 , [ '{{{' , '' , 'This is a Line' , '' , 'This is a Line' , '' , '}}}' ]) 1921let & ul =& ul 1922setl foldmethod = marker 1923call cursor ( 2 , 1 ) 1924norm ! zo1vjdu 1925call assert_equal ( 1 , foldlevel ( '.' )) 1926bwipe ! 1927new 1928call setline ( 1 , [ '' , '{{{' , '' , 'This is a Line' , '' , 'This is a Line' , '' , '}}}' ]) 1929let & ul =& ul 1930setl foldmethod = marker 1931call cursor ( 3 , 1 ) 1932norm ! zo 1933norm ! vjdu 1934call assert_equal ( 1 , foldlevel ( '.' )) 1935bwipe ! 1936endfunc 1937 1938" The fold size must be compared against 'foldminlines' of the window that 1939" contains the fold, not of the current window. 1940func Test_foldminlines_per_window () 1941" Window with 'foldminlines'=5: its two-line fold stays displayed open. 1942new 1943setlocal foldenable foldmethod = manualfoldminlines = 5 1944call setline ( 1 , [ 'one' , 'two' , 'three' , 'four' ]) 19452 , 3 fold 1946call assert_equal ( -1 , foldclosed ( 2 )) 1947let winid = win_getid () 1948 1949" Split; only the new current window gets 'foldminlines'=0. setline() 1950" invalidates the fold sizes of both windows and measures them again right 1951" away, the other window's while this window is current. win_execute() 1952" below only reads that cached verdict, without measuring again. 1953split 1954setlocal foldminlines = 0 1955call setline ( 2 , 'changed' ) 1956call assert_equal ( 2 , foldclosed ( 2 )) 1957call win_execute ( winid , 'call assert_equal(-1, foldclosed(2))' ) 1958bwipe ! 1959 1960" The same the other way around: 'foldminlines'=0 in the other window, so 1961" its fold must remain closed while the current window has a higher value. 1962new 1963setlocal foldenable foldmethod = manualfoldminlines = 0 1964call setline ( 1 , [ 'one' , 'two' , 'three' , 'four' ]) 19652 , 3 fold 1966call assert_equal ( 2 , foldclosed ( 2 )) 1967let winid = win_getid () 1968split 1969setlocal foldminlines = 5 1970call setline ( 2 , 'changed' ) 1971call assert_equal ( -1 , foldclosed ( 2 )) 1972call win_execute ( winid , 'call assert_equal(2, foldclosed(2))' ) 1973bwipe ! 1974endfunc 1975 1976" vim: shiftwidth=2 sts=2 expandtab