blob: 640aa0b305d3223084858b08438619d9dd59e634 [file] [log] [blame]
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001" Test for variable tabstops
2
3if !has("vartabs")
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02004 throw 'Skipped, vartabs feature missing'
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005endif
6
Bram Moolenaara87b72c2018-06-25 21:24:51 +02007source view_util.vim
Bram Moolenaar037c54f2019-04-20 23:47:46 +02008
Bram Moolenaar64f41072018-10-15 22:51:50 +02009func s:compare_lines(expect, actual)
Bram Moolenaara87b72c2018-06-25 21:24:51 +020010 call assert_equal(join(a:expect, "\n"), join(a:actual, "\n"))
Bram Moolenaar64f41072018-10-15 22:51:50 +020011endfunc
Bram Moolenaara87b72c2018-06-25 21:24:51 +020012
Bram Moolenaar64f41072018-10-15 22:51:50 +020013func Test_vartabs()
Bram Moolenaar04958cb2018-06-23 19:23:02 +020014 new
15 %d
16
17 " Test normal operation of tabstops ...
18 set ts=4
19 call setline(1, join(split('aaaaa', '\zs'), "\t"))
20 retab 8
21 let expect = "a a\<tab>a a\<tab>a"
22 call assert_equal(expect, getline(1))
23
24 " ... and softtabstops
25 set ts=8 sts=6
26 exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
27 let expect = "b b\<tab> b\<tab> b\<tab>b"
28 call assert_equal(expect, getline(1))
29
30 " Test variable tabstops.
31 set sts=0 vts=4,8,4,8
32 exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
33 retab 8
34 let expect = "c c\<tab> c\<tab>c\<tab>c\<tab>c"
35 call assert_equal(expect, getline(1))
36
37 set et vts=4,8,4,8
38 exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
39 let expect = "d d d d d d"
40 call assert_equal(expect, getline(1))
41
42 " Changing ts should have no effect if vts is in use.
43 call cursor(1, 1)
44 set ts=6
45 exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
46 let expect = "e e e e e e"
47 call assert_equal(expect, getline(1))
48
49 " Clearing vts should revert to using ts.
50 set vts=
51 exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
52 let expect = "f f f f f f"
53 call assert_equal(expect, getline(1))
54
55 " Test variable softtabstops.
56 set noet ts=8 vsts=12,2,6
57 exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
58 let expect = "g\<tab> g g\<tab> g\<tab> g\<tab>g"
59 call assert_equal(expect, getline(1))
60
61 " Variable tabstops and softtabstops combined.
62 set vsts=6,12,8 vts=4,6,8
63 exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
64 let expect = "h\<tab> h\<tab>\<tab>h\<tab>h\<tab>h"
65 call assert_equal(expect, getline(1))
66
67 " Retab with a single value, not using vts.
68 set ts=8 sts=0 vts= vsts=
69 exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
70 retab 4
71 let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
72 call assert_equal(expect, getline(1))
73
74 " Retab with a single value, using vts.
75 set ts=8 sts=0 vts=6 vsts=
76 exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
77 retab 4
78 let expect = "j\<tab> j\<tab>\<tab>j\<tab> j\<tab>\<tab>j"
79 call assert_equal(expect, getline(1))
80
81 " Retab with multiple values, not using vts.
82 set ts=6 sts=0 vts= vsts=
83 exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
84 retab 4,8
85 let expect = "k\<tab> k\<tab>k k\<tab> k\<tab> k"
86 call assert_equal(expect, getline(1))
87
88 " Retab with multiple values, using vts.
89 set ts=8 sts=0 vts=6 vsts=
90 exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
91 retab 4,8
92 let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l"
93 call assert_equal(expect, getline(1))
94
95 " Check that global and local values are set.
96 set ts=4 vts=6 sts=8 vsts=10
97 call assert_equal(&ts, 4)
98 call assert_equal(&vts, '6')
99 call assert_equal(&sts, 8)
100 call assert_equal(&vsts, '10')
101 new
102 call assert_equal(&ts, 4)
103 call assert_equal(&vts, '6')
104 call assert_equal(&sts, 8)
105 call assert_equal(&vsts, '10')
106 bwipeout!
107
108 " Check that local values only are set.
109 setlocal ts=5 vts=7 sts=9 vsts=11
110 call assert_equal(&ts, 5)
111 call assert_equal(&vts, '7')
112 call assert_equal(&sts, 9)
113 call assert_equal(&vsts, '11')
114 new
115 call assert_equal(&ts, 4)
116 call assert_equal(&vts, '6')
117 call assert_equal(&sts, 8)
118 call assert_equal(&vsts, '10')
119 bwipeout!
120
121 " Check that global values only are set.
122 setglobal ts=6 vts=8 sts=10 vsts=12
123 call assert_equal(&ts, 5)
124 call assert_equal(&vts, '7')
125 call assert_equal(&sts, 9)
126 call assert_equal(&vsts, '11')
127 new
128 call assert_equal(&ts, 6)
129 call assert_equal(&vts, '8')
130 call assert_equal(&sts, 10)
131 call assert_equal(&vsts, '12')
132 bwipeout!
133
134 set ts& vts& sts& vsts& et&
135 bwipeout!
136endfunc
137
Bram Moolenaar1e115362019-01-09 23:01:02 +0100138func Test_vartabs_breakindent()
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200139 if !exists("+breakindent")
140 return
141 endif
142 new
143 %d
144
145 " Test normal operation of tabstops ...
146 set ts=4
147 call setline(1, join(split('aaaaa', '\zs'), "\t"))
148 retab 8
149 let expect = "a a\<tab>a a\<tab>a"
150 call assert_equal(expect, getline(1))
151
152 " ... and softtabstops
153 set ts=8 sts=6
154 exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
155 let expect = "b b\<tab> b\<tab> b\<tab>b"
156 call assert_equal(expect, getline(1))
157
158 " Test variable tabstops.
159 set sts=0 vts=4,8,4,8
160 exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
161 retab 8
162 let expect = "c c\<tab> c\<tab>c\<tab>c\<tab>c"
163 call assert_equal(expect, getline(1))
164
165 set et vts=4,8,4,8
166 exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
167 let expect = "d d d d d d"
168 call assert_equal(expect, getline(1))
169
170 " Changing ts should have no effect if vts is in use.
171 call cursor(1, 1)
172 set ts=6
173 exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
174 let expect = "e e e e e e"
175 call assert_equal(expect, getline(1))
176
177 " Clearing vts should revert to using ts.
178 set vts=
179 exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
180 let expect = "f f f f f f"
181 call assert_equal(expect, getline(1))
182
183 " Test variable softtabstops.
184 set noet ts=8 vsts=12,2,6
185 exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
186 let expect = "g\<tab> g g\<tab> g\<tab> g\<tab>g"
187 call assert_equal(expect, getline(1))
188
189 " Variable tabstops and softtabstops combined.
190 set vsts=6,12,8 vts=4,6,8
191 exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
192 let expect = "h\<tab> h\<tab>\<tab>h\<tab>h\<tab>h"
193 call assert_equal(expect, getline(1))
194
195 " Retab with a single value, not using vts.
196 set ts=8 sts=0 vts= vsts=
197 exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
198 retab 4
199 let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
200 call assert_equal(expect, getline(1))
201
202 " Retab with a single value, using vts.
203 set ts=8 sts=0 vts=6 vsts=
204 exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
205 retab 4
206 let expect = "j\<tab> j\<tab>\<tab>j\<tab> j\<tab>\<tab>j"
207 call assert_equal(expect, getline(1))
208
209 " Retab with multiple values, not using vts.
210 set ts=6 sts=0 vts= vsts=
211 exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
212 retab 4,8
213 let expect = "k\<tab> k\<tab>k k\<tab> k\<tab> k"
214 call assert_equal(expect, getline(1))
215
216 " Retab with multiple values, using vts.
217 set ts=8 sts=0 vts=6 vsts=
218 exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
219 retab 4,8
220 let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l"
221 call assert_equal(expect, getline(1))
222
223 " Check that global and local values are set.
224 set ts=4 vts=6 sts=8 vsts=10
225 call assert_equal(&ts, 4)
226 call assert_equal(&vts, '6')
227 call assert_equal(&sts, 8)
228 call assert_equal(&vsts, '10')
229 new
230 call assert_equal(&ts, 4)
231 call assert_equal(&vts, '6')
232 call assert_equal(&sts, 8)
233 call assert_equal(&vsts, '10')
234 bwipeout!
235
236 " Check that local values only are set.
237 setlocal ts=5 vts=7 sts=9 vsts=11
238 call assert_equal(&ts, 5)
239 call assert_equal(&vts, '7')
240 call assert_equal(&sts, 9)
241 call assert_equal(&vsts, '11')
242 new
243 call assert_equal(&ts, 4)
244 call assert_equal(&vts, '6')
245 call assert_equal(&sts, 8)
246 call assert_equal(&vsts, '10')
247 bwipeout!
248
249 " Check that global values only are set.
250 setglobal ts=6 vts=8 sts=10 vsts=12
251 call assert_equal(&ts, 5)
252 call assert_equal(&vts, '7')
253 call assert_equal(&sts, 9)
254 call assert_equal(&vsts, '11')
255 new
256 call assert_equal(&ts, 6)
257 call assert_equal(&vts, '8')
258 call assert_equal(&sts, 10)
259 call assert_equal(&vsts, '12')
260 bwipeout!
261
262 bwipeout!
263endfunc
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200264
Bram Moolenaar64f41072018-10-15 22:51:50 +0200265func Test_vartabs_linebreak()
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200266 if winwidth(0) < 40
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200267 return
268 endif
269 new
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200270 40vnew
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200271 %d
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200272 setl linebreak vartabstop=10,20,30,40
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200273 call setline(1, "\tx\tx\tx\tx")
274
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200275 let expect = [' x ',
276 \ 'x x ',
277 \ 'x ']
278 let lines = ScreenLines([1, 3], winwidth(0))
279 call s:compare_lines(expect, lines)
280 setl list listchars=tab:>-
281 let expect = ['>---------x>------------------ ',
282 \ 'x>------------------x>------------------',
283 \ 'x ']
284 let lines = ScreenLines([1, 3], winwidth(0))
285 call s:compare_lines(expect, lines)
286 setl linebreak vartabstop=40
287 let expect = ['>---------------------------------------',
288 \ 'x>--------------------------------------',
289 \ 'x>--------------------------------------',
290 \ 'x>--------------------------------------',
291 \ 'x ']
292 let lines = ScreenLines([1, 5], winwidth(0))
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200293 call s:compare_lines(expect, lines)
294
295 " cleanup
296 bw!
297 bw!
Bram Moolenaar307ac5c2018-06-28 22:23:00 +0200298 set nolist listchars&vim
Bram Moolenaara87b72c2018-06-25 21:24:51 +0200299endfunc
Bram Moolenaar64f41072018-10-15 22:51:50 +0200300
Bram Moolenaarf9514162018-11-22 03:08:29 +0100301func Test_vartabs_shiftwidth()
302 "return
303 if winwidth(0) < 40
304 return
305 endif
306 new
307 40vnew
308 %d
309" setl varsofttabstop=10,20,30,40
310 setl shiftwidth=0 vartabstop=10,20,30,40
311 call setline(1, "x")
312
313 " Check without any change.
314 let expect = ['x ']
315 let lines = ScreenLines(1, winwidth(0))
316 call s:compare_lines(expect, lines)
317 " Test 1:
318 " shiftwidth depends on the indent, first check with cursor at the end of the
319 " line (which is the same as the start of the line, since there is only one
320 " character).
321 norm! $>>
322 let expect1 = [' x ']
323 let lines = ScreenLines(1, winwidth(0))
324 call s:compare_lines(expect1, lines)
325 call assert_equal(10, shiftwidth())
326 call assert_equal(10, shiftwidth(1))
327 call assert_equal(20, shiftwidth(virtcol('.')))
328 norm! $>>
329 let expect2 = [' x ', '~ ']
330 let lines = ScreenLines([1, 2], winwidth(0))
331 call s:compare_lines(expect2, lines)
332 call assert_equal(20, shiftwidth(virtcol('.')-2))
333 call assert_equal(30, shiftwidth(virtcol('.')))
334 norm! $>>
335 let expect3 = [' ', ' x ', '~ ']
336 let lines = ScreenLines([1, 3], winwidth(0))
337 call s:compare_lines(expect3, lines)
338 call assert_equal(30, shiftwidth(virtcol('.')-2))
339 call assert_equal(40, shiftwidth(virtcol('.')))
340 norm! $>>
341 let expect4 = [' ', ' ', ' x ']
342 let lines = ScreenLines([1, 3], winwidth(0))
343 call assert_equal(40, shiftwidth(virtcol('.')))
344 call s:compare_lines(expect4, lines)
345
346 " Test 2: Put the cursor at the first column, result should be the same
347 call setline(1, "x")
348 norm! 0>>
349 let lines = ScreenLines(1, winwidth(0))
350 call s:compare_lines(expect1, lines)
351 norm! 0>>
352 let lines = ScreenLines([1, 2], winwidth(0))
353 call s:compare_lines(expect2, lines)
354 norm! 0>>
355 let lines = ScreenLines([1, 3], winwidth(0))
356 call s:compare_lines(expect3, lines)
357 norm! 0>>
358 let lines = ScreenLines([1, 3], winwidth(0))
359 call s:compare_lines(expect4, lines)
360
361 " cleanup
362 bw!
363 bw!
364endfunc
365
Bram Moolenaar64f41072018-10-15 22:51:50 +0200366func Test_vartabs_failures()
367 call assert_fails('set vts=8,')
368 call assert_fails('set vsts=8,')
369 call assert_fails('set vts=8,,8')
370 call assert_fails('set vsts=8,,8')
371 call assert_fails('set vts=8,,8,')
372 call assert_fails('set vsts=8,,8,')
373 call assert_fails('set vts=,8')
374 call assert_fails('set vsts=,8')
375endfunc
Bram Moolenaar037c54f2019-04-20 23:47:46 +0200376
377func Test_vartabs_reset()
378 set vts=8
379 set all&
380 call assert_equal('', &vts)
381endfunc