1" Test for breakindent
2"
3" Note: if you get strange failures when adding new tests, it might be that
4" while the test is run, the breakindent caching gets in its way.
5" It helps to change the tabstop setting and force a redraw (e.g. see
6" Test_breakindent08())
7source check.vim
8CheckOption breakindent
9
10source view_util.vim
11
12let s:input ="\tabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP"
13
14func s:screen_lines(lnum, width) abort
15  return ScreenLines([a:lnum, a:lnum + 2], a:width)
16endfunc
17
18func s:compare_lines(expect, actual)
19  call assert_equal(join(a:expect, "\n"), join(a:actual, "\n"))
20endfunc
21
22func s:test_windows(...)
23  call NewWindow(10, 20)
24  setl ts=4 sw=4 sts=4 breakindent
25  put =s:input
26  exe get(a:000, 0, '')
27endfunc
28
29func s:close_windows(...)
30  call CloseWindow()
31  exe get(a:000, 0, '')
32endfunc
33
34func Test_breakindent01()
35  " simple breakindent test
36  call s:test_windows('setl briopt=min:0')
37  let lines = s:screen_lines(line('.'),8)
38  let expect = [
39	\ "    abcd",
40	\ "    qrst",
41	\ "    GHIJ",
42	\ ]
43  call s:compare_lines(expect, lines)
44  call s:close_windows()
45endfunc
46
47func Test_breakindent01_vartabs()
48  " like 01 but with vartabs feature
49  if !has("vartabs")
50    return
51  endif
52  call s:test_windows('setl briopt=min:0 vts=4')
53  let lines = s:screen_lines(line('.'),8)
54  let expect = [
55	\ "    abcd",
56	\ "    qrst",
57	\ "    GHIJ",
58	\ ]
59  call s:compare_lines(expect, lines)
60  call s:close_windows('set vts&')
61endfunc
62
63func Test_breakindent02()
64  " simple breakindent test with showbreak set
65  set sbr=>>
66  call s:test_windows('setl briopt=min:0 sbr=')
67  let lines = s:screen_lines(line('.'),8)
68  let expect = [
69	\ "    abcd",
70	\ "    >>qr",
71	\ "    >>EF",
72	\ ]
73  call s:compare_lines(expect, lines)
74  call s:close_windows('set sbr=')
75endfunc
76
77func Test_breakindent02_vartabs()
78  if !has("vartabs")
79    return
80  endif
81  " simple breakindent test with showbreak set
82  call s:test_windows('setl briopt=min:0 sbr=>> vts=4')
83  let lines = s:screen_lines(line('.'),8)
84  let expect = [
85	\ "    abcd",
86	\ "    >>qr",
87	\ "    >>EF",
88	\ ]
89  call s:compare_lines(expect, lines)
90  call s:close_windows('set sbr= vts&')
91endfunc
92
93func Test_breakindent03()
94  " simple breakindent test with showbreak set and briopt including sbr
95  call s:test_windows('setl briopt=sbr,min:0 sbr=++')
96  let lines = s:screen_lines(line('.'),8)
97  let expect = [
98	\ "    abcd",
99	\ "++  qrst",
100	\ "++  GHIJ",
101	\ ]
102  call s:compare_lines(expect, lines)
103  " clean up
104  call s:close_windows('set sbr=')
105endfunc
106
107func Test_breakindent03_vartabs()
108  " simple breakindent test with showbreak set and briopt including sbr
109  if !has("vartabs")
110    return
111  endif
112  call s:test_windows('setl briopt=sbr,min:0 sbr=++ vts=4')
113  let lines = s:screen_lines(line('.'),8)
114  let expect = [
115	\ "    abcd",
116	\ "++  qrst",
117	\ "++  GHIJ",
118	\ ]
119  call s:compare_lines(expect, lines)
120  " clean up
121  call s:close_windows('set sbr= vts&')
122endfunc
123
124func Test_breakindent04()
125  " breakindent set with min width 18
126  set sbr=<<<
127  call s:test_windows('setl sbr=NONE briopt=min:18')
128  let lines = s:screen_lines(line('.'),8)
129  let expect = [
130	\ "    abcd",
131	\ "  qrstuv",
132	\ "  IJKLMN",
133	\ ]
134  call s:compare_lines(expect, lines)
135  " clean up
136  call s:close_windows('set sbr=')
137  set sbr=
138endfunc
139
140func Test_breakindent04_vartabs()
141  " breakindent set with min width 18
142  if !has("vartabs")
143    return
144  endif
145  call s:test_windows('setl sbr= briopt=min:18 vts=4')
146  let lines = s:screen_lines(line('.'),8)
147  let expect = [
148	\ "    abcd",
149	\ "  qrstuv",
150	\ "  IJKLMN",
151	\ ]
152  call s:compare_lines(expect, lines)
153  " clean up
154  call s:close_windows('set sbr= vts&')
155endfunc
156
157func Test_breakindent05()
158  " breakindent set and shift by 2
159  call s:test_windows('setl briopt=shift:2,min:0')
160  let lines = s:screen_lines(line('.'),8)
161  let expect = [
162	\ "    abcd",
163	\ "      qr",
164	\ "      EF",
165	\ ]
166  call s:compare_lines(expect, lines)
167  call s:close_windows()
168endfunc
169
170func Test_breakindent05_vartabs()
171  " breakindent set and shift by 2
172  if !has("vartabs")
173    return
174  endif
175  call s:test_windows('setl briopt=shift:2,min:0 vts=4')
176  let lines = s:screen_lines(line('.'),8)
177  let expect = [
178	\ "    abcd",
179	\ "      qr",
180	\ "      EF",
181	\ ]
182  call s:compare_lines(expect, lines)
183  call s:close_windows('set vts&')
184endfunc
185
186func Test_breakindent06()
187  " breakindent set and shift by -1
188  call s:test_windows('setl briopt=shift:-1,min:0')
189  let lines = s:screen_lines(line('.'),8)
190  let expect = [
191	\ "    abcd",
192	\ "   qrstu",
193	\ "   HIJKL",
194	\ ]
195  call s:compare_lines(expect, lines)
196  call s:close_windows()
197endfunc
198
199func Test_breakindent06_vartabs()
200  " breakindent set and shift by -1
201  if !has("vartabs")
202    return
203  endif
204  call s:test_windows('setl briopt=shift:-1,min:0 vts=4')
205  let lines = s:screen_lines(line('.'),8)
206  let expect = [
207	\ "    abcd",
208	\ "   qrstu",
209	\ "   HIJKL",
210	\ ]
211  call s:compare_lines(expect, lines)
212  call s:close_windows('set vts&')
213endfunc
214
215func Test_breakindent07()
216  " breakindent set and shift by 1, Number  set sbr=? and briopt:sbr
217  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu sbr=? nuw=4 cpo+=n')
218  let lines = s:screen_lines(line('.'),10)
219  let expect = [
220	\ "  2     ab",
221	\ "?        m",
222	\ "?        x",
223	\ ]
224  call s:compare_lines(expect, lines)
225  " clean up
226  call s:close_windows('set sbr= cpo-=n')
227endfunc
228
229func Test_breakindent07_vartabs()
230  if !has("vartabs")
231    return
232  endif
233  " breakindent set and shift by 1, Number  set sbr=? and briopt:sbr
234  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu sbr=? nuw=4 cpo+=n vts=4')
235  let lines = s:screen_lines(line('.'),10)
236  let expect = [
237	\ "  2     ab",
238	\ "?        m",
239	\ "?        x",
240	\ ]
241  call s:compare_lines(expect, lines)
242  " clean up
243  call s:close_windows('set sbr= cpo-=n vts&')
244endfunc
245
246func Test_breakindent07a()
247  " breakindent set and shift by 1, Number  set sbr=? and briopt:sbr
248  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu sbr=? nuw=4')
249  let lines = s:screen_lines(line('.'),10)
250  let expect = [
251	\ "  2     ab",
252	\ "    ?    m",
253	\ "    ?    x",
254	\ ]
255  call s:compare_lines(expect, lines)
256  " clean up
257  call s:close_windows('set sbr=')
258endfunc
259
260func Test_breakindent07a_vartabs()
261  if !has("vartabs")
262    return
263  endif
264  " breakindent set and shift by 1, Number  set sbr=? and briopt:sbr
265  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu sbr=? nuw=4 vts=4')
266  let lines = s:screen_lines(line('.'),10)
267  let expect = [
268	\ "  2     ab",
269	\ "    ?    m",
270	\ "    ?    x",
271	\ ]
272  call s:compare_lines(expect, lines)
273  " clean up
274  call s:close_windows('set sbr= vts&')
275endfunc
276
277func Test_breakindent08()
278  " breakindent set and shift by 1, Number and list set sbr=# and briopt:sbr
279  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu nuw=4 sbr=# list cpo+=n ts=4')
280  " make sure, cache is invalidated!
281  set ts=8
282  redraw!
283  set ts=4
284  redraw!
285  let lines = s:screen_lines(line('.'),10)
286  let expect = [
287	\ "  2 ^Iabcd",
288	\ "#      opq",
289	\ "#      BCD",
290	\ ]
291  call s:compare_lines(expect, lines)
292  call s:close_windows('set sbr= cpo-=n')
293endfunc
294
295func Test_breakindent08_vartabs()
296  if !has("vartabs")
297    return
298  endif
299  " breakindent set and shift by 1, Number and list set sbr=# and briopt:sbr
300  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu nuw=4 sbr=# list cpo+=n ts=4 vts=4')
301  " make sure, cache is invalidated!
302  set ts=8
303  redraw!
304  set ts=4
305  redraw!
306  let lines = s:screen_lines(line('.'),10)
307  let expect = [
308	\ "  2 ^Iabcd",
309	\ "#      opq",
310	\ "#      BCD",
311	\ ]
312  call s:compare_lines(expect, lines)
313  call s:close_windows('set sbr= cpo-=n vts&')
314endfunc
315
316func Test_breakindent08a()
317  " breakindent set and shift by 1, Number and list set sbr=# and briopt:sbr
318  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu nuw=4 sbr=# list')
319  let lines = s:screen_lines(line('.'),10)
320  let expect = [
321	\ "  2 ^Iabcd",
322	\ "    #  opq",
323	\ "    #  BCD",
324	\ ]
325  call s:compare_lines(expect, lines)
326  call s:close_windows('set sbr=')
327endfunc
328
329func Test_breakindent08a_vartabs()
330  if !has("vartabs")
331    return
332  endif
333  " breakindent set and shift by 1, Number and list set sbr=# and briopt:sbr
334  call s:test_windows('setl briopt=shift:1,sbr,min:0 nu nuw=4 sbr=# list vts=4')
335  let lines = s:screen_lines(line('.'),10)
336  let expect = [
337	\ "  2 ^Iabcd",
338	\ "    #  opq",
339	\ "    #  BCD",
340	\ ]
341  call s:compare_lines(expect, lines)
342  call s:close_windows('set sbr= vts&')
343endfunc
344
345func Test_breakindent09()
346  " breakindent set and shift by 1, Number and list set sbr=#
347  call s:test_windows('setl briopt=shift:1,min:0 nu nuw=4 sbr=# list')
348  let lines = s:screen_lines(line('.'),10)
349  let expect = [
350	\ "  2 ^Iabcd",
351	\ "       #op",
352	\ "       #AB",
353	\ ]
354  call s:compare_lines(expect, lines)
355  call s:close_windows('set sbr=')
356endfunc
357
358func Test_breakindent09_vartabs()
359  if !has("vartabs")
360    return
361  endif
362  " breakindent set and shift by 1, Number and list set sbr=#
363  call s:test_windows('setl briopt=shift:1,min:0 nu nuw=4 sbr=# list vts=4')
364  let lines = s:screen_lines(line('.'),10)
365  let expect = [
366	\ "  2 ^Iabcd",
367	\ "       #op",
368	\ "       #AB",
369	\ ]
370  call s:compare_lines(expect, lines)
371  call s:close_windows('set sbr= vts&')
372endfunc
373
374func Test_breakindent10()
375  " breakindent set, Number set sbr=~
376  call s:test_windows('setl cpo+=n sbr=~ nu nuw=4 nolist briopt=sbr,min:0')
377  " make sure, cache is invalidated!
378  set ts=8
379  redraw!
380  set ts=4
381  redraw!
382  let lines = s:screen_lines(line('.'),10)
383  let expect = [
384	\ "  2     ab",
385	\ "~       mn",
386	\ "~       yz",
387	\ ]
388  call s:compare_lines(expect, lines)
389  call s:close_windows('set sbr= cpo-=n')
390endfunc
391
392func Test_breakindent10_vartabs()
393  if !has("vartabs")
394    return
395  endif
396  " breakindent set, Number set sbr=~
397  call s:test_windows('setl cpo+=n sbr=~ nu nuw=4 nolist briopt=sbr,min:0 vts=4')
398  " make sure, cache is invalidated!
399  set ts=8
400  redraw!
401  set ts=4
402  redraw!
403  let lines = s:screen_lines(line('.'),10)
404  let expect = [
405	\ "  2     ab",
406	\ "~       mn",
407	\ "~       yz",
408	\ ]
409  call s:compare_lines(expect, lines)
410  call s:close_windows('set sbr= cpo-=n vts&')
411endfunc
412
413func Test_breakindent11()
414  " test strdisplaywidth()
415  call s:test_windows('setl cpo-=n sbr=>> nu nuw=4 nolist briopt= ts=4')
416  let text = getline(2)
417  let width = strlen(text[1:]) + indent(2) + strlen(&sbr) * 3 " text wraps 3 times
418  call assert_equal(width, strdisplaywidth(text))
419  call s:close_windows('set sbr=')
420endfunc
421
422func Test_breakindent11_vartabs()
423  if !has("vartabs")
424    return
425  endif
426  " test strdisplaywidth()
427  call s:test_windows('setl cpo-=n sbr=>> nu nuw=4 nolist briopt= ts=4 vts=4')
428  let text = getline(2)
429  let width = strlen(text[1:]) + 2->indent() + strlen(&sbr) * 3 " text wraps 3 times
430  call assert_equal(width, text->strdisplaywidth())
431  call s:close_windows('set sbr= vts&')
432endfunc
433
434func Test_breakindent12()
435  " test breakindent with long indent
436  let s:input = "\t\t\t\t\t{"
437  call s:test_windows('setl breakindent linebreak briopt=min:10 nu numberwidth=3 ts=4 list listchars=tab:>-')
438  let lines = s:screen_lines(2,16)
439  let expect = [
440	\ " 2 >--->--->--->",
441	\ "          ---{  ",
442	\ "~               ",
443	\ ]
444  call s:compare_lines(expect, lines)
445  call s:close_windows('set nuw=4 listchars=')
446endfunc
447
448func Test_breakindent12_vartabs()
449  if !has("vartabs")
450    return
451  endif
452  " test breakindent with long indent
453  let s:input = "\t\t\t\t\t{"
454  call s:test_windows('setl breakindent linebreak briopt=min:10 nu numberwidth=3 ts=4 list listchars=tab:>- vts=4')
455  let lines = s:screen_lines(2,16)
456  let expect = [
457	\ " 2 >--->--->--->",
458	\ "          ---{  ",
459	\ "~               ",
460	\ ]
461  call s:compare_lines(expect, lines)
462  call s:close_windows('set nuw=4 listchars= vts&')
463endfunc
464
465func Test_breakindent13()
466  let s:input = ""
467  call s:test_windows('setl breakindent briopt=min:10 ts=8')
468  vert resize 20
469  call setline(1, ["    a\tb\tc\td\te", "    z   y       x       w       v"])
470  1
471  norm! fbgj"ayl
472  2
473  norm! fygj"byl
474  call assert_equal('d', @a)
475  call assert_equal('w', @b)
476  call s:close_windows()
477endfunc
478
479func Test_breakindent13_vartabs()
480  if !has("vartabs")
481    return
482  endif
483  let s:input = ""
484  call s:test_windows('setl breakindent briopt=min:10 ts=8 vts=8')
485  vert resize 20
486  call setline(1, ["    a\tb\tc\td\te", "    z   y       x       w       v"])
487  1
488  norm! fbgj"ayl
489  2
490  norm! fygj"byl
491  call assert_equal('d', @a)
492  call assert_equal('w', @b)
493  call s:close_windows('set vts&')
494endfunc
495
496func Test_breakindent14()
497  let s:input = ""
498  call s:test_windows('setl breakindent briopt= ts=8')
499  vert resize 30
500  norm! 3a1234567890
501  norm! a    abcde
502  exec "norm! 0\<C-V>tex"
503  let lines = s:screen_lines(line('.'),8)
504  let expect = [
505	\ "e       ",
506	\ "~       ",
507	\ "~       ",
508	\ ]
509  call s:compare_lines(expect, lines)
510  call s:close_windows()
511endfunc
512
513func Test_breakindent14_vartabs()
514  if !has("vartabs")
515    return
516  endif
517  let s:input = ""
518  call s:test_windows('setl breakindent briopt= ts=8 vts=8')
519  vert resize 30
520  norm! 3a1234567890
521  norm! a    abcde
522  exec "norm! 0\<C-V>tex"
523  let lines = s:screen_lines(line('.'),8)
524  let expect = [
525	\ "e       ",
526	\ "~       ",
527	\ "~       ",
528	\ ]
529  call s:compare_lines(expect, lines)
530  call s:close_windows('set vts&')
531endfunc
532
533func Test_breakindent15()
534  let s:input = ""
535  call s:test_windows('setl breakindent briopt= ts=8 sw=8')
536  vert resize 30
537  norm! 4a1234567890
538  exe "normal! >>\<C-V>3f0x"
539  let lines = s:screen_lines(line('.'),20)
540  let expect = [
541	\ "        1234567890  ",
542	\ "~                   ",
543	\ "~                   ",
544	\ ]
545  call s:compare_lines(expect, lines)
546  call s:close_windows()
547endfunc
548
549func Test_breakindent15_vartabs()
550  if !has("vartabs")
551    return
552  endif
553  let s:input = ""
554  call s:test_windows('setl breakindent briopt= ts=8 sw=8 vts=8')
555  vert resize 30
556  norm! 4a1234567890
557  exe "normal! >>\<C-V>3f0x"
558  let lines = s:screen_lines(line('.'),20)
559  let expect = [
560	\ "        1234567890  ",
561	\ "~                   ",
562	\ "~                   ",
563	\ ]
564  call s:compare_lines(expect, lines)
565  call s:close_windows('set vts&')
566endfunc
567
568func Test_breakindent16()
569  " Check that overlong lines are indented correctly.
570  let s:input = ""
571  call s:test_windows('setl breakindent briopt=min:0 ts=4')
572  call setline(1, "\t".repeat("1234567890", 10))
573  resize 6
574  norm! 1gg$
575  redraw!
576  let lines = s:screen_lines(1,10)
577  let expect = [
578	\ "    789012",
579	\ "    345678",
580	\ "    901234",
581	\ ]
582  call s:compare_lines(expect, lines)
583  let lines = s:screen_lines(4,10)
584  let expect = [
585	\ "    567890",
586	\ "    123456",
587	\ "    7890  ",
588	\ ]
589  call s:compare_lines(expect, lines)
590  call s:close_windows()
591endfunc
592
593func Test_breakindent16_vartabs()
594  if !has("vartabs")
595    return
596  endif
597  " Check that overlong lines are indented correctly.
598  let s:input = ""
599  call s:test_windows('setl breakindent briopt=min:0 ts=4 vts=4')
600  call setline(1, "\t".repeat("1234567890", 10))
601  resize 6
602  norm! 1gg$
603  redraw!
604  let lines = s:screen_lines(1,10)
605  let expect = [
606	\ "    789012",
607	\ "    345678",
608	\ "    901234",
609	\ ]
610  call s:compare_lines(expect, lines)
611  let lines = s:screen_lines(4,10)
612  let expect = [
613	\ "    567890",
614	\ "    123456",
615	\ "    7890  ",
616	\ ]
617  call s:compare_lines(expect, lines)
618  call s:close_windows('set vts&')
619endfunc
620
621func Test_breakindent17_vartabs()
622  if !has("vartabs")
623    return
624  endif
625  let s:input = ""
626  call s:test_windows('setl breakindent list listchars=tab:<-> showbreak=+++')
627  call setline(1, "\t" . repeat('a', 63))
628  vert resize 30
629  norm! 1gg$
630  redraw!
631  let lines = s:screen_lines(1, 30)
632  let expect = [
633	\ "<-->aaaaaaaaaaaaaaaaaaaaaaaaaa",
634	\ "    +++aaaaaaaaaaaaaaaaaaaaaaa",
635	\ "    +++aaaaaaaaaaaaaa         ",
636	\ ]
637  call s:compare_lines(expect, lines)
638  call s:close_windows('set breakindent& list& listchars& showbreak&')
639endfunc
640
641func Test_breakindent18_vartabs()
642  if !has("vartabs")
643    return
644  endif
645  let s:input = ""
646  call s:test_windows('setl breakindent list listchars=tab:<->')
647  call setline(1, "\t" . repeat('a', 63))
648  vert resize 30
649  norm! 1gg$
650  redraw!
651  let lines = s:screen_lines(1, 30)
652  let expect = [
653	\ "<-->aaaaaaaaaaaaaaaaaaaaaaaaaa",
654	\ "    aaaaaaaaaaaaaaaaaaaaaaaaaa",
655	\ "    aaaaaaaaaaa               ",
656	\ ]
657  call s:compare_lines(expect, lines)
658  call s:close_windows('set breakindent& list& listchars&')
659endfunc
660
661func Test_breakindent19_sbr_nextpage()
662  let s:input = ""
663  call s:test_windows('setl breakindent briopt=shift:2,sbr,min:18 sbr=>')
664  call setline(1, repeat('a', 200))
665  norm! 1gg
666  redraw!
667  let lines = s:screen_lines(1, 20)
668  let expect = [
669	\ "aaaaaaaaaaaaaaaaaaaa",
670	\ "> aaaaaaaaaaaaaaaaaa",
671	\ "> aaaaaaaaaaaaaaaaaa",
672	\ ]
673  call s:compare_lines(expect, lines)
674  " Scroll down one screen line
675  setl scrolloff=5
676  norm! 5gj
677  redraw!
678  let lines = s:screen_lines(1, 20)
679  let expect = [
680	\ "> aaaaaaaaaaaaaaaaaa",
681	\ "> aaaaaaaaaaaaaaaaaa",
682	\ "> aaaaaaaaaaaaaaaaaa",
683	\ ]
684  call s:compare_lines(expect, lines)
685
686  setl breakindent briopt=min:18 sbr=>
687  norm! 5gj
688  let lines = s:screen_lines(1, 20)
689  let expect = [
690	\ ">aaaaaaaaaaaaaaaaaaa",
691	\ ">aaaaaaaaaaaaaaaaaaa",
692	\ ">aaaaaaaaaaaaaaaaaaa",
693	\ ]
694  call s:compare_lines(expect, lines)
695  call s:close_windows('set breakindent& briopt& sbr&')
696endfunc
697