Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 1 | " Test for various indent options |
| 2 | |
| 3 | func 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! |
| 33 | endfunc |
| 34 | |
| 35 | " Test for indent() |
| 36 | func 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! |
| 46 | endfunc |
| 47 | |
| 48 | " Test for reindenting a line using the '=' operator |
| 49 | func 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! |
| 60 | endfunc |
| 61 | |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 62 | " Test indent operator creating one undo entry |
| 63 | func 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 |
| 83 | endfunc |
| 84 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 85 | " Test for shifting a line with a preprocessor directive ('#') |
| 86 | func 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! |
| 107 | endfunc |
| 108 | |
| 109 | " Test for 'copyindent' |
| 110 | func 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! |
| 122 | endfunc |
| 123 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 124 | " Test for changing multiple lines with lisp indent |
| 125 | func 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! |
| 132 | endfunc |
| 133 | |
| 134 | func 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! |
| 145 | endfunc |
| 146 | |
Bram Moolenaar | 0e8e938 | 2022-06-18 12:51:11 +0100 | [diff] [blame] | 147 | func Test_lisp_indent_quoted() |
| 148 | " This was going past the end of the line |
| 149 | new |
| 150 | setlocal lisp autoindent |
| 151 | call setline(1, ['"[', '=']) |
| 152 | normal Gvk= |
| 153 | |
| 154 | bwipe! |
| 155 | endfunc |
| 156 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 157 | " Test for setting the 'indentexpr' from a modeline |
| 158 | func Test_modeline_indent_expr() |
| 159 | let modeline = &modeline |
| 160 | set modeline |
| 161 | func GetIndent() |
| 162 | return line('.') * 2 |
| 163 | endfunc |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 164 | call writefile(['# vim: indentexpr=GetIndent()'], 'Xmlfile.txt') |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 165 | set modelineexpr |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 166 | new Xmlfile.txt |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 167 | call assert_equal('GetIndent()', &indentexpr) |
| 168 | exe "normal Oa\nb\n" |
| 169 | call assert_equal([' a', ' b'], getline(1, 2)) |
Bram Moolenaar | 95e59a3 | 2020-03-19 20:33:33 +0100 | [diff] [blame] | 170 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 171 | set modelineexpr& |
| 172 | delfunc GetIndent |
| 173 | let &modeline = modeline |
| 174 | close! |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 175 | call delete('Xmlfile.txt') |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 176 | endfunc |
| 177 | |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 178 | func Test_indent_func_with_gq() |
| 179 | |
| 180 | function GetTeXIndent() |
| 181 | " Sample indent expression for TeX files |
| 182 | let lnum = prevnonblank(v:lnum - 1) |
| 183 | " At the start of the file use zero indent. |
| 184 | if lnum == 0 |
| 185 | return 0 |
| 186 | endif |
| 187 | let line = getline(lnum) |
| 188 | let ind = indent(lnum) |
| 189 | " Add a 'shiftwidth' after beginning of environments. |
| 190 | if line =~ '\\begin{center}' |
| 191 | let ind = ind + shiftwidth() |
| 192 | endif |
| 193 | return ind |
| 194 | endfunction |
| 195 | |
| 196 | new |
| 197 | setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent() |
| 198 | put =[ '\documentclass{article}', '', '\begin{document}', '', |
| 199 | \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non', |
| 200 | \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit', |
| 201 | \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,', |
| 202 | \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.', |
| 203 | \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius', |
| 204 | \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,', |
| 205 | \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '', |
| 206 | \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue', |
| 207 | \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu', |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 208 | \ 'ipsum congue nisl, quis euismod purus libero in ante.', '', |
| 209 | \ 'Donec id semper purus.', |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 210 | \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus', |
| 211 | \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis', |
| 212 | \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl', |
| 213 | \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et', |
| 214 | \ 'ultrices posuere cubilia curae;', '', ''] |
| 215 | 1d_ |
| 216 | call cursor(5, 1) |
| 217 | ka |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 218 | call cursor(14, 1) |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 219 | kb |
| 220 | norm! 'agqap |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 221 | norm! 'bgqG |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 222 | let expected = [ '\documentclass{article}', '', '\begin{document}', '', |
| 223 | \ 'Lorem ipsum dolor sit amet, consectetur adipiscing', |
| 224 | \ 'elit. Fusce ut enim non libero efficitur aliquet.', |
| 225 | \ 'Maecenas metus justo, facilisis convallis blandit', |
| 226 | \ 'non, semper eu urna. Suspendisse diam diam,', |
| 227 | \ 'iaculis faucibus lorem eu, fringilla condimentum', |
| 228 | \ 'lectus. Quisque euismod diam at convallis', |
| 229 | \ 'vulputate. Pellentesque laoreet tortor sit amet', |
| 230 | \ 'mauris euismod ornare. Sed varius bibendum orci', |
| 231 | \ 'vel vehicula. Pellentesque tempor, ipsum et auctor', |
| 232 | \ 'accumsan, metus lectus ultrices odio, sed', |
| 233 | \ 'elementum mi ante at arcu.', '', '\begin{center}', '', |
| 234 | \ ' Proin nec risus consequat nunc dapibus', |
| 235 | \ ' consectetur. Mauris lacinia est a augue', |
| 236 | \ ' tristique accumsan. Morbi pretium, felis', |
| 237 | \ ' molestie eleifend condimentum, arcu ipsum congue', |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 238 | \ ' nisl, quis euismod purus libero in ante.', |
| 239 | \ '', |
| 240 | \ ' Donec id semper purus. Suspendisse eget aliquam', |
| 241 | \ ' nunc. Maecenas fringilla mauris vitae maximus', |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 242 | \ ' condimentum. Cras a quam in mi dictum eleifend', |
| 243 | \ ' at a lorem. Sed convallis ante a commodo', |
| 244 | \ ' facilisis. Nam suscipit vulputate odio, vel', |
| 245 | \ ' dapibus nisl dignissim facilisis. Vestibulum', |
| 246 | \ ' ante ipsum primis in faucibus orci luctus et', |
| 247 | \ ' ultrices posuere cubilia curae;', '', ''] |
| 248 | call assert_equal(expected, getline(1, '$')) |
| 249 | |
| 250 | bwipe! |
| 251 | delmark ab |
| 252 | delfunction GetTeXIndent |
| 253 | endfu |
| 254 | |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 255 | func Test_formatting_keeps_first_line_indent() |
| 256 | let lines =<< trim END |
| 257 | foo() |
| 258 | { |
| 259 | int x; // manually positioned |
| 260 | // more text that will be formatted |
| 261 | // but not reindented |
| 262 | END |
| 263 | new |
| 264 | call setline(1, lines) |
| 265 | setlocal sw=4 cindent tw=45 et |
| 266 | normal! 4Ggqj |
| 267 | let expected =<< trim END |
| 268 | foo() |
| 269 | { |
| 270 | int x; // manually positioned |
| 271 | // more text that will be |
| 272 | // formatted but not |
| 273 | // reindented |
| 274 | END |
| 275 | call assert_equal(expected, getline(1, '$')) |
| 276 | bwipe! |
| 277 | endfunc |
| 278 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 279 | " vim: shiftwidth=2 sts=2 expandtab |