Ai
12 Star 47 Fork 0

Gitee 极速下载/Vim

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/vim/vim
克隆/下载
test_fold.vim 55.15 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
" Test for folding
source util/screendump.vim
func PrepIndent(arg)
return [a:arg] + repeat(["\t".a:arg], 5)
endfu
func Test_address_fold_new_default_commentstring()
" Test with the new commentstring defaults, that includes padding after v9.1.464
new
call setline(1, ['int FuncName() {/* {{{ */', 1, 2, 3, 4, 5, '}/* }}} */',
\ 'after fold 1', 'after fold 2', 'after fold 3'])
setl fen fdm=marker
" The next commands should all copy the same part of the buffer,
" regardless of the addressing type, since the part to be copied
" is folded away
:1y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
:.y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
:.+y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
:.,.y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
:sil .1,.y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
" use silent to make E493 go away
:sil .+,.y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
:,y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
:,+y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */','after fold 1'], getreg(0,1,1))
" using .+3 as second address should c opy the whole folded line + the next 3
" lines
:.,+3y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */',
\ 'after fold 1', 'after fold 2' , 'after fold 3'], getreg(0,1,1))
:sil .,-2y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3', '4', '5', '}/* }}} */'], getreg(0,1,1))
" now test again with folding disabled
set nofoldenable
:1y
call assert_equal(['int FuncName() {/* {{{ */'], getreg(0,1,1))
:.y
call assert_equal(['int FuncName() {/* {{{ */'], getreg(0,1,1))
:.+y
call assert_equal(['1'], getreg(0,1,1) )
:.,.y
call assert_equal(['int FuncName() {/* {{{ */'], getreg(0,1,1))
" use silent to make E493 go away
:sil .1,.y
call assert_equal(['int FuncName() {/* {{{ */', '1'], getreg(0,1,1))
" use silent to make E493 go away
:sil .+,.y
call assert_equal(['int FuncName() {/* {{{ */', '1'], getreg(0,1,1))
:,y
call assert_equal(['int FuncName() {/* {{{ */'], getreg(0,1,1))
:,+y
call assert_equal(['int FuncName() {/* {{{ */', '1'], getreg(0,1,1))
" using .+3 as second address should c opy the whole folded line + the next 3
" lines
:.,+3y
call assert_equal(['int FuncName() {/* {{{ */', '1', '2', '3'], getreg(0,1,1))
:7
:sil .,-2y
call assert_equal(['4', '5', '}/* }}} */'], getreg(0,1,1))
quit!
endfunc
func Test_address_fold_old_default_commentstring()
" Test with the old commentstring defaults, before v9.1.464
new
call setline(1, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
\ 'after fold 1', 'after fold 2', 'after fold 3'])
setl fen fdm=marker
" The next commands should all copy the same part of the buffer,
" regardless of the addressing type, since the part to be copied
" is folded away
:1y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
:.y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
:.+y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
:.,.y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
:sil .1,.y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
" use silent to make E493 go away
:sil .+,.y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
:,y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
:,+y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/','after fold 1'], getreg(0,1,1))
" using .+3 as second address should copy the whole folded line + the next 3
" lines
:.,+3y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/',
\ 'after fold 1', 'after fold 2', 'after fold 3'], getreg(0,1,1))
:sil .,-2y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
" now test again with folding disabled
set nofoldenable
:1y
call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
:.y
call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
:.+y
call assert_equal(['1'], getreg(0,1,1))
:.,.y
call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
" use silent to make E493 go away
:sil .1,.y
call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
" use silent to make E493 go away
:sil .+,.y
call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
:,y
call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
:,+y
call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
" using .+3 as second address should copy the whole folded line + the next 3
" lines
:.,+3y
call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3'], getreg(0,1,1))
:7
:sil .,-2y
call assert_equal(['4', '5', '}/*}}}*/'], getreg(0,1,1))
quit!
endfunc
func Test_address_offsets()
" check the help for :range-closed-fold
enew
call setline(1, [
\ '1 one',
\ '2 two',
\ '3 three',
\ '4 four FOLDED',
\ '5 five FOLDED',
\ '6 six',
\ '7 seven',
\ '8 eight',
\])
set foldmethod=manual
normal 4Gvjzf
3,4+2yank
call assert_equal([
\ '3 three',
\ '4 four FOLDED',
\ '5 five FOLDED',
\ '6 six',
\ '7 seven',
\ ], getreg(0,1,1))
enew!
call setline(1, [
\ '1 one',
\ '2 two',
\ '3 three FOLDED',
\ '4 four FOLDED',
\ '5 five FOLDED',
\ '6 six FOLDED',
\ '7 seven',
\ '8 eight',
\])
normal 3Gv3jzf
2,4-1yank
call assert_equal([
\ '2 two',
\ '3 three FOLDED',
\ '4 four FOLDED',
\ '5 five FOLDED',
\ '6 six FOLDED',
\ ], getreg(0,1,1))
bwipe!
endfunc
func Test_indent_fold()
new
call setline(1, ['', 'a', ' b', ' c'])
setl fen fdm=indent
2
norm! >>
let a=map(range(1,4), 'foldclosed(v:val)')
call assert_equal([-1,-1,-1,-1], a)
bw!
endfunc
func Test_indent_fold2()
new
call setline(1, ['', '{{{', '}}}', '{{{', '}}}'])
setl fen fdm=marker
2
norm! >>
let a=map(range(1,5), 'v:val->foldclosed()')
call assert_equal([-1,-1,-1,4,4], a)
bw!
endfunc
" Test for fold indent with indents greater than 'foldnestmax'
func Test_indent_fold_max()
new
setlocal foldmethod=indent
setlocal shiftwidth=2
" 'foldnestmax' default value is 20
call setline(1, "\t\t\t\t\t\ta")
call assert_equal(20, foldlevel(1))
setlocal foldnestmax=10
call assert_equal(10, foldlevel(1))
setlocal foldnestmax=-1
call assert_equal(0, foldlevel(1))
bw!
endfunc
func Test_indent_fold_tabstop()
call setline(1, ['0', ' 1', ' 1', "\t2", "\t2"])
setlocal shiftwidth=4
setlocal foldcolumn=1
setlocal foldlevel=2
setlocal foldmethod=indent
redraw
call assert_equal('2 2', ScreenLines(5, 10)[0])
vsplit
windo diffthis
botright new
" This 'tabstop' value should not be used for folding in other buffers.
setlocal tabstop=4
diffoff!
redraw
call assert_equal('2 2', ScreenLines(5, 10)[0])
bwipe!
bwipe!
endfunc
func Test_manual_fold_with_filter()
CheckExecutable cat
for type in ['manual', 'marker']
exe 'set foldmethod=' . type
new
call setline(1, range(1, 20))
4,$fold
%foldopen
10,$fold
%foldopen
" This filter command should not have an effect
1,8! cat
call feedkeys('5ggzdzMGdd', 'xt')
call assert_equal(['1', '2', '3', '4', '5', '6', '7', '8', '9'], getline(1, '$'))
bwipe!
set foldmethod&
endfor
endfunc
func Test_indent_fold_with_read()
new
set foldmethod=indent
call setline(1, repeat(["\<Tab>a"], 4))
for n in range(1, 4)
call assert_equal(1, foldlevel(n))
endfor
call writefile(["a", "", "\<Tab>a"], 'Xinfofile', 'D')
foldopen
2read Xinfofile
%foldclose
call assert_equal(1, foldlevel(1))
call assert_equal(2, foldclosedend(1))
call assert_equal(0, foldlevel(3))
call assert_equal(0, foldlevel(4))
call assert_equal(1, foldlevel(5))
call assert_equal(7, 5->foldclosedend())
bwipe!
set foldmethod&
endfunc
func Test_combining_folds_indent()
new
let one = "\<Tab>a"
let zero = 'a'
call setline(1, [one, one, zero, zero, zero, one, one, one])
set foldmethod=indent
3,5d
%foldclose
call assert_equal(5, foldclosedend(1))
set foldmethod&
bwipe!
endfunc
func Test_combining_folds_marker()
new
call setline(1, ['{{{', '}}}', '', '', '', '{{{', '', '}}}'])
set foldmethod=marker
3,5d
%foldclose
call assert_equal(2, foldclosedend(1))
set foldmethod&
bwipe!
endfunc
func Test_folds_marker_in_comment()
new
call setline(1, ['" foo', 'bar', 'baz'])
setl fen fdm=marker
setl com=sO:\"\ -,mO:\"\ \ ,eO:\"\",:\" cms=\"%s
norm! zf2j
setl nofen
:1y
call assert_equal(['" foo{{{'], getreg(0,1,1))
:+2y
call assert_equal(['baz"}}}'], getreg(0,1,1))
set foldmethod&
bwipe!
endfunc
func s:TestFoldExpr(lnum)
let thisline = getline(a:lnum)
if thisline == 'a'
return 1
elseif thisline == 'b'
return 0
elseif thisline == 'c'
return '<1'
elseif thisline == 'd'
return '>1'
endif
return 0
endfunction
func Test_update_folds_expr_read()
new
call setline(1, ['a', 'a', 'a', 'a', 'a', 'a'])
set foldmethod=expr
set foldexpr=s:TestFoldExpr(v:lnum)
2
foldopen
call writefile(['b', 'b', 'a', 'a', 'd', 'a', 'a', 'c'], 'Xupfofile', 'D')
read Xupfofile
%foldclose
call assert_equal(2, foldclosedend(1))
call assert_equal(0, foldlevel(3))
call assert_equal(0, 4->foldlevel())
call assert_equal(6, foldclosedend(5))
call assert_equal(10, foldclosedend(7))
call assert_equal(14, foldclosedend(11))
bwipe!
set foldmethod& foldexpr&
endfunc
" Test for what patch 8.1.0535 fixes.
func Test_foldexpr_no_interrupt_addsub()
new
func! FoldFunc()
call setpos('.', getcurpos())
return '='
endfunc
set foldmethod=expr
set foldexpr=FoldFunc()
call setline(1, '1.2')
exe "norm! $\<C-A>"
call assert_equal('1.3', getline(1))
bwipe!
delfunc FoldFunc
set foldmethod& foldexpr&
endfunc
" Fold function defined in another script
func Test_foldexpr_compiled()
new
let lines =<< trim END
vim9script
def FoldFunc(): number
return v:lnum
enddef
set foldmethod=expr
set foldexpr=s:FoldFunc()
END
call writefile(lines, 'XfoldExpr', 'D')
source XfoldExpr
call setline(1, ['one', 'two', 'three'])
redraw
call assert_equal(1, foldlevel(1))
call assert_equal(2, foldlevel(2))
call assert_equal(3, foldlevel(3))
bwipe!
set foldmethod& foldexpr&
endfunc
func Check_foldlevels(expected)
call assert_equal(a:expected, map(range(1, line('$')), 'foldlevel(v:val)'))
endfunc
func Test_move_folds_around_manual()
new
let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c")
call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14]
" all folds closed
set foldenable foldlevel=0 fdm=indent
" needs a forced redraw
redraw!
set fdm=manual
call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
call assert_equal(input, getline(1, '$'))
7,12m0
call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
10,12m0
call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$'))
call 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)'))
" moving should not close the folds
%d
call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
set fdm=indent
redraw!
set fdm=manual
call cursor(2, 1)
%foldopen
7,12m0
let folds=repeat([-1], 18)
call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
norm! zM
" folds are not corrupted and all have been closed
call 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)'))
%d
call setline(1, ["a", "\tb", "\tc", "\td", "\te"])
set fdm=indent
redraw!
set fdm=manual
%foldopen
3m4
%foldclose
call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$'))
call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)'))
%d
call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"])
set fdm=indent foldlevel=0
set fdm=manual
%foldopen
3m1
%foldclose
call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$'))
call assert_equal(0, foldlevel(2))
call assert_equal(5, foldclosedend(3))
call assert_equal([-1, -1, 3, 3, 3, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)'))
2,6m$
%foldclose
call assert_equal(5, foldclosedend(2))
call assert_equal(0, foldlevel(6))
call assert_equal(9, foldclosedend(7))
call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, -1], map(range(1, line('$')), 'foldclosed(v:val)'))
%d
" Ensure moving around the edges still works.
call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"])
set fdm=indent foldlevel=0
set fdm=manual
%foldopen
6m$
" The first fold has been truncated to the 5'th line.
" Second fold has been moved up because the moved line is now below it.
call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 0])
%delete
set fdm=indent foldlevel=0
call setline(1, [
\ "a",
\ "\ta",
\ "\t\ta",
\ "\t\ta",
\ "\t\ta",
\ "a",
\ "a"])
set fdm=manual
%foldopen!
4,5m6
call Check_foldlevels([0, 1, 2, 0, 0, 0, 0])
%delete
set fdm=indent
call setline(1, [
\ "\ta",
\ "\t\ta",
\ "\t\ta",
\ "\t\ta",
\ "\ta",
\ "\t\ta",
\ "\t\ta",
\ "\t\ta",
\ "\ta",
\ "\t\ta",
\ "\t\ta",
\ "\t\ta",
\ "\t\ta",
\ "\ta",
\ "a"])
set fdm=manual
%foldopen!
13m7
call Check_foldlevels([1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0])
bw!
endfunc
func Test_move_folds_around_indent()
new
let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c")
call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14]
" all folds closed
set fdm=indent
call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
call assert_equal(input, getline(1, '$'))
7,12m0
call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
10,12m0
call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$'))
call 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)'))
" moving should not close the folds
%d
call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
set fdm=indent
call cursor(2, 1)
%foldopen
7,12m0
let folds=repeat([-1], 18)
call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
norm! zM
" folds are not corrupted and all have been closed
call 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)'))
%d
call setline(1, ["a", "\tb", "\tc", "\td", "\te"])
set fdm=indent
%foldopen
3m4
%foldclose
call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$'))
call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)'))
%d
call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"])
set fdm=indent foldlevel=0
%foldopen
3m1
%foldclose
call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$'))
call assert_equal(1, foldlevel(2))
call assert_equal(5, foldclosedend(3))
call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)'))
2,6m$
%foldclose
call assert_equal(9, foldclosedend(2))
call assert_equal(1, foldlevel(6))
call assert_equal(9, foldclosedend(7))
call assert_equal([-1, 2, 2, 2, 2, 2, 2, 2, 2, -1], map(range(1, line('$')), 'foldclosed(v:val)'))
" Ensure moving around the edges still works.
%d
call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"])
set fdm=indent foldlevel=0
%foldopen
6m$
" The first fold has been truncated to the 5'th line.
" Second fold has been moved up because the moved line is now below it.
call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 1])
bw!
endfunc
func Test_folddoopen_folddoclosed()
new
call setline(1, range(1, 9))
set foldmethod=manual
1,3 fold
6,8 fold
" Test without range.
folddoopen s/$/o/
folddoclosed s/$/c/
call assert_equal(['1c', '2c', '3c',
\ '4o', '5o',
\ '6c', '7c', '8c',
\ '9o'], getline(1, '$'))
" Test with range.
call setline(1, range(1, 9))
1,8 folddoopen s/$/o/
4,$ folddoclosed s/$/c/
call assert_equal(['1', '2', '3',
\ '4o', '5o',
\ '6c', '7c', '8c',
\ '9'], getline(1, '$'))
set foldmethod&
bw!
endfunc
func Test_fold_error()
new
call setline(1, [1, 2])
for fm in ['indent', 'expr', 'syntax', 'diff']
exe 'set foldmethod=' . fm
call assert_fails('norm zf', 'E350:')
call assert_fails('norm zd', 'E351:')
call assert_fails('norm zE', 'E352:')
endfor
set foldmethod=manual
call assert_fails('norm zd', 'E490:')
call assert_fails('norm zo', 'E490:')
call assert_fails('3fold', 'E16:')
set foldmethod=marker
set nomodifiable
call assert_fails('1,2fold', 'E21:')
set modifiable&
set foldmethod&
bw!
endfunc
func Test_foldtext_recursive()
new
call setline(1, ['{{{', 'some text', '}}}'])
setlocal foldenable foldmethod=marker foldtext=foldtextresult(v\:foldstart)
" This was crashing because of endless recursion.
2foldclose
redraw
call assert_equal(1, foldlevel(2))
call assert_equal(1, foldclosed(2))
call assert_equal(3, foldclosedend(2))
bwipe!
endfunc
" Various fold related tests
" Basic test if a fold can be created, opened, moving to the end and closed
func Test_fold_manual()
new
set fdm=manual
let content = ['1 aa', '2 bb', '3 cc']
call append(0, content)
call cursor(1, 1)
normal zf2j
call assert_equal('1 aa', getline(foldclosed('.')))
normal zo
call assert_equal(-1, foldclosed('.'))
normal ]z
call assert_equal('3 cc', getline('.'))
normal zc
call assert_equal('1 aa', getline(foldclosed('.')))
" Create a fold inside a closed fold after setting 'foldlevel'
%d _
call setline(1, range(1, 5))
1,5fold
normal zR
2,4fold
set foldlevel=1
3fold
call assert_equal([1, 3, 3, 3, 1], map(range(1, 5), {->foldlevel(v:val)}))
set foldlevel&
" Create overlapping folds (at the start and at the end)
normal zE
2,3fold
normal zR
3,4fold
call assert_equal([0, 2, 2, 1, 0], map(range(1, 5), {->foldlevel(v:val)}))
normal zE
3,4fold
normal zR
2,3fold
call assert_equal([0, 1, 2, 2, 0], map(range(1, 5), {->foldlevel(v:val)}))
" Create a nested fold across two non-adjoining folds
%d _
call setline(1, range(1, 7))
1,2fold
normal zR
4,5fold
normal zR
6,7fold
normal zR
1,5fold
call assert_equal([2, 2, 1, 2, 2, 1, 1],
\ map(range(1, 7), {->foldlevel(v:val)}))
" A newly created nested fold should be closed
%d _
call setline(1, range(1, 6))
1,6fold
normal zR
3,4fold
normal zR
2,5fold
call assert_equal([1, 2, 3, 3, 2, 1], map(range(1, 6), {->foldlevel(v:val)}))
call assert_equal(2, foldclosed(4))
call assert_equal(5, foldclosedend(4))
" Test zO, zC and zA on a line with no folds.
normal zE
call assert_fails('normal zO', 'E490:')
call assert_fails('normal zC', 'E490:')
call assert_fails('normal zA', 'E490:')
set fdm&
bw!
endfunc
" test folding with markers.
func Test_fold_marker()
new
set fdm=marker fdl=1 fdc=3
let content = ['4 dd {{{', '5 ee {{{ }}}', '6 ff }}}']
call append(0, content)
call cursor(2, 1)
call assert_equal(2, foldlevel('.'))
normal [z
call assert_equal(1, foldlevel('.'))
exe "normal jo{{ \<Esc>r{jj"
call assert_equal(1, foldlevel('.'))
normal kYpj
call assert_equal(0, foldlevel('.'))
" Use only closing fold marker (without and with a count)
set fdl&
%d _
call setline(1, ['one }}}', 'two'])
call assert_equal([0, 0], [foldlevel(1), foldlevel(2)])
%d _
call setline(1, ['one }}}4', 'two'])
call assert_equal([4, 3], [foldlevel(1), foldlevel(2)])
set fdm& fdl& fdc&
bw!
endfunc
" test create fold markers with C filetype
func Test_fold_create_marker_in_C()
bw!
set fdm=marker fdl=9
set filetype=c
let content =<< trim [CODE]
/*
* comment
*
*
*/
int f(int* p) {
*p = 3;
return 0;
}
[CODE]
for c in range(len(content) - 1)
bw!
call append(0, content)
call cursor(c + 1, 1)
norm! zfG
call assert_equal(content[c] . (c < 4 ? '{{{' : '/* {{{ */'), getline(c + 1))
endfor
set fdm& fdl&
bw!
endfunc
" test folding with indent
func Test_fold_indent()
new
set fdm=indent sw=2
let content = ['1 aa', '2 bb', '3 cc']
call append(0, content)
call cursor(2, 1)
exe "normal i \<Esc>jI "
call assert_equal(2, foldlevel('.'))
normal k
call assert_equal(1, foldlevel('.'))
set fdm& sw&
bw!
endfunc
" test syntax folding
func Test_fold_syntax()
CheckFeature syntax
new
set fdm=syntax fdl=0
syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3
syn region Fd1 start="ee" end="ff" fold contained
syn region Fd2 start="gg" end="hh" fold contained
syn region Fd3 start="commentstart" end="commentend" fold contained
let content = ['3 cc', '4 dd {{{', '5 ee {{{ }}}', '{{{{', '6 ff }}}',
\ '6 ff }}}', '7 gg', '8 hh', '9 ii']
call append(0, content)
normal Gzk
call assert_equal('9 ii', getline('.'))
normal k
call assert_equal('3 cc', getline('.'))
exe "normal jAcommentstart \<Esc>Acommentend"
set fdl=1
normal 3j
call assert_equal('7 gg', getline('.'))
set fdl=0
exe "normal zO\<C-L>j"
call assert_equal('8 hh', getline('.'))
syn clear Fd1 Fd2 Fd3 Hup
set fdm& fdl&
bw!
endfunc
func Flvl()
let l = getline(v:lnum)
if l =~ "bb$"
return 2
elseif l =~ "gg$"
return "s1"
elseif l =~ "ii$"
return ">2"
elseif l =~ "kk$"
return "0"
endif
return "="
endfun
" test expression folding
func Test_fold_expr()
new
set fdm=expr fde=Flvl()
let content = ['1 aa',
\ '2 bb',
\ '3 cc',
\ '4 dd {{{commentstart commentend',
\ '5 ee {{{ }}}',
\ '{{{',
\ '6 ff }}}',
\ '6 ff }}}',
\ ' 7 gg',
\ ' 8 hh',
\ '9 ii',
\ 'a jj',
\ 'b kk']
call append(0, content)
call cursor(1, 1)
exe "normal /bb$\<CR>"
call assert_equal(2, foldlevel('.'))
exe "normal /hh$\<CR>"
call assert_equal(1, foldlevel('.'))
exe "normal /ii$\<CR>"
call assert_equal(2, foldlevel('.'))
exe "normal /kk$\<CR>"
call assert_equal(0, foldlevel('.'))
set fdm& fde&
bw!
endfunc
" Bug with fdm=indent and moving folds
" Moving a fold a few times, messes up the folds below the moved fold.
" Fixed by 7.4.700
func Test_fold_move()
new
set fdm=indent sw=2 fdl=0
let content = ['', '', 'Line1', ' Line2', ' Line3',
\ 'Line4', ' Line5', ' Line6',
\ 'Line7', ' Line8', ' Line9']
call append(0, content)
normal zM
call cursor(4, 1)
move 2
move 1
call assert_equal(7, foldclosed(7))
call assert_equal(8, foldclosedend(7))
call assert_equal(0, foldlevel(9))
call assert_equal(10, foldclosed(10))
call assert_equal(11, foldclosedend(10))
call assert_equal('+-- 2 lines: Line2', foldtextresult(2))
call assert_equal('+-- 2 lines: Line8', 10->foldtextresult())
set fdm& sw& fdl&
bw!
endfunc
" test for patch 7.3.637
" Cannot catch the error caused by a foldopen when there is no fold.
func Test_foldopen_exception()
new
let a = 'No error caught'
try
foldopen
catch
let a = matchstr(v:exception,'^[^ ]*')
endtry
call assert_equal('Vim(foldopen):E490:', a)
let a = 'No error caught'
try
foobar
catch
let a = matchstr(v:exception,'^[^ ]*')
endtry
call assert_match('E492:', a)
bw!
endfunc
func Test_fold_last_line_with_pagedown()
new
set fdm=manual
let expect = '+-- 11 lines: 9---'
let content = range(1,19)
call append(0, content)
normal dd9G
normal zfG
normal zt
call assert_equal('9', getline(foldclosed('.')))
call assert_equal('19', getline(foldclosedend('.')))
call assert_equal(expect, ScreenLines(1, len(expect))[0])
call feedkeys("\<C-F>", 'xt')
call assert_equal(expect, ScreenLines(1, len(expect))[0])
call feedkeys("\<C-F>", 'xt')
call assert_equal(expect, ScreenLines(1, len(expect))[0])
call feedkeys("\<C-B>\<C-F>\<C-F>", 'xt')
call assert_equal(expect, ScreenLines(1, len(expect))[0])
set fdm&
bw!
endfunc
func Test_folds_with_rnu()
CheckScreendump
call writefile([
\ 'set fdm=marker rnu foldcolumn=2',
\ 'call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])',
\ ], 'Xtest_folds_with_rnu', 'D')
let buf = RunVimInTerminal('-S Xtest_folds_with_rnu', {})
call VerifyScreenDump(buf, 'Test_folds_with_rnu_01', {})
call term_sendkeys(buf, "j")
call VerifyScreenDump(buf, 'Test_folds_with_rnu_02', {})
" clean up
call StopVimInTerminal(buf)
endfunc
func Test_folds_marker_in_comment2()
new
call setline(1, ['Lorem ipsum dolor sit', 'Lorem ipsum dolor sit', 'Lorem ipsum dolor sit'])
setl fen fdm=marker
setl commentstring=<!--%s-->
setl comments=s:<!--,m:\ \ \ \ ,e:-->
norm! zf2j
setl nofen
:1y
call assert_equal(['Lorem ipsum dolor sit<!--{{{-->'], getreg(0,1,1))
:+2y
call assert_equal(['Lorem ipsum dolor sit<!--}}}-->'], getreg(0,1,1))
set foldmethod&
bwipe!
endfunc
func Test_fold_delete_with_marker()
new
call setline(1, ['func Func() {{{1', 'endfunc'])
1,2yank
new
set fdm=marker
call setline(1, 'x')
normal! Vp
normal! zd
call assert_equal(['func Func() ', 'endfunc'], getline(1, '$'))
set fdm&
bwipe!
bwipe!
endfunc
func Test_fold_delete_with_marker_and_whichwrap()
new
let content1 = ['']
let content2 = ['folded line 1 "{{{1', ' test', ' test2', ' test3', '', 'folded line 2 "{{{1', ' test', ' test2', ' test3']
call setline(1, content1 + content2)
set fdm=marker ww+=l
normal! x
call assert_equal(content2, getline(1, '$'))
set fdm& ww&
bwipe!
endfunc
func Test_fold_delete_first_line()
new
call setline(1, [
\ '" x {{{1',
\ '" a',
\ '" aa',
\ '" x {{{1',
\ '" b',
\ '" bb',
\ '" x {{{1',
\ '" c',
\ '" cc',
\ ])
set foldmethod=marker
1
normal dj
call assert_equal([
\ '" x {{{1',
\ '" c',
\ '" cc',
\ ], getline(1,'$'))
bwipe!
set foldmethod&
endfunc
" Add a test for deleting the outer fold of a nested fold and promoting the
" inner folds to one level up with already a fold at that level following the
" nested fold.
func Test_fold_delete_recursive_fold()
new
call setline(1, range(1, 7))
2,3fold
normal zR
4,5fold
normal zR
1,5fold
normal zR
6,7fold
normal zR
normal 1Gzd
normal 1Gzj
call assert_equal(2, line('.'))
normal zj
call assert_equal(4, line('.'))
normal zj
call assert_equal(6, line('.'))
bw!
endfunc
" Test for errors in 'foldexpr'
func Test_fold_expr_error()
new
call setline(1, ['one', 'two', 'three'])
" In a window with no folds, foldlevel() should return 0
call assert_equal(0, foldlevel(1))
" Return a list from the expression
set foldexpr=[]
set foldmethod=expr
for i in range(3)
call assert_equal(0, foldlevel(i))
endfor
" expression error
set foldexpr=[{]
set foldmethod=expr
for i in range(3)
call assert_equal(0, foldlevel(i))
endfor
set foldmethod& foldexpr&
close!
endfunc
func Test_undo_fold_deletion()
new
set fdm=marker
let lines =<< trim END
" {{{
" }}}1
" {{{
END
call setline(1, lines)
3d
g/"/d
undo
redo
eval getline(1, '$')->assert_equal([''])
set fdm&vim
bwipe!
endfunc
" this was crashing
func Test_move_no_folds()
new
fold
setlocal fdm=expr
normal zj
bwipe!
endfunc
" this was crashing
func Test_fold_create_delete_create()
new
fold
fold
normal zd
fold
bwipe!
endfunc
" this was crashing
func Test_fold_create_delete()
new
norm zFzFzdzj
bwipe!
endfunc
func Test_fold_relative_move()
new
set fdm=indent sw=2 wrap tw=80
let longtext = repeat('x', &columns + 1)
let content = [ ' foo', ' ' .. longtext, ' baz',
\ longtext,
\ ' foo', ' ' .. longtext, ' baz'
\ ]
call append(0, content)
normal zM
for lnum in range(1, 3)
call cursor(lnum, 1)
call assert_true(foldclosed(line('.')))
normal gj
call assert_equal(2, winline())
endfor
call cursor(2, 1)
call assert_true(foldclosed(line('.')))
normal 2gj
call assert_equal(3, winline())
for lnum in range(5, 7)
call cursor(lnum, 1)
call assert_true(foldclosed(line('.')))
normal gk
call assert_equal(3, winline())
endfor
call cursor(6, 1)
call assert_true(foldclosed(line('.')))
normal 2gk
call assert_equal(2, winline())
set fdm& sw& wrap& tw&
bw!
endfunc
" Test for using multibyte characters as 'foldopen', 'foldclose' and
" 'foldsetp' items in 'fillchars'
func s:mbyte_fillchar_tests(fo, fc, fs)
setlocal foldcolumn=3
normal zE
1,2fold
call assert_equal([a:fc .. ' +-- 2 ', ' three '],
\ ScreenLines([1, 2], 10))
1,2foldopen
call assert_equal([a:fo .. ' one ', a:fs .. ' two '],
\ ScreenLines([1, 2], 7))
1,2foldclose
redraw!
call assert_equal([a:fc .. ' +-- 2 ', ' three '],
\ ScreenLines([1, 2], 10))
" Two level fold
normal zE
2,3fold
1,4fold
call assert_equal([a:fc .. ' +-- 4 ', ' five '],
\ ScreenLines([1, 2], 10))
1,4foldopen
call assert_equal([a:fo .. ' one ', a:fs .. a:fc .. ' +--- 2'],
\ ScreenLines([1, 2], 10))
1,4foldopen
call assert_equal([a:fo .. ' one ', a:fs .. a:fo .. ' two ',
\ a:fs .. a:fs .. ' three '], ScreenLines([1, 3], 10))
2,3foldclose
call assert_equal([a:fo .. ' one ', a:fs .. a:fc .. ' +--- 2'],
\ ScreenLines([1, 2], 10))
1,4foldclose
call assert_equal([a:fc .. ' +-- 4 ', ' five '],
\ ScreenLines([1, 2], 10))
" Three level fold
normal zE
3,4fold
2,5fold
1,6fold
call assert_equal([a:fc .. ' +-- 6 '], ScreenLines(1, 10))
" open all the folds
normal zR
call assert_equal([
\ a:fo .. ' one ',
\ a:fs .. a:fo .. ' two ',
\ '2' .. a:fo .. ' three ',
\ '23 four ',
\ a:fs .. a:fs .. ' five ',
\ a:fs .. ' six ',
\ ], ScreenLines([1, 6], 10))
" close the innermost fold
3,4foldclose
call assert_equal([
\ a:fo .. ' one ',
\ a:fs .. a:fo .. ' two ',
\ a:fs .. a:fs .. a:fc .. '+---- ',
\ a:fs .. a:fs .. ' five ',
\ a:fs .. ' six ',
\ ], ScreenLines([1, 5], 10))
" close the next fold
2,5foldclose
call assert_equal([
\ a:fo .. ' one ',
\ a:fs .. a:fc .. ' +--- 4',
\ a:fs .. ' six ',
\ ], ScreenLines([1, 3], 10))
" set the fold column size to 2
setlocal fdc=2
normal zR
call assert_equal([
\ a:fo .. ' one ',
\ a:fo .. ' two ',
\ a:fo .. ' three',
\ '3 four ',
\ '2 five ',
\ a:fs .. ' six ',
\ ], ScreenLines([1, 6], 7))
" set the fold column size to 1
setlocal fdc=1
normal zR
call assert_equal([
\ a:fo .. 'one ',
\ a:fo .. 'two ',
\ a:fo .. 'three ',
\ '3four ',
\ '2five ',
\ a:fs .. 'six ',
\ ], ScreenLines([1, 6], 7))
" Enable number and sign columns and place some signs
setlocal fdc=3
setlocal number
setlocal signcolumn=auto
sign define S1 text=->
sign place 10 line=3 name=S1
call assert_equal([
\ a:fo .. ' 1 one ',
\ a:fs .. a:fo .. ' 2 two ',
\ '2' .. a:fo .. ' -> 3 three',
\ '23 4 four ',
\ a:fs .. a:fs .. ' 5 five ',
\ a:fs .. ' 6 six '
\ ], ScreenLines([1, 6], 14))
" Test with 'rightleft'
if has('rightleft')
setlocal rightleft
let lines = ScreenLines([1, 6], winwidth(0))
call assert_equal('o 1 ' .. a:fo,
\ strcharpart(lines[0], strchars(lines[0]) - 10, 10))
call assert_equal('t 2 ' .. a:fo .. a:fs,
\ strcharpart(lines[1], strchars(lines[1]) - 10, 10))
call assert_equal('t 3 >- ' .. a:fo .. '2',
\ strcharpart(lines[2], strchars(lines[2]) - 10, 10))
call assert_equal('f 4 32',
\ strcharpart(lines[3], strchars(lines[3]) - 10, 10))
call assert_equal('f 5 ' .. a:fs .. a:fs,
\ strcharpart(lines[4], strchars(lines[4]) - 10, 10))
call assert_equal('s 6 ' .. a:fs,
\ strcharpart(lines[5], strchars(lines[5]) - 10, 10))
setlocal norightleft
endif
sign unplace *
sign undefine S1
setlocal number& signcolumn&
" Add a test with more than 9 folds (and then delete some folds)
normal zE
for i in range(1, 10)
normal zfGzo
endfor
normal zR
call assert_equal([
\ a:fo .. a:fo .. ' one ',
\ '9> two '
\ ], ScreenLines([1, 2], 7))
normal 1Gzd
call assert_equal([
\ a:fo .. a:fo .. ' one ',
\ '89 two '
\ ], ScreenLines([1, 2], 7))
normal 1Gzdzdzdzdzdzdzd
call assert_equal([
\ a:fo .. a:fo .. ' one ',
\ a:fs .. a:fs .. ' two '
\ ], ScreenLines([1, 2], 7))
setlocal foldcolumn& number& signcolumn&
endfunc
func Test_foldcolumn_multibyte_char()
new
call setline(1, ['one', 'two', 'three', 'four', 'five', 'six'])
setlocal foldenable foldmethod=manual
" First test with the default setting
call s:mbyte_fillchar_tests('-', '+', '|')
" Use multi-byte characters
set fillchars+=foldopen:,foldsep:,foldclose:
call s:mbyte_fillchar_tests('▾', '▸', '│')
" Use a mix of multi-byte and single-byte characters
set fillchars+=foldopen:¬,foldsep:\|,foldclose:+
call s:mbyte_fillchar_tests('¬', '+', '|')
set fillchars+=foldopen:+,foldsep:\|,foldclose:¬
call s:mbyte_fillchar_tests('+', '¬', '|')
bw!
set foldenable& fdc& fdm& fillchars&
endfunc
" Test for calling foldlevel() from a fold expression
let g:FoldLevels = []
func FoldExpr1(lnum)
let f = [a:lnum]
for i in range(1, line('$'))
call add(f, foldlevel(i))
endfor
call add(g:FoldLevels, f)
return getline(a:lnum)[0] == "\t"
endfunc
func Test_foldexpr_foldlevel()
new
call setline(1, ['one', "\ttwo", "\tthree"])
setlocal foldmethod=expr
setlocal foldexpr=FoldExpr1(v:lnum)
setlocal foldenable
setlocal foldcolumn=3
redraw!
call assert_equal([[1, -1, -1, -1], [2, -1, -1, -1], [3, 0, 1, -1]],
\ g:FoldLevels)
set foldmethod& foldexpr& foldenable& foldcolumn&
bw!
endfunc
" Test for returning different values from a fold expression
func FoldExpr2(lnum)
if a:lnum == 1 || a:lnum == 4
return -2
elseif a:lnum == 2
return 'a1'
elseif a:lnum == 3
return 's4'
endif
return '='
endfunc
func Test_foldexpr_2()
new
call setline(1, ['one', 'two', 'three', 'four'])
setlocal foldexpr=FoldExpr2(v:lnum)
setlocal foldmethod=expr
call assert_equal([0, 1, 1, 0], [foldlevel(1), foldlevel(2), foldlevel(3),
\ foldlevel(4)])
bw!
endfunc
" Test for the 'foldclose' option
func Test_foldclose_opt()
CheckScreendump
let lines =<< trim END
set foldmethod=manual foldclose=all foldopen=all
call setline(1, ['one', 'two', 'three', 'four'])
2,3fold
func XsaveFoldLevels()
redraw!
call writefile([json_encode([foldclosed(1), foldclosed(2), foldclosed(3),
\ foldclosed(4)])], 'Xoutput', 'a')
endfunc
END
call writefile(lines, 'Xscript', 'D')
let rows = 10
let buf = RunVimInTerminal('-S Xscript', {'rows': rows})
call term_wait(buf)
call term_sendkeys(buf, ":set noruler\n")
call term_wait(buf)
call term_sendkeys(buf, ":call XsaveFoldLevels()\n")
call term_sendkeys(buf, "2G")
call WaitForAssert({-> assert_equal('two', term_getline(buf, 2))})
call term_sendkeys(buf, ":call XsaveFoldLevels()\n")
call term_sendkeys(buf, "4G")
call WaitForAssert({-> assert_equal('four', term_getline(buf, 3))})
call term_sendkeys(buf, ":call XsaveFoldLevels()\n")
call term_sendkeys(buf, "3G")
call WaitForAssert({-> assert_equal('three', term_getline(buf, 3))})
call term_sendkeys(buf, ":call XsaveFoldLevels()\n")
call term_sendkeys(buf, "1G")
call WaitForAssert({-> assert_equal('four', term_getline(buf, 3))})
call term_sendkeys(buf, ":call XsaveFoldLevels()\n")
call term_sendkeys(buf, "2G")
call WaitForAssert({-> assert_equal('two', term_getline(buf, 2))})
call term_sendkeys(buf, "k")
call WaitForAssert({-> assert_equal('four', term_getline(buf, 3))})
" clean up
call StopVimInTerminal(buf)
call assert_equal(['[-1,2,2,-1]', '[-1,-1,-1,-1]', '[-1,2,2,-1]',
\ '[-1,-1,-1,-1]', '[-1,2,2,-1]'], readfile('Xoutput'))
call delete('Xoutput')
endfunc
" Test for foldtextresult()
func Test_foldtextresult()
new
call assert_equal('', foldtextresult(-1))
call assert_equal('', foldtextresult(0))
call assert_equal('', foldtextresult(1))
call setline(1, ['one', 'two', 'three', 'four'])
2,3fold
call assert_equal('', foldtextresult(1))
call assert_equal('+-- 2 lines: two', foldtextresult(2))
setlocal foldtext=
call assert_equal('+-- 2 lines folded ', foldtextresult(2))
" Fold text for a C comment fold
%d _
setlocal foldtext&
call setline(1, ['', '/*', ' * Comment', ' */', ''])
2,4fold
call assert_equal('+-- 3 lines: Comment', foldtextresult(2))
bw!
endfunc
" Test for merging two recursive folds when an intermediate line with no fold
" is removed
func Test_fold_merge_recursive()
new
call setline(1, [' one', ' two', 'xxxx', ' three',
\ ' four', "\tfive"])
setlocal foldmethod=indent shiftwidth=2
3d_
%foldclose
call assert_equal([1, 5], [foldclosed(5), foldclosedend(1)])
bw!
endfunc
" Test for moving a line which is the start of a fold from a recursive fold to
" outside. The fold length should reduce.
func Test_fold_move_foldlevel()
new
call setline(1, ['a{{{', 'b{{{', 'c{{{', 'd}}}', 'e}}}', 'f}}}', 'g'])
setlocal foldmethod=marker
normal zR
call assert_equal([3, 2, 1], [foldlevel(4), foldlevel(5), foldlevel(6)])
3move 7
call assert_equal([2, 1, 0], [foldlevel(3), foldlevel(4), foldlevel(5)])
call assert_equal(1, foldlevel(7))
" Move a line from outside a fold to inside the fold.
%d _
call setline(1, ['a', 'b{{{', 'c}}}'])
normal zR
1move 2
call assert_equal([1, 1, 1], [foldlevel(1), foldlevel(2), foldlevel(3)])
" Move the start of one fold to inside another fold
%d _
call setline(1, ['a', 'b{{{', 'c}}}', 'd{{{', 'e}}}'])
normal zR
call assert_equal([0, 1, 1, 1, 1], [foldlevel(1), foldlevel(2),
\ foldlevel(3), foldlevel(4), foldlevel(5)])
1,2move 4
call assert_equal([0, 1, 1, 2, 2], [foldlevel(1), foldlevel(2),
\ foldlevel(3), foldlevel(4), foldlevel(5)])
bw!
endfunc
" Test for using zj and zk to move downwards and upwards to the start and end
" of the next fold.
" Test for using [z and ]z in a closed fold to jump to the beginning and end
" of the fold.
func Test_fold_jump()
new
call setline(1, ["\t1", "\t2", "\t\t3", "\t\t4", "\t\t\t5", "\t\t\t6", "\t\t7", "\t\t8", "\t9", "\t10"])
setlocal foldmethod=indent
normal zR
normal zj
call assert_equal(3, line('.'))
normal zj
call assert_equal(5, line('.'))
call assert_beeps('normal zj')
call assert_equal(5, line('.'))
call assert_beeps('normal 9Gzj')
call assert_equal(9, line('.'))
normal Gzk
call assert_equal(8, line('.'))
normal zk
call assert_equal(6, line('.'))
call assert_beeps('normal zk')
call assert_equal(6, line('.'))
call assert_beeps('normal 2Gzk')
call assert_equal(2, line('.'))
" Using [z or ]z in a closed fold should not move the cursor
%d _
call setline(1, ["1", "\t2", "\t3", "\t4", "\t5", "\t6", "7"])
normal zR4Gzc
call assert_equal(4, line('.'))
call assert_beeps('normal [z')
call assert_equal(4, line('.'))
call assert_beeps('normal ]z')
call assert_equal(4, line('.'))
bw!
endfunc
" Test for using a script-local function for 'foldexpr'
func Test_foldexpr_scriptlocal_func()
func! s:FoldFunc()
let g:FoldLnum = v:lnum
endfunc
new | only
call setline(1, 'abc')
let g:FoldLnum = 0
set foldmethod=expr foldexpr=s:FoldFunc()
redraw!
call assert_equal(expand('<SID>') .. 'FoldFunc()', &foldexpr)
call assert_equal(expand('<SID>') .. 'FoldFunc()', &g:foldexpr)
call assert_equal(1, g:FoldLnum)
set foldmethod& foldexpr=
bw!
new | only
call setline(1, 'abc')
let g:FoldLnum = 0
set foldmethod=expr foldexpr=<SID>FoldFunc()
redraw!
call assert_equal(expand('<SID>') .. 'FoldFunc()', &foldexpr)
call assert_equal(expand('<SID>') .. 'FoldFunc()', &g:foldexpr)
call assert_equal(1, g:FoldLnum)
bw!
call setline(1, 'abc')
setlocal foldmethod& foldexpr&
setglobal foldmethod=expr foldexpr=s:FoldFunc()
call assert_equal(expand('<SID>') .. 'FoldFunc()', &g:foldexpr)
call assert_equal('0', &foldexpr)
enew!
call setline(1, 'abc')
redraw!
call assert_equal(expand('<SID>') .. 'FoldFunc()', &foldexpr)
call assert_equal(1, g:FoldLnum)
bw!
call setline(1, 'abc')
setlocal foldmethod& foldexpr&
setglobal foldmethod=expr foldexpr=<SID>FoldFunc()
call assert_equal(expand('<SID>') .. 'FoldFunc()', &g:foldexpr)
call assert_equal('0', &foldexpr)
enew!
call setline(1, 'abc')
redraw!
call assert_equal(expand('<SID>') .. 'FoldFunc()', &foldexpr)
call assert_equal(1, g:FoldLnum)
set foldmethod& foldexpr&
delfunc s:FoldFunc
bw!
endfunc
" Test for using a script-local function for 'foldtext'
func Test_foldtext_scriptlocal_func()
func! s:FoldText()
let g:FoldTextArgs = [v:foldstart, v:foldend]
return foldtext()
endfunc
new | only
call setline(1, range(50))
let g:FoldTextArgs = []
set foldtext=s:FoldText()
norm! 4Gzf4j
redraw!
call assert_equal(expand('<SID>') .. 'FoldText()', &foldtext)
call assert_equal(expand('<SID>') .. 'FoldText()', &g:foldtext)
call assert_equal([4, 8], g:FoldTextArgs)
set foldtext&
bw!
new | only
call setline(1, range(50))
let g:FoldTextArgs = []
set foldtext=<SID>FoldText()
norm! 8Gzf4j
redraw!
call assert_equal(expand('<SID>') .. 'FoldText()', &foldtext)
call assert_equal(expand('<SID>') .. 'FoldText()', &g:foldtext)
call assert_equal([8, 12], g:FoldTextArgs)
set foldtext&
bw!
call setline(1, range(50))
let g:FoldTextArgs = []
setlocal foldtext&
setglobal foldtext=s:FoldText()
call assert_equal(expand('<SID>') .. 'FoldText()', &g:foldtext)
call assert_equal('foldtext()', &foldtext)
enew!
call setline(1, range(50))
norm! 12Gzf4j
redraw!
call assert_equal(expand('<SID>') .. 'FoldText()', &foldtext)
call assert_equal([12, 16], g:FoldTextArgs)
set foldtext&
bw!
call setline(1, range(50))
let g:FoldTextArgs = []
setlocal foldtext&
setglobal foldtext=<SID>FoldText()
call assert_equal(expand('<SID>') .. 'FoldText()', &g:foldtext)
call assert_equal('foldtext()', &foldtext)
enew!
call setline(1, range(50))
norm! 16Gzf4j
redraw!
call assert_equal(expand('<SID>') .. 'FoldText()', &foldtext)
call assert_equal([16, 20], g:FoldTextArgs)
set foldtext&
bw!
delfunc s:FoldText
endfunc
" Test for setting 'foldtext' from the modeline and executing the expression
" in a sandbox
func Test_foldtext_in_modeline()
func ModelineFoldText()
call writefile(['after'], 'Xmodelinefoldtext_write')
return "folded text"
endfunc
let lines =<< trim END
func T()
let i = 1
endfunc
" vim: foldenable foldtext=ModelineFoldText()
END
call writefile(lines, 'Xmodelinefoldtext', 'D')
func Check_foldtext_in_modeline(set_cmd)
call writefile(['before'], 'Xmodelinefoldtext_write', 'D')
split Xmodelinefoldtext
call cursor(1, 1)
normal! zf3j
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['before'], readfile('Xmodelinefoldtext_write'))
split
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['before'], readfile('Xmodelinefoldtext_write'))
close
setglobal foldtext=ModelineFoldText()
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['before'], readfile('Xmodelinefoldtext_write'))
setglobal foldtext&
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['before'], readfile('Xmodelinefoldtext_write'))
exe a:set_cmd 'foldtext=ModelineFoldText()'
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['after'], readfile('Xmodelinefoldtext_write'))
call writefile(['before'], 'Xmodelinefoldtext_write')
exe 'sandbox' a:set_cmd 'foldtext=ModelineFoldText()'
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['before'], readfile('Xmodelinefoldtext_write'))
exe a:set_cmd 'foldtext=ModelineFoldText()'
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['after'], readfile('Xmodelinefoldtext_write'))
bw!
endfunc
set modeline modelineexpr
call Check_foldtext_in_modeline('setlocal')
call Check_foldtext_in_modeline('set')
new Xa
sandbox setglobal foldenable foldtext=ModelineFoldText()
setlocal bufhidden=wipe
call writefile(['before'], 'Xmodelinefoldtext_write', 'D')
edit! Xb
call setline(1, ['func T()', ' let i = 1', 'endfunc']) | %fold
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['before'], readfile('Xmodelinefoldtext_write'))
setglobal foldtext=ModelineFoldText()
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['before'], readfile('Xmodelinefoldtext_write'))
setlocal foldtext=ModelineFoldText()
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['after'], readfile('Xmodelinefoldtext_write'))
setlocal bufhidden=wipe
call writefile(['before'], 'Xmodelinefoldtext_write', 'D')
edit! Xc
call setline(1, ['func T()', ' let i = 1', 'endfunc']) | %fold
call assert_equal('folded text', foldtextresult(1))
call assert_equal(['after'], readfile('Xmodelinefoldtext_write'))
bwipe!
set modeline& modelineexpr&
delfunc ModelineFoldText
delfunc Check_foldtext_in_modeline
endfunc
" Test for setting 'foldexpr' from the modeline and executing the expression
" in a sandbox
func Test_foldexpr_in_modeline()
func ModelineFoldExpr()
call writefile(['after'], 'Xmodelinefoldexpr_write')
return strlen(matchstr(getline(v:lnum),'^\s*'))
endfunc
let lines =<< trim END
aaa
bbb
ccc
ccc
bbb
aaa
" vim: foldenable foldmethod=expr foldexpr=ModelineFoldExpr()
END
call writefile(lines, 'Xmodelinefoldexpr', 'D')
func Check_foldexpr_in_modeline(set_cmd)
call writefile(['before'], 'Xmodelinefoldexpr_write', 'D')
split Xmodelinefoldexpr
call assert_equal(2, foldlevel(3))
call assert_equal(['before'], readfile('Xmodelinefoldexpr_write'))
split
call assert_equal(2, foldlevel(3))
call assert_equal(['before'], readfile('Xmodelinefoldexpr_write'))
close
setglobal foldexpr=ModelineFoldExpr()
call assert_equal(2, foldlevel(3))
call assert_equal(['before'], readfile('Xmodelinefoldexpr_write'))
setglobal foldexpr&
call assert_equal(2, foldlevel(3))
call assert_equal(['before'], readfile('Xmodelinefoldexpr_write'))
exe a:set_cmd 'foldexpr=ModelineFoldExpr()'
call assert_equal(2, foldlevel(3))
call assert_equal(['after'], readfile('Xmodelinefoldexpr_write'))
call writefile(['before'], 'Xmodelinefoldexpr_write')
exe 'sandbox' a:set_cmd 'foldexpr=ModelineFoldExpr()'
call assert_equal(2, foldlevel(3))
call assert_equal(['before'], readfile('Xmodelinefoldexpr_write'))
exe a:set_cmd 'foldexpr=ModelineFoldExpr()'
call assert_equal(2, foldlevel(3))
call assert_equal(['after'], readfile('Xmodelinefoldexpr_write'))
bw!
endfunc
set modeline modelineexpr
call Check_foldexpr_in_modeline('setlocal')
call Check_foldexpr_in_modeline('set')
new Xa
sandbox setglobal foldenable foldmethod=expr foldexpr=ModelineFoldExpr()
setlocal bufhidden=wipe
call writefile(['before'], 'Xmodelinefoldexpr_write', 'D')
edit! Xb
call setline(1, lines[0:5])
call assert_equal(2, foldlevel(3))
call assert_equal(['before'], readfile('Xmodelinefoldexpr_write'))
setglobal foldexpr=ModelineFoldExpr()
call assert_equal(2, foldlevel(3))
call assert_equal(['before'], readfile('Xmodelinefoldexpr_write'))
setlocal foldexpr=ModelineFoldExpr()
call assert_equal(2, foldlevel(3))
call assert_equal(['after'], readfile('Xmodelinefoldexpr_write'))
setlocal bufhidden=wipe
call writefile(['before'], 'Xmodelinefoldexpr_write', 'D')
edit! Xc
call setline(1, lines[0:5])
call assert_equal(2, foldlevel(3))
call assert_equal(['after'], readfile('Xmodelinefoldexpr_write'))
bwipe!
set modeline& modelineexpr&
delfunc ModelineFoldExpr
delfunc Check_foldexpr_in_modeline
endfunc
" Make sure a fold containing a nested fold is split correctly when using
" foldmethod=indent
func Test_fold_split()
new
let lines =<< trim END
line 1
line 2
line 3
line 4
line 5
END
call setline(1, lines)
setlocal sw=2
setlocal foldmethod=indent foldenable
call assert_equal([0, 1, 1, 2, 2], range(1, 5)->map('foldlevel(v:val)'))
call append(2, 'line 2.5')
call assert_equal([0, 1, 0, 1, 2, 2], range(1, 6)->map('foldlevel(v:val)'))
3d
call assert_equal([0, 1, 1, 2, 2], range(1, 5)->map('foldlevel(v:val)'))
bw!
endfunc
" Make sure that when you append under a blank line that is under a fold with
" the same indent level as your appended line, the fold expands across the
" blank line
func Test_indent_append_under_blank_line()
new
let lines =<< trim END
line 1
line 2
line 3
END
call setline(1, lines)
setlocal sw=2
setlocal foldmethod=indent foldenable
call assert_equal([0, 1, 1], range(1, 3)->map('foldlevel(v:val)'))
call append(3, '')
call append(4, ' line 5')
call assert_equal([0, 1, 1, 1, 1], range(1, 5)->map('foldlevel(v:val)'))
bw!
endfunc
" Make sure that when you delete 1 line of a fold whose length is 2 lines, the
" fold can't be closed since its length (1) is now less than foldminlines.
func Test_indent_one_line_fold_close()
let lines =<< trim END
line 1
line 2
line 3
END
new
setlocal sw=2 foldmethod=indent
call setline(1, lines)
" open all folds, delete line, then close all folds
normal zR
3delete
normal zM
call assert_equal(-1, foldclosed(2)) " the fold should not be closed
" Now do the same, but delete line 2 this time; this covers different code.
" (Combining this code with the above code doesn't expose both bugs.)
1,$delete
call setline(1, lines)
normal zR
2delete
normal zM
call assert_equal(-1, foldclosed(2))
bw!
endfunc
" Make sure that when appending [an indented line then a blank line] right
" before a single indented line, the resulting extended fold can be closed
func Test_indent_append_blank_small_fold_close()
new
setlocal sw=2 foldmethod=indent
" at first, the fold at the second line can't be closed since it's smaller
" than foldminlines
let lines =<< trim END
line 1
line 4
END
call setline(1, lines)
call append(1, [' line 2', ''])
" close all folds
normal zM
call assert_notequal(-1, foldclosed(2)) " the fold should be closed now
bw!
endfunc
func Test_sort_closed_fold()
CheckExecutable sort
call setline(1, [
\ 'Section 1',
\ ' how',
\ ' now',
\ ' brown',
\ ' cow',
\ 'Section 2',
\ ' how',
\ ' now',
\ ' brown',
\ ' cow',
\])
setlocal foldmethod=indent sw=3
normal 2G
" The "!!" expands to ".,.+3" and must only sort four lines
call feedkeys("!!sort\<CR>", 'xt')
call assert_equal([
\ 'Section 1',
\ ' brown',
\ ' cow',
\ ' how',
\ ' now',
\ 'Section 2',
\ ' how',
\ ' now',
\ ' brown',
\ ' cow',
\ ], getline(1, 10))
bwipe!
endfunc
func Test_indent_with_L_command()
" The "L" command moved the cursor to line zero, causing the text saved for
" undo to use line number -1, which caused trouble for undo later.
new
sil! norm 8RV{zf8=Lu
bwipe!
endfunc
" Make sure that when there is a fold at the bottom of the buffer and a newline
" character is appended to the line, the fold gets expanded (instead of the new
" line not being part of the fold).
func Test_expand_fold_at_bottom_of_buffer()
new
" create a fold on the only line
fold
execute "normal A\<CR>"
call assert_equal([1, 1], range(1, 2)->map('foldlevel(v:val)'))
bwipe!
endfunc
func Test_fold_screenrow_motion()
call setline(1, repeat(['aaaa'], 5))
1,4fold
norm Ggkzo
call assert_equal(1, line('.'))
endfunc
" This was using freed memory
func Test_foldcolumn_linebreak_control_char()
CheckFeature linebreak
5vnew
setlocal foldcolumn=1 linebreak
call setline(1, "aaa\<C-A>b")
redraw
call assert_equal([' aaa^', ' Ab '], ScreenLines([1, 2], 5))
call assert_equal(screenattr(1, 5), screenattr(2, 2))
bwipe!
endfunc
" This used to cause invalid memory access
func Test_foldexpr_return_empty_string()
new
setlocal foldexpr='' foldmethod=expr
redraw
bwipe!
endfunc
" Make sure that when ending a fold that hasn't been started, it does not
" start a new fold.
func Test_foldexpr_end_fold()
new
setlocal foldmethod=expr
let &l:foldexpr = 'v:lnum == 2 ? "<2" : "="'
call setline(1, range(1, 3))
redraw
call assert_equal([0, 0, 0], range(1, 3)->map('foldlevel(v:val)'))
bwipe!
endfunc
" Test moving cursor down to or beyond start of folded end of buffer.
func Test_cursor_down_fold_eob()
call setline(1, range(1, 4))
norm Gzf2kj
call assert_equal(2, line('.'))
norm zojzc
call assert_equal(3, line('.'))
norm j
call assert_equal(3, line('.'))
norm k2j
call assert_equal(4, line('.'))
bwipe!
endfunc
" issue: #15455
func Test_cursor_fold_marker_undo()
new
call setline(1, ['{{{', '', 'This is a Line', '', 'This is a Line', '', '}}}'])
let &ul=&ul
setl foldmethod=marker
call cursor(2, 1)
norm! zo1vjdu
call assert_equal(1, foldlevel('.'))
bwipe!
new
call setline(1, ['', '{{{', '', 'This is a Line', '', 'This is a Line', '', '}}}'])
let &ul=&ul
setl foldmethod=marker
call cursor(3, 1)
norm! zo
norm! vjdu
call assert_equal(1, foldlevel('.'))
bwipe!
endfunc
" vim: shiftwidth=2 sts=2 expandtab
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/mirrors/vim.git
git@gitee.com:mirrors/vim.git
mirrors
vim
Vim
master

搜索帮助