blob: 95fffe05aefef8e21b6da4a22a7d69841972c5ed [file] [log] [blame]
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001" Test for variable tabstops
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature vartabs
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005
Bram Moolenaara87b72c2018-06-25 21:24:51 +02006source view_util.vim
Bram Moolenaar037c54f2019-04-20 23:47:46 +02007
Bram Moolenaar64f41072018-10-15 22:51:50 +02008func s:compare_lines(expect, actual)
Bram Moolenaara87b72c2018-06-25 21:24:51 +02009 call assert_equal(join(a:expect, "\n"), join(a:actual, "\n"))
Bram Moolenaar64f41072018-10-15 22:51:50 +020010endfunc
Bram Moolenaara87b72c2018-06-25 21:24:51 +020011
Bram Moolenaar64f41072018-10-15 22:51:50 +020012func Test_vartabs()
Bram Moolenaar04958cb2018-06-23 19:23:02 +020013 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
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010094 " 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 vlaues 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
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200106 " 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
Bram Moolenaar1e115362019-01-09 23:01:02 +0100149func Test_vartabs_breakindent()
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200150 if !exists("+breakindent")
151 return
152 endif
153 new
154 %d
155
156 " Test normal operation of tabstops ...
157 set ts=4
158 call setline(1, join(split('aaaaa', '\zs'), "\t"))
159 retab 8
160 let expect = "a a\<tab>a a\<tab>a"
161 call assert_equal(expect, getline(1))
162
163 " ... and softtabstops
164 set ts=8 sts=6
165 exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
166 let expect = "b b\<tab> b\<tab> b\<tab>b"
167 call assert_equal(expect, getline(1))
168
169 " Test variable tabstops.
170 set sts=0 vts=4,8,4,8
171 exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
172 retab 8
173 let expect = "c c\<tab> c\<tab>c\<tab>c\<tab>c"
174 call assert_equal(expect, getline(1))
175
176 set et vts=4,8,4,8
177 exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
178 let expect = "d d d d d d"
179 call assert_equal(expect, getline(1))
180
181 " Changing ts should have no effect if vts is in use.
182 call cursor(1, 1)
183 set ts=6
184 exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
185 let expect = "e e e e e e"
186 call assert_equal(expect, getline(1))
187
188 " Clearing vts should revert to using ts.
189 set vts=
190 exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
191 let expect = "f f f f f f"
192 call assert_equal(expect, getline(1))
193
194 " Test variable softtabstops.
195 set noet ts=8 vsts=12,2,6
196 exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
197 let expect = "g\<tab> g g\<tab> g\<tab> g\<tab>g"
198 call assert_equal(expect, getline(1))
199
200 " Variable tabstops and softtabstops combined.
201 set vsts=6,12,8 vts=4,6,8
202 exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
203 let expect = "h\<tab> h\<tab>\<tab>h\<tab>h\<tab>h"
204 call assert_equal(expect, getline(1))
205
206 " Retab with a single value, not using vts.
207 set ts=8 sts=0 vts= vsts=
208 exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
209 retab 4
210 let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
211 call assert_equal(expect, getline(1))
212
213 " Retab with a single value, using vts.
214 set ts=8 sts=0 vts=6 vsts=
215 exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
216 retab 4
217 let expect = "j\<tab> j\<tab>\<tab>j\<tab> j\<tab>\<tab>j"
218 call assert_equal(expect, getline(1))
219
220 " Retab with multiple values, not using vts.
221 set ts=6 sts=0 vts= vsts=
222 exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
223 retab 4,8
224 let expect = "k\<tab> k\<tab>k k\<tab> k\<tab> k"
225 call assert_equal(expect, getline(1))
226
227 " Retab with multiple values, using vts.
228 set ts=8 sts=0 vts=6 vsts=
229 exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
230 retab 4,8
231 let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l"
232 call assert_equal(expect, getline(1))
233
234 " Check that global and local values are set.
235 set ts=4 vts=6 sts=8 vsts=10
236 call assert_equal(&ts, 4)
237 call assert_equal(&vts, '6')
238 call assert_equal(&sts, 8)
239 call assert_equal(&vsts, '10')
240 new
241 call assert_equal(&ts, 4)
242 call assert_equal(&vts, '6')
243 call assert_equal(&sts, 8)
244 call assert_equal(&vsts, '10')
245 bwipeout!
246
247 " Check that local values only are set.
248 setlocal ts=5 vts=7 sts=9 vsts=11
249 call assert_equal(&ts, 5)
250 call assert_equal(&vts, '7')
251 call assert_equal(&sts, 9)
252 call assert_equal(&vsts, '11')
253 new
254 call assert_equal(&ts, 4)
255 call assert_equal(&vts, '6')
256 call assert_equal(&sts, 8)
257 call assert_equal(&vsts, '10')
258 bwipeout!
259
260 " Check that global values only are set.
261 setglobal ts=6 vts=8 sts=10 vsts=12
262 call assert_equal(&ts, 5)
263 call assert_equal(&vts, '7')
264 call assert_equal(&sts, 9)
265 call assert_equal(&vsts, '11')
266 new
267 call assert_equal(&ts, 6)
268 call assert_equal(&vts, '8')
269 call assert_equal(&sts, 10)
270 call assert_equal(&vsts, '12')
271 bwipeout!
272
273 bwipeout!
274endfunc
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200275
Bram Moolenaar64f41072018-10-15 22:51:50 +0200276func Test_vartabs_linebreak()
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200277 if winwidth(0) < 40
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200278 return
279 endif
280 new
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200281 40vnew
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200282 %d
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200283 setl linebreak vartabstop=10,20,30,40
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200284 call setline(1, "\tx\tx\tx\tx")
285
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200286 let expect = [' x ',
287 \ 'x x ',
288 \ 'x ']
289 let lines = ScreenLines([1, 3], winwidth(0))
290 call s:compare_lines(expect, lines)
291 setl list listchars=tab:>-
292 let expect = ['>---------x>------------------ ',
293 \ 'x>------------------x>------------------',
294 \ 'x ']
295 let lines = ScreenLines([1, 3], winwidth(0))
296 call s:compare_lines(expect, lines)
297 setl linebreak vartabstop=40
298 let expect = ['>---------------------------------------',
299 \ 'x>--------------------------------------',
300 \ 'x>--------------------------------------',
301 \ 'x>--------------------------------------',
302 \ 'x ']
303 let lines = ScreenLines([1, 5], winwidth(0))
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200304 call s:compare_lines(expect, lines)
305
306 " cleanup
307 bw!
308 bw!
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200309 set nolist listchars&vim
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200310endfunc
Bram Moolenaar64f41072018-10-15 22:51:50 +0200311
Bram Moolenaarf9514162018-11-22 03:08:29 +0100312func Test_vartabs_shiftwidth()
313 "return
314 if winwidth(0) < 40
315 return
316 endif
317 new
318 40vnew
319 %d
320" setl varsofttabstop=10,20,30,40
321 setl shiftwidth=0 vartabstop=10,20,30,40
322 call setline(1, "x")
323
324 " Check without any change.
325 let expect = ['x ']
326 let lines = ScreenLines(1, winwidth(0))
327 call s:compare_lines(expect, lines)
328 " Test 1:
329 " shiftwidth depends on the indent, first check with cursor at the end of the
330 " line (which is the same as the start of the line, since there is only one
331 " character).
332 norm! $>>
333 let expect1 = [' x ']
334 let lines = ScreenLines(1, winwidth(0))
335 call s:compare_lines(expect1, lines)
336 call assert_equal(10, shiftwidth())
337 call assert_equal(10, shiftwidth(1))
338 call assert_equal(20, shiftwidth(virtcol('.')))
339 norm! $>>
340 let expect2 = [' x ', '~ ']
341 let lines = ScreenLines([1, 2], winwidth(0))
342 call s:compare_lines(expect2, lines)
343 call assert_equal(20, shiftwidth(virtcol('.')-2))
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200344 call assert_equal(30, virtcol('.')->shiftwidth())
Bram Moolenaarf9514162018-11-22 03:08:29 +0100345 norm! $>>
346 let expect3 = [' ', ' x ', '~ ']
347 let lines = ScreenLines([1, 3], winwidth(0))
348 call s:compare_lines(expect3, lines)
349 call assert_equal(30, shiftwidth(virtcol('.')-2))
350 call assert_equal(40, shiftwidth(virtcol('.')))
351 norm! $>>
352 let expect4 = [' ', ' ', ' x ']
353 let lines = ScreenLines([1, 3], winwidth(0))
354 call assert_equal(40, shiftwidth(virtcol('.')))
355 call s:compare_lines(expect4, lines)
356
357 " Test 2: Put the cursor at the first column, result should be the same
358 call setline(1, "x")
359 norm! 0>>
360 let lines = ScreenLines(1, winwidth(0))
361 call s:compare_lines(expect1, lines)
362 norm! 0>>
363 let lines = ScreenLines([1, 2], winwidth(0))
364 call s:compare_lines(expect2, lines)
365 norm! 0>>
366 let lines = ScreenLines([1, 3], winwidth(0))
367 call s:compare_lines(expect3, lines)
368 norm! 0>>
369 let lines = ScreenLines([1, 3], winwidth(0))
370 call s:compare_lines(expect4, lines)
371
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100372 call assert_fails('call shiftwidth([])', 'E745:')
373
Bram Moolenaarf9514162018-11-22 03:08:29 +0100374 " cleanup
375 bw!
376 bw!
377endfunc
378
Bram Moolenaar64f41072018-10-15 22:51:50 +0200379func Test_vartabs_failures()
380 call assert_fails('set vts=8,')
381 call assert_fails('set vsts=8,')
382 call assert_fails('set vts=8,,8')
383 call assert_fails('set vsts=8,,8')
384 call assert_fails('set vts=8,,8,')
385 call assert_fails('set vsts=8,,8,')
386 call assert_fails('set vts=,8')
387 call assert_fails('set vsts=,8')
388endfunc
Bram Moolenaar037c54f2019-04-20 23:47:46 +0200389
390func Test_vartabs_reset()
391 set vts=8
392 set all&
393 call assert_equal('', &vts)
394endfunc
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100395
396func s:SaveCol(l)
397 call add(a:l, [col('.'), virtcol('.')])
398 return ''
399endfunc
400
401" Test for 'varsofttabstop'
402func Test_varsofttabstop()
403 new
404 inoremap <expr> <F2> s:SaveCol(g:cols)
405
406 set backspace=indent,eol,start
407 set varsofttabstop=6,2,5,3
408 let g:cols = []
409 call feedkeys("a\t\<F2>\t\<F2>\t\<F2>\t\<F2> ", 'xt')
410 call assert_equal("\t\t ", getline(1))
411 call assert_equal([[7, 7], [2, 9], [7, 14], [3, 17]], g:cols)
412
413 let g:cols = []
414 call feedkeys("a\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>", 'xt')
415 call assert_equal('', getline(1))
416 call assert_equal([[3, 17], [7, 14], [2, 9], [7, 7], [1, 1]], g:cols)
417
418 set varsofttabstop&
419 set backspace&
420 iunmap <F2>
421 close!
422endfunc
423
424" vim: shiftwidth=2 sts=2 expandtab