blob: d529b8520afbfc2dcbbd7b5169e6be844ae89da1 [file] [log] [blame]
Bram Moolenaar209d3872017-11-16 21:52:51 +01001" Tests for the various 'formatoptions' settings
Bram Moolenaar396b7c72019-10-21 23:08:59 +02002
3source check.vim
4
Bram Moolenaar209d3872017-11-16 21:52:51 +01005func Test_text_format()
6 enew!
7
8 setl noai tw=2 fo=t
9 call append('$', [
10 \ '{',
11 \ ' ',
12 \ '',
13 \ '}'])
14 exe "normal /^{/+1\n0"
15 normal gRa b
16 let lnum = line('.')
17 call assert_equal([
18 \ 'a',
19 \ 'b'], getline(lnum - 1, lnum))
20
21 normal ggdG
22 setl ai tw=2 fo=tw
23 call append('$', [
24 \ '{',
25 \ 'a b ',
26 \ '',
27 \ 'a ',
28 \ '}'])
29 exe "normal /^{/+1\n0"
30 normal gqgqjjllab
31 let lnum = line('.')
32 call assert_equal([
33 \ 'a ',
34 \ 'b ',
35 \ '',
36 \ 'a ',
37 \ 'b'], getline(lnum - 4, lnum))
38
39 normal ggdG
40 setl tw=3 fo=t
41 call append('$', [
42 \ '{',
43 \ "a \<C-A>",
44 \ '}'])
45 exe "normal /^{/+1\n0"
46 exe "normal gqgqo\na \<C-V>\<C-A>"
47 let lnum = line('.')
48 call assert_equal([
49 \ 'a',
50 \ "\<C-A>",
51 \ '',
52 \ 'a',
53 \ "\<C-A>"], getline(lnum - 4, lnum))
54
55 normal ggdG
56 setl tw=2 fo=tcq1 comments=:#
57 call append('$', [
58 \ '{',
59 \ 'a b',
60 \ '#a b',
61 \ '}'])
62 exe "normal /^{/+1\n0"
63 exe "normal gqgqjgqgqo\na b\n#a b"
64 let lnum = line('.')
65 call assert_equal([
66 \ 'a b',
67 \ '#a b',
68 \ '',
69 \ 'a b',
70 \ '#a b'], getline(lnum - 4, lnum))
71
72 normal ggdG
73 setl tw=5 fo=tcn comments=:#
74 call append('$', [
75 \ '{',
76 \ ' 1 a',
77 \ '# 1 a',
78 \ '}'])
79 exe "normal /^{/+1\n0"
80 exe "normal A b\<Esc>jA b"
81 let lnum = line('.')
82 call assert_equal([
83 \ ' 1 a',
84 \ ' b',
85 \ '# 1 a',
86 \ '# b'], getline(lnum - 3, lnum))
87
88 normal ggdG
89 setl tw=5 fo=t2a si
90 call append('$', [
91 \ '{',
92 \ '',
93 \ ' x a',
94 \ ' b',
95 \ ' c',
96 \ '',
97 \ '}'])
98 exe "normal /^{/+3\n0"
99 exe "normal i \<Esc>A_"
100 let lnum = line('.')
101 call assert_equal([
102 \ '',
103 \ ' x a',
104 \ ' b_',
105 \ ' c',
106 \ ''], getline(lnum - 2, lnum + 2))
107
108 normal ggdG
109 setl tw=5 fo=qn comments=:#
110 call append('$', [
111 \ '{',
112 \ '# 1 a b',
113 \ '}'])
114 exe "normal /^{/+1\n5|"
115 normal gwap
116 call assert_equal(5, col('.'))
117 let lnum = line('.')
118 call assert_equal([
119 \ '# 1 a',
120 \ '# b'], getline(lnum, lnum + 1))
121
122 normal ggdG
123 setl tw=5 fo=q2 comments=:#
124 call append('$', [
125 \ '{',
126 \ '# x',
127 \ '# a b',
128 \ '}'])
129 exe "normal /^{/+1\n0"
130 normal gwap
131 let lnum = line('.')
132 call assert_equal([
133 \ '# x a',
134 \ '# b'], getline(lnum, lnum + 1))
135
136 normal ggdG
137 setl tw& fo=a
138 call append('$', [
139 \ '{',
140 \ ' 1aa',
141 \ ' 2bb',
142 \ '}'])
143 exe "normal /^{/+2\n0"
144 normal I^^
145 call assert_equal('{ 1aa ^^2bb }', getline('.'))
146
147 normal ggdG
148 setl tw=20 fo=an12wcq comments=s1:/*,mb:*,ex:*/
149 call append('$', [
150 \ '/* abc def ghi jkl ',
151 \ ' * mno pqr stu',
152 \ ' */'])
153 exe "normal /mno pqr/\n"
154 normal A vwx yz
155 let lnum = line('.')
156 call assert_equal([
157 \ ' * mno pqr stu ',
158 \ ' * vwx yz',
159 \ ' */'], getline(lnum - 1, lnum + 1))
160
161 normal ggdG
162 setl tw=12 fo=tqnc comments=:#
163 call setline('.', '# 1 xxxxx')
164 normal A foobar
165 call assert_equal([
166 \ '# 1 xxxxx',
167 \ '# foobar'], getline(1, 2))
168
Bram Moolenaarc3c31582019-01-11 22:15:05 +0100169 " Test the 'p' flag for 'formatoptions'
170 " First test without the flag: that it will break "Mr. Feynman" at the space
171 normal ggdG
172 setl tw=28 fo=tcq
173 call setline('.', 'Surely you''re joking, Mr. Feynman!')
174 normal gqq
175 call assert_equal([
176 \ 'Surely you''re joking, Mr.',
177 \ 'Feynman!'], getline(1, 2))
178 " Now test with the flag: that it will push the name with the title onto the
179 " next line
180 normal ggdG
181 setl fo+=p
182 call setline('.', 'Surely you''re joking, Mr. Feynman!')
183 normal gqq
184 call assert_equal([
185 \ 'Surely you''re joking,',
186 \ 'Mr. Feynman!'], getline(1, 2))
187 " Ensure that it will still break if two spaces are entered
188 normal ggdG
189 call setline('.', 'Surely you''re joking, Mr. Feynman!')
190 normal gqq
191 call assert_equal([
192 \ 'Surely you''re joking, Mr.',
193 \ 'Feynman!'], getline(1, 2))
194
Bram Moolenaar209d3872017-11-16 21:52:51 +0100195 setl ai& tw& fo& si& comments&
196 enew!
197endfunc
Bram Moolenaarcada7892018-01-31 19:30:24 +0100198
199" Tests for :right, :center and :left on text with embedded TAB.
200func Test_format_align()
201 enew!
202 set tw=65
203
204 " :left alignment
205 call append(0, [
206 \ " test for :left",
207 \ " a a",
208 \ " fa a",
209 \ " dfa a",
210 \ " sdfa a",
211 \ " asdfa a",
212 \ " xasdfa a",
213 \ "asxxdfa a",
214 \ ])
215 %left
216 call assert_equal([
217 \ "test for :left",
218 \ "a a",
219 \ "fa a",
220 \ "dfa a",
221 \ "sdfa a",
222 \ "asdfa a",
223 \ "xasdfa a",
224 \ "asxxdfa a",
225 \ ""
226 \ ], getline(1, '$'))
227 enew!
228
229 " :center alignment
230 call append(0, [
231 \ " test for :center",
232 \ " a a",
233 \ " fa afd asdf",
234 \ " dfa a",
235 \ " sdfa afd asdf",
236 \ " asdfa a",
237 \ " xasdfa asdfasdfasdfasdfasdf",
238 \ "asxxdfa a"
239 \ ])
240 %center
241 call assert_equal([
242 \ " test for :center",
243 \ " a a",
244 \ " fa afd asdf",
245 \ " dfa a",
246 \ " sdfa afd asdf",
247 \ " asdfa a",
248 \ " xasdfa asdfasdfasdfasdfasdf",
249 \ " asxxdfa a",
250 \ ""
251 \ ], getline(1, '$'))
252 enew!
253
254 " :right alignment
255 call append(0, [
256 \ " test for :right",
257 \ " a a",
258 \ " fa a",
259 \ " dfa a",
260 \ " sdfa a",
261 \ " asdfa a",
262 \ " xasdfa a",
263 \ " asxxdfa a",
264 \ " asxa;ofa a",
265 \ " asdfaqwer a",
266 \ " a ax",
267 \ " fa ax",
268 \ " dfa ax",
269 \ " sdfa ax",
270 \ " asdfa ax",
271 \ " xasdfa ax",
272 \ " asxxdfa ax",
273 \ " asxa;ofa ax",
274 \ " asdfaqwer ax",
275 \ " a axx",
276 \ " fa axx",
277 \ " dfa axx",
278 \ " sdfa axx",
279 \ " asdfa axx",
280 \ " xasdfa axx",
281 \ " asxxdfa axx",
282 \ " asxa;ofa axx",
283 \ " asdfaqwer axx",
284 \ " a axxx",
285 \ " fa axxx",
286 \ " dfa axxx",
287 \ " sdfa axxx",
288 \ " asdfa axxx",
289 \ " xasdfa axxx",
290 \ " asxxdfa axxx",
291 \ " asxa;ofa axxx",
292 \ " asdfaqwer axxx",
293 \ " a axxxo",
294 \ " fa axxxo",
295 \ " dfa axxxo",
296 \ " sdfa axxxo",
297 \ " asdfa axxxo",
298 \ " xasdfa axxxo",
299 \ " asxxdfa axxxo",
300 \ " asxa;ofa axxxo",
301 \ " asdfaqwer axxxo",
302 \ " a axxxoi",
303 \ " fa axxxoi",
304 \ " dfa axxxoi",
305 \ " sdfa axxxoi",
306 \ " asdfa axxxoi",
307 \ " xasdfa axxxoi",
308 \ " asxxdfa axxxoi",
309 \ " asxa;ofa axxxoi",
310 \ " asdfaqwer axxxoi",
311 \ " a axxxoik",
312 \ " fa axxxoik",
313 \ " dfa axxxoik",
314 \ " sdfa axxxoik",
315 \ " asdfa axxxoik",
316 \ " xasdfa axxxoik",
317 \ " asxxdfa axxxoik",
318 \ " asxa;ofa axxxoik",
319 \ " asdfaqwer axxxoik",
320 \ " a axxxoike",
321 \ " fa axxxoike",
322 \ " dfa axxxoike",
323 \ " sdfa axxxoike",
324 \ " asdfa axxxoike",
325 \ " xasdfa axxxoike",
326 \ " asxxdfa axxxoike",
327 \ " asxa;ofa axxxoike",
328 \ " asdfaqwer axxxoike",
329 \ " a axxxoikey",
330 \ " fa axxxoikey",
331 \ " dfa axxxoikey",
332 \ " sdfa axxxoikey",
333 \ " asdfa axxxoikey",
334 \ " xasdfa axxxoikey",
335 \ " asxxdfa axxxoikey",
336 \ " asxa;ofa axxxoikey",
337 \ " asdfaqwer axxxoikey",
338 \ ])
339 %right
340 call assert_equal([
341 \ "\t\t\t\t test for :right",
342 \ "\t\t\t\t a a",
343 \ "\t\t\t\t fa a",
344 \ "\t\t\t\t dfa a",
345 \ "\t\t\t\t sdfa a",
346 \ "\t\t\t\t asdfa a",
347 \ "\t\t\t\t xasdfa a",
348 \ "\t\t\t\t asxxdfa a",
349 \ "\t\t\t\t asxa;ofa a",
350 \ "\t\t\t\t asdfaqwer a",
351 \ "\t\t\t\t a ax",
352 \ "\t\t\t\t fa ax",
353 \ "\t\t\t\t dfa ax",
354 \ "\t\t\t\t sdfa ax",
355 \ "\t\t\t\t asdfa ax",
356 \ "\t\t\t\t xasdfa ax",
357 \ "\t\t\t\t asxxdfa ax",
358 \ "\t\t\t\t asxa;ofa ax",
359 \ "\t\t\t\t asdfaqwer ax",
360 \ "\t\t\t\t a axx",
361 \ "\t\t\t\t fa axx",
362 \ "\t\t\t\t dfa axx",
363 \ "\t\t\t\t sdfa axx",
364 \ "\t\t\t\t asdfa axx",
365 \ "\t\t\t\t xasdfa axx",
366 \ "\t\t\t\t asxxdfa axx",
367 \ "\t\t\t\t asxa;ofa axx",
368 \ "\t\t\t\t asdfaqwer axx",
369 \ "\t\t\t\t a axxx",
370 \ "\t\t\t\t fa axxx",
371 \ "\t\t\t\t dfa axxx",
372 \ "\t\t\t\t sdfa axxx",
373 \ "\t\t\t\t asdfa axxx",
374 \ "\t\t\t\t xasdfa axxx",
375 \ "\t\t\t\t asxxdfa axxx",
376 \ "\t\t\t\t asxa;ofa axxx",
377 \ "\t\t\t\t asdfaqwer axxx",
378 \ "\t\t\t\t a axxxo",
379 \ "\t\t\t\t fa axxxo",
380 \ "\t\t\t\t dfa axxxo",
381 \ "\t\t\t\t sdfa axxxo",
382 \ "\t\t\t\t asdfa axxxo",
383 \ "\t\t\t\t xasdfa axxxo",
384 \ "\t\t\t\t asxxdfa axxxo",
385 \ "\t\t\t\t asxa;ofa axxxo",
386 \ "\t\t\t\t asdfaqwer axxxo",
387 \ "\t\t\t\t a axxxoi",
388 \ "\t\t\t\t fa axxxoi",
389 \ "\t\t\t\t dfa axxxoi",
390 \ "\t\t\t\t sdfa axxxoi",
391 \ "\t\t\t\t asdfa axxxoi",
392 \ "\t\t\t\t xasdfa axxxoi",
393 \ "\t\t\t\t asxxdfa axxxoi",
394 \ "\t\t\t\t asxa;ofa axxxoi",
395 \ "\t\t\t\t asdfaqwer axxxoi",
396 \ "\t\t\t\t a axxxoik",
397 \ "\t\t\t\t fa axxxoik",
398 \ "\t\t\t\t dfa axxxoik",
399 \ "\t\t\t\t sdfa axxxoik",
400 \ "\t\t\t\t asdfa axxxoik",
401 \ "\t\t\t\t xasdfa axxxoik",
402 \ "\t\t\t\t asxxdfa axxxoik",
403 \ "\t\t\t\t asxa;ofa axxxoik",
404 \ "\t\t\t\t asdfaqwer axxxoik",
405 \ "\t\t\t\t a axxxoike",
406 \ "\t\t\t\t fa axxxoike",
407 \ "\t\t\t\t dfa axxxoike",
408 \ "\t\t\t\t sdfa axxxoike",
409 \ "\t\t\t\t asdfa axxxoike",
410 \ "\t\t\t\t xasdfa axxxoike",
411 \ "\t\t\t\t asxxdfa axxxoike",
412 \ "\t\t\t\t asxa;ofa axxxoike",
413 \ "\t\t\t\t asdfaqwer axxxoike",
414 \ "\t\t\t\t a axxxoikey",
415 \ "\t\t\t\t fa axxxoikey",
416 \ "\t\t\t\t dfa axxxoikey",
417 \ "\t\t\t\t sdfa axxxoikey",
418 \ "\t\t\t\t asdfa axxxoikey",
419 \ "\t\t\t\t xasdfa axxxoikey",
420 \ "\t\t\t\t asxxdfa axxxoikey",
421 \ "\t\t\t\t asxa;ofa axxxoikey",
422 \ "\t\t\t\t asdfaqwer axxxoikey",
423 \ ""
424 \ ], getline(1, '$'))
425 enew!
426
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100427 " align text with 'wrapmargin'
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100428 50vnew
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100429 call setline(1, ['Vim'])
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100430 setl textwidth=0
431 setl wrapmargin=30
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100432 right
433 call assert_equal("\t\t Vim", getline(1))
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100434 q!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100435
Bram Moolenaarea3db912020-02-02 15:32:13 +0100436 " align text with 'rightleft'
437 if has('rightleft')
438 new
439 call setline(1, 'Vim')
440 setlocal rightleft
441 left 20
442 setlocal norightleft
443 call assert_equal("\t\t Vim", getline(1))
444 setlocal rightleft
445 right
446 setlocal norightleft
447 call assert_equal("Vim", getline(1))
448 close!
449 endif
450
Bram Moolenaarcada7892018-01-31 19:30:24 +0100451 set tw&
452endfunc
453
454" Test formatting a paragraph.
455func Test_format_para()
456 enew!
457 set fo+=tcroql tw=72
458
459 call append(0, [
460 \ "xxxxx xx xxxxxx ",
461 \ "xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
462 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx",
463 \ "xx xxxxxxx. xxxx xxxx.",
464 \ "",
465 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx",
466 \ "> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx"
467 \ ])
468 exe "normal /xxxxxxxx$\<CR>"
469 normal 0gq6kk
470 call assert_equal([
471 \ "xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
472 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx.",
473 \ "xxxx xxxx.",
474 \ "",
475 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx",
476 \ "> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx",
477 \ ""
478 \ ], getline(1, '$'))
479
480 set fo& tw&
481 enew!
482endfunc
483
484" Test undo after ":%s" and formatting.
485func Test_format_undo()
486 enew!
487 map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq
488
489 call append(0, [
490 \ "aa aa aa aa",
491 \ "bb bb bb bb",
492 \ "cc cc cc cc"
493 \ ])
494 " undo/redo here to make the next undo only work on the following changes
495 exe "normal i\<C-G>u"
496 call cursor(1,1)
497 normal ggu
498 call assert_equal([
499 \ "aa aa aa aa",
500 \ "bb bb bb bb",
501 \ "cc cc cc cc",
502 \ ""
503 \ ], getline(1, '$'))
504
505 unmap gg
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100506 set tw&
Bram Moolenaarcada7892018-01-31 19:30:24 +0100507 enew!
508endfunc
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100509
510func Test_format_list_auto()
511 new
512 call setline(1, ['1. abc', '2. def', '3. ghi'])
513 set fo=tan ai bs=2
514 call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
515 call assert_equal('2. defx ghi', getline(2))
516 bwipe!
517 set fo& ai& bs&
518endfunc
Bram Moolenaar396b7c72019-10-21 23:08:59 +0200519
520func Test_crash_github_issue_5095()
521 CheckFeature autocmd
522
523 " This used to segfault, see https://github.com/vim/vim/issues/5095
524 augroup testing
525 au BufNew x center
526 augroup END
527
528 next! x
529
530 bw
531 augroup testing
532 au!
533 augroup END
534 augroup! testing
535endfunc
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100536
537" Test for formatting multi-byte text with 'fo=t'
538func Test_tw_2_fo_t()
539 new
540 let t =<< trim END
541 {
542 XYZ
543 abc XYZ
544 }
545 END
546 call setline(1, t)
547 call cursor(2, 1)
548
549 set tw=2 fo=t
550 let t =<< trim END
551 XYZ
552 abc XYZ
553 END
554 exe "normal gqgqjgqgq"
555 exe "normal o\n" . join(t, "\n")
556
557 let expected =<< trim END
558 {
559 XYZ
560 abc
561 XYZ
562
563 XYZ
564 abc
565 XYZ
566 }
567 END
568 call assert_equal(expected, getline(1, '$'))
569
570 set tw& fo&
571 bwipe!
572endfunc
573
574" Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
575func Test_tw_1_fo_tm()
576 new
577 let t =<< trim END
578 {
579
580 Xa
581 X a
582 XY
583 X Y
584 }
585 END
586 call setline(1, t)
587 call cursor(2, 1)
588
589 set tw=1 fo=tm
590 let t =<< trim END
591
592 Xa
593 X a
594 XY
595 X Y
596 END
597 exe "normal gqgqjgqgqjgqgqjgqgqjgqgq"
598 exe "normal o\n" . join(t, "\n")
599
600 let expected =<< trim END
601 {
602
603
604 a
605
606 a
607
608
609
610
611
612
613
614 a
615
616 a
617
618
619
620
621 }
622 END
623 call assert_equal(expected, getline(1, '$'))
624
625 set tw& fo&
626 bwipe!
627endfunc
628
629" Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
630func Test_tw_2_fo_tm()
631 new
632 let t =<< trim END
633 {
634
635 a
636 a
637 XY
638
639 a
640 ab
641 abc
642 ab c
643 abXY
644 }
645 END
646 call setline(1, t)
647 call cursor(2, 1)
648
649 set tw=2 fo=tm
650 let t =<< trim END
651
652 a
653 a
654 XY
655
656 a
657 ab
658 abc
659 ab c
660 abXY
661 END
662 exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
663 exe "normal o\n" . join(t, "\n")
664
665 let expected =<< trim END
666 {
667
668
669 a
670
671 a
672
673
674
675
676 a
677
678 ab
679
680 abc
681
682 ab
683
684 c
685 ab
686
687
688
689
690
691 a
692
693 a
694
695
696
697
698 a
699
700 ab
701
702 abc
703
704 ab
705
706 c
707 ab
708
709
710 }
711 END
712 call assert_equal(expected, getline(1, '$'))
713
714 set tw& fo&
715 bwipe!
716endfunc
717
718" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
719func Test_tw_2_fo_tm_ai()
720 new
721 let t =<< trim END
722 {
723
724 Xa
725 }
726 END
727 call setline(1, t)
728 call cursor(2, 1)
729
730 set ai tw=2 fo=tm
731 let t =<< trim END
732
733 Xa
734 END
735 exe "normal gqgqjgqgq"
736 exe "normal o\n" . join(t, "\n")
737
738 let expected =<< trim END
739 {
740
741
742 a
743
744
745
746 a
747 }
748 END
749 call assert_equal(expected, getline(1, '$'))
750
751 set tw& fo& ai&
752 bwipe!
753endfunc
754
755" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
756func Test_tw_2_fo_tm_noai()
757 new
758 let t =<< trim END
759 {
760
761 a
762 }
763 END
764 call setline(1, t)
765 call cursor(2, 1)
766
767 set noai tw=2 fo=tm
768 exe "normal gqgqjgqgqo\n X\n Xa"
769
770 let expected =<< trim END
771 {
772
773
774 a
775
776
777
778 a
779 }
780 END
781 call assert_equal(expected, getline(1, '$'))
782
783 set tw& fo& ai&
784 bwipe!
785endfunc
786
787func Test_tw_2_fo_cqm_com()
788 new
789 let t =<< trim END
790 {
791
792 a
793 a
794 XY
795 XYZ
796
797 YZ
798 XX
799 XXa
800 XXY
801 }
802 END
803 call setline(1, t)
804 call cursor(2, 1)
805
806 set tw=2 fo=cqm comments=n:X
807 exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
808 let t =<< trim END
809
810 a
811 a
812 XY
813 XYZ
814
815 YZ
816 XX
817 XXa
818 XXY
819 END
820 exe "normal o\n" . join(t, "\n")
821
822 let expected =<< trim END
823 {
824
825 a
826 a
827 XY
828 XY
829 XY
830 XZ
831
832
833
834 XX
835 XXa
836 XXY
837
838
839 a
840 a
841 XY
842 XY
843 XY
844 XZ
845
846
847
848 XX
849 XXa
850 XXY
851 }
852 END
853 call assert_equal(expected, getline(1, '$'))
854
855 set tw& fo& comments&
856 bwipe!
857endfunc
858
859func Test_tw_2_fo_tm_replace()
860 new
861 let t =<< trim END
862 {
863
864 }
865 END
866 call setline(1, t)
867 call cursor(2, 1)
868
869 set tw=2 fo=tm
870 exe "normal RXa"
871
872 let expected =<< trim END
873 {
874
875 a
876 }
877 END
878 call assert_equal(expected, getline(1, '$'))
879
880 set tw& fo&
881 bwipe!
882endfunc
883
884" Test for 'matchpairs' with multibyte chars
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200885func Test_mps_multibyte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100886 new
887 let t =<< trim END
888 {
889 ‘ two three ’ four
890 }
891 END
892 call setline(1, t)
893 call cursor(2, 1)
894
895 exe "set mps+=\u2018:\u2019"
896 normal d%
897
898 let expected =<< trim END
899 {
900 four
901 }
902 END
903 call assert_equal(expected, getline(1, '$'))
904
905 set mps&
906 bwipe!
907endfunc
908
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200909" Test for 'matchpairs' in latin1 encoding
910func Test_mps_latin1()
911 new
912 let save_enc = &encoding
913 set encoding=latin1
914 call setline(1, 'abc(def)ghi')
915 normal %
916 call assert_equal(8, col('.'))
917 normal %
918 call assert_equal(4, col('.'))
919 call cursor(1, 6)
920 normal [(
921 call assert_equal(4, col('.'))
922 normal %
923 call assert_equal(8, col('.'))
924 call cursor(1, 6)
925 normal ])
926 call assert_equal(8, col('.'))
927 normal %
928 call assert_equal(4, col('.'))
929 let &encoding = save_enc
930 close!
931endfunc
932
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100933" Test for ra on multi-byte characters
934func Test_ra_multibyte()
935 new
936 let t =<< trim END
937 ra test
938 abba
939 aab
940 END
941 call setline(1, t)
942 call cursor(1, 1)
943
944 normal jVjra
945
946 let expected =<< trim END
947 ra test
948 aaaa
949 aaa
950 END
951 call assert_equal(expected, getline(1, '$'))
952
953 bwipe!
954endfunc
955
956" Test for 'whichwrap' with multi-byte character
Bram Moolenaara48e78e2019-12-17 20:29:26 +0100957func Test_whichwrap_multi_byte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100958 new
959 let t =<< trim END
960 á
961 x
962 END
963 call setline(1, t)
964 call cursor(2, 1)
965
966 set whichwrap+=h
967 normal dh
Bram Moolenaara48e78e2019-12-17 20:29:26 +0100968 set whichwrap&
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100969
970 let expected =<< trim END
971 áx
972 END
973 call assert_equal(expected, getline(1, '$'))
974
975 bwipe!
976endfunc
977
Bram Moolenaarf9ab52e2020-05-05 19:57:18 +0200978" Test for automatically adding comment leaders in insert mode
979func Test_threepiece_comment()
980 new
981 setlocal expandtab
982 call setline(1, ["\t/*"])
983 setlocal formatoptions=croql
984 call cursor(1, 3)
985 call feedkeys("A\<cr>\<cr>/", 'tnix')
986 call assert_equal(["\t/*", " *", " */"], getline(1, '$'))
987
988 " If a comment ends in a single line, then don't add it in the next line
989 %d
990 call setline(1, '/* line1 */')
991 call feedkeys("A\<CR>next line", 'xt')
992 call assert_equal(['/* line1 */', 'next line'], getline(1, '$'))
993
994 %d
995 " Copy the trailing indentation from the leader comment to a new line
996 setlocal autoindent noexpandtab
997 call feedkeys("a\t/*\tone\ntwo\n/", 'xt')
998 call assert_equal(["\t/*\tone", "\t *\ttwo", "\t */"], getline(1, '$'))
999 close!
1000endfunc
1001
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001002" Test for the 'f' flag in 'comments' (only the first line has the comment
1003" string)
1004func Test_firstline_comment()
1005 new
1006 setlocal comments=f:- fo+=ro
1007 exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>"
1008 call assert_equal(['A', '- B', ' C', ' D'], getline(1, '$'))
1009 %d
1010 setlocal comments=:-
1011 exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>"
1012 call assert_equal(['- A', '- B', '- C', '- D'], getline(1, '$'))
1013 %bw!
1014endfunc
1015
1016" Test for the 'r' flag in 'comments' (right align comment)
1017func Test_comment_rightalign()
1018 new
1019 setlocal comments=sr:/***,m:**,ex-2:******/ fo+=ro
1020 exe "normal i=\<C-C>o\t /***\nD\n/"
1021 exe "normal 2GOA\<C-C>joB\<C-C>jOC\<C-C>joE\<C-C>GOF\<C-C>joG"
1022 let expected =<< trim END
1023 =
1024 A
1025 /***
1026 ** B
1027 ** C
1028 ** D
1029 ** E
1030 ** F
1031 ******/
1032 G
1033 END
1034 call assert_equal(expected, getline(1, '$'))
1035 %bw!
1036endfunc
1037
1038" Test for the 'b' flag in 'comments'
1039func Test_comment_blank()
1040 new
1041 setlocal comments=b:* fo+=ro
1042 exe "normal i* E\nF\n\<BS>G\nH\<C-C>ggOC\<C-C>O\<BS>B\<C-C>OA\<C-C>2joD"
1043 let expected =<< trim END
1044 A
1045 *B
1046 * C
1047 * D
1048 * E
1049 * F
1050 *G
1051 H
1052 END
1053 call assert_equal(expected, getline(1, '$'))
1054 %bw!
1055endfunc
1056
1057" Test for the 'n' flag in comments
1058func Test_comment_nested()
1059 new
1060 setlocal comments=n:> fo+=ro
1061 exe "normal i> B\nD\<C-C>ggOA\<C-C>joC\<C-C>Go\<BS>>>> F\nH"
1062 exe "normal 5GOE\<C-C>6GoG"
1063 let expected =<< trim END
1064 > A
1065 > B
1066 > C
1067 > D
1068 >>>> E
1069 >>>> F
1070 >>>> G
1071 >>>> H
1072 END
1073 call assert_equal(expected, getline(1, '$'))
1074 %bw!
1075endfunc
1076
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001077" Test for a space character in 'comments' setting
1078func Test_comment_space()
1079 new
1080 setlocal comments=b:\ > fo+=ro
1081 exe "normal i> B\nD\<C-C>ggOA\<C-C>joC"
1082 exe "normal Go > F\nH\<C-C>kOE\<C-C>joG"
1083 let expected =<< trim END
1084 A
1085 > B
1086 C
1087 D
1088 > E
1089 > F
1090 > G
1091 > H
1092 END
1093 call assert_equal(expected, getline(1, '$'))
1094 %bw!
1095endfunc
1096
1097" Test for the 'O' flag in 'comments'
1098func Test_comment_O()
1099 new
1100 setlocal comments=Ob:* fo+=ro
1101 exe "normal i* B\nD\<C-C>kOA\<C-C>joC"
1102 let expected =<< trim END
1103 A
1104 * B
1105 * C
1106 * D
1107 END
1108 call assert_equal(expected, getline(1, '$'))
1109 %bw!
1110endfunc
1111
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001112" Test for 'a' and 'w' flags in 'formatoptions'
1113func Test_fo_a_w()
1114 new
1115 setlocal fo+=aw tw=10
1116 call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
1117 call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001118
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001119 " when a line ends with space, it is not broken up.
1120 %d
1121 call feedkeys("ione two to ", 'xt')
1122 call assert_equal('one two to ', getline(1))
1123
1124 " when a line ends with spaces and backspace is used in the next line, the
1125 " last space in the previous line should be removed.
1126 %d
1127 set backspace=indent,eol,start
1128 call setline(1, ['one ', 'two'])
1129 exe "normal 2Gi\<BS>"
1130 call assert_equal(['one two'], getline(1, '$'))
1131 set backspace&
1132
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001133 " Test for 'a', 'w' and '1' options.
1134 setlocal textwidth=0
1135 setlocal fo=1aw
1136 %d
1137 call setline(1, '. foo')
1138 normal 72ig
1139 call feedkeys('a uu uu uu', 'xt')
1140 call assert_equal('g uu uu ', getline(1)[-8:])
1141 call assert_equal(['uu. foo'], getline(2, '$'))
1142
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001143 %bw!
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001144endfunc
1145
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001146" Test for 'j' flag in 'formatoptions'
1147func Test_fo_j()
1148 new
1149 setlocal fo+=j comments=://
1150 call setline(1, ['i++; // comment1', ' // comment2'])
1151 normal J
1152 call assert_equal('i++; // comment1 comment2', getline(1))
1153 setlocal fo-=j
1154 call setline(1, ['i++; // comment1', ' // comment2'])
1155 normal J
1156 call assert_equal('i++; // comment1 // comment2', getline(1))
1157 " Test with nested comments
1158 setlocal fo+=j comments=n:>,n:)
1159 call setline(1, ['i++; > ) > ) comment1', ' > ) comment2'])
1160 normal J
1161 call assert_equal('i++; > ) > ) comment1 comment2', getline(1))
1162 %bw!
1163endfunc
1164
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001165" Test for formatting lines using gq in visual mode
1166func Test_visual_gq_format()
1167 new
1168 call setline(1, ['one two three four', 'five six', 'one two'])
1169 setl textwidth=10
1170 call feedkeys('ggv$jj', 'xt')
1171 redraw!
1172 normal gq
1173 %d
1174 call setline(1, ['one two three four', 'five six', 'one two'])
1175 normal G$
1176 call feedkeys('v0kk', 'xt')
1177 redraw!
1178 normal gq
1179 setl textwidth&
1180 close!
1181endfunc
1182
1183" Test for 'n' flag in 'formatoptions' to format numbered lists
1184func Test_fo_n()
1185 new
1186 setlocal autoindent
1187 setlocal textwidth=12
1188 setlocal fo=n
1189 call setline(1, [' 1) one two three four', ' 2) two'])
1190 normal gggqG
1191 call assert_equal([' 1) one two', ' three', ' four', ' 2) two'],
1192 \ getline(1, '$'))
1193 close!
1194endfunc
1195
1196" Test for 'formatlistpat' option
1197func Test_formatlistpat()
1198 new
1199 setlocal autoindent
1200 setlocal textwidth=10
1201 setlocal fo=n
1202 setlocal formatlistpat=^\\s*-\\s*
1203 call setline(1, [' - one two three', ' - two'])
1204 normal gggqG
1205 call assert_equal([' - one', ' two', ' three', ' - two'],
1206 \ getline(1, '$'))
1207 close!
1208endfunc
1209
1210" Test for the 'b' and 'v' flags in 'formatoptions'
1211" Text should wrap only if a space character is inserted at or before
1212" 'textwidth'
1213func Test_fo_b()
1214 new
1215 setlocal textwidth=20
1216
1217 setlocal formatoptions=t
1218 call setline(1, 'one two three four')
1219 call feedkeys('Amore', 'xt')
1220 call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
1221
1222 setlocal formatoptions=bt
1223 %d
1224 call setline(1, 'one two three four')
1225 call feedkeys('Amore five', 'xt')
1226 call assert_equal(['one two three fourmore five'], getline(1, '$'))
1227
1228 setlocal formatoptions=bt
1229 %d
1230 call setline(1, 'one two three four')
1231 call feedkeys('A five', 'xt')
1232 call assert_equal(['one two three four', 'five'], getline(1, '$'))
1233
1234 setlocal formatoptions=vt
1235 %d
1236 call setline(1, 'one two three four')
1237 call feedkeys('Amore five', 'xt')
1238 call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
1239
1240 close!
1241endfunc
1242
1243" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
1244" word.
1245func Test_fo_1()
1246 new
1247 setlocal textwidth=20
1248
1249 setlocal formatoptions=t
1250 call setline(1, 'one two three four')
1251 call feedkeys('A a bird', 'xt')
1252 call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
1253
1254 %d
1255 setlocal formatoptions=t1
1256 call setline(1, 'one two three four')
1257 call feedkeys('A a bird', 'xt')
1258 call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
1259
1260 close!
1261endfunc
1262
1263" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
1264" is longer than 'textwidth', then it is not broken.
1265func Test_fo_l()
1266 new
1267 setlocal textwidth=20
1268
1269 setlocal formatoptions=t
1270 call setline(1, 'one two three four five')
1271 call feedkeys('A six', 'xt')
1272 call assert_equal(['one two three four', 'five six'], getline(1, '$'))
1273
1274 %d
1275 setlocal formatoptions=tl
1276 call setline(1, 'one two three four five')
1277 call feedkeys('A six', 'xt')
1278 call assert_equal(['one two three four five six'], getline(1, '$'))
1279
1280 close!
1281endfunc
1282
1283" Test for the '2' flag in 'formatoptions'
1284func Test_fo_2()
1285 new
1286 setlocal autoindent
1287 setlocal formatoptions=t2
1288 setlocal textwidth=30
1289 call setline(1, ["\tfirst line of a paragraph.",
1290 \ "second line of the same paragraph.",
1291 \ "third line."])
1292 normal gggqG
1293 call assert_equal(["\tfirst line of a",
1294 \ "paragraph. second line of the",
1295 \ "same paragraph. third line."], getline(1, '$'))
1296 close!
1297endfunc
1298
1299" Test for formatting lines where only the first line has a comment.
1300func Test_fo_gq_with_firstline_comment()
1301 new
1302 setlocal formatoptions=tcq
1303 call setline(1, ['- one two', 'three'])
1304 normal gggqG
1305 call assert_equal(['- one two three'], getline(1, '$'))
1306
1307 %d
1308 call setline(1, ['- one', '- two'])
1309 normal gggqG
1310 call assert_equal(['- one', '- two'], getline(1, '$'))
1311 close!
1312endfunc
1313
1314" Test for trying to join a comment line with a non-comment line
1315func Test_join_comments()
1316 new
1317 call setline(1, ['one', '/* two */', 'three'])
1318 normal gggqG
1319 call assert_equal(['one', '/* two */', 'three'], getline(1, '$'))
1320 close!
1321endfunc
1322
1323" Test for using 'a' in 'formatoptions' with comments
1324func Test_autoformat_comments()
1325 new
1326 setlocal formatoptions+=a
1327 call feedkeys("a- one\n- two\n", 'xt')
1328 call assert_equal(['- one', '- two', ''], getline(1, '$'))
1329
1330 %d
1331 call feedkeys("a\none\n", 'xt')
1332 call assert_equal(['', 'one', ''], getline(1, '$'))
1333
1334 setlocal formatoptions+=aw
1335 %d
1336 call feedkeys("aone \ntwo\n", 'xt')
1337 call assert_equal(['one two', ''], getline(1, '$'))
1338
1339 %d
1340 call feedkeys("aone\ntwo\n", 'xt')
1341 call assert_equal(['one', 'two', ''], getline(1, '$'))
1342
1343 close!
1344endfunc
1345
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001346" vim: shiftwidth=2 sts=2 expandtab