blob: e20694792cc0f69a7228c332fb75c55da43cb07d [file] [log] [blame]
Bram Moolenaarbd7206e2020-03-06 20:36:04 +01001" Test for various indent options
2
3func Test_preserveindent()
4 new
5 " Test for autoindent copying indent from the previous line
6 setlocal autoindent
7 call setline(1, [repeat(' ', 16) .. 'line1'])
8 call feedkeys("A\nline2", 'xt')
9 call assert_equal("\t\tline2", getline(2))
10 setlocal autoindent&
11
12 " Test for using CTRL-T with and without 'preserveindent'
13 set shiftwidth=4
14 call cursor(1, 1)
15 call setline(1, " \t ")
16 call feedkeys("Al\<C-T>", 'xt')
17 call assert_equal("\t\tl", getline(1))
18 set preserveindent
19 call setline(1, " \t ")
20 call feedkeys("Al\<C-T>", 'xt')
21 call assert_equal(" \t \tl", getline(1))
22 set pi& sw&
23
24 " Test for using CTRL-T with 'expandtab' and 'preserveindent'
25 call cursor(1, 1)
26 call setline(1, "\t \t")
27 set shiftwidth=4 expandtab preserveindent
28 call feedkeys("Al\<C-T>", 'xt')
29 call assert_equal("\t \t l", getline(1))
30 set sw& et& pi&
31
32 close!
33endfunc
34
35" Test for indent()
36func Test_indent_func()
37 call assert_equal(-1, indent(-1))
38 new
39 call setline(1, "\tabc")
40 call assert_equal(8, indent(1))
41 call setline(1, " abc")
42 call assert_equal(4, indent(1))
43 call setline(1, " \t abc")
44 call assert_equal(12, indent(1))
45 close!
46endfunc
47
48" Test for reindenting a line using the '=' operator
49func Test_reindent()
50 new
51 call setline(1, 'abc')
52 set nomodifiable
53 call assert_fails('normal ==', 'E21:')
54 set modifiable
55
56 call setline(1, ['foo', 'bar'])
57 call feedkeys('ggVG=', 'xt')
58 call assert_equal(['foo', 'bar'], getline(1, 2))
59 close!
60endfunc
61
Bram Moolenaare4686982022-04-19 18:28:45 +010062" Test indent operator creating one undo entry
63func Test_indent_operator_undo()
64 enew
65 call setline(1, range(12)->map('"\t" .. v:val'))
66 func FoldExpr()
67 let g:foldcount += 1
68 return '='
69 endfunc
70 set foldmethod=expr foldexpr=FoldExpr()
71 let g:foldcount = 0
72 redraw
73 call assert_equal(12, g:foldcount)
74 normal gg=G
75 call assert_equal(24, g:foldcount)
76 undo
77 call assert_equal(38, g:foldcount)
78
79 bwipe!
80 set foldmethod& foldexpr=
81 delfunc FoldExpr
82 unlet g:foldcount
83endfunc
84
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010085" Test for shifting a line with a preprocessor directive ('#')
86func Test_preproc_indent()
87 new
88 set sw=4
89 call setline(1, '#define FOO 1')
90 normal >>
91 call assert_equal(' #define FOO 1', getline(1))
92
93 " with 'smartindent'
94 call setline(1, '#define FOO 1')
95 set smartindent
96 normal >>
97 call assert_equal('#define FOO 1', getline(1))
98 set smartindent&
99
100 " with 'cindent'
101 set cindent
102 normal >>
103 call assert_equal('#define FOO 1', getline(1))
104 set cindent&
105
106 close!
107endfunc
108
Anttoni Erkkiläf4d87ff2025-03-09 16:07:15 +0100109func Test_userlabel_indent()
110 new
111 call setline(1, ['{', 'label:'])
112 normal GV=
113 call assert_equal('label:', getline(2))
114
115 call setline(2, 'läbél:')
116 normal GV=
117 call assert_equal('läbél:', getline(2))
118
119 close!
120endfunc
121
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100122" Test for 'copyindent'
123func Test_copyindent()
124 new
125 set shiftwidth=4 autoindent expandtab copyindent
126 call setline(1, " \t abc")
127 call feedkeys("ol", 'xt')
128 call assert_equal(" \t l", getline(2))
129 set noexpandtab
130 call setline(1, " \t abc")
131 call feedkeys("ol", 'xt')
132 call assert_equal(" \t l", getline(2))
133 set sw& ai& et& ci&
134 close!
135endfunc
136
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100137" Test for changing multiple lines with lisp indent
138func Test_lisp_indent_change_multiline()
139 new
140 setlocal lisp autoindent
141 call setline(1, ['(if a', ' (if b', ' (return 5)))'])
142 normal! jc2j(return 4))
143 call assert_equal(' (return 4))', getline(2))
144 close!
145endfunc
146
147func Test_lisp_indent()
148 new
149 setlocal lisp autoindent
150 call setline(1, ['(if a', ' ;; comment', ' \ abc', '', ' " str1\', ' " st\b', ' (return 5)'])
151 normal! jo;; comment
152 normal! jo\ abc
153 normal! jo;; ret
154 normal! jostr1"
155 normal! jostr2"
156 call assert_equal([' ;; comment', ' ;; comment', ' \ abc', ' \ abc', '', ' ;; ret', ' " str1\', ' str1"', ' " st\b', ' str2"'], getline(2, 11))
157 close!
158endfunc
159
Bram Moolenaar0e8e9382022-06-18 12:51:11 +0100160func Test_lisp_indent_quoted()
161 " This was going past the end of the line
162 new
163 setlocal lisp autoindent
164 call setline(1, ['"[', '='])
165 normal Gvk=
166
167 bwipe!
168endfunc
169
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100170" Test for setting the 'indentexpr' from a modeline
171func Test_modeline_indent_expr()
172 let modeline = &modeline
173 set modeline
174 func GetIndent()
175 return line('.') * 2
176 endfunc
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100177 call writefile(['# vim: indentexpr=GetIndent()'], 'Xmlfile.txt', 'D')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100178 set modelineexpr
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100179 new Xmlfile.txt
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100180 call assert_equal('GetIndent()', &indentexpr)
181 exe "normal Oa\nb\n"
182 call assert_equal([' a', ' b'], getline(1, 2))
Bram Moolenaar95e59a32020-03-19 20:33:33 +0100183
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100184 set modelineexpr&
185 delfunc GetIndent
186 let &modeline = modeline
187 close!
188endfunc
189
Christian Brabandt818ff252021-11-18 13:56:37 +0000190func Test_indent_func_with_gq()
Bram Moolenaar94722c52023-01-28 19:19:03 +0000191
Christian Brabandt818ff252021-11-18 13:56:37 +0000192 function GetTeXIndent()
193 " Sample indent expression for TeX files
194 let lnum = prevnonblank(v:lnum - 1)
195 " At the start of the file use zero indent.
196 if lnum == 0
197 return 0
198 endif
199 let line = getline(lnum)
200 let ind = indent(lnum)
201 " Add a 'shiftwidth' after beginning of environments.
Bram Moolenaar94722c52023-01-28 19:19:03 +0000202 if line =~ '\\begin{center}'
Christian Brabandt818ff252021-11-18 13:56:37 +0000203 let ind = ind + shiftwidth()
204 endif
205 return ind
206 endfunction
207
208 new
209 setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
210 put =[ '\documentclass{article}', '', '\begin{document}', '',
211 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
212 \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
213 \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
214 \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
215 \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
216 \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
217 \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
218 \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
219 \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000220 \ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
221 \ 'Donec id semper purus.',
Christian Brabandt818ff252021-11-18 13:56:37 +0000222 \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
223 \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
224 \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
225 \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
226 \ 'ultrices posuere cubilia curae;', '', '']
227 1d_
228 call cursor(5, 1)
229 ka
Bram Moolenaarecabb512021-12-06 19:51:01 +0000230 call cursor(14, 1)
Christian Brabandt818ff252021-11-18 13:56:37 +0000231 kb
232 norm! 'agqap
Bram Moolenaarecabb512021-12-06 19:51:01 +0000233 norm! 'bgqG
Christian Brabandt818ff252021-11-18 13:56:37 +0000234 let expected = [ '\documentclass{article}', '', '\begin{document}', '',
235 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
236 \ 'elit. Fusce ut enim non libero efficitur aliquet.',
237 \ 'Maecenas metus justo, facilisis convallis blandit',
238 \ 'non, semper eu urna. Suspendisse diam diam,',
239 \ 'iaculis faucibus lorem eu, fringilla condimentum',
240 \ 'lectus. Quisque euismod diam at convallis',
241 \ 'vulputate. Pellentesque laoreet tortor sit amet',
242 \ 'mauris euismod ornare. Sed varius bibendum orci',
243 \ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
244 \ 'accumsan, metus lectus ultrices odio, sed',
245 \ 'elementum mi ante at arcu.', '', '\begin{center}', '',
246 \ ' Proin nec risus consequat nunc dapibus',
247 \ ' consectetur. Mauris lacinia est a augue',
248 \ ' tristique accumsan. Morbi pretium, felis',
249 \ ' molestie eleifend condimentum, arcu ipsum congue',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000250 \ ' nisl, quis euismod purus libero in ante.',
251 \ '',
252 \ ' Donec id semper purus. Suspendisse eget aliquam',
253 \ ' nunc. Maecenas fringilla mauris vitae maximus',
Christian Brabandt818ff252021-11-18 13:56:37 +0000254 \ ' condimentum. Cras a quam in mi dictum eleifend',
255 \ ' at a lorem. Sed convallis ante a commodo',
256 \ ' facilisis. Nam suscipit vulputate odio, vel',
257 \ ' dapibus nisl dignissim facilisis. Vestibulum',
258 \ ' ante ipsum primis in faucibus orci luctus et',
259 \ ' ultrices posuere cubilia curae;', '', '']
260 call assert_equal(expected, getline(1, '$'))
261
262 bwipe!
263 delmark ab
Bram Moolenaar94722c52023-01-28 19:19:03 +0000264 delfunction GetTeXIndent
Christian Brabandt818ff252021-11-18 13:56:37 +0000265endfu
266
Bram Moolenaarecabb512021-12-06 19:51:01 +0000267func Test_formatting_keeps_first_line_indent()
268 let lines =<< trim END
269 foo()
270 {
271 int x; // manually positioned
272 // more text that will be formatted
273 // but not reindented
274 END
275 new
276 call setline(1, lines)
277 setlocal sw=4 cindent tw=45 et
278 normal! 4Ggqj
279 let expected =<< trim END
280 foo()
281 {
282 int x; // manually positioned
283 // more text that will be
284 // formatted but not
285 // reindented
286 END
287 call assert_equal(expected, getline(1, '$'))
288 bwipe!
289endfunc
290
Christian Brabandt6bf13182023-11-14 22:42:59 +0100291" Test for indenting with large amount, causes overflow
292func Test_indent_overflow_count()
293 new
294 setl sw=8
295 call setline(1, "abc")
296 norm! V2147483647>
297 " indents by INT_MAX
298 call assert_equal(2147483647, indent(1))
299 close!
300endfunc
301
Christian Brabandt37705742023-11-22 22:18:35 +0100302func Test_indent_overflow_count2()
303 new
304 " this only works, when long is 64bits
305 try
306 setl sw=0x180000000
307 catch /^Vim\%((\a\+)\)\=:E487:/
308 throw 'Skipped: value negative on this platform'
309 endtry
310 call setline(1, "\tabc")
311 norm! <<
312 call assert_equal(0, indent(1))
313 close!
314endfunc
315
zeertzjq6c302772024-12-17 20:26:45 +0100316" Test that mouse shape is restored to Normal mode after using "gq" when
317" 'indentexpr' executes :normal.
zeertzjqf25d8f92024-12-18 21:12:25 +0100318func Test_mouse_shape_indent_norm_with_gq()
zeertzjq6c302772024-12-17 20:26:45 +0100319 CheckFeature mouseshape
320 CheckCanRunGui
321
322 let lines =<< trim END
323 func Indent()
324 exe "normal! \<Ignore>"
325 return 0
326 endfunc
327
328 setlocal indentexpr=Indent()
329 END
330 call writefile(lines, 'Xindentexpr.vim', 'D')
331
332 let lines =<< trim END
333 vim9script
334 var mouse_shapes = []
335
336 setline(1, [repeat('a', 80), repeat('b', 80)])
337
338 feedkeys('ggVG')
339 timer_start(50, (_) => {
340 mouse_shapes += [getmouseshape()]
341 timer_start(50, (_) => {
342 feedkeys('gq')
343 timer_start(50, (_) => {
344 mouse_shapes += [getmouseshape()]
345 timer_start(50, (_) => {
346 writefile(mouse_shapes, 'Xmouseshapes')
347 quit!
348 })
349 })
350 })
351 })
352 END
353 call writefile(lines, 'Xmouseshape.vim', 'D')
354
355 call RunVim([], [], "-g -S Xindentexpr.vim -S Xmouseshape.vim")
356 call WaitForAssert({-> assert_equal(['rightup-arrow', 'arrow'],
357 \ readfile('Xmouseshapes'))}, 300)
358
359 call delete('Xmouseshapes')
360endfunc
361
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100362" vim: shiftwidth=2 sts=2 expandtab