blob: 97c44b8bb5d236aeade92f7b36663a663b42661f [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 Moolenaarb18b4962022-09-02 21:55:50 +0100164 call writefile(['# vim: indentexpr=GetIndent()'], 'Xmlfile.txt')
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!
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100175 call delete('Xmlfile.txt')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100176endfunc
177
Christian Brabandt818ff252021-11-18 13:56:37 +0000178func Test_indent_func_with_gq()
179
180 function GetTeXIndent()
181 " Sample indent expression for TeX files
182 let lnum = prevnonblank(v:lnum - 1)
183 " At the start of the file use zero indent.
184 if lnum == 0
185 return 0
186 endif
187 let line = getline(lnum)
188 let ind = indent(lnum)
189 " Add a 'shiftwidth' after beginning of environments.
190 if line =~ '\\begin{center}'
191 let ind = ind + shiftwidth()
192 endif
193 return ind
194 endfunction
195
196 new
197 setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
198 put =[ '\documentclass{article}', '', '\begin{document}', '',
199 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
200 \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
201 \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
202 \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
203 \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
204 \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
205 \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
206 \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
207 \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000208 \ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
209 \ 'Donec id semper purus.',
Christian Brabandt818ff252021-11-18 13:56:37 +0000210 \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
211 \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
212 \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
213 \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
214 \ 'ultrices posuere cubilia curae;', '', '']
215 1d_
216 call cursor(5, 1)
217 ka
Bram Moolenaarecabb512021-12-06 19:51:01 +0000218 call cursor(14, 1)
Christian Brabandt818ff252021-11-18 13:56:37 +0000219 kb
220 norm! 'agqap
Bram Moolenaarecabb512021-12-06 19:51:01 +0000221 norm! 'bgqG
Christian Brabandt818ff252021-11-18 13:56:37 +0000222 let expected = [ '\documentclass{article}', '', '\begin{document}', '',
223 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
224 \ 'elit. Fusce ut enim non libero efficitur aliquet.',
225 \ 'Maecenas metus justo, facilisis convallis blandit',
226 \ 'non, semper eu urna. Suspendisse diam diam,',
227 \ 'iaculis faucibus lorem eu, fringilla condimentum',
228 \ 'lectus. Quisque euismod diam at convallis',
229 \ 'vulputate. Pellentesque laoreet tortor sit amet',
230 \ 'mauris euismod ornare. Sed varius bibendum orci',
231 \ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
232 \ 'accumsan, metus lectus ultrices odio, sed',
233 \ 'elementum mi ante at arcu.', '', '\begin{center}', '',
234 \ ' Proin nec risus consequat nunc dapibus',
235 \ ' consectetur. Mauris lacinia est a augue',
236 \ ' tristique accumsan. Morbi pretium, felis',
237 \ ' molestie eleifend condimentum, arcu ipsum congue',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000238 \ ' nisl, quis euismod purus libero in ante.',
239 \ '',
240 \ ' Donec id semper purus. Suspendisse eget aliquam',
241 \ ' nunc. Maecenas fringilla mauris vitae maximus',
Christian Brabandt818ff252021-11-18 13:56:37 +0000242 \ ' condimentum. Cras a quam in mi dictum eleifend',
243 \ ' at a lorem. Sed convallis ante a commodo',
244 \ ' facilisis. Nam suscipit vulputate odio, vel',
245 \ ' dapibus nisl dignissim facilisis. Vestibulum',
246 \ ' ante ipsum primis in faucibus orci luctus et',
247 \ ' ultrices posuere cubilia curae;', '', '']
248 call assert_equal(expected, getline(1, '$'))
249
250 bwipe!
251 delmark ab
252 delfunction GetTeXIndent
253endfu
254
Bram Moolenaarecabb512021-12-06 19:51:01 +0000255func Test_formatting_keeps_first_line_indent()
256 let lines =<< trim END
257 foo()
258 {
259 int x; // manually positioned
260 // more text that will be formatted
261 // but not reindented
262 END
263 new
264 call setline(1, lines)
265 setlocal sw=4 cindent tw=45 et
266 normal! 4Ggqj
267 let expected =<< trim END
268 foo()
269 {
270 int x; // manually positioned
271 // more text that will be
272 // formatted but not
273 // reindented
274 END
275 call assert_equal(expected, getline(1, '$'))
276 bwipe!
277endfunc
278
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100279" vim: shiftwidth=2 sts=2 expandtab