blob: f4dedfacb1414ebe4226dd3db7e5fe3a3fef9a2d [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
Bram Moolenaar209d3872017-11-16 21:52:51 +01003func Test_text_format()
4 enew!
5
6 setl noai tw=2 fo=t
7 call append('$', [
8 \ '{',
9 \ ' ',
10 \ '',
11 \ '}'])
12 exe "normal /^{/+1\n0"
13 normal gRa b
14 let lnum = line('.')
15 call assert_equal([
16 \ 'a',
17 \ 'b'], getline(lnum - 1, lnum))
18
19 normal ggdG
20 setl ai tw=2 fo=tw
21 call append('$', [
22 \ '{',
23 \ 'a b ',
24 \ '',
25 \ 'a ',
26 \ '}'])
27 exe "normal /^{/+1\n0"
28 normal gqgqjjllab
29 let lnum = line('.')
30 call assert_equal([
31 \ 'a ',
32 \ 'b ',
33 \ '',
34 \ 'a ',
35 \ 'b'], getline(lnum - 4, lnum))
36
37 normal ggdG
38 setl tw=3 fo=t
39 call append('$', [
40 \ '{',
41 \ "a \<C-A>",
42 \ '}'])
43 exe "normal /^{/+1\n0"
44 exe "normal gqgqo\na \<C-V>\<C-A>"
45 let lnum = line('.')
46 call assert_equal([
47 \ 'a',
48 \ "\<C-A>",
49 \ '',
50 \ 'a',
51 \ "\<C-A>"], getline(lnum - 4, lnum))
52
53 normal ggdG
54 setl tw=2 fo=tcq1 comments=:#
55 call append('$', [
56 \ '{',
57 \ 'a b',
58 \ '#a b',
59 \ '}'])
60 exe "normal /^{/+1\n0"
61 exe "normal gqgqjgqgqo\na b\n#a b"
62 let lnum = line('.')
63 call assert_equal([
64 \ 'a b',
65 \ '#a b',
66 \ '',
67 \ 'a b',
68 \ '#a b'], getline(lnum - 4, lnum))
69
70 normal ggdG
71 setl tw=5 fo=tcn comments=:#
72 call append('$', [
73 \ '{',
74 \ ' 1 a',
75 \ '# 1 a',
76 \ '}'])
77 exe "normal /^{/+1\n0"
78 exe "normal A b\<Esc>jA b"
79 let lnum = line('.')
80 call assert_equal([
81 \ ' 1 a',
82 \ ' b',
83 \ '# 1 a',
84 \ '# b'], getline(lnum - 3, lnum))
85
86 normal ggdG
87 setl tw=5 fo=t2a si
88 call append('$', [
89 \ '{',
90 \ '',
91 \ ' x a',
92 \ ' b',
93 \ ' c',
94 \ '',
95 \ '}'])
96 exe "normal /^{/+3\n0"
97 exe "normal i \<Esc>A_"
98 let lnum = line('.')
99 call assert_equal([
100 \ '',
101 \ ' x a',
102 \ ' b_',
103 \ ' c',
104 \ ''], getline(lnum - 2, lnum + 2))
105
106 normal ggdG
107 setl tw=5 fo=qn comments=:#
108 call append('$', [
109 \ '{',
110 \ '# 1 a b',
111 \ '}'])
112 exe "normal /^{/+1\n5|"
113 normal gwap
114 call assert_equal(5, col('.'))
115 let lnum = line('.')
116 call assert_equal([
117 \ '# 1 a',
118 \ '# b'], getline(lnum, lnum + 1))
119
120 normal ggdG
121 setl tw=5 fo=q2 comments=:#
122 call append('$', [
123 \ '{',
124 \ '# x',
125 \ '# a b',
126 \ '}'])
127 exe "normal /^{/+1\n0"
128 normal gwap
129 let lnum = line('.')
130 call assert_equal([
131 \ '# x a',
132 \ '# b'], getline(lnum, lnum + 1))
133
134 normal ggdG
135 setl tw& fo=a
136 call append('$', [
137 \ '{',
138 \ ' 1aa',
139 \ ' 2bb',
140 \ '}'])
141 exe "normal /^{/+2\n0"
142 normal I^^
143 call assert_equal('{ 1aa ^^2bb }', getline('.'))
144
145 normal ggdG
146 setl tw=20 fo=an12wcq comments=s1:/*,mb:*,ex:*/
147 call append('$', [
148 \ '/* abc def ghi jkl ',
149 \ ' * mno pqr stu',
150 \ ' */'])
151 exe "normal /mno pqr/\n"
152 normal A vwx yz
153 let lnum = line('.')
154 call assert_equal([
155 \ ' * mno pqr stu ',
156 \ ' * vwx yz',
157 \ ' */'], getline(lnum - 1, lnum + 1))
158
159 normal ggdG
160 setl tw=12 fo=tqnc comments=:#
161 call setline('.', '# 1 xxxxx')
162 normal A foobar
163 call assert_equal([
164 \ '# 1 xxxxx',
165 \ '# foobar'], getline(1, 2))
166
Bram Moolenaarc3c31582019-01-11 22:15:05 +0100167 " Test the 'p' flag for 'formatoptions'
168 " First test without the flag: that it will break "Mr. Feynman" at the space
169 normal ggdG
170 setl tw=28 fo=tcq
171 call setline('.', 'Surely you''re joking, Mr. Feynman!')
172 normal gqq
173 call assert_equal([
174 \ 'Surely you''re joking, Mr.',
175 \ 'Feynman!'], getline(1, 2))
176 " Now test with the flag: that it will push the name with the title onto the
177 " next line
178 normal ggdG
179 setl fo+=p
180 call setline('.', 'Surely you''re joking, Mr. Feynman!')
181 normal gqq
182 call assert_equal([
183 \ 'Surely you''re joking,',
184 \ 'Mr. Feynman!'], getline(1, 2))
185 " Ensure that it will still break if two spaces are entered
186 normal ggdG
187 call setline('.', 'Surely you''re joking, Mr. Feynman!')
188 normal gqq
189 call assert_equal([
190 \ 'Surely you''re joking, Mr.',
191 \ 'Feynman!'], getline(1, 2))
192
Bram Moolenaar209d3872017-11-16 21:52:51 +0100193 setl ai& tw& fo& si& comments&
194 enew!
195endfunc
Bram Moolenaarcada7892018-01-31 19:30:24 +0100196
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000197func Test_format_c_comment()
198 new
199 setl ai cindent tw=40 et fo=croql
200 let text =<< trim END
201 var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
202 END
203 call setline(1, text)
204 normal gql
205 let expected =<< trim END
206 var = 2345; // asdf asdf asdf asdf asdf
207 // asdf asdf asdf asdf asdf
208 END
209 call assert_equal(expected, getline(1, '$'))
210
211 %del
212 let text =<< trim END
213 var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
214 END
215 call setline(1, text)
216 normal gql
217 let expected =<< trim END
218 var = 2345; // asdf asdf asdf asdf asdf
219 // asdf asdf asdf asdf asdf
220 // asdf asdf
221 END
222 call assert_equal(expected, getline(1, '$'))
223
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000224 %del
225 let text =<< trim END
226 #if 0 // This is another long end of
227 // line comment that
228 // wraps.
229 END
230 call setline(1, text)
231 normal gq2j
232 let expected =<< trim END
233 #if 0 // This is another long
234 // end of line comment
235 // that wraps.
236 END
237 call assert_equal(expected, getline(1, '$'))
238
zeertzjq51ab7c72022-02-08 12:58:37 +0000239 " Using either "o" or "O" repeats a line comment occupying a whole line.
240 %del
241 let text =<< trim END
242 nop;
243 // This is a comment
244 val = val;
245 END
246 call setline(1, text)
247 normal 2Go
248 let expected =<< trim END
249 nop;
250 // This is a comment
251 //
252 val = val;
253 END
254 call assert_equal(expected, getline(1, '$'))
255 normal 2GO
256 let expected =<< trim END
257 nop;
258 //
259 // This is a comment
260 //
261 val = val;
262 END
263 call assert_equal(expected, getline(1, '$'))
264
265 " Using "o" repeats a line comment after a statement, "O" does not.
Bram Moolenaar5ea5f372021-12-29 15:15:47 +0000266 %del
267 let text =<< trim END
268 nop;
269 val = val; // This is a comment
270 END
271 call setline(1, text)
272 normal 2Go
273 let expected =<< trim END
274 nop;
275 val = val; // This is a comment
276 //
277 END
278 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar2bf875f2022-05-07 14:54:11 +0100279 3delete
280
281 " No comment repeated with a slash in 'formatoptions'
282 set fo+=/
283 normal 2Gox
284 let expected =<< trim END
285 nop;
286 val = val; // This is a comment
287 x
288 END
289 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar7e667782022-05-23 13:10:48 +0100290
291 " Comment is formatted when it wraps
292 normal 2GA with some more text added
293 let expected =<< trim END
294 nop;
295 val = val; // This is a comment
296 // with some more text
297 // added
298 x
299 END
300 call assert_equal(expected, getline(1, '$'))
301
Bram Moolenaar2bf875f2022-05-07 14:54:11 +0100302 set fo-=/
Bram Moolenaar16556192022-04-11 17:36:37 +0100303
304 " using 'indentexpr' instead of 'cindent' does not repeat a comment
305 setl nocindent indentexpr=2
Bram Moolenaar7e667782022-05-23 13:10:48 +0100306 %del
307 let text =<< trim END
308 nop;
309 val = val; // This is a comment
310 END
311 call setline(1, text)
Bram Moolenaar16556192022-04-11 17:36:37 +0100312 normal 2Gox
313 let expected =<< trim END
314 nop;
315 val = val; // This is a comment
316 x
317 END
318 call assert_equal(expected, getline(1, '$'))
319 setl cindent indentexpr=
320 3delete
321
Bram Moolenaar5ea5f372021-12-29 15:15:47 +0000322 normal 2GO
323 let expected =<< trim END
324 nop;
325
326 val = val; // This is a comment
Bram Moolenaar5ea5f372021-12-29 15:15:47 +0000327 END
328 call assert_equal(expected, getline(1, '$'))
329
Bram Moolenaarba263672021-12-29 18:09:13 +0000330 " Using "o" does not repeat a comment in a string
331 %del
332 let text =<< trim END
333 nop;
334 val = " // This is not a comment";
335 END
336 call setline(1, text)
337 normal 2Gox
338 let expected =<< trim END
339 nop;
340 val = " // This is not a comment";
341 x
342 END
343 call assert_equal(expected, getline(1, '$'))
344
Bram Moolenaar5d20fbf2021-12-29 16:05:31 +0000345 " Using CTRL-U after "o" fixes the indent
346 %del
347 let text =<< trim END
348 {
349 val = val; // This is a comment
350 END
351 call setline(1, text)
352 exe "normal! 2Go\<C-U>x\<Esc>"
353 let expected =<< trim END
354 {
355 val = val; // This is a comment
356 x
357 END
358 call assert_equal(expected, getline(1, '$'))
359
Bram Moolenaar48a8a832022-05-07 15:43:52 +0100360 " typing comment text auto-wraps
361 %del
362 call setline(1, text)
363 exe "normal! 2GA blah more text blah.\<Esc>"
364 let expected =<< trim END
365 {
366 val = val; // This is a comment
367 // blah more text
368 // blah.
369 END
370 call assert_equal(expected, getline(1, '$'))
371
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000372 bwipe!
373endfunc
374
Bram Moolenaarcada7892018-01-31 19:30:24 +0100375" Tests for :right, :center and :left on text with embedded TAB.
376func Test_format_align()
377 enew!
378 set tw=65
379
380 " :left alignment
381 call append(0, [
382 \ " test for :left",
383 \ " a a",
384 \ " fa a",
385 \ " dfa a",
386 \ " sdfa a",
387 \ " asdfa a",
388 \ " xasdfa a",
389 \ "asxxdfa a",
390 \ ])
391 %left
392 call assert_equal([
393 \ "test for :left",
394 \ "a a",
395 \ "fa a",
396 \ "dfa a",
397 \ "sdfa a",
398 \ "asdfa a",
399 \ "xasdfa a",
400 \ "asxxdfa a",
401 \ ""
402 \ ], getline(1, '$'))
403 enew!
404
405 " :center alignment
406 call append(0, [
407 \ " test for :center",
408 \ " a a",
409 \ " fa afd asdf",
410 \ " dfa a",
411 \ " sdfa afd asdf",
412 \ " asdfa a",
413 \ " xasdfa asdfasdfasdfasdfasdf",
414 \ "asxxdfa a"
415 \ ])
416 %center
417 call assert_equal([
418 \ " test for :center",
419 \ " a a",
420 \ " fa afd asdf",
421 \ " dfa a",
422 \ " sdfa afd asdf",
423 \ " asdfa a",
424 \ " xasdfa asdfasdfasdfasdfasdf",
425 \ " asxxdfa a",
426 \ ""
427 \ ], getline(1, '$'))
428 enew!
429
430 " :right alignment
431 call append(0, [
432 \ " test for :right",
433 \ " a a",
434 \ " fa a",
435 \ " dfa a",
436 \ " sdfa a",
437 \ " asdfa a",
438 \ " xasdfa a",
439 \ " asxxdfa a",
440 \ " asxa;ofa a",
441 \ " asdfaqwer a",
442 \ " a ax",
443 \ " fa ax",
444 \ " dfa ax",
445 \ " sdfa ax",
446 \ " asdfa ax",
447 \ " xasdfa ax",
448 \ " asxxdfa ax",
449 \ " asxa;ofa ax",
450 \ " asdfaqwer ax",
451 \ " a axx",
452 \ " fa axx",
453 \ " dfa axx",
454 \ " sdfa axx",
455 \ " asdfa axx",
456 \ " xasdfa axx",
457 \ " asxxdfa axx",
458 \ " asxa;ofa axx",
459 \ " asdfaqwer axx",
460 \ " a axxx",
461 \ " fa axxx",
462 \ " dfa axxx",
463 \ " sdfa axxx",
464 \ " asdfa axxx",
465 \ " xasdfa axxx",
466 \ " asxxdfa axxx",
467 \ " asxa;ofa axxx",
468 \ " asdfaqwer axxx",
469 \ " a axxxo",
470 \ " fa axxxo",
471 \ " dfa axxxo",
472 \ " sdfa axxxo",
473 \ " asdfa axxxo",
474 \ " xasdfa axxxo",
475 \ " asxxdfa axxxo",
476 \ " asxa;ofa axxxo",
477 \ " asdfaqwer axxxo",
478 \ " a axxxoi",
479 \ " fa axxxoi",
480 \ " dfa axxxoi",
481 \ " sdfa axxxoi",
482 \ " asdfa axxxoi",
483 \ " xasdfa axxxoi",
484 \ " asxxdfa axxxoi",
485 \ " asxa;ofa axxxoi",
486 \ " asdfaqwer axxxoi",
487 \ " a axxxoik",
488 \ " fa axxxoik",
489 \ " dfa axxxoik",
490 \ " sdfa axxxoik",
491 \ " asdfa axxxoik",
492 \ " xasdfa axxxoik",
493 \ " asxxdfa axxxoik",
494 \ " asxa;ofa axxxoik",
495 \ " asdfaqwer axxxoik",
496 \ " a axxxoike",
497 \ " fa axxxoike",
498 \ " dfa axxxoike",
499 \ " sdfa axxxoike",
500 \ " asdfa axxxoike",
501 \ " xasdfa axxxoike",
502 \ " asxxdfa axxxoike",
503 \ " asxa;ofa axxxoike",
504 \ " asdfaqwer axxxoike",
505 \ " a axxxoikey",
506 \ " fa axxxoikey",
507 \ " dfa axxxoikey",
508 \ " sdfa axxxoikey",
509 \ " asdfa axxxoikey",
510 \ " xasdfa axxxoikey",
511 \ " asxxdfa axxxoikey",
512 \ " asxa;ofa axxxoikey",
513 \ " asdfaqwer axxxoikey",
514 \ ])
515 %right
516 call assert_equal([
517 \ "\t\t\t\t test for :right",
518 \ "\t\t\t\t a a",
519 \ "\t\t\t\t fa a",
520 \ "\t\t\t\t dfa a",
521 \ "\t\t\t\t sdfa a",
522 \ "\t\t\t\t asdfa a",
523 \ "\t\t\t\t xasdfa a",
524 \ "\t\t\t\t asxxdfa a",
525 \ "\t\t\t\t asxa;ofa a",
526 \ "\t\t\t\t asdfaqwer a",
527 \ "\t\t\t\t a ax",
528 \ "\t\t\t\t fa ax",
529 \ "\t\t\t\t dfa ax",
530 \ "\t\t\t\t sdfa ax",
531 \ "\t\t\t\t asdfa ax",
532 \ "\t\t\t\t xasdfa ax",
533 \ "\t\t\t\t asxxdfa ax",
534 \ "\t\t\t\t asxa;ofa ax",
535 \ "\t\t\t\t asdfaqwer ax",
536 \ "\t\t\t\t a axx",
537 \ "\t\t\t\t fa axx",
538 \ "\t\t\t\t dfa axx",
539 \ "\t\t\t\t sdfa axx",
540 \ "\t\t\t\t asdfa axx",
541 \ "\t\t\t\t xasdfa axx",
542 \ "\t\t\t\t asxxdfa axx",
543 \ "\t\t\t\t asxa;ofa axx",
544 \ "\t\t\t\t asdfaqwer axx",
545 \ "\t\t\t\t a axxx",
546 \ "\t\t\t\t fa axxx",
547 \ "\t\t\t\t dfa axxx",
548 \ "\t\t\t\t sdfa axxx",
549 \ "\t\t\t\t asdfa axxx",
550 \ "\t\t\t\t xasdfa axxx",
551 \ "\t\t\t\t asxxdfa axxx",
552 \ "\t\t\t\t asxa;ofa axxx",
553 \ "\t\t\t\t asdfaqwer axxx",
554 \ "\t\t\t\t a axxxo",
555 \ "\t\t\t\t fa axxxo",
556 \ "\t\t\t\t dfa axxxo",
557 \ "\t\t\t\t sdfa axxxo",
558 \ "\t\t\t\t asdfa axxxo",
559 \ "\t\t\t\t xasdfa axxxo",
560 \ "\t\t\t\t asxxdfa axxxo",
561 \ "\t\t\t\t asxa;ofa axxxo",
562 \ "\t\t\t\t asdfaqwer axxxo",
563 \ "\t\t\t\t a axxxoi",
564 \ "\t\t\t\t fa axxxoi",
565 \ "\t\t\t\t dfa axxxoi",
566 \ "\t\t\t\t sdfa axxxoi",
567 \ "\t\t\t\t asdfa axxxoi",
568 \ "\t\t\t\t xasdfa axxxoi",
569 \ "\t\t\t\t asxxdfa axxxoi",
570 \ "\t\t\t\t asxa;ofa axxxoi",
571 \ "\t\t\t\t asdfaqwer axxxoi",
572 \ "\t\t\t\t a axxxoik",
573 \ "\t\t\t\t fa axxxoik",
574 \ "\t\t\t\t dfa axxxoik",
575 \ "\t\t\t\t sdfa axxxoik",
576 \ "\t\t\t\t asdfa axxxoik",
577 \ "\t\t\t\t xasdfa axxxoik",
578 \ "\t\t\t\t asxxdfa axxxoik",
579 \ "\t\t\t\t asxa;ofa axxxoik",
580 \ "\t\t\t\t asdfaqwer axxxoik",
581 \ "\t\t\t\t a axxxoike",
582 \ "\t\t\t\t fa axxxoike",
583 \ "\t\t\t\t dfa axxxoike",
584 \ "\t\t\t\t sdfa axxxoike",
585 \ "\t\t\t\t asdfa axxxoike",
586 \ "\t\t\t\t xasdfa axxxoike",
587 \ "\t\t\t\t asxxdfa axxxoike",
588 \ "\t\t\t\t asxa;ofa axxxoike",
589 \ "\t\t\t\t asdfaqwer axxxoike",
590 \ "\t\t\t\t a axxxoikey",
591 \ "\t\t\t\t fa axxxoikey",
592 \ "\t\t\t\t dfa axxxoikey",
593 \ "\t\t\t\t sdfa axxxoikey",
594 \ "\t\t\t\t asdfa axxxoikey",
595 \ "\t\t\t\t xasdfa axxxoikey",
596 \ "\t\t\t\t asxxdfa axxxoikey",
597 \ "\t\t\t\t asxa;ofa axxxoikey",
598 \ "\t\t\t\t asdfaqwer axxxoikey",
599 \ ""
600 \ ], getline(1, '$'))
601 enew!
602
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100603 " align text with 'wrapmargin'
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100604 50vnew
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100605 call setline(1, ['Vim'])
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100606 setl textwidth=0
607 setl wrapmargin=30
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100608 right
609 call assert_equal("\t\t Vim", getline(1))
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100610 q!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100611
Bram Moolenaarea3db912020-02-02 15:32:13 +0100612 " align text with 'rightleft'
613 if has('rightleft')
614 new
615 call setline(1, 'Vim')
616 setlocal rightleft
617 left 20
618 setlocal norightleft
619 call assert_equal("\t\t Vim", getline(1))
620 setlocal rightleft
621 right
622 setlocal norightleft
623 call assert_equal("Vim", getline(1))
624 close!
625 endif
626
Bram Moolenaarcada7892018-01-31 19:30:24 +0100627 set tw&
628endfunc
629
630" Test formatting a paragraph.
631func Test_format_para()
632 enew!
633 set fo+=tcroql tw=72
634
635 call append(0, [
636 \ "xxxxx xx xxxxxx ",
637 \ "xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
638 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx",
639 \ "xx xxxxxxx. xxxx xxxx.",
640 \ "",
641 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx",
642 \ "> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx"
643 \ ])
644 exe "normal /xxxxxxxx$\<CR>"
645 normal 0gq6kk
646 call assert_equal([
647 \ "xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
648 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx.",
649 \ "xxxx xxxx.",
650 \ "",
651 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx",
652 \ "> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx",
653 \ ""
654 \ ], getline(1, '$'))
655
656 set fo& tw&
657 enew!
658endfunc
659
660" Test undo after ":%s" and formatting.
661func Test_format_undo()
662 enew!
663 map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq
664
665 call append(0, [
666 \ "aa aa aa aa",
667 \ "bb bb bb bb",
668 \ "cc cc cc cc"
669 \ ])
670 " undo/redo here to make the next undo only work on the following changes
671 exe "normal i\<C-G>u"
672 call cursor(1,1)
673 normal ggu
674 call assert_equal([
675 \ "aa aa aa aa",
676 \ "bb bb bb bb",
677 \ "cc cc cc cc",
678 \ ""
679 \ ], getline(1, '$'))
680
681 unmap gg
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100682 set tw&
Bram Moolenaarcada7892018-01-31 19:30:24 +0100683 enew!
684endfunc
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100685
686func Test_format_list_auto()
687 new
688 call setline(1, ['1. abc', '2. def', '3. ghi'])
689 set fo=tan ai bs=2
690 call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
691 call assert_equal('2. defx ghi', getline(2))
692 bwipe!
693 set fo& ai& bs&
694endfunc
Bram Moolenaar396b7c72019-10-21 23:08:59 +0200695
696func Test_crash_github_issue_5095()
697 CheckFeature autocmd
698
699 " This used to segfault, see https://github.com/vim/vim/issues/5095
700 augroup testing
701 au BufNew x center
702 augroup END
703
704 next! x
705
706 bw
707 augroup testing
708 au!
709 augroup END
710 augroup! testing
711endfunc
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100712
713" Test for formatting multi-byte text with 'fo=t'
714func Test_tw_2_fo_t()
715 new
716 let t =<< trim END
717 {
718 XYZ
719 abc XYZ
720 }
721 END
722 call setline(1, t)
723 call cursor(2, 1)
724
725 set tw=2 fo=t
726 let t =<< trim END
727 XYZ
728 abc XYZ
729 END
730 exe "normal gqgqjgqgq"
731 exe "normal o\n" . join(t, "\n")
732
733 let expected =<< trim END
734 {
735 XYZ
736 abc
737 XYZ
738
739 XYZ
740 abc
741 XYZ
742 }
743 END
744 call assert_equal(expected, getline(1, '$'))
745
746 set tw& fo&
747 bwipe!
748endfunc
749
750" Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
751func Test_tw_1_fo_tm()
752 new
753 let t =<< trim END
754 {
755
756 a
757 a
758 XY
759
760 }
761 END
762 call setline(1, t)
763 call cursor(2, 1)
764
765 set tw=1 fo=tm
766 let t =<< trim END
767
768 a
769 a
770 XY
771
772 END
773 exe "normal gqgqjgqgqjgqgqjgqgqjgqgq"
774 exe "normal o\n" . join(t, "\n")
775
776 let expected =<< trim END
777 {
778
779
780 a
781
782 a
783
784
785
786
787
788
789
790 a
791
792 a
793
794
795
796
797 }
798 END
799 call assert_equal(expected, getline(1, '$'))
800
801 set tw& fo&
802 bwipe!
803endfunc
804
805" Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
806func Test_tw_2_fo_tm()
807 new
808 let t =<< trim END
809 {
810
811 Xa
812 X a
813 XY
814 X Y
815 aX
816 abX
817 abcX
818 abX c
819 abXY
820 }
821 END
822 call setline(1, t)
823 call cursor(2, 1)
824
825 set tw=2 fo=tm
826 let t =<< trim END
827
828 Xa
829 X a
830 XY
831 X Y
832 aX
833 abX
834 abcX
835 abX c
836 abXY
837 END
838 exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
839 exe "normal o\n" . join(t, "\n")
840
841 let expected =<< trim END
842 {
843
844
845 a
846
847 a
848
849
850
851
852 a
853
854 ab
855
856 abc
857
858 ab
859
860 c
861 ab
862
863
864
865
866
867 a
868
869 a
870
871
872
873
874 a
875
876 ab
877
878 abc
879
880 ab
881
882 c
883 ab
884
885
886 }
887 END
888 call assert_equal(expected, getline(1, '$'))
889
890 set tw& fo&
891 bwipe!
892endfunc
893
894" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
895func Test_tw_2_fo_tm_ai()
896 new
897 let t =<< trim END
898 {
899
900 a
901 }
902 END
903 call setline(1, t)
904 call cursor(2, 1)
905
906 set ai tw=2 fo=tm
907 let t =<< trim END
908
909 a
910 END
911 exe "normal gqgqjgqgq"
912 exe "normal o\n" . join(t, "\n")
913
914 let expected =<< trim END
915 {
916
917
918 a
919
920
921
922 a
923 }
924 END
925 call assert_equal(expected, getline(1, '$'))
926
927 set tw& fo& ai&
928 bwipe!
929endfunc
930
931" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
932func Test_tw_2_fo_tm_noai()
933 new
934 let t =<< trim END
935 {
936
937 Xa
938 }
939 END
940 call setline(1, t)
941 call cursor(2, 1)
942
943 set noai tw=2 fo=tm
944 exe "normal gqgqjgqgqo\n \n a"
945
946 let expected =<< trim END
947 {
948
949
950 a
951
952
953
954 a
955 }
956 END
957 call assert_equal(expected, getline(1, '$'))
958
959 set tw& fo& ai&
960 bwipe!
961endfunc
962
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100963func Test_tw_2_fo_tm_replace()
964 new
965 let t =<< trim END
966 {
967
968 }
969 END
970 call setline(1, t)
971 call cursor(2, 1)
972
973 set tw=2 fo=tm
974 exe "normal Ra"
975
976 let expected =<< trim END
977 {
978
979 a
980 }
981 END
982 call assert_equal(expected, getline(1, '$'))
983
984 set tw& fo&
985 bwipe!
986endfunc
987
988" Test for 'matchpairs' with multibyte chars
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200989func Test_mps_multibyte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100990 new
991 let t =<< trim END
992 {
993 two three four
994 }
995 END
996 call setline(1, t)
997 call cursor(2, 1)
998
999 exe "set mps+=\u2018:\u2019"
1000 normal d%
1001
1002 let expected =<< trim END
1003 {
1004 four
1005 }
1006 END
1007 call assert_equal(expected, getline(1, '$'))
1008
1009 set mps&
1010 bwipe!
1011endfunc
1012
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001013" Test for 'matchpairs' in latin1 encoding
1014func Test_mps_latin1()
1015 new
1016 let save_enc = &encoding
1017 set encoding=latin1
1018 call setline(1, 'abc(def)ghi')
1019 normal %
1020 call assert_equal(8, col('.'))
1021 normal %
1022 call assert_equal(4, col('.'))
1023 call cursor(1, 6)
1024 normal [(
1025 call assert_equal(4, col('.'))
1026 normal %
1027 call assert_equal(8, col('.'))
1028 call cursor(1, 6)
1029 normal ])
1030 call assert_equal(8, col('.'))
1031 normal %
1032 call assert_equal(4, col('.'))
1033 let &encoding = save_enc
1034 close!
1035endfunc
1036
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02001037func Test_empty_matchpairs()
1038 split
1039 set matchpairs= showmatch
1040 call assert_nobeep('call feedkeys("ax\tx\t\<Esc>", "xt")')
1041 set matchpairs& noshowmatch
1042 bwipe!
1043endfunc
1044
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01001045func Test_mps_error()
1046 let encoding_save = &encoding
1047
1048 for e in ['utf-8', 'latin1']
1049 exe 'set encoding=' .. e
1050
1051 call assert_fails('set mps=<:', 'E474:', e)
1052 call assert_fails('set mps=:>', 'E474:', e)
1053 call assert_fails('set mps=<>', 'E474:', e)
1054 call assert_fails('set mps=<:>_', 'E474:', e)
1055 endfor
1056
1057 let &encoding = encoding_save
1058endfunc
1059
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001060" Test for ra on multi-byte characters
1061func Test_ra_multibyte()
1062 new
1063 let t =<< trim END
1064 ra test
1065 bb
1066 aab
1067 END
1068 call setline(1, t)
1069 call cursor(1, 1)
1070
1071 normal jVjra
1072
1073 let expected =<< trim END
1074 ra test
1075 aaaa
1076 aaa
1077 END
1078 call assert_equal(expected, getline(1, '$'))
1079
1080 bwipe!
1081endfunc
1082
1083" Test for 'whichwrap' with multi-byte character
Bram Moolenaara48e78e2019-12-17 20:29:26 +01001084func Test_whichwrap_multi_byte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001085 new
1086 let t =<< trim END
1087 á
1088 x
1089 END
1090 call setline(1, t)
1091 call cursor(2, 1)
1092
1093 set whichwrap+=h
1094 normal dh
Bram Moolenaara48e78e2019-12-17 20:29:26 +01001095 set whichwrap&
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001096
1097 let expected =<< trim END
1098 áx
1099 END
1100 call assert_equal(expected, getline(1, '$'))
1101
1102 bwipe!
1103endfunc
1104
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001105" Test for 'a' and 'w' flags in 'formatoptions'
1106func Test_fo_a_w()
1107 new
1108 setlocal fo+=aw tw=10
1109 call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
1110 call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001111
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001112 " when a line ends with space, it is not broken up.
1113 %d
1114 call feedkeys("ione two to ", 'xt')
1115 call assert_equal('one two to ', getline(1))
1116
1117 " when a line ends with spaces and backspace is used in the next line, the
1118 " last space in the previous line should be removed.
1119 %d
1120 set backspace=indent,eol,start
1121 call setline(1, ['one ', 'two'])
1122 exe "normal 2Gi\<BS>"
1123 call assert_equal(['one two'], getline(1, '$'))
1124 set backspace&
1125
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001126 " Test for 'a', 'w' and '1' options.
1127 setlocal textwidth=0
1128 setlocal fo=1aw
1129 %d
1130 call setline(1, '. foo')
1131 normal 72ig
1132 call feedkeys('a uu uu uu', 'xt')
1133 call assert_equal('g uu uu ', getline(1)[-8:])
1134 call assert_equal(['uu. foo'], getline(2, '$'))
1135
Bram Moolenaard0a1dee2020-12-20 13:07:48 +01001136 " using backspace or "x" triggers reformat
1137 call setline(1, ['1 2 3 4 5 ', '6 7 8 9'])
1138 set tw=10
1139 set fo=taw
1140 set bs=indent,eol,start
1141 exe "normal 1G4la\<BS>\<BS>\<Esc>"
1142 call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2))
1143 exe "normal f4xx"
1144 call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2))
1145
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001146 " using "cw" leaves cursor in right spot
1147 call setline(1, ['Now we g whether that nation, or',
1148 \ 'any nation so conceived and,'])
1149 set fo=tcqa tw=35
1150 exe "normal 2G0cwx\<Esc>"
1151 call assert_equal(['Now we g whether that nation, or x', 'nation so conceived and,'], getline(1, 2))
1152
Bram Moolenaard0a1dee2020-12-20 13:07:48 +01001153 set tw=0
1154 set fo&
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001155 %bw!
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001156endfunc
1157
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001158" Test for formatting lines using gq in visual mode
1159func Test_visual_gq_format()
1160 new
1161 call setline(1, ['one two three four', 'five six', 'one two'])
1162 setl textwidth=10
1163 call feedkeys('ggv$jj', 'xt')
1164 redraw!
1165 normal gq
1166 %d
1167 call setline(1, ['one two three four', 'five six', 'one two'])
1168 normal G$
1169 call feedkeys('v0kk', 'xt')
1170 redraw!
1171 normal gq
1172 setl textwidth&
1173 close!
1174endfunc
1175
1176" Test for 'n' flag in 'formatoptions' to format numbered lists
1177func Test_fo_n()
1178 new
1179 setlocal autoindent
1180 setlocal textwidth=12
1181 setlocal fo=n
1182 call setline(1, [' 1) one two three four', ' 2) two'])
1183 normal gggqG
1184 call assert_equal([' 1) one two', ' three', ' four', ' 2) two'],
1185 \ getline(1, '$'))
1186 close!
1187endfunc
1188
1189" Test for 'formatlistpat' option
1190func Test_formatlistpat()
1191 new
1192 setlocal autoindent
1193 setlocal textwidth=10
1194 setlocal fo=n
1195 setlocal formatlistpat=^\\s*-\\s*
1196 call setline(1, [' - one two three', ' - two'])
1197 normal gggqG
1198 call assert_equal([' - one', ' two', ' three', ' - two'],
1199 \ getline(1, '$'))
1200 close!
1201endfunc
1202
1203" Test for the 'b' and 'v' flags in 'formatoptions'
1204" Text should wrap only if a space character is inserted at or before
1205" 'textwidth'
1206func Test_fo_b()
1207 new
1208 setlocal textwidth=20
1209
1210 setlocal formatoptions=t
1211 call setline(1, 'one two three four')
1212 call feedkeys('Amore', 'xt')
1213 call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
1214
1215 setlocal formatoptions=bt
1216 %d
1217 call setline(1, 'one two three four')
1218 call feedkeys('Amore five', 'xt')
1219 call assert_equal(['one two three fourmore five'], getline(1, '$'))
1220
1221 setlocal formatoptions=bt
1222 %d
1223 call setline(1, 'one two three four')
1224 call feedkeys('A five', 'xt')
1225 call assert_equal(['one two three four', 'five'], getline(1, '$'))
1226
1227 setlocal formatoptions=vt
1228 %d
1229 call setline(1, 'one two three four')
1230 call feedkeys('Amore five', 'xt')
1231 call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
1232
1233 close!
1234endfunc
1235
1236" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
1237" word.
1238func Test_fo_1()
1239 new
1240 setlocal textwidth=20
1241
1242 setlocal formatoptions=t
1243 call setline(1, 'one two three four')
1244 call feedkeys('A a bird', 'xt')
1245 call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
1246
1247 %d
1248 setlocal formatoptions=t1
1249 call setline(1, 'one two three four')
1250 call feedkeys('A a bird', 'xt')
1251 call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
1252
1253 close!
1254endfunc
1255
1256" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
1257" is longer than 'textwidth', then it is not broken.
1258func Test_fo_l()
1259 new
1260 setlocal textwidth=20
1261
1262 setlocal formatoptions=t
1263 call setline(1, 'one two three four five')
1264 call feedkeys('A six', 'xt')
1265 call assert_equal(['one two three four', 'five six'], getline(1, '$'))
1266
1267 %d
1268 setlocal formatoptions=tl
1269 call setline(1, 'one two three four five')
1270 call feedkeys('A six', 'xt')
1271 call assert_equal(['one two three four five six'], getline(1, '$'))
1272
1273 close!
1274endfunc
1275
1276" Test for the '2' flag in 'formatoptions'
1277func Test_fo_2()
1278 new
1279 setlocal autoindent
1280 setlocal formatoptions=t2
1281 setlocal textwidth=30
1282 call setline(1, ["\tfirst line of a paragraph.",
1283 \ "second line of the same paragraph.",
1284 \ "third line."])
1285 normal gggqG
1286 call assert_equal(["\tfirst line of a",
1287 \ "paragraph. second line of the",
1288 \ "same paragraph. third line."], getline(1, '$'))
1289 close!
1290endfunc
1291
Bram Moolenaar78d52882022-05-24 13:57:54 +01001292" This was leaving the cursor after the end of a line. Complicated way to
1293" have the problem show up with valgrind.
1294func Test_correct_cursor_position()
1295 set encoding=iso8859
1296 new
1297 norm a000“0
1298 sil! norm gggg0i0gw0gg
1299
1300 bwipe!
1301 set encoding=utf8
1302endfunc
1303
Christian Brabandtbba79802024-04-11 22:54:44 +02001304" This was crashing Vim
zeertzjq75a73552024-04-28 16:20:55 +02001305func Test_textwidth_overflow()
Christian Brabandtbba79802024-04-11 22:54:44 +02001306 new
1307 setl tw=999999999
1308 normal 10ig
1309 call feedkeys('a ab cd ef', 'xt')
1310 bw!
1311endfunc
1312
Christian Brabandt86ef8152024-04-27 11:55:08 +02001313func Test_breakindent_reformat()
1314 " Make sure textformatting uses the full width
1315 " of the textwidth and does not consider the indent
1316 " from breakindent into account when calculating the
1317 " line length. Should break at tw 78 and not at 70
1318 CheckOption breakindent
1319 new
1320 80vnew
1321 39vnew
1322 setl ai breakindent tw=78
1323 let lorem = [
1324 \ ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam luctus',
1325 \ ' lectus sodales, dictum augue vel, molestie augue. Duis sit amet',
1326 \ ' rhoncus justo. Nullam posuere risus semper magna commodo scelerisque.',
1327 \ ' Duis et venenatis sem. In rhoncus augue sed tempor mattis. Mauris id',
1328 \ ' aliquet odio.']
1329 call setline(1, lorem)
1330 norm! gqap
1331 call assert_equal(lorem, getline(1, '$'))
1332 bw!
1333 bw!
1334 bw!
1335endfunc
1336
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001337" vim: shiftwidth=2 sts=2 expandtab