blob: 67e5a48fe614f29991d30076ca2b72f748b81ac8 [file] [log] [blame]
Bram Moolenaarded27822017-01-02 14:27:34 +01001" Test for folding
2
Bram Moolenaar907dad72018-07-10 15:07:15 +02003source view_util.vim
Bram Moolenaar7701f302018-10-02 21:20:32 +02004source screendump.vim
Bram Moolenaar907dad72018-07-10 15:07:15 +02005
Bram Moolenaar94be6192017-04-22 22:40:11 +02006func PrepIndent(arg)
Bram Moolenaar88d298a2017-03-14 21:53:58 +01007 return [a:arg] + repeat(["\t".a:arg], 5)
8endfu
9
Bram Moolenaar94be6192017-04-22 22:40:11 +020010func Test_address_fold()
Bram Moolenaarded27822017-01-02 14:27:34 +010011 new
12 call setline(1, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
13 \ 'after fold 1', 'after fold 2', 'after fold 3'])
14 setl fen fdm=marker
Bram Moolenaar518c9b12017-03-21 11:48:39 +010015 " The next commands should all copy the same part of the buffer,
16 " regardless of the addressing type, since the part to be copied
Bram Moolenaarded27822017-01-02 14:27:34 +010017 " is folded away
18 :1y
19 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
20 :.y
21 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
22 :.+y
23 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
24 :.,.y
25 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
26 :sil .1,.y
27 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
28 " use silent to make E493 go away
29 :sil .+,.y
30 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
31 :,y
32 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
33 :,+y
34 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/','after fold 1'], getreg(0,1,1))
35 " using .+3 as second address should copy the whole folded line + the next 3
36 " lines
37 :.,+3y
38 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/',
39 \ 'after fold 1', 'after fold 2', 'after fold 3'], getreg(0,1,1))
40 :sil .,-2y
41 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
42
43 " now test again with folding disabled
44 set nofoldenable
45 :1y
46 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
47 :.y
48 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
49 :.+y
50 call assert_equal(['1'], getreg(0,1,1))
51 :.,.y
52 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
53 " use silent to make E493 go away
54 :sil .1,.y
55 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
56 " use silent to make E493 go away
57 :sil .+,.y
58 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
59 :,y
60 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
61 :,+y
62 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
63 " using .+3 as second address should copy the whole folded line + the next 3
64 " lines
65 :.,+3y
66 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3'], getreg(0,1,1))
67 :7
68 :sil .,-2y
69 call assert_equal(['4', '5', '}/*}}}*/'], getreg(0,1,1))
70
71 quit!
Bram Moolenaar1159b162017-02-28 21:53:56 +010072endfunc
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010073
Bram Moolenaar94be6192017-04-22 22:40:11 +020074func Test_indent_fold()
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010075 new
76 call setline(1, ['', 'a', ' b', ' c'])
77 setl fen fdm=indent
78 2
79 norm! >>
80 let a=map(range(1,4), 'foldclosed(v:val)')
81 call assert_equal([-1,-1,-1,-1], a)
82 bw!
Bram Moolenaar1159b162017-02-28 21:53:56 +010083endfunc
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010084
Bram Moolenaar94be6192017-04-22 22:40:11 +020085func Test_indent_fold2()
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010086 new
87 call setline(1, ['', '{{{', '}}}', '{{{', '}}}'])
88 setl fen fdm=marker
89 2
90 norm! >>
91 let a=map(range(1,5), 'foldclosed(v:val)')
92 call assert_equal([-1,-1,-1,4,4], a)
93 bw!
Bram Moolenaar1159b162017-02-28 21:53:56 +010094endfunc
95
96func Test_manual_fold_with_filter()
97 if !executable('cat')
98 return
99 endif
Bram Moolenaar3f3897e2017-03-04 15:28:53 +0100100 for type in ['manual', 'marker']
101 exe 'set foldmethod=' . type
102 new
103 call setline(1, range(1, 20))
104 4,$fold
105 %foldopen
106 10,$fold
107 %foldopen
108 " This filter command should not have an effect
109 1,8! cat
110 call feedkeys('5ggzdzMGdd', 'xt')
111 call assert_equal(['1', '2', '3', '4', '5', '6', '7', '8', '9'], getline(1, '$'))
112
113 bwipe!
114 set foldmethod&
115 endfor
Bram Moolenaar1159b162017-02-28 21:53:56 +0100116endfunc
Bram Moolenaareadbc2b2017-03-04 18:42:39 +0100117
Bram Moolenaar94be6192017-04-22 22:40:11 +0200118func Test_indent_fold_with_read()
Bram Moolenaareadbc2b2017-03-04 18:42:39 +0100119 new
120 set foldmethod=indent
121 call setline(1, repeat(["\<Tab>a"], 4))
122 for n in range(1, 4)
123 call assert_equal(1, foldlevel(n))
124 endfor
125
126 call writefile(["a", "", "\<Tab>a"], 'Xfile')
127 foldopen
128 2read Xfile
129 %foldclose
130 call assert_equal(1, foldlevel(1))
131 call assert_equal(2, foldclosedend(1))
132 call assert_equal(0, foldlevel(3))
133 call assert_equal(0, foldlevel(4))
134 call assert_equal(1, foldlevel(5))
135 call assert_equal(7, foldclosedend(5))
136
137 bwipe!
138 set foldmethod&
139 call delete('Xfile')
140endfunc
141
142func Test_combining_folds_indent()
143 new
144 let one = "\<Tab>a"
145 let zero = 'a'
146 call setline(1, [one, one, zero, zero, zero, one, one, one])
147 set foldmethod=indent
148 3,5d
149 %foldclose
150 call assert_equal(5, foldclosedend(1))
151
152 set foldmethod&
153 bwipe!
154endfunc
155
156func Test_combining_folds_marker()
157 new
158 call setline(1, ['{{{', '}}}', '', '', '', '{{{', '', '}}}'])
159 set foldmethod=marker
160 3,5d
161 %foldclose
162 call assert_equal(2, foldclosedend(1))
163
164 set foldmethod&
165 bwipe!
166endfunc
167
Bram Moolenaar025a6b72017-03-12 20:37:21 +0100168func Test_folds_marker_in_comment()
169 new
170 call setline(1, ['" foo', 'bar', 'baz'])
171 setl fen fdm=marker
172 setl com=sO:\"\ -,mO:\"\ \ ,eO:\"\",:\" cms=\"%s
173 norm! zf2j
174 setl nofen
175 :1y
176 call assert_equal(['" foo{{{'], getreg(0,1,1))
177 :+2y
178 call assert_equal(['baz"}}}'], getreg(0,1,1))
179
180 set foldmethod&
181 bwipe!
182endfunc
183
Bram Moolenaareadbc2b2017-03-04 18:42:39 +0100184func s:TestFoldExpr(lnum)
185 let thisline = getline(a:lnum)
186 if thisline == 'a'
187 return 1
188 elseif thisline == 'b'
189 return 0
190 elseif thisline == 'c'
191 return '<1'
192 elseif thisline == 'd'
193 return '>1'
194 endif
195 return 0
196endfunction
197
198func Test_update_folds_expr_read()
199 new
200 call setline(1, ['a', 'a', 'a', 'a', 'a', 'a'])
201 set foldmethod=expr
202 set foldexpr=s:TestFoldExpr(v:lnum)
203 2
204 foldopen
205 call writefile(['b', 'b', 'a', 'a', 'd', 'a', 'a', 'c'], 'Xfile')
206 read Xfile
207 %foldclose
208 call assert_equal(2, foldclosedend(1))
209 call assert_equal(0, foldlevel(3))
210 call assert_equal(0, foldlevel(4))
211 call assert_equal(6, foldclosedend(5))
212 call assert_equal(10, foldclosedend(7))
213 call assert_equal(14, foldclosedend(11))
214
215 call delete('Xfile')
216 bwipe!
217 set foldmethod& foldexpr&
218endfunc
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100219
Bram Moolenaar94be6192017-04-22 22:40:11 +0200220func Check_foldlevels(expected)
221 call assert_equal(a:expected, map(range(1, line('$')), 'foldlevel(v:val)'))
222endfunc
223
224func Test_move_folds_around_manual()
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100225 new
226 let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c")
227 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
228 let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14]
229 " all folds closed
230 set foldenable foldlevel=0 fdm=indent
231 " needs a forced redraw
232 redraw!
233 set fdm=manual
234 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
235 call assert_equal(input, getline(1, '$'))
236 7,12m0
237 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
238 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
239 10,12m0
240 call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$'))
241 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)'))
242 " moving should not close the folds
243 %d
244 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
245 set fdm=indent
246 redraw!
247 set fdm=manual
248 call cursor(2, 1)
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100249 %foldopen
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100250 7,12m0
251 let folds=repeat([-1], 18)
252 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
253 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
254 norm! zM
255 " folds are not corrupted and all have been closed
256 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)'))
257 %d
258 call setline(1, ["a", "\tb", "\tc", "\td", "\te"])
259 set fdm=indent
260 redraw!
261 set fdm=manual
262 %foldopen
263 3m4
264 %foldclose
265 call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$'))
266 call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)'))
267 %d
268 call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"])
269 set fdm=indent foldlevel=0
270 set fdm=manual
271 %foldopen
272 3m1
273 %foldclose
274 call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$'))
275 call assert_equal(0, foldlevel(2))
276 call assert_equal(5, foldclosedend(3))
277 call assert_equal([-1, -1, 3, 3, 3, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)'))
278 2,6m$
279 %foldclose
280 call assert_equal(5, foldclosedend(2))
281 call assert_equal(0, foldlevel(6))
282 call assert_equal(9, foldclosedend(7))
283 call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, -1], map(range(1, line('$')), 'foldclosed(v:val)'))
Bram Moolenaar495b7dd2017-09-16 17:19:22 +0200284
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100285 %d
286 " Ensure moving around the edges still works.
287 call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"])
288 set fdm=indent foldlevel=0
289 set fdm=manual
290 %foldopen
291 6m$
292 " The first fold has been truncated to the 5'th line.
293 " Second fold has been moved up because the moved line is now below it.
Bram Moolenaar94be6192017-04-22 22:40:11 +0200294 call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 0])
295
296 %delete
297 set fdm=indent foldlevel=0
298 call setline(1, [
299 \ "a",
300 \ "\ta",
301 \ "\t\ta",
302 \ "\t\ta",
303 \ "\t\ta",
304 \ "a",
305 \ "a"])
306 set fdm=manual
307 %foldopen!
308 4,5m6
309 call Check_foldlevels([0, 1, 2, 0, 0, 0, 0])
310
311 %delete
312 set fdm=indent
313 call setline(1, [
314 \ "\ta",
315 \ "\t\ta",
316 \ "\t\ta",
317 \ "\t\ta",
318 \ "\ta",
319 \ "\t\ta",
320 \ "\t\ta",
321 \ "\t\ta",
322 \ "\ta",
323 \ "\t\ta",
324 \ "\t\ta",
325 \ "\t\ta",
326 \ "\t\ta",
327 \ "\ta",
328 \ "a"])
329 set fdm=manual
330 %foldopen!
331 13m7
332 call Check_foldlevels([1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0])
333
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100334 bw!
335endfunc
336
Bram Moolenaar94be6192017-04-22 22:40:11 +0200337func Test_move_folds_around_indent()
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100338 new
339 let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c")
340 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
341 let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14]
342 " all folds closed
343 set fdm=indent
344 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
345 call assert_equal(input, getline(1, '$'))
346 7,12m0
347 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
348 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
349 10,12m0
350 call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$'))
351 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)'))
352 " moving should not close the folds
353 %d
354 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
355 set fdm=indent
356 call cursor(2, 1)
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100357 %foldopen
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100358 7,12m0
359 let folds=repeat([-1], 18)
360 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
361 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
362 norm! zM
363 " folds are not corrupted and all have been closed
364 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)'))
365 %d
366 call setline(1, ["a", "\tb", "\tc", "\td", "\te"])
367 set fdm=indent
368 %foldopen
369 3m4
370 %foldclose
371 call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$'))
372 call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)'))
373 %d
374 call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"])
375 set fdm=indent foldlevel=0
376 %foldopen
377 3m1
378 %foldclose
379 call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$'))
380 call assert_equal(1, foldlevel(2))
381 call assert_equal(5, foldclosedend(3))
382 call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)'))
383 2,6m$
384 %foldclose
385 call assert_equal(9, foldclosedend(2))
386 call assert_equal(1, foldlevel(6))
387 call assert_equal(9, foldclosedend(7))
388 call assert_equal([-1, 2, 2, 2, 2, 2, 2, 2, 2, -1], map(range(1, line('$')), 'foldclosed(v:val)'))
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100389 " Ensure moving around the edges still works.
390 %d
391 call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"])
392 set fdm=indent foldlevel=0
393 %foldopen
394 6m$
395 " The first fold has been truncated to the 5'th line.
396 " Second fold has been moved up because the moved line is now below it.
Bram Moolenaar94be6192017-04-22 22:40:11 +0200397 call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 1])
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100398 bw!
399endfunc
Bram Moolenaar518c9b12017-03-21 11:48:39 +0100400
401func Test_folddoopen_folddoclosed()
402 new
403 call setline(1, range(1, 9))
404 set foldmethod=manual
405 1,3 fold
406 6,8 fold
407
408 " Test without range.
409 folddoopen s/$/o/
410 folddoclosed s/$/c/
411 call assert_equal(['1c', '2c', '3c',
412 \ '4o', '5o',
413 \ '6c', '7c', '8c',
414 \ '9o'], getline(1, '$'))
415
416 " Test with range.
417 call setline(1, range(1, 9))
418 1,8 folddoopen s/$/o/
419 4,$ folddoclosed s/$/c/
420 call assert_equal(['1', '2', '3',
421 \ '4o', '5o',
422 \ '6c', '7c', '8c',
423 \ '9'], getline(1, '$'))
424
425 set foldmethod&
426 bw!
427endfunc
428
429func Test_fold_error()
430 new
431 call setline(1, [1, 2])
432
433 for fm in ['indent', 'expr', 'syntax', 'diff']
434 exe 'set foldmethod=' . fm
435 call assert_fails('norm zf', 'E350:')
436 call assert_fails('norm zd', 'E351:')
437 call assert_fails('norm zE', 'E352:')
438 endfor
439
440 set foldmethod=manual
441 call assert_fails('norm zd', 'E490:')
442 call assert_fails('norm zo', 'E490:')
443 call assert_fails('3fold', 'E16:')
444
445 set foldmethod=marker
446 set nomodifiable
447 call assert_fails('1,2fold', 'E21:')
448
449 set modifiable&
450 set foldmethod&
451 bw!
452endfunc
Bram Moolenaar495b7dd2017-09-16 17:19:22 +0200453
454func Test_foldtext_recursive()
455 new
456 call setline(1, ['{{{', 'some text', '}}}'])
457 setlocal foldenable foldmethod=marker foldtext=foldtextresult(v\:foldstart)
458 " This was crashing because of endless recursion.
459 2foldclose
460 redraw
461 call assert_equal(1, foldlevel(2))
462 call assert_equal(1, foldclosed(2))
463 call assert_equal(3, foldclosedend(2))
464 bwipe!
465endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +0100466
467" Various fold related tests
468
469" Basic test if a fold can be created, opened, moving to the end and closed
470func Test_fold_manual()
471 enew!
472 set fdm=manual
473
474 let content = ['1 aa', '2 bb', '3 cc']
475 call append(0, content)
476 call cursor(1, 1)
477 normal zf2j
478 call assert_equal('1 aa', getline(foldclosed('.')))
479 normal zo
480 call assert_equal(-1, foldclosed('.'))
481 normal ]z
482 call assert_equal('3 cc', getline('.'))
483 normal zc
484 call assert_equal('1 aa', getline(foldclosed('.')))
485
486 set fdm&
487 enew!
488endfunc
489
490" test folding with markers.
491func Test_fold_marker()
492 enew!
493 set fdm=marker fdl=1 fdc=3
494
495 let content = ['4 dd {{{', '5 ee {{{ }}}', '6 ff }}}']
496 call append(0, content)
497 call cursor(2, 1)
498 call assert_equal(2, foldlevel('.'))
499 normal [z
500 call assert_equal(1, foldlevel('.'))
501 exe "normal jo{{ \<Esc>r{jj"
502 call assert_equal(1, foldlevel('.'))
503 normal kYpj
504 call assert_equal(0, foldlevel('.'))
505
506 set fdm& fdl& fdc&
507 enew!
508endfunc
509
Bram Moolenaar4af72592018-12-09 15:00:52 +0100510" test create fold markers with C filetype
511func Test_fold_create_marker_in_C()
512 enew!
513 set fdm=marker fdl=9
514 set filetype=c
515
516 let content = [
517 \ '/*',
518 \ ' * comment',
519 \ ' * ',
520 \ ' *',
521 \ ' */',
522 \ 'int f(int* p) {',
523 \ ' *p = 3;',
524 \ ' return 0;',
525 \ '}'
526 \]
527 for c in range(len(content) - 1)
528 bw!
529 call append(0, content)
530 call cursor(c + 1, 1)
531 norm! zfG
532 call assert_equal(content[c] . (c < 4 ? '{{{' : '/*{{{*/'), getline(c + 1))
533 endfor
534
535 set fdm& fdl&
536 enew!
537endfunc
538
Bram Moolenaar430dc5d2017-11-02 21:04:47 +0100539" test folding with indent
540func Test_fold_indent()
541 enew!
542 set fdm=indent sw=2
543
544 let content = ['1 aa', '2 bb', '3 cc']
545 call append(0, content)
546 call cursor(2, 1)
547 exe "normal i \<Esc>jI "
548 call assert_equal(2, foldlevel('.'))
549 normal k
550 call assert_equal(1, foldlevel('.'))
551
552 set fdm& sw&
553 enew!
554endfunc
555
556" test syntax folding
557func Test_fold_syntax()
558 if !has('syntax')
559 return
560 endif
561
562 enew!
563 set fdm=syntax fdl=0
564
565 syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3
566 syn region Fd1 start="ee" end="ff" fold contained
567 syn region Fd2 start="gg" end="hh" fold contained
568 syn region Fd3 start="commentstart" end="commentend" fold contained
569 let content = ['3 cc', '4 dd {{{', '5 ee {{{ }}}', '{{{{', '6 ff }}}',
570 \ '6 ff }}}', '7 gg', '8 hh', '9 ii']
571 call append(0, content)
572 normal Gzk
573 call assert_equal('9 ii', getline('.'))
574 normal k
575 call assert_equal('3 cc', getline('.'))
576 exe "normal jAcommentstart \<Esc>Acommentend"
577 set fdl=1
578 normal 3j
579 call assert_equal('7 gg', getline('.'))
580 set fdl=0
581 exe "normal zO\<C-L>j"
582 call assert_equal('8 hh', getline('.'))
583 syn clear Fd1 Fd2 Fd3 Hup
584
585 set fdm& fdl&
586 enew!
587endfunc
588
589func Flvl()
590 let l = getline(v:lnum)
591 if l =~ "bb$"
592 return 2
593 elseif l =~ "gg$"
594 return "s1"
595 elseif l =~ "ii$"
596 return ">2"
597 elseif l =~ "kk$"
598 return "0"
599 endif
600 return "="
601endfun
602
603" test expression folding
604func Test_fold_expr()
605 enew!
606 set fdm=expr fde=Flvl()
607
608 let content = ['1 aa',
609 \ '2 bb',
610 \ '3 cc',
611 \ '4 dd {{{commentstart commentend',
612 \ '5 ee {{{ }}}',
613 \ '{{{',
614 \ '6 ff }}}',
615 \ '6 ff }}}',
616 \ ' 7 gg',
617 \ ' 8 hh',
618 \ '9 ii',
619 \ 'a jj',
620 \ 'b kk']
621 call append(0, content)
622 call cursor(1, 1)
623 exe "normal /bb$\<CR>"
624 call assert_equal(2, foldlevel('.'))
625 exe "normal /hh$\<CR>"
626 call assert_equal(1, foldlevel('.'))
627 exe "normal /ii$\<CR>"
628 call assert_equal(2, foldlevel('.'))
629 exe "normal /kk$\<CR>"
630 call assert_equal(0, foldlevel('.'))
631
632 set fdm& fde&
633 enew!
634endfunc
635
636" Bug with fdm=indent and moving folds
637" Moving a fold a few times, messes up the folds below the moved fold.
638" Fixed by 7.4.700
639func Test_fold_move()
640 enew!
641 set fdm=indent sw=2 fdl=0
642
643 let content = ['', '', 'Line1', ' Line2', ' Line3',
644 \ 'Line4', ' Line5', ' Line6',
645 \ 'Line7', ' Line8', ' Line9']
646 call append(0, content)
647 normal zM
648 call cursor(4, 1)
649 move 2
650 move 1
651 call assert_equal(7, foldclosed(7))
652 call assert_equal(8, foldclosedend(7))
653 call assert_equal(0, foldlevel(9))
654 call assert_equal(10, foldclosed(10))
655 call assert_equal(11, foldclosedend(10))
656 call assert_equal('+-- 2 lines: Line2', foldtextresult(2))
657 call assert_equal('+-- 2 lines: Line8', foldtextresult(10))
658
659 set fdm& sw& fdl&
660 enew!
661endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100662
663" test for patch 7.3.637
664" Cannot catch the error caused by a foldopen when there is no fold.
665func Test_foldopen_exception()
666 enew!
667 let a = 'No error caught'
668 try
669 foldopen
670 catch
671 let a = matchstr(v:exception,'^[^ ]*')
672 endtry
673 call assert_equal('Vim(foldopen):E490:', a)
674
675 let a = 'No error caught'
676 try
677 foobar
678 catch
679 let a = matchstr(v:exception,'^[^ ]*')
680 endtry
681 call assert_match('E492:', a)
682endfunc
Bram Moolenaar907dad72018-07-10 15:07:15 +0200683
684func Test_fold_last_line_with_pagedown()
685 enew!
686 set fdm=manual
687
688 let expect = '+-- 11 lines: 9---'
689 let content = range(1,19)
690 call append(0, content)
691 normal dd9G
692 normal zfG
693 normal zt
694 call assert_equal('9', getline(foldclosed('.')))
695 call assert_equal('19', getline(foldclosedend('.')))
696 call assert_equal(expect, ScreenLines(1, len(expect))[0])
697 call feedkeys("\<C-F>", 'xt')
698 call assert_equal(expect, ScreenLines(1, len(expect))[0])
699 call feedkeys("\<C-F>", 'xt')
700 call assert_equal(expect, ScreenLines(1, len(expect))[0])
701 call feedkeys("\<C-B>\<C-F>\<C-F>", 'xt')
702 call assert_equal(expect, ScreenLines(1, len(expect))[0])
703
704 set fdm&
705 enew!
706endfunc
Bram Moolenaar7701f302018-10-02 21:20:32 +0200707
708func Test_folds_with_rnu()
709 if !CanRunVimInTerminal()
710 return
711 endif
712
713 call writefile([
714 \ 'set fdm=marker rnu foldcolumn=2',
715 \ 'call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])',
716 \ ], 'Xtest_folds_with_rnu')
717 let buf = RunVimInTerminal('-S Xtest_folds_with_rnu', {})
718
719 call VerifyScreenDump(buf, 'Test_folds_with_rnu_01', {})
720 call term_sendkeys(buf, "j")
721 call VerifyScreenDump(buf, 'Test_folds_with_rnu_02', {})
722
723 " clean up
724 call StopVimInTerminal(buf)
725 call delete('Xtest_folds_with_rnu')
726endfunc
Bram Moolenaar53932812018-12-07 21:08:49 +0100727
728func Test_folds_marker_in_comment2()
729 new
730 call setline(1, ['Lorem ipsum dolor sit', 'Lorem ipsum dolor sit', 'Lorem ipsum dolor sit'])
731 setl fen fdm=marker
732 setl commentstring=<!--%s-->
733 setl comments=s:<!--,m:\ \ \ \ ,e:-->
734 norm! zf2j
735 setl nofen
736 :1y
737 call assert_equal(['Lorem ipsum dolor sit<!--{{{-->'], getreg(0,1,1))
738 :+2y
739 call assert_equal(['Lorem ipsum dolor sit<!--}}}-->'], getreg(0,1,1))
740
741 set foldmethod&
742 bwipe!
743endfunc