blob: 3b8a9ce0b1ab9ec4f076a31afb3a1bc731858c17 [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
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100100 " Test for 'retab' with same values as vts
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100101 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 Moolenaar6d91bcb2020-08-12 18:50:36 +0200150 CheckOption breakindent
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200151 new
152 %d
153
154 " Test normal operation of tabstops ...
155 set ts=4
156 call setline(1, join(split('aaaaa', '\zs'), "\t"))
157 retab 8
158 let expect = "a a\<tab>a a\<tab>a"
159 call assert_equal(expect, getline(1))
160
161 " ... and softtabstops
162 set ts=8 sts=6
163 exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
164 let expect = "b b\<tab> b\<tab> b\<tab>b"
165 call assert_equal(expect, getline(1))
166
167 " Test variable tabstops.
168 set sts=0 vts=4,8,4,8
169 exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
170 retab 8
171 let expect = "c c\<tab> c\<tab>c\<tab>c\<tab>c"
172 call assert_equal(expect, getline(1))
173
174 set et vts=4,8,4,8
175 exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
176 let expect = "d d d d d d"
177 call assert_equal(expect, getline(1))
178
179 " Changing ts should have no effect if vts is in use.
180 call cursor(1, 1)
181 set ts=6
182 exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
183 let expect = "e e e e e e"
184 call assert_equal(expect, getline(1))
185
186 " Clearing vts should revert to using ts.
187 set vts=
188 exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
189 let expect = "f f f f f f"
190 call assert_equal(expect, getline(1))
191
192 " Test variable softtabstops.
193 set noet ts=8 vsts=12,2,6
194 exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
195 let expect = "g\<tab> g g\<tab> g\<tab> g\<tab>g"
196 call assert_equal(expect, getline(1))
197
198 " Variable tabstops and softtabstops combined.
199 set vsts=6,12,8 vts=4,6,8
200 exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
201 let expect = "h\<tab> h\<tab>\<tab>h\<tab>h\<tab>h"
202 call assert_equal(expect, getline(1))
203
204 " Retab with a single value, not using vts.
205 set ts=8 sts=0 vts= vsts=
206 exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
207 retab 4
208 let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
209 call assert_equal(expect, getline(1))
210
211 " Retab with a single value, using vts.
212 set ts=8 sts=0 vts=6 vsts=
213 exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
214 retab 4
215 let expect = "j\<tab> j\<tab>\<tab>j\<tab> j\<tab>\<tab>j"
216 call assert_equal(expect, getline(1))
217
218 " Retab with multiple values, not using vts.
219 set ts=6 sts=0 vts= vsts=
220 exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
221 retab 4,8
222 let expect = "k\<tab> k\<tab>k k\<tab> k\<tab> k"
223 call assert_equal(expect, getline(1))
224
225 " Retab with multiple values, using vts.
226 set ts=8 sts=0 vts=6 vsts=
227 exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
228 retab 4,8
229 let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l"
230 call assert_equal(expect, getline(1))
231
232 " Check that global and local values are set.
233 set ts=4 vts=6 sts=8 vsts=10
234 call assert_equal(&ts, 4)
235 call assert_equal(&vts, '6')
236 call assert_equal(&sts, 8)
237 call assert_equal(&vsts, '10')
238 new
239 call assert_equal(&ts, 4)
240 call assert_equal(&vts, '6')
241 call assert_equal(&sts, 8)
242 call assert_equal(&vsts, '10')
243 bwipeout!
244
245 " Check that local values only are set.
246 setlocal ts=5 vts=7 sts=9 vsts=11
247 call assert_equal(&ts, 5)
248 call assert_equal(&vts, '7')
249 call assert_equal(&sts, 9)
250 call assert_equal(&vsts, '11')
251 new
252 call assert_equal(&ts, 4)
253 call assert_equal(&vts, '6')
254 call assert_equal(&sts, 8)
255 call assert_equal(&vsts, '10')
256 bwipeout!
257
258 " Check that global values only are set.
259 setglobal ts=6 vts=8 sts=10 vsts=12
260 call assert_equal(&ts, 5)
261 call assert_equal(&vts, '7')
262 call assert_equal(&sts, 9)
263 call assert_equal(&vsts, '11')
264 new
265 call assert_equal(&ts, 6)
266 call assert_equal(&vts, '8')
267 call assert_equal(&sts, 10)
268 call assert_equal(&vsts, '12')
269 bwipeout!
270
271 bwipeout!
272endfunc
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200273
Bram Moolenaar64f41072018-10-15 22:51:50 +0200274func Test_vartabs_linebreak()
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200275 if winwidth(0) < 40
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200276 return
277 endif
278 new
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200279 40vnew
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200280 %d
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200281 setl linebreak vartabstop=10,20,30,40
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200282 call setline(1, "\tx\tx\tx\tx")
283
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200284 let expect = [' x ',
285 \ 'x x ',
286 \ 'x ']
287 let lines = ScreenLines([1, 3], winwidth(0))
288 call s:compare_lines(expect, lines)
289 setl list listchars=tab:>-
290 let expect = ['>---------x>------------------ ',
291 \ 'x>------------------x>------------------',
292 \ 'x ']
293 let lines = ScreenLines([1, 3], winwidth(0))
294 call s:compare_lines(expect, lines)
295 setl linebreak vartabstop=40
296 let expect = ['>---------------------------------------',
297 \ 'x>--------------------------------------',
298 \ 'x>--------------------------------------',
299 \ 'x>--------------------------------------',
300 \ 'x ']
301 let lines = ScreenLines([1, 5], winwidth(0))
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200302 call s:compare_lines(expect, lines)
303
304 " cleanup
305 bw!
306 bw!
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200307 set nolist listchars&vim
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200308endfunc
Bram Moolenaar64f41072018-10-15 22:51:50 +0200309
Bram Moolenaarf9514162018-11-22 03:08:29 +0100310func Test_vartabs_shiftwidth()
311 "return
312 if winwidth(0) < 40
313 return
314 endif
315 new
316 40vnew
317 %d
318" setl varsofttabstop=10,20,30,40
319 setl shiftwidth=0 vartabstop=10,20,30,40
320 call setline(1, "x")
321
322 " Check without any change.
323 let expect = ['x ']
324 let lines = ScreenLines(1, winwidth(0))
325 call s:compare_lines(expect, lines)
326 " Test 1:
327 " shiftwidth depends on the indent, first check with cursor at the end of the
328 " line (which is the same as the start of the line, since there is only one
329 " character).
330 norm! $>>
331 let expect1 = [' x ']
332 let lines = ScreenLines(1, winwidth(0))
333 call s:compare_lines(expect1, lines)
334 call assert_equal(10, shiftwidth())
335 call assert_equal(10, shiftwidth(1))
336 call assert_equal(20, shiftwidth(virtcol('.')))
337 norm! $>>
338 let expect2 = [' x ', '~ ']
339 let lines = ScreenLines([1, 2], winwidth(0))
340 call s:compare_lines(expect2, lines)
341 call assert_equal(20, shiftwidth(virtcol('.')-2))
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200342 call assert_equal(30, virtcol('.')->shiftwidth())
Bram Moolenaarf9514162018-11-22 03:08:29 +0100343 norm! $>>
344 let expect3 = [' ', ' x ', '~ ']
345 let lines = ScreenLines([1, 3], winwidth(0))
346 call s:compare_lines(expect3, lines)
347 call assert_equal(30, shiftwidth(virtcol('.')-2))
348 call assert_equal(40, shiftwidth(virtcol('.')))
349 norm! $>>
350 let expect4 = [' ', ' ', ' x ']
351 let lines = ScreenLines([1, 3], winwidth(0))
352 call assert_equal(40, shiftwidth(virtcol('.')))
353 call s:compare_lines(expect4, lines)
354
355 " Test 2: Put the cursor at the first column, result should be the same
356 call setline(1, "x")
357 norm! 0>>
358 let lines = ScreenLines(1, winwidth(0))
359 call s:compare_lines(expect1, lines)
360 norm! 0>>
361 let lines = ScreenLines([1, 2], winwidth(0))
362 call s:compare_lines(expect2, lines)
363 norm! 0>>
364 let lines = ScreenLines([1, 3], winwidth(0))
365 call s:compare_lines(expect3, lines)
366 norm! 0>>
367 let lines = ScreenLines([1, 3], winwidth(0))
368 call s:compare_lines(expect4, lines)
369
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100370 call assert_fails('call shiftwidth([])', 'E745:')
371
Bram Moolenaarf9514162018-11-22 03:08:29 +0100372 " cleanup
373 bw!
374 bw!
375endfunc
376
Bram Moolenaar64f41072018-10-15 22:51:50 +0200377func Test_vartabs_failures()
378 call assert_fails('set vts=8,')
379 call assert_fails('set vsts=8,')
380 call assert_fails('set vts=8,,8')
381 call assert_fails('set vsts=8,,8')
382 call assert_fails('set vts=8,,8,')
383 call assert_fails('set vsts=8,,8,')
384 call assert_fails('set vts=,8')
385 call assert_fails('set vsts=,8')
386endfunc
Bram Moolenaar037c54f2019-04-20 23:47:46 +0200387
388func Test_vartabs_reset()
389 set vts=8
390 set all&
391 call assert_equal('', &vts)
392endfunc
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100393
394func s:SaveCol(l)
395 call add(a:l, [col('.'), virtcol('.')])
396 return ''
397endfunc
398
399" Test for 'varsofttabstop'
400func Test_varsofttabstop()
401 new
402 inoremap <expr> <F2> s:SaveCol(g:cols)
403
404 set backspace=indent,eol,start
405 set varsofttabstop=6,2,5,3
406 let g:cols = []
407 call feedkeys("a\t\<F2>\t\<F2>\t\<F2>\t\<F2> ", 'xt')
408 call assert_equal("\t\t ", getline(1))
409 call assert_equal([[7, 7], [2, 9], [7, 14], [3, 17]], g:cols)
410
411 let g:cols = []
412 call feedkeys("a\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>", 'xt')
413 call assert_equal('', getline(1))
414 call assert_equal([[3, 17], [7, 14], [2, 9], [7, 7], [1, 1]], g:cols)
415
416 set varsofttabstop&
417 set backspace&
418 iunmap <F2>
419 close!
420endfunc
421
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200422" Setting 'shiftwidth' to a negative value, should set it to either the value
423" of 'tabstop' (if 'vartabstop' is not set) or to the first value in
424" 'vartabstop'
425func Test_shiftwidth_vartabstop()
426 setlocal tabstop=7 vartabstop=
427 call assert_fails('set shiftwidth=-1', 'E487:')
428 call assert_equal(7, &shiftwidth)
429 setlocal tabstop=7 vartabstop=5,7,10
430 call assert_fails('set shiftwidth=-1', 'E487:')
431 call assert_equal(5, &shiftwidth)
432 setlocal shiftwidth& vartabstop& tabstop&
433endfunc
434
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100435" vim: shiftwidth=2 sts=2 expandtab