robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
15.5 KiB527 linesraw
1" Test for variable tabstops
2
3source check.vim
4CheckFeature vartabs
5
6source view_util.vim
7
8func s:compare_lines(expect, actual)
9  call assert_equal(join(a:expect, "\n"), join(a:actual, "\n"))
10endfunc
11
12func Test_vartabs()
13  new
14  %d
15
16  " Test normal operation of tabstops ...
17  set ts=4
18  call setline(1, join(split('aaaaa', '\zs'), "\t"))
19  retab 8
20  let expect = "a   a\<tab>a   a\<tab>a"
21  call assert_equal(expect, getline(1))
22
23  " ... and softtabstops
24  set ts=8 sts=6
25  exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
26  let expect = "b     b\<tab>    b\<tab>  b\<tab>b"
27  call assert_equal(expect, getline(1))
28
29  " Test variable tabstops.
30  set sts=0 vts=4,8,4,8
31  exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
32  retab 8
33  let expect = "c   c\<tab>    c\<tab>c\<tab>c\<tab>c"
34  call assert_equal(expect, getline(1))
35
36  set et vts=4,8,4,8
37  exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
38  let expect = "d   d       d   d       d       d"
39  call assert_equal(expect, getline(1))
40
41  " Changing ts should have no effect if vts is in use.
42  call cursor(1, 1)
43  set ts=6
44  exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
45  let expect = "e   e       e   e       e       e"
46  call assert_equal(expect, getline(1))
47
48  " Clearing vts should revert to using ts.
49  set vts=
50  exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
51  let expect = "f     f     f     f     f     f"
52  call assert_equal(expect, getline(1))
53
54  " Test variable softtabstops.
55  set noet ts=8 vsts=12,2,6
56  exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
57  let expect = "g\<tab>    g g\<tab>    g\<tab>  g\<tab>g"
58  call assert_equal(expect, getline(1))
59
60  " Variable tabstops and softtabstops combined.
61  set vsts=6,12,8 vts=4,6,8
62  exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
63  let expect = "h\<tab>  h\<tab>\<tab>h\<tab>h\<tab>h"
64  call assert_equal(expect, getline(1))
65
66  " Retab with a single value, not using vts.
67  set ts=8 sts=0 vts= vsts=
68  exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
69  retab 4
70  let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
71  call assert_equal(expect, getline(1))
72
73  " Retab with a single value, using vts.
74  set ts=8 sts=0 vts=6 vsts=
75  exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
76  retab 4
77  let expect = "j\<tab>  j\<tab>\<tab>j\<tab>  j\<tab>\<tab>j"
78  call assert_equal(expect, getline(1))
79
80  " Retab with multiple values, not using vts.
81  set ts=6 sts=0 vts= vsts=
82  exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
83  retab 4,8
84  let expect = "k\<tab>  k\<tab>k     k\<tab>    k\<tab>  k"
85  call assert_equal(expect, getline(1))
86
87  " Retab with multiple values, using vts.
88  set ts=8 sts=0 vts=6 vsts=
89  exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
90  retab 4,8
91  let expect = "l\<tab>  l\<tab>l     l\<tab>    l\<tab>  l"
92  call assert_equal(expect, getline(1))
93
94  " Test for 'retab' with vts
95  set ts=8 sts=0 vts=5,3,6,2 vsts=
96  exe "norm! S                l"
97  .retab!
98  call assert_equal("\t\t\t\tl", getline(1))
99
100  " Test for 'retab' with same values as vts
101  set ts=8 sts=0 vts=5,3,6,2 vsts=
102  exe "norm! S                l"
103  .retab! 5,3,6,2
104  call assert_equal("\t\t\t\tl", getline(1))
105
106  " Check that global and local values are set.
107  set ts=4 vts=6 sts=8 vsts=10
108  call assert_equal(&ts, 4)
109  call assert_equal(&vts, '6')
110  call assert_equal(&sts, 8)
111  call assert_equal(&vsts, '10')
112  new
113  call assert_equal(&ts, 4)
114  call assert_equal(&vts, '6')
115  call assert_equal(&sts, 8)
116  call assert_equal(&vsts, '10')
117  bwipeout!
118
119  " Check that local values only are set.
120  setlocal ts=5 vts=7 sts=9 vsts=11
121  call assert_equal(&ts, 5)
122  call assert_equal(&vts, '7')
123  call assert_equal(&sts, 9)
124  call assert_equal(&vsts, '11')
125  new
126  call assert_equal(&ts, 4)
127  call assert_equal(&vts, '6')
128  call assert_equal(&sts, 8)
129  call assert_equal(&vsts, '10')
130  bwipeout!
131
132  " Check that global values only are set.
133  setglobal ts=6 vts=8 sts=10 vsts=12
134  call assert_equal(&ts, 5)
135  call assert_equal(&vts, '7')
136  call assert_equal(&sts, 9)
137  call assert_equal(&vsts, '11')
138  new
139  call assert_equal(&ts, 6)
140  call assert_equal(&vts, '8')
141  call assert_equal(&sts, 10)
142  call assert_equal(&vsts, '12')
143  bwipeout!
144
145  set ts& vts& sts& vsts& et&
146  bwipeout!
147endfunc
148
149func Test_retab_invalid_arg()
150  new
151  call setline(1, "\ttext")
152  retab 0
153  call assert_fails("retab -8", 'E487: Argument must be positive')
154  call assert_fails("retab 10000", 'E475:')
155  call assert_fails("retab 720575940379279360", 'E475:')
156  bwipe!
157endfunc
158
159func Test_vartabs_breakindent()
160  if !exists("+breakindent")
161    return
162  endif
163  new
164  %d
165
166  " Test normal operation of tabstops ...
167  set ts=4
168  call setline(1, join(split('aaaaa', '\zs'), "\t"))
169  retab 8
170  let expect = "a   a\<tab>a   a\<tab>a"
171  call assert_equal(expect, getline(1))
172
173  " ... and softtabstops
174  set ts=8 sts=6
175  exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
176  let expect = "b     b\<tab>    b\<tab>  b\<tab>b"
177  call assert_equal(expect, getline(1))
178
179  " Test variable tabstops.
180  set sts=0 vts=4,8,4,8
181  exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
182  retab 8
183  let expect = "c   c\<tab>    c\<tab>c\<tab>c\<tab>c"
184  call assert_equal(expect, getline(1))
185
186  set et vts=4,8,4,8
187  exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
188  let expect = "d   d       d   d       d       d"
189  call assert_equal(expect, getline(1))
190
191  " Changing ts should have no effect if vts is in use.
192  call cursor(1, 1)
193  set ts=6
194  exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
195  let expect = "e   e       e   e       e       e"
196  call assert_equal(expect, getline(1))
197
198  " Clearing vts should revert to using ts.
199  set vts=
200  exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
201  let expect = "f     f     f     f     f     f"
202  call assert_equal(expect, getline(1))
203
204  " Test variable softtabstops.
205  set noet ts=8 vsts=12,2,6
206  exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
207  let expect = "g\<tab>    g g\<tab>    g\<tab>  g\<tab>g"
208  call assert_equal(expect, getline(1))
209
210  " Variable tabstops and softtabstops combined.
211  set vsts=6,12,8 vts=4,6,8
212  exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
213  let expect = "h\<tab>  h\<tab>\<tab>h\<tab>h\<tab>h"
214  call assert_equal(expect, getline(1))
215
216  " Retab with a single value, not using vts.
217  set ts=8 sts=0 vts= vsts=
218  exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
219  retab 4
220  let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
221  call assert_equal(expect, getline(1))
222
223  " Retab with a single value, using vts.
224  set ts=8 sts=0 vts=6 vsts=
225  exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
226  retab 4
227  let expect = "j\<tab>  j\<tab>\<tab>j\<tab>  j\<tab>\<tab>j"
228  call assert_equal(expect, getline(1))
229
230  " Retab with multiple values, not using vts.
231  set ts=6 sts=0 vts= vsts=
232  exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
233  retab 4,8
234  let expect = "k\<tab>  k\<tab>k     k\<tab>    k\<tab>  k"
235  call assert_equal(expect, getline(1))
236
237  " Retab with multiple values, using vts.
238  set ts=8 sts=0 vts=6 vsts=
239  exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
240  retab 4,8
241  let expect = "l\<tab>  l\<tab>l     l\<tab>    l\<tab>  l"
242  call assert_equal(expect, getline(1))
243
244  " Check that global and local values are set.
245  set ts=4 vts=6 sts=8 vsts=10
246  call assert_equal(&ts, 4)
247  call assert_equal(&vts, '6')
248  call assert_equal(&sts, 8)
249  call assert_equal(&vsts, '10')
250  new
251  call assert_equal(&ts, 4)
252  call assert_equal(&vts, '6')
253  call assert_equal(&sts, 8)
254  call assert_equal(&vsts, '10')
255  bwipeout!
256
257  " Check that local values only are set.
258  setlocal ts=5 vts=7 sts=9 vsts=11
259  call assert_equal(&ts, 5)
260  call assert_equal(&vts, '7')
261  call assert_equal(&sts, 9)
262  call assert_equal(&vsts, '11')
263  new
264  call assert_equal(&ts, 4)
265  call assert_equal(&vts, '6')
266  call assert_equal(&sts, 8)
267  call assert_equal(&vsts, '10')
268  bwipeout!
269
270  " Check that global values only are set.
271  setglobal ts=6 vts=8 sts=10 vsts=12
272  call assert_equal(&ts, 5)
273  call assert_equal(&vts, '7')
274  call assert_equal(&sts, 9)
275  call assert_equal(&vsts, '11')
276  new
277  call assert_equal(&ts, 6)
278  call assert_equal(&vts, '8')
279  call assert_equal(&sts, 10)
280  call assert_equal(&vsts, '12')
281  bwipeout!
282
283  bwipeout!
284endfunc
285
286func Test_vartabs_linebreak()
287  if winwidth(0) < 40
288    return
289  endif
290  new
291  40vnew
292  %d
293  setl linebreak vartabstop=10,20,30,40
294  call setline(1, "\tx\tx\tx\tx")
295
296  let expect = ['          x                             ',
297        \       'x                   x                   ',
298        \       '                    x                   ']
299  let lines = ScreenLines([1, 3], winwidth(0))
300  call s:compare_lines(expect, lines)
301  setl list listchars=tab:>-
302  let expect = ['>---------x>------------------          ',
303        \       'x>------------------x>------------------',
304        \       '--------------------x                   ']
305  let lines = ScreenLines([1, 3], winwidth(0))
306  call s:compare_lines(expect, lines)
307  setl nolinebreak
308  let expect = ['>---------x>------------------x>--------',
309        \       '--------------------x>------------------',
310        \       '--------------------x                   ']
311  let lines = ScreenLines([1, 3], winwidth(0))
312  call s:compare_lines(expect, lines)
313  setl linebreak vartabstop=40
314  let expect = ['>---------------------------------------',
315        \       'x>--------------------------------------',
316        \       'x>--------------------------------------',
317        \       'x>--------------------------------------',
318        \       'x                                       ']
319  let lines = ScreenLines([1, 5], winwidth(0))
320  call s:compare_lines(expect, lines)
321
322  " cleanup
323  bw!
324  bw!
325  set nolist listchars&vim
326endfunc
327
328func Test_vartabs_shiftwidth()
329  "return
330  if winwidth(0) < 40
331    return
332  endif
333  new
334  40vnew
335  %d
336"  setl varsofttabstop=10,20,30,40
337  setl shiftwidth=0 vartabstop=10,20,30,40
338  call setline(1, "x")
339
340  " Check without any change.
341  let expect = ['x                                       ']
342  let lines = ScreenLines(1, winwidth(0))
343  call s:compare_lines(expect, lines)
344  " Test 1:
345  " shiftwidth depends on the indent, first check with cursor at the end of the
346  " line (which is the same as the start of the line, since there is only one
347  " character).
348  norm! $>>
349  let expect1 = ['          x                             ']
350  let lines = ScreenLines(1, winwidth(0))
351  call s:compare_lines(expect1, lines)
352  call assert_equal(10, shiftwidth())
353  call assert_equal(10, shiftwidth(1))
354  call assert_equal(20, shiftwidth(virtcol('.')))
355  norm! $>>
356  let expect2 = ['                              x         ', '~                                       ']
357  let lines = ScreenLines([1, 2], winwidth(0))
358  call s:compare_lines(expect2, lines)
359  call assert_equal(20, shiftwidth(virtcol('.')-2))
360  call assert_equal(30, virtcol('.')->shiftwidth())
361  norm! $>>
362  let expect3 = ['                                        ', '                    x                   ', '~                                       ']
363  let lines = ScreenLines([1, 3], winwidth(0))
364  call s:compare_lines(expect3, lines)
365  call assert_equal(30, shiftwidth(virtcol('.')-2))
366  call assert_equal(40, shiftwidth(virtcol('.')))
367  norm! $>>
368  let expect4 = ['                                        ', '                                        ', '                    x                   ']
369  let lines = ScreenLines([1, 3], winwidth(0))
370  call assert_equal(40, shiftwidth(virtcol('.')))
371  call s:compare_lines(expect4, lines)
372
373  " Test 2: Put the cursor at the first column, result should be the same
374  call setline(1, "x")
375  norm! 0>>
376  let lines = ScreenLines(1, winwidth(0))
377  call s:compare_lines(expect1, lines)
378  norm! 0>>
379  let lines = ScreenLines([1, 2], winwidth(0))
380  call s:compare_lines(expect2, lines)
381  norm! 0>>
382  let lines = ScreenLines([1, 3], winwidth(0))
383  call s:compare_lines(expect3, lines)
384  norm! 0>>
385  let lines = ScreenLines([1, 3], winwidth(0))
386  call s:compare_lines(expect4, lines)
387
388  call assert_fails('call shiftwidth([])', 'E745:')
389
390  " cleanup
391  bw!
392  bw!
393endfunc
394
395func Test_vartabs_failures()
396  call assert_fails('set vts=8,', 'E475: Invalid argument: 8,')
397  call assert_fails('set vsts=8,', 'E475: Invalid argument: 8,')
398  call assert_fails('set vts=8,,8', 'E474: Invalid argument: vts=8,,8')
399  call assert_fails('set vsts=8,,8', 'E474: Invalid argument: vsts=8,,8')
400  call assert_fails('set vts=8,,8,', 'E474: Invalid argument: vts=8,,8,')
401  call assert_fails('set vsts=8,,8,', 'E474: Invalid argument: vsts=8,,8,')
402  call assert_fails('set vts=,8', 'E474: Invalid argument: vts=,8')
403  call assert_fails('set vsts=,8', 'E474: Invalid argument: vsts=,8')
404endfunc
405
406func Test_vartabs_reset()
407  set vts=8
408  set all&
409  call assert_equal('', &vts)
410endfunc
411
412func s:SaveCol(l)
413  call add(a:l, [col('.'), virtcol('.')])
414  return ''
415endfunc
416
417" Test for 'varsofttabstop'
418func Test_varsofttabstop()
419  new
420  inoremap <expr> <F2>  s:SaveCol(g:cols)
421
422  set backspace=indent,eol,start
423  set varsofttabstop=6,2,5,3
424  let g:cols = []
425  call feedkeys("a\t\<F2>\t\<F2>\t\<F2>\t\<F2> ", 'xt')
426  call assert_equal("\t\t ", getline(1))
427  call assert_equal([[7, 7], [2, 9], [7, 14], [3, 17]], g:cols)
428
429  let g:cols = []
430  call feedkeys("a\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>", 'xt')
431  call assert_equal('', getline(1))
432  call assert_equal([[3, 17], [7, 14], [2, 9], [7, 7], [1, 1]], g:cols)
433
434  set varsofttabstop&
435  set backspace&
436  iunmap <F2>
437  close!
438endfunc
439
440" Setting 'shiftwidth' to a negative value, should set it to either the value
441" of 'tabstop' (if 'vartabstop' is not set) or to the first value in
442" 'vartabstop'
443func Test_shiftwidth_vartabstop()
444  throw 'Skipped: Nvim removed this behavior in #6377'
445  setlocal tabstop=7 vartabstop=
446  call assert_fails('set shiftwidth=-1', 'E487:')
447  call assert_equal(7, &shiftwidth)
448  setlocal tabstop=7 vartabstop=5,7,10
449  call assert_fails('set shiftwidth=-1', 'E487:')
450  call assert_equal(5, &shiftwidth)
451  setlocal shiftwidth& vartabstop& tabstop&
452endfunc
453
454func Test_vartabstop_latin1()
455  throw "Skipped: Nvim does not support 'compatible'"
456  let save_encoding = &encoding
457  new
458  set encoding=iso8859-1
459  set compatible linebreak list revins smarttab
460  set vartabstop=400
461  exe "norm i00\t\<C-D>"
462  bwipe!
463  let &encoding = save_encoding
464  set nocompatible linebreak& list& revins& smarttab& vartabstop&
465endfunc
466
467" Verify that right-shifting and left-shifting adjust lines to the proper
468" tabstops.
469func Test_vartabstop_shift_right_left()
470  new
471  set expandtab
472  set shiftwidth=0
473  set vartabstop=17,11,7
474  exe "norm! aword"
475  let expect = "word"
476  call assert_equal(expect, getline(1))
477
478  " Shift to first tabstop.
479  norm! >>
480  let expect = "                 word"
481  call assert_equal(expect, getline(1))
482
483  " Shift to second tabstop.
484  norm! >>
485  let expect = "                            word"
486  call assert_equal(expect, getline(1))
487
488  " Shift to third tabstop.
489  norm! >>
490  let expect = "                                   word"
491  call assert_equal(expect, getline(1))
492
493  " Shift to fourth tabstop, repeating the third shift width.
494  norm! >>
495  let expect = "                                          word"
496  call assert_equal(expect, getline(1))
497
498  " Shift back to the third tabstop.
499  norm! <<
500  let expect = "                                   word"
501  call assert_equal(expect, getline(1))
502
503  " Shift back to the second tabstop.
504  norm! <<
505  let expect = "                            word"
506  call assert_equal(expect, getline(1))
507
508  " Shift back to the first tabstop.
509  norm! <<
510  let expect = "                 word"
511  call assert_equal(expect, getline(1))
512
513  " Shift back to the left margin.
514  norm! <<
515  let expect = "word"
516  call assert_equal(expect, getline(1))
517
518  " Shift again back to the left margin.
519  norm! <<
520  let expect = "word"
521  call assert_equal(expect, getline(1))
522
523  bwipeout!
524endfunc
525
526
527" vim: shiftwidth=2 sts=2 expandtab