blob: 9bb7b9c22753ae6151ad3d6e63628b1b416d052f [file] [log] [blame]
Bram Moolenaar69a76fe2017-08-03 17:54:03 +02001" Tests for smartindent
Bram Moolenaar53f16732016-09-07 20:46:39 +02002
3" Tests for not doing smart indenting when it isn't set.
Bram Moolenaar1e115362019-01-09 23:01:02 +01004func Test_nosmartindent()
Bram Moolenaar53f16732016-09-07 20:46:39 +02005 new
6 call append(0, [" some test text",
Bram Moolenaar1e115362019-01-09 23:01:02 +01007 \ " test text",
8 \ "test text",
9 \ " test text"])
Bram Moolenaar53f16732016-09-07 20:46:39 +020010 set nocindent nosmartindent autoindent
11 exe "normal! gg/some\<CR>"
12 exe "normal! 2cc#test\<Esc>"
13 call assert_equal(" #test", getline(1))
14 enew! | close
Bram Moolenaar1e115362019-01-09 23:01:02 +010015endfunc
Bram Moolenaar69a76fe2017-08-03 17:54:03 +020016
Bram Moolenaar1e115362019-01-09 23:01:02 +010017func MyIndent()
18endfunc
Bram Moolenaar69a76fe2017-08-03 17:54:03 +020019
20" When 'indentexpr' is set, setting 'si' has no effect.
Bram Moolenaar1e115362019-01-09 23:01:02 +010021func Test_smartindent_has_no_effect()
Bram Moolenaar69a76fe2017-08-03 17:54:03 +020022 new
23 exe "normal! i\<Tab>one\<Esc>"
Bram Moolenaarca68ae12020-03-30 19:32:53 +020024 setlocal noautoindent smartindent indentexpr=
Bram Moolenaar69a76fe2017-08-03 17:54:03 +020025 exe "normal! Gotwo\<Esc>"
26 call assert_equal("\ttwo", getline("$"))
27
28 set indentexpr=MyIndent
29 exe "normal! Gothree\<Esc>"
30 call assert_equal("three", getline("$"))
31
32 delfunction! MyIndent
Bram Moolenaar69a76fe2017-08-03 17:54:03 +020033 bwipe!
Bram Moolenaar1e115362019-01-09 23:01:02 +010034endfunc
Bram Moolenaar69a76fe2017-08-03 17:54:03 +020035
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010036" Test for inserting '{' and '} with smartindent
37func Test_smartindent_braces()
38 new
Bram Moolenaarca68ae12020-03-30 19:32:53 +020039 setlocal smartindent shiftwidth=4
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010040 call setline(1, [' if (a)', "\tif (b)", "\t return 1"])
41 normal 2ggO{
42 normal 3ggA {
43 normal 4ggo}
44 normal o}
45 normal 4ggO#define FOO 1
46 call assert_equal([
47 \ ' if (a)',
48 \ ' {',
49 \ "\tif (b) {",
50 \ '#define FOO 1',
51 \ "\t return 1",
52 \ "\t}",
53 \ ' }'
54 \ ], getline(1, '$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +020055 close!
56endfunc
57
58" Test for adding a new line before and after comments with smartindent
59func Test_si_add_line_around_comment()
60 new
61 setlocal smartindent shiftwidth=4
62 call setline(1, [' A', '# comment1', '# comment2'])
63 exe "normal GoC\<Esc>2GOB"
64 call assert_equal([' A', ' B', '# comment1', '# comment2', ' C'],
65 \ getline(1, '$'))
66 close!
67endfunc
68
69" After a C style comment, indent for a following line should line up with the
70" line containing the start of the comment.
71func Test_si_indent_after_c_comment()
72 new
73 setlocal smartindent shiftwidth=4 fo+=ro
74 exe "normal i\<C-t>/*\ncomment\n/\n#define FOOBAR\n75\<Esc>ggOabc"
75 normal 3jOcont
76 call assert_equal([' abc', ' /*', ' * comment', ' * cont',
77 \ ' */', '#define FOOBAR', ' 75'], getline(1, '$'))
78 close!
79endfunc
80
81" Test for indenting a statement after a if condition split across lines
82func Test_si_if_cond_split_across_lines()
83 new
84 setlocal smartindent shiftwidth=4
85 exe "normal i\<C-t>if (cond1 &&\n\<C-t>cond2) {\ni = 10;\n}"
86 call assert_equal([' if (cond1 &&', "\t cond2) {", "\ti = 10;",
87 \ ' }'], getline(1, '$'))
88 close!
89endfunc
90
91" Test for inserting lines before and after a one line comment
92func Test_si_one_line_comment()
93 new
94 setlocal smartindent shiftwidth=4
95 exe "normal i\<C-t>abc;\n\<C-t>/* comment */"
96 normal oi = 10;
97 normal kOj = 1;
98 call assert_equal([' abc;', "\tj = 1;", "\t/* comment */", "\ti = 10;"],
99 \ getline(1, '$'))
100 close!
101endfunc
102
103" Test for smartindent with a comment continued across multiple lines
104func Test_si_comment_line_continuation()
105 new
106 setlocal smartindent shiftwidth=4
107 call setline(1, ['# com1', '# com2 \', ' contd', '# com3', ' xyz'])
108 normal ggOabc
109 call assert_equal([' abc', '# com1', '# com2 \', ' contd', '# com3',
110 \ ' xyz'], getline(1, '$'))
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100111 close!
112endfunc
113
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200114" When 'paste' is set, 'smartindent' should not take effect.
115func Test_si_with_paste()
116 new
117 setlocal smartindent autoindent
118 set paste
119 " insert text that will trigger smartindent
120 exe "norm! i {\nif (x)\ni = 1;\n#define FOO 1\nj = 2;\n}"
121 exe "norm! Ok = 3;"
122 exe "norm! 4G>>"
123 call assert_equal([' {', 'if (x)', 'i = 1;', '#define FOO 1',
124 \ 'j = 2;', 'k = 3;', '}'], getline(1, '$'))
125 call assert_true(&smartindent)
126 set nopaste
127 %d _
128 exe "norm! i {\nif (x)\ni = 1;\n#define FOO 1\nj = 2;\n}"
129 exe "norm! Ok = 3;"
130 exe "norm! 4G>>"
131 call assert_equal([' {', "\t if (x)", "\t\t i = 1;",
132 \ '#define FOO 1', "\t\t j = 2;", "\t k = 3;", ' }'],
133 \ getline(1, '$'))
134 bw!
135endfunc
136
Bram Moolenaarde5cf282022-05-14 11:52:23 +0100137func Test_si_after_completion()
138 new
139 setlocal ai smartindent indentexpr=
140 call setline(1, 'foo foot')
141 call feedkeys("o f\<C-X>\<C-N>#", 'tx')
142 call assert_equal(' foo#', getline(2))
Bram Moolenaar2e444bb2022-05-14 12:54:23 +0100143
144 call setline(2, '')
145 call feedkeys("1Go f\<C-X>\<C-N>}", 'tx')
146 call assert_equal(' foo}', getline(2))
147
Bram Moolenaarde5cf282022-05-14 11:52:23 +0100148 bwipe!
149endfunc
150
151func Test_no_si_after_completion()
152 new
153 call setline(1, 'foo foot')
154 call feedkeys("o f\<C-X>\<C-N>#", 'tx')
155 call assert_equal(' foo#', getline(2))
156 bwipe!
157endfunc
158
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200159" vim: shiftwidth=2 sts=2 expandtab