blob: be55227bd20cb2c3ec24302e7049bce30bd46aab [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
147" Test for setting the 'indentexpr' from a modeline
148func Test_modeline_indent_expr()
149 let modeline = &modeline
150 set modeline
151 func GetIndent()
152 return line('.') * 2
153 endfunc
154 call writefile(['# vim: indentexpr=GetIndent()'], 'Xfile.txt')
155 set modelineexpr
156 new Xfile.txt
157 call assert_equal('GetIndent()', &indentexpr)
158 exe "normal Oa\nb\n"
159 call assert_equal([' a', ' b'], getline(1, 2))
Bram Moolenaar95e59a32020-03-19 20:33:33 +0100160
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100161 set modelineexpr&
162 delfunc GetIndent
163 let &modeline = modeline
164 close!
Bram Moolenaar95e59a32020-03-19 20:33:33 +0100165 call delete('Xfile.txt')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100166endfunc
167
Christian Brabandt818ff252021-11-18 13:56:37 +0000168func Test_indent_func_with_gq()
169
170 function GetTeXIndent()
171 " Sample indent expression for TeX files
172 let lnum = prevnonblank(v:lnum - 1)
173 " At the start of the file use zero indent.
174 if lnum == 0
175 return 0
176 endif
177 let line = getline(lnum)
178 let ind = indent(lnum)
179 " Add a 'shiftwidth' after beginning of environments.
180 if line =~ '\\begin{center}'
181 let ind = ind + shiftwidth()
182 endif
183 return ind
184 endfunction
185
186 new
187 setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
188 put =[ '\documentclass{article}', '', '\begin{document}', '',
189 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
190 \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
191 \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
192 \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
193 \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
194 \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
195 \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
196 \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
197 \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000198 \ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
199 \ 'Donec id semper purus.',
Christian Brabandt818ff252021-11-18 13:56:37 +0000200 \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
201 \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
202 \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
203 \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
204 \ 'ultrices posuere cubilia curae;', '', '']
205 1d_
206 call cursor(5, 1)
207 ka
Bram Moolenaarecabb512021-12-06 19:51:01 +0000208 call cursor(14, 1)
Christian Brabandt818ff252021-11-18 13:56:37 +0000209 kb
210 norm! 'agqap
Bram Moolenaarecabb512021-12-06 19:51:01 +0000211 norm! 'bgqG
Christian Brabandt818ff252021-11-18 13:56:37 +0000212 let expected = [ '\documentclass{article}', '', '\begin{document}', '',
213 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
214 \ 'elit. Fusce ut enim non libero efficitur aliquet.',
215 \ 'Maecenas metus justo, facilisis convallis blandit',
216 \ 'non, semper eu urna. Suspendisse diam diam,',
217 \ 'iaculis faucibus lorem eu, fringilla condimentum',
218 \ 'lectus. Quisque euismod diam at convallis',
219 \ 'vulputate. Pellentesque laoreet tortor sit amet',
220 \ 'mauris euismod ornare. Sed varius bibendum orci',
221 \ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
222 \ 'accumsan, metus lectus ultrices odio, sed',
223 \ 'elementum mi ante at arcu.', '', '\begin{center}', '',
224 \ ' Proin nec risus consequat nunc dapibus',
225 \ ' consectetur. Mauris lacinia est a augue',
226 \ ' tristique accumsan. Morbi pretium, felis',
227 \ ' molestie eleifend condimentum, arcu ipsum congue',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000228 \ ' nisl, quis euismod purus libero in ante.',
229 \ '',
230 \ ' Donec id semper purus. Suspendisse eget aliquam',
231 \ ' nunc. Maecenas fringilla mauris vitae maximus',
Christian Brabandt818ff252021-11-18 13:56:37 +0000232 \ ' condimentum. Cras a quam in mi dictum eleifend',
233 \ ' at a lorem. Sed convallis ante a commodo',
234 \ ' facilisis. Nam suscipit vulputate odio, vel',
235 \ ' dapibus nisl dignissim facilisis. Vestibulum',
236 \ ' ante ipsum primis in faucibus orci luctus et',
237 \ ' ultrices posuere cubilia curae;', '', '']
238 call assert_equal(expected, getline(1, '$'))
239
240 bwipe!
241 delmark ab
242 delfunction GetTeXIndent
243endfu
244
Bram Moolenaarecabb512021-12-06 19:51:01 +0000245func Test_formatting_keeps_first_line_indent()
246 let lines =<< trim END
247 foo()
248 {
249 int x; // manually positioned
250 // more text that will be formatted
251 // but not reindented
252 END
253 new
254 call setline(1, lines)
255 setlocal sw=4 cindent tw=45 et
256 normal! 4Ggqj
257 let expected =<< trim END
258 foo()
259 {
260 int x; // manually positioned
261 // more text that will be
262 // formatted but not
263 // reindented
264 END
265 call assert_equal(expected, getline(1, '$'))
266 bwipe!
267endfunc
268
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100269" vim: shiftwidth=2 sts=2 expandtab