blob: f96da6969bc865c64072ff9358590ae87b16fcc3 [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
62" Test for shifting a line with a preprocessor directive ('#')
63func Test_preproc_indent()
64 new
65 set sw=4
66 call setline(1, '#define FOO 1')
67 normal >>
68 call assert_equal(' #define FOO 1', getline(1))
69
70 " with 'smartindent'
71 call setline(1, '#define FOO 1')
72 set smartindent
73 normal >>
74 call assert_equal('#define FOO 1', getline(1))
75 set smartindent&
76
77 " with 'cindent'
78 set cindent
79 normal >>
80 call assert_equal('#define FOO 1', getline(1))
81 set cindent&
82
83 close!
84endfunc
85
86" Test for 'copyindent'
87func Test_copyindent()
88 new
89 set shiftwidth=4 autoindent expandtab copyindent
90 call setline(1, " \t abc")
91 call feedkeys("ol", 'xt')
92 call assert_equal(" \t l", getline(2))
93 set noexpandtab
94 call setline(1, " \t abc")
95 call feedkeys("ol", 'xt')
96 call assert_equal(" \t l", getline(2))
97 set sw& ai& et& ci&
98 close!
99endfunc
100
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100101" Test for changing multiple lines with lisp indent
102func Test_lisp_indent_change_multiline()
103 new
104 setlocal lisp autoindent
105 call setline(1, ['(if a', ' (if b', ' (return 5)))'])
106 normal! jc2j(return 4))
107 call assert_equal(' (return 4))', getline(2))
108 close!
109endfunc
110
111func Test_lisp_indent()
112 new
113 setlocal lisp autoindent
114 call setline(1, ['(if a', ' ;; comment', ' \ abc', '', ' " str1\', ' " st\b', ' (return 5)'])
115 normal! jo;; comment
116 normal! jo\ abc
117 normal! jo;; ret
118 normal! jostr1"
119 normal! jostr2"
120 call assert_equal([' ;; comment', ' ;; comment', ' \ abc', ' \ abc', '', ' ;; ret', ' " str1\', ' str1"', ' " st\b', ' str2"'], getline(2, 11))
121 close!
122endfunc
123
124" Test for setting the 'indentexpr' from a modeline
125func Test_modeline_indent_expr()
126 let modeline = &modeline
127 set modeline
128 func GetIndent()
129 return line('.') * 2
130 endfunc
131 call writefile(['# vim: indentexpr=GetIndent()'], 'Xfile.txt')
132 set modelineexpr
133 new Xfile.txt
134 call assert_equal('GetIndent()', &indentexpr)
135 exe "normal Oa\nb\n"
136 call assert_equal([' a', ' b'], getline(1, 2))
Bram Moolenaar95e59a32020-03-19 20:33:33 +0100137
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100138 set modelineexpr&
139 delfunc GetIndent
140 let &modeline = modeline
141 close!
Bram Moolenaar95e59a32020-03-19 20:33:33 +0100142 call delete('Xfile.txt')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100143endfunc
144
Christian Brabandt818ff252021-11-18 13:56:37 +0000145func Test_indent_func_with_gq()
146
147 function GetTeXIndent()
148 " Sample indent expression for TeX files
149 let lnum = prevnonblank(v:lnum - 1)
150 " At the start of the file use zero indent.
151 if lnum == 0
152 return 0
153 endif
154 let line = getline(lnum)
155 let ind = indent(lnum)
156 " Add a 'shiftwidth' after beginning of environments.
157 if line =~ '\\begin{center}'
158 let ind = ind + shiftwidth()
159 endif
160 return ind
161 endfunction
162
163 new
164 setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
165 put =[ '\documentclass{article}', '', '\begin{document}', '',
166 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
167 \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
168 \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
169 \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
170 \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
171 \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
172 \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
173 \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
174 \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000175 \ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
176 \ 'Donec id semper purus.',
Christian Brabandt818ff252021-11-18 13:56:37 +0000177 \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
178 \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
179 \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
180 \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
181 \ 'ultrices posuere cubilia curae;', '', '']
182 1d_
183 call cursor(5, 1)
184 ka
Bram Moolenaarecabb512021-12-06 19:51:01 +0000185 call cursor(14, 1)
Christian Brabandt818ff252021-11-18 13:56:37 +0000186 kb
187 norm! 'agqap
Bram Moolenaarecabb512021-12-06 19:51:01 +0000188 norm! 'bgqG
Christian Brabandt818ff252021-11-18 13:56:37 +0000189 let expected = [ '\documentclass{article}', '', '\begin{document}', '',
190 \ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
191 \ 'elit. Fusce ut enim non libero efficitur aliquet.',
192 \ 'Maecenas metus justo, facilisis convallis blandit',
193 \ 'non, semper eu urna. Suspendisse diam diam,',
194 \ 'iaculis faucibus lorem eu, fringilla condimentum',
195 \ 'lectus. Quisque euismod diam at convallis',
196 \ 'vulputate. Pellentesque laoreet tortor sit amet',
197 \ 'mauris euismod ornare. Sed varius bibendum orci',
198 \ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
199 \ 'accumsan, metus lectus ultrices odio, sed',
200 \ 'elementum mi ante at arcu.', '', '\begin{center}', '',
201 \ ' Proin nec risus consequat nunc dapibus',
202 \ ' consectetur. Mauris lacinia est a augue',
203 \ ' tristique accumsan. Morbi pretium, felis',
204 \ ' molestie eleifend condimentum, arcu ipsum congue',
Bram Moolenaarecabb512021-12-06 19:51:01 +0000205 \ ' nisl, quis euismod purus libero in ante.',
206 \ '',
207 \ ' Donec id semper purus. Suspendisse eget aliquam',
208 \ ' nunc. Maecenas fringilla mauris vitae maximus',
Christian Brabandt818ff252021-11-18 13:56:37 +0000209 \ ' condimentum. Cras a quam in mi dictum eleifend',
210 \ ' at a lorem. Sed convallis ante a commodo',
211 \ ' facilisis. Nam suscipit vulputate odio, vel',
212 \ ' dapibus nisl dignissim facilisis. Vestibulum',
213 \ ' ante ipsum primis in faucibus orci luctus et',
214 \ ' ultrices posuere cubilia curae;', '', '']
215 call assert_equal(expected, getline(1, '$'))
216
217 bwipe!
218 delmark ab
219 delfunction GetTeXIndent
220endfu
221
Bram Moolenaarecabb512021-12-06 19:51:01 +0000222func Test_formatting_keeps_first_line_indent()
223 let lines =<< trim END
224 foo()
225 {
226 int x; // manually positioned
227 // more text that will be formatted
228 // but not reindented
229 END
230 new
231 call setline(1, lines)
232 setlocal sw=4 cindent tw=45 et
233 normal! 4Ggqj
234 let expected =<< trim END
235 foo()
236 {
237 int x; // manually positioned
238 // more text that will be
239 // formatted but not
240 // reindented
241 END
242 call assert_equal(expected, getline(1, '$'))
243 bwipe!
244endfunc
245
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100246" vim: shiftwidth=2 sts=2 expandtab