blob: 96e9d2300883c9ee9eb5d30ade4500b0880b66c9 [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
109" Test for 'copyindent'
110func Test_copyindent()
111 new
112 set shiftwidth=4 autoindent expandtab copyindent
113 call setline(1, " \t abc")
114 call feedkeys("ol", 'xt')
115 call assert_equal(" \t l", getline(2))
116 set noexpandtab
117 call setline(1, " \t abc")
118 call feedkeys("ol", 'xt')
119 call assert_equal(" \t l", getline(2))
120 set sw& ai& et& ci&
121 close!
122endfunc
123
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100124" Test for changing multiple lines with lisp indent
125func Test_lisp_indent_change_multiline()
126 new
127 setlocal lisp autoindent
128 call setline(1, ['(if a', ' (if b', ' (return 5)))'])
129 normal! jc2j(return 4))
130 call assert_equal(' (return 4))', getline(2))
131 close!
132endfunc
133
134func Test_lisp_indent()
135 new
136 setlocal lisp autoindent
137 call setline(1, ['(if a', ' ;; comment', ' \ abc', '', ' " str1\', ' " st\b', ' (return 5)'])
138 normal! jo;; comment
139 normal! jo\ abc
140 normal! jo;; ret
141 normal! jostr1"
142 normal! jostr2"
143 call assert_equal([' ;; comment', ' ;; comment', ' \ abc', ' \ abc', '', ' ;; ret', ' " str1\', ' str1"', ' " st\b', ' str2"'], getline(2, 11))
144 close!
145endfunc
146
Bram Moolenaar0e8e9382022-06-18 12:51:11 +0100147func Test_lisp_indent_quoted()
148 " This was going past the end of the line
149 new
150 setlocal lisp autoindent
151 call setline(1, ['"[', '='])
152 normal Gvk=
153
154 bwipe!
155endfunc
156
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100157" Test for setting the 'indentexpr' from a modeline
158func Test_modeline_indent_expr()
159 let modeline = &modeline
160 set modeline
161 func GetIndent()
162 return line('.') * 2
163 endfunc
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100164 call writefile(['# vim: indentexpr=GetIndent()'], 'Xmlfile.txt', 'D')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100165 set modelineexpr
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100166 new Xmlfile.txt
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100167 call assert_equal('GetIndent()', &indentexpr)
168 exe "normal Oa\nb\n"
169 call assert_equal([' a', ' b'], getline(1, 2))
Bram Moolenaar95e59a32020-03-19 20:33:33 +0100170
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100171 set modelineexpr&
172 delfunc GetIndent
173 let &modeline = modeline
174 close!
175endfunc
176
Christian Brabandt818ff252021-11-18 13:56:37 +0000177func Test_indent_func_with_gq()
Bram Moolenaar94722c52023-01-28 19:19:03 +0000178
Christian Brabandt818ff252021-11-18 13:56:37 +0000179 function GetTeXIndent()
180 " Sample indent expression for TeX files
181 let lnum = prevnonblank(v:lnum - 1)
182 " At the start of the file use zero indent.
183 if lnum == 0
184 return 0
185 endif
186 let line = getline(lnum)
187 let ind = indent(lnum)
188 " Add a 'shiftwidth' after beginning of environments.
Bram Moolenaar94722c52023-01-28 19:19:03 +0000189 if line =~ '\\begin{center}'
Christian Brabandt818ff252021-11-18 13:56:37 +0000190 let ind = ind + shiftwidth()
191 endif
192 return ind
193 endfunction
194
195 new
196 setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
197 put =[ '\documentclass{article}', '', '\begin{document}', '',
198 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
199 \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
200 \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
201 \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
202 \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
203 \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
204 \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
205 \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
206 \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000207 \ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
208 \ 'Donec id semper purus.',
Christian Brabandt818ff252021-11-18 13:56:37 +0000209 \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
210 \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
211 \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
212 \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
213 \ 'ultrices posuere cubilia curae;', '', '']
214 1d_
215 call cursor(5, 1)
216 ka
Bram Moolenaarecabb512021-12-06 19:51:01 +0000217 call cursor(14, 1)
Christian Brabandt818ff252021-11-18 13:56:37 +0000218 kb
219 norm! 'agqap
Bram Moolenaarecabb512021-12-06 19:51:01 +0000220 norm! 'bgqG
Christian Brabandt818ff252021-11-18 13:56:37 +0000221 let expected = [ '\documentclass{article}', '', '\begin{document}', '',
222 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
223 \ 'elit. Fusce ut enim non libero efficitur aliquet.',
224 \ 'Maecenas metus justo, facilisis convallis blandit',
225 \ 'non, semper eu urna. Suspendisse diam diam,',
226 \ 'iaculis faucibus lorem eu, fringilla condimentum',
227 \ 'lectus. Quisque euismod diam at convallis',
228 \ 'vulputate. Pellentesque laoreet tortor sit amet',
229 \ 'mauris euismod ornare. Sed varius bibendum orci',
230 \ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
231 \ 'accumsan, metus lectus ultrices odio, sed',
232 \ 'elementum mi ante at arcu.', '', '\begin{center}', '',
233 \ ' Proin nec risus consequat nunc dapibus',
234 \ ' consectetur. Mauris lacinia est a augue',
235 \ ' tristique accumsan. Morbi pretium, felis',
236 \ ' molestie eleifend condimentum, arcu ipsum congue',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000237 \ ' nisl, quis euismod purus libero in ante.',
238 \ '',
239 \ ' Donec id semper purus. Suspendisse eget aliquam',
240 \ ' nunc. Maecenas fringilla mauris vitae maximus',
Christian Brabandt818ff252021-11-18 13:56:37 +0000241 \ ' condimentum. Cras a quam in mi dictum eleifend',
242 \ ' at a lorem. Sed convallis ante a commodo',
243 \ ' facilisis. Nam suscipit vulputate odio, vel',
244 \ ' dapibus nisl dignissim facilisis. Vestibulum',
245 \ ' ante ipsum primis in faucibus orci luctus et',
246 \ ' ultrices posuere cubilia curae;', '', '']
247 call assert_equal(expected, getline(1, '$'))
248
249 bwipe!
250 delmark ab
Bram Moolenaar94722c52023-01-28 19:19:03 +0000251 delfunction GetTeXIndent
Christian Brabandt818ff252021-11-18 13:56:37 +0000252endfu
253
Bram Moolenaarecabb512021-12-06 19:51:01 +0000254func Test_formatting_keeps_first_line_indent()
255 let lines =<< trim END
256 foo()
257 {
258 int x; // manually positioned
259 // more text that will be formatted
260 // but not reindented
261 END
262 new
263 call setline(1, lines)
264 setlocal sw=4 cindent tw=45 et
265 normal! 4Ggqj
266 let expected =<< trim END
267 foo()
268 {
269 int x; // manually positioned
270 // more text that will be
271 // formatted but not
272 // reindented
273 END
274 call assert_equal(expected, getline(1, '$'))
275 bwipe!
276endfunc
277
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100278" vim: shiftwidth=2 sts=2 expandtab