blob: 0f0f9da8beb280d8f0dd8db7eb0eb0bc5ca89044 [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
112" Test for 'copyindent'
113func Test_copyindent()
114 new
115 set shiftwidth=4 autoindent expandtab copyindent
116 call setline(1, " \t abc")
117 call feedkeys("ol", 'xt')
118 call assert_equal(" \t l", getline(2))
119 set noexpandtab
120 call setline(1, " \t abc")
121 call feedkeys("ol", 'xt')
122 call assert_equal(" \t l", getline(2))
123 set sw& ai& et& ci&
124 close!
125endfunc
126
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100127" Test for changing multiple lines with lisp indent
128func Test_lisp_indent_change_multiline()
129 new
130 setlocal lisp autoindent
131 call setline(1, ['(if a', ' (if b', ' (return 5)))'])
132 normal! jc2j(return 4))
133 call assert_equal(' (return 4))', getline(2))
134 close!
135endfunc
136
137func Test_lisp_indent()
138 new
139 setlocal lisp autoindent
140 call setline(1, ['(if a', ' ;; comment', ' \ abc', '', ' " str1\', ' " st\b', ' (return 5)'])
141 normal! jo;; comment
142 normal! jo\ abc
143 normal! jo;; ret
144 normal! jostr1"
145 normal! jostr2"
146 call assert_equal([' ;; comment', ' ;; comment', ' \ abc', ' \ abc', '', ' ;; ret', ' " str1\', ' str1"', ' " st\b', ' str2"'], getline(2, 11))
147 close!
148endfunc
149
Bram Moolenaar0e8e9382022-06-18 12:51:11 +0100150func Test_lisp_indent_quoted()
151 " This was going past the end of the line
152 new
153 setlocal lisp autoindent
154 call setline(1, ['"[', '='])
155 normal Gvk=
156
157 bwipe!
158endfunc
159
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100160" Test for setting the 'indentexpr' from a modeline
161func Test_modeline_indent_expr()
162 let modeline = &modeline
163 set modeline
164 func GetIndent()
165 return line('.') * 2
166 endfunc
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100167 call writefile(['# vim: indentexpr=GetIndent()'], 'Xmlfile.txt', 'D')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100168 set modelineexpr
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100169 new Xmlfile.txt
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100170 call assert_equal('GetIndent()', &indentexpr)
171 exe "normal Oa\nb\n"
172 call assert_equal([' a', ' b'], getline(1, 2))
Bram Moolenaar95e59a32020-03-19 20:33:33 +0100173
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100174 set modelineexpr&
175 delfunc GetIndent
176 let &modeline = modeline
177 close!
178endfunc
179
Christian Brabandt818ff252021-11-18 13:56:37 +0000180func Test_indent_func_with_gq()
Bram Moolenaar94722c52023-01-28 19:19:03 +0000181
Christian Brabandt818ff252021-11-18 13:56:37 +0000182 function GetTeXIndent()
183 " Sample indent expression for TeX files
184 let lnum = prevnonblank(v:lnum - 1)
185 " At the start of the file use zero indent.
186 if lnum == 0
187 return 0
188 endif
189 let line = getline(lnum)
190 let ind = indent(lnum)
191 " Add a 'shiftwidth' after beginning of environments.
Bram Moolenaar94722c52023-01-28 19:19:03 +0000192 if line =~ '\\begin{center}'
Christian Brabandt818ff252021-11-18 13:56:37 +0000193 let ind = ind + shiftwidth()
194 endif
195 return ind
196 endfunction
197
198 new
199 setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
200 put =[ '\documentclass{article}', '', '\begin{document}', '',
201 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
202 \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
203 \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
204 \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
205 \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
206 \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
207 \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
208 \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
209 \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000210 \ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
211 \ 'Donec id semper purus.',
Christian Brabandt818ff252021-11-18 13:56:37 +0000212 \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
213 \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
214 \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
215 \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
216 \ 'ultrices posuere cubilia curae;', '', '']
217 1d_
218 call cursor(5, 1)
219 ka
Bram Moolenaarecabb512021-12-06 19:51:01 +0000220 call cursor(14, 1)
Christian Brabandt818ff252021-11-18 13:56:37 +0000221 kb
222 norm! 'agqap
Bram Moolenaarecabb512021-12-06 19:51:01 +0000223 norm! 'bgqG
Christian Brabandt818ff252021-11-18 13:56:37 +0000224 let expected = [ '\documentclass{article}', '', '\begin{document}', '',
225 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
226 \ 'elit. Fusce ut enim non libero efficitur aliquet.',
227 \ 'Maecenas metus justo, facilisis convallis blandit',
228 \ 'non, semper eu urna. Suspendisse diam diam,',
229 \ 'iaculis faucibus lorem eu, fringilla condimentum',
230 \ 'lectus. Quisque euismod diam at convallis',
231 \ 'vulputate. Pellentesque laoreet tortor sit amet',
232 \ 'mauris euismod ornare. Sed varius bibendum orci',
233 \ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
234 \ 'accumsan, metus lectus ultrices odio, sed',
235 \ 'elementum mi ante at arcu.', '', '\begin{center}', '',
236 \ ' Proin nec risus consequat nunc dapibus',
237 \ ' consectetur. Mauris lacinia est a augue',
238 \ ' tristique accumsan. Morbi pretium, felis',
239 \ ' molestie eleifend condimentum, arcu ipsum congue',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000240 \ ' nisl, quis euismod purus libero in ante.',
241 \ '',
242 \ ' Donec id semper purus. Suspendisse eget aliquam',
243 \ ' nunc. Maecenas fringilla mauris vitae maximus',
Christian Brabandt818ff252021-11-18 13:56:37 +0000244 \ ' condimentum. Cras a quam in mi dictum eleifend',
245 \ ' at a lorem. Sed convallis ante a commodo',
246 \ ' facilisis. Nam suscipit vulputate odio, vel',
247 \ ' dapibus nisl dignissim facilisis. Vestibulum',
248 \ ' ante ipsum primis in faucibus orci luctus et',
249 \ ' ultrices posuere cubilia curae;', '', '']
250 call assert_equal(expected, getline(1, '$'))
251
252 bwipe!
253 delmark ab
Bram Moolenaar94722c52023-01-28 19:19:03 +0000254 delfunction GetTeXIndent
Christian Brabandt818ff252021-11-18 13:56:37 +0000255endfu
256
Bram Moolenaarecabb512021-12-06 19:51:01 +0000257func Test_formatting_keeps_first_line_indent()
258 let lines =<< trim END
259 foo()
260 {
261 int x; // manually positioned
262 // more text that will be formatted
263 // but not reindented
264 END
265 new
266 call setline(1, lines)
267 setlocal sw=4 cindent tw=45 et
268 normal! 4Ggqj
269 let expected =<< trim END
270 foo()
271 {
272 int x; // manually positioned
273 // more text that will be
274 // formatted but not
275 // reindented
276 END
277 call assert_equal(expected, getline(1, '$'))
278 bwipe!
279endfunc
280
Christian Brabandt6bf13182023-11-14 22:42:59 +0100281" Test for indenting with large amount, causes overflow
282func Test_indent_overflow_count()
283 new
284 setl sw=8
285 call setline(1, "abc")
286 norm! V2147483647>
287 " indents by INT_MAX
288 call assert_equal(2147483647, indent(1))
289 close!
290endfunc
291
Christian Brabandt37705742023-11-22 22:18:35 +0100292func Test_indent_overflow_count2()
293 new
294 " this only works, when long is 64bits
295 try
296 setl sw=0x180000000
297 catch /^Vim\%((\a\+)\)\=:E487:/
298 throw 'Skipped: value negative on this platform'
299 endtry
300 call setline(1, "\tabc")
301 norm! <<
302 call assert_equal(0, indent(1))
303 close!
304endfunc
305
zeertzjq6c302772024-12-17 20:26:45 +0100306" Test that mouse shape is restored to Normal mode after using "gq" when
307" 'indentexpr' executes :normal.
zeertzjqf25d8f92024-12-18 21:12:25 +0100308func Test_mouse_shape_indent_norm_with_gq()
zeertzjq6c302772024-12-17 20:26:45 +0100309 CheckFeature mouseshape
310 CheckCanRunGui
311
312 let lines =<< trim END
313 func Indent()
314 exe "normal! \<Ignore>"
315 return 0
316 endfunc
317
318 setlocal indentexpr=Indent()
319 END
320 call writefile(lines, 'Xindentexpr.vim', 'D')
321
322 let lines =<< trim END
323 vim9script
324 var mouse_shapes = []
325
326 setline(1, [repeat('a', 80), repeat('b', 80)])
327
328 feedkeys('ggVG')
329 timer_start(50, (_) => {
330 mouse_shapes += [getmouseshape()]
331 timer_start(50, (_) => {
332 feedkeys('gq')
333 timer_start(50, (_) => {
334 mouse_shapes += [getmouseshape()]
335 timer_start(50, (_) => {
336 writefile(mouse_shapes, 'Xmouseshapes')
337 quit!
338 })
339 })
340 })
341 })
342 END
343 call writefile(lines, 'Xmouseshape.vim', 'D')
344
345 call RunVim([], [], "-g -S Xindentexpr.vim -S Xmouseshape.vim")
346 call WaitForAssert({-> assert_equal(['rightup-arrow', 'arrow'],
347 \ readfile('Xmouseshapes'))}, 300)
348
349 call delete('Xmouseshapes')
350endfunc
351
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100352" vim: shiftwidth=2 sts=2 expandtab