blob: 5e90c87c461738042bf1b8c5f40deef70a63ac54 [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
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100145" vim: shiftwidth=2 sts=2 expandtab