Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 1 | " Tests for smartindent |
Bram Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 2 | |
| 3 | " Tests for not doing smart indenting when it isn't set. |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 4 | func Test_nosmartindent() |
Bram Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 5 | new |
| 6 | call append(0, [" some test text", |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 7 | \ " test text", |
| 8 | \ "test text", |
| 9 | \ " test text"]) |
Bram Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 10 | 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 Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 15 | endfunc |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 16 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 17 | func MyIndent() |
| 18 | endfunc |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 19 | |
| 20 | " When 'indentexpr' is set, setting 'si' has no effect. |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 21 | func Test_smartindent_has_no_effect() |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 22 | new |
| 23 | exe "normal! i\<Tab>one\<Esc>" |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 24 | setlocal noautoindent smartindent indentexpr= |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 25 | 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 Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 33 | bwipe! |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 34 | endfunc |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 35 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 36 | " Test for inserting '{' and '} with smartindent |
| 37 | func Test_smartindent_braces() |
| 38 | new |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 39 | setlocal smartindent shiftwidth=4 |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 40 | 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 Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 55 | close! |
| 56 | endfunc |
| 57 | |
| 58 | " Test for adding a new line before and after comments with smartindent |
| 59 | func 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! |
| 67 | endfunc |
| 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. |
| 71 | func 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! |
| 79 | endfunc |
| 80 | |
| 81 | " Test for indenting a statement after a if condition split across lines |
| 82 | func 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! |
| 89 | endfunc |
| 90 | |
| 91 | " Test for inserting lines before and after a one line comment |
| 92 | func 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! |
| 101 | endfunc |
| 102 | |
| 103 | " Test for smartindent with a comment continued across multiple lines |
| 104 | func 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 Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 111 | close! |
| 112 | endfunc |
| 113 | |
Yegappan Lakshmanan | 41a7f82 | 2021-06-16 15:53:17 +0200 | [diff] [blame] | 114 | " When 'paste' is set, 'smartindent' should not take effect. |
| 115 | func 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! |
| 135 | endfunc |
| 136 | |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 137 | func 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 Moolenaar | 2e444bb | 2022-05-14 12:54:23 +0100 | [diff] [blame] | 143 | |
| 144 | call setline(2, '') |
| 145 | call feedkeys("1Go f\<C-X>\<C-N>}", 'tx') |
| 146 | call assert_equal(' foo}', getline(2)) |
| 147 | |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 148 | bwipe! |
| 149 | endfunc |
| 150 | |
| 151 | func 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! |
| 157 | endfunc |
| 158 | |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 159 | " vim: shiftwidth=2 sts=2 expandtab |