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