Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 1 | " Test for various indent options |
| 2 | |
zeertzjq | 6c30277 | 2024-12-17 20:26:45 +0100 | [diff] [blame] | 3 | source shared.vim |
| 4 | source check.vim |
| 5 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 6 | func Test_preserveindent() |
| 7 | new |
| 8 | " Test for autoindent copying indent from the previous line |
| 9 | setlocal autoindent |
| 10 | call setline(1, [repeat(' ', 16) .. 'line1']) |
| 11 | call feedkeys("A\nline2", 'xt') |
| 12 | call assert_equal("\t\tline2", getline(2)) |
| 13 | setlocal autoindent& |
| 14 | |
| 15 | " Test for using CTRL-T with and without 'preserveindent' |
| 16 | set shiftwidth=4 |
| 17 | call cursor(1, 1) |
| 18 | call setline(1, " \t ") |
| 19 | call feedkeys("Al\<C-T>", 'xt') |
| 20 | call assert_equal("\t\tl", getline(1)) |
| 21 | set preserveindent |
| 22 | call setline(1, " \t ") |
| 23 | call feedkeys("Al\<C-T>", 'xt') |
| 24 | call assert_equal(" \t \tl", getline(1)) |
| 25 | set pi& sw& |
| 26 | |
| 27 | " Test for using CTRL-T with 'expandtab' and 'preserveindent' |
| 28 | call cursor(1, 1) |
| 29 | call setline(1, "\t \t") |
| 30 | set shiftwidth=4 expandtab preserveindent |
| 31 | call feedkeys("Al\<C-T>", 'xt') |
| 32 | call assert_equal("\t \t l", getline(1)) |
| 33 | set sw& et& pi& |
| 34 | |
| 35 | close! |
| 36 | endfunc |
| 37 | |
| 38 | " Test for indent() |
| 39 | func Test_indent_func() |
| 40 | call assert_equal(-1, indent(-1)) |
| 41 | new |
| 42 | call setline(1, "\tabc") |
| 43 | call assert_equal(8, indent(1)) |
| 44 | call setline(1, " abc") |
| 45 | call assert_equal(4, indent(1)) |
| 46 | call setline(1, " \t abc") |
| 47 | call assert_equal(12, indent(1)) |
| 48 | close! |
| 49 | endfunc |
| 50 | |
| 51 | " Test for reindenting a line using the '=' operator |
| 52 | func Test_reindent() |
| 53 | new |
| 54 | call setline(1, 'abc') |
| 55 | set nomodifiable |
| 56 | call assert_fails('normal ==', 'E21:') |
| 57 | set modifiable |
| 58 | |
| 59 | call setline(1, ['foo', 'bar']) |
| 60 | call feedkeys('ggVG=', 'xt') |
| 61 | call assert_equal(['foo', 'bar'], getline(1, 2)) |
| 62 | close! |
| 63 | endfunc |
| 64 | |
Bram Moolenaar | e468698 | 2022-04-19 18:28:45 +0100 | [diff] [blame] | 65 | " Test indent operator creating one undo entry |
| 66 | func Test_indent_operator_undo() |
| 67 | enew |
| 68 | call setline(1, range(12)->map('"\t" .. v:val')) |
| 69 | func FoldExpr() |
| 70 | let g:foldcount += 1 |
| 71 | return '=' |
| 72 | endfunc |
| 73 | set foldmethod=expr foldexpr=FoldExpr() |
| 74 | let g:foldcount = 0 |
| 75 | redraw |
| 76 | call assert_equal(12, g:foldcount) |
| 77 | normal gg=G |
| 78 | call assert_equal(24, g:foldcount) |
| 79 | undo |
| 80 | call assert_equal(38, g:foldcount) |
| 81 | |
| 82 | bwipe! |
| 83 | set foldmethod& foldexpr= |
| 84 | delfunc FoldExpr |
| 85 | unlet g:foldcount |
| 86 | endfunc |
| 87 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 88 | " Test for shifting a line with a preprocessor directive ('#') |
| 89 | func Test_preproc_indent() |
| 90 | new |
| 91 | set sw=4 |
| 92 | call setline(1, '#define FOO 1') |
| 93 | normal >> |
| 94 | call assert_equal(' #define FOO 1', getline(1)) |
| 95 | |
| 96 | " with 'smartindent' |
| 97 | call setline(1, '#define FOO 1') |
| 98 | set smartindent |
| 99 | normal >> |
| 100 | call assert_equal('#define FOO 1', getline(1)) |
| 101 | set smartindent& |
| 102 | |
| 103 | " with 'cindent' |
| 104 | set cindent |
| 105 | normal >> |
| 106 | call assert_equal('#define FOO 1', getline(1)) |
| 107 | set cindent& |
| 108 | |
| 109 | close! |
| 110 | endfunc |
| 111 | |
| 112 | " Test for 'copyindent' |
| 113 | func Test_copyindent() |
| 114 | new |
| 115 | set shiftwidth=4 autoindent expandtab copyindent |
| 116 | call setline(1, " \t abc") |
| 117 | call feedkeys("ol", 'xt') |
| 118 | call assert_equal(" \t l", getline(2)) |
| 119 | set noexpandtab |
| 120 | call setline(1, " \t abc") |
| 121 | call feedkeys("ol", 'xt') |
| 122 | call assert_equal(" \t l", getline(2)) |
| 123 | set sw& ai& et& ci& |
| 124 | close! |
| 125 | endfunc |
| 126 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 127 | " Test for changing multiple lines with lisp indent |
| 128 | func Test_lisp_indent_change_multiline() |
| 129 | new |
| 130 | setlocal lisp autoindent |
| 131 | call setline(1, ['(if a', ' (if b', ' (return 5)))']) |
| 132 | normal! jc2j(return 4)) |
| 133 | call assert_equal(' (return 4))', getline(2)) |
| 134 | close! |
| 135 | endfunc |
| 136 | |
| 137 | func Test_lisp_indent() |
| 138 | new |
| 139 | setlocal lisp autoindent |
| 140 | call setline(1, ['(if a', ' ;; comment', ' \ abc', '', ' " str1\', ' " st\b', ' (return 5)']) |
| 141 | normal! jo;; comment |
| 142 | normal! jo\ abc |
| 143 | normal! jo;; ret |
| 144 | normal! jostr1" |
| 145 | normal! jostr2" |
| 146 | call assert_equal([' ;; comment', ' ;; comment', ' \ abc', ' \ abc', '', ' ;; ret', ' " str1\', ' str1"', ' " st\b', ' str2"'], getline(2, 11)) |
| 147 | close! |
| 148 | endfunc |
| 149 | |
Bram Moolenaar | 0e8e938 | 2022-06-18 12:51:11 +0100 | [diff] [blame] | 150 | func Test_lisp_indent_quoted() |
| 151 | " This was going past the end of the line |
| 152 | new |
| 153 | setlocal lisp autoindent |
| 154 | call setline(1, ['"[', '=']) |
| 155 | normal Gvk= |
| 156 | |
| 157 | bwipe! |
| 158 | endfunc |
| 159 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 160 | " Test for setting the 'indentexpr' from a modeline |
| 161 | func Test_modeline_indent_expr() |
| 162 | let modeline = &modeline |
| 163 | set modeline |
| 164 | func GetIndent() |
| 165 | return line('.') * 2 |
| 166 | endfunc |
Bram Moolenaar | 7dd5a78 | 2022-09-29 21:01:57 +0100 | [diff] [blame] | 167 | call writefile(['# vim: indentexpr=GetIndent()'], 'Xmlfile.txt', 'D') |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 168 | set modelineexpr |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 169 | new Xmlfile.txt |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 170 | call assert_equal('GetIndent()', &indentexpr) |
| 171 | exe "normal Oa\nb\n" |
| 172 | call assert_equal([' a', ' b'], getline(1, 2)) |
Bram Moolenaar | 95e59a3 | 2020-03-19 20:33:33 +0100 | [diff] [blame] | 173 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 174 | set modelineexpr& |
| 175 | delfunc GetIndent |
| 176 | let &modeline = modeline |
| 177 | close! |
| 178 | endfunc |
| 179 | |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 180 | func Test_indent_func_with_gq() |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 181 | |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 182 | function GetTeXIndent() |
| 183 | " Sample indent expression for TeX files |
| 184 | let lnum = prevnonblank(v:lnum - 1) |
| 185 | " At the start of the file use zero indent. |
| 186 | if lnum == 0 |
| 187 | return 0 |
| 188 | endif |
| 189 | let line = getline(lnum) |
| 190 | let ind = indent(lnum) |
| 191 | " Add a 'shiftwidth' after beginning of environments. |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 192 | if line =~ '\\begin{center}' |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 193 | let ind = ind + shiftwidth() |
| 194 | endif |
| 195 | return ind |
| 196 | endfunction |
| 197 | |
| 198 | new |
| 199 | setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent() |
| 200 | put =[ '\documentclass{article}', '', '\begin{document}', '', |
| 201 | \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non', |
| 202 | \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit', |
| 203 | \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,', |
| 204 | \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.', |
| 205 | \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius', |
| 206 | \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,', |
| 207 | \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '', |
| 208 | \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue', |
| 209 | \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu', |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 210 | \ 'ipsum congue nisl, quis euismod purus libero in ante.', '', |
| 211 | \ 'Donec id semper purus.', |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 212 | \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus', |
| 213 | \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis', |
| 214 | \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl', |
| 215 | \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et', |
| 216 | \ 'ultrices posuere cubilia curae;', '', ''] |
| 217 | 1d_ |
| 218 | call cursor(5, 1) |
| 219 | ka |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 220 | call cursor(14, 1) |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 221 | kb |
| 222 | norm! 'agqap |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 223 | norm! 'bgqG |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 224 | let expected = [ '\documentclass{article}', '', '\begin{document}', '', |
| 225 | \ 'Lorem ipsum dolor sit amet, consectetur adipiscing', |
| 226 | \ 'elit. Fusce ut enim non libero efficitur aliquet.', |
| 227 | \ 'Maecenas metus justo, facilisis convallis blandit', |
| 228 | \ 'non, semper eu urna. Suspendisse diam diam,', |
| 229 | \ 'iaculis faucibus lorem eu, fringilla condimentum', |
| 230 | \ 'lectus. Quisque euismod diam at convallis', |
| 231 | \ 'vulputate. Pellentesque laoreet tortor sit amet', |
| 232 | \ 'mauris euismod ornare. Sed varius bibendum orci', |
| 233 | \ 'vel vehicula. Pellentesque tempor, ipsum et auctor', |
| 234 | \ 'accumsan, metus lectus ultrices odio, sed', |
| 235 | \ 'elementum mi ante at arcu.', '', '\begin{center}', '', |
| 236 | \ ' Proin nec risus consequat nunc dapibus', |
| 237 | \ ' consectetur. Mauris lacinia est a augue', |
| 238 | \ ' tristique accumsan. Morbi pretium, felis', |
| 239 | \ ' molestie eleifend condimentum, arcu ipsum congue', |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 240 | \ ' nisl, quis euismod purus libero in ante.', |
| 241 | \ '', |
| 242 | \ ' Donec id semper purus. Suspendisse eget aliquam', |
| 243 | \ ' nunc. Maecenas fringilla mauris vitae maximus', |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 244 | \ ' condimentum. Cras a quam in mi dictum eleifend', |
| 245 | \ ' at a lorem. Sed convallis ante a commodo', |
| 246 | \ ' facilisis. Nam suscipit vulputate odio, vel', |
| 247 | \ ' dapibus nisl dignissim facilisis. Vestibulum', |
| 248 | \ ' ante ipsum primis in faucibus orci luctus et', |
| 249 | \ ' ultrices posuere cubilia curae;', '', ''] |
| 250 | call assert_equal(expected, getline(1, '$')) |
| 251 | |
| 252 | bwipe! |
| 253 | delmark ab |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 254 | delfunction GetTeXIndent |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 255 | endfu |
| 256 | |
Bram Moolenaar | ecabb51 | 2021-12-06 19:51:01 +0000 | [diff] [blame] | 257 | func Test_formatting_keeps_first_line_indent() |
| 258 | let lines =<< trim END |
| 259 | foo() |
| 260 | { |
| 261 | int x; // manually positioned |
| 262 | // more text that will be formatted |
| 263 | // but not reindented |
| 264 | END |
| 265 | new |
| 266 | call setline(1, lines) |
| 267 | setlocal sw=4 cindent tw=45 et |
| 268 | normal! 4Ggqj |
| 269 | let expected =<< trim END |
| 270 | foo() |
| 271 | { |
| 272 | int x; // manually positioned |
| 273 | // more text that will be |
| 274 | // formatted but not |
| 275 | // reindented |
| 276 | END |
| 277 | call assert_equal(expected, getline(1, '$')) |
| 278 | bwipe! |
| 279 | endfunc |
| 280 | |
Christian Brabandt | 6bf1318 | 2023-11-14 22:42:59 +0100 | [diff] [blame] | 281 | " Test for indenting with large amount, causes overflow |
| 282 | func Test_indent_overflow_count() |
| 283 | new |
| 284 | setl sw=8 |
| 285 | call setline(1, "abc") |
| 286 | norm! V2147483647> |
| 287 | " indents by INT_MAX |
| 288 | call assert_equal(2147483647, indent(1)) |
| 289 | close! |
| 290 | endfunc |
| 291 | |
Christian Brabandt | 3770574 | 2023-11-22 22:18:35 +0100 | [diff] [blame] | 292 | func Test_indent_overflow_count2() |
| 293 | new |
| 294 | " this only works, when long is 64bits |
| 295 | try |
| 296 | setl sw=0x180000000 |
| 297 | catch /^Vim\%((\a\+)\)\=:E487:/ |
| 298 | throw 'Skipped: value negative on this platform' |
| 299 | endtry |
| 300 | call setline(1, "\tabc") |
| 301 | norm! << |
| 302 | call assert_equal(0, indent(1)) |
| 303 | close! |
| 304 | endfunc |
| 305 | |
zeertzjq | 6c30277 | 2024-12-17 20:26:45 +0100 | [diff] [blame] | 306 | " Test that mouse shape is restored to Normal mode after using "gq" when |
| 307 | " 'indentexpr' executes :normal. |
zeertzjq | f25d8f9 | 2024-12-18 21:12:25 +0100 | [diff] [blame] | 308 | func Test_mouse_shape_indent_norm_with_gq() |
zeertzjq | 6c30277 | 2024-12-17 20:26:45 +0100 | [diff] [blame] | 309 | CheckFeature mouseshape |
| 310 | CheckCanRunGui |
| 311 | |
| 312 | let lines =<< trim END |
| 313 | func Indent() |
| 314 | exe "normal! \<Ignore>" |
| 315 | return 0 |
| 316 | endfunc |
| 317 | |
| 318 | setlocal indentexpr=Indent() |
| 319 | END |
| 320 | call writefile(lines, 'Xindentexpr.vim', 'D') |
| 321 | |
| 322 | let lines =<< trim END |
| 323 | vim9script |
| 324 | var mouse_shapes = [] |
| 325 | |
| 326 | setline(1, [repeat('a', 80), repeat('b', 80)]) |
| 327 | |
| 328 | feedkeys('ggVG') |
| 329 | timer_start(50, (_) => { |
| 330 | mouse_shapes += [getmouseshape()] |
| 331 | timer_start(50, (_) => { |
| 332 | feedkeys('gq') |
| 333 | timer_start(50, (_) => { |
| 334 | mouse_shapes += [getmouseshape()] |
| 335 | timer_start(50, (_) => { |
| 336 | writefile(mouse_shapes, 'Xmouseshapes') |
| 337 | quit! |
| 338 | }) |
| 339 | }) |
| 340 | }) |
| 341 | }) |
| 342 | END |
| 343 | call writefile(lines, 'Xmouseshape.vim', 'D') |
| 344 | |
| 345 | call RunVim([], [], "-g -S Xindentexpr.vim -S Xmouseshape.vim") |
| 346 | call WaitForAssert({-> assert_equal(['rightup-arrow', 'arrow'], |
| 347 | \ readfile('Xmouseshapes'))}, 300) |
| 348 | |
| 349 | call delete('Xmouseshapes') |
| 350 | endfunc |
| 351 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 352 | " vim: shiftwidth=2 sts=2 expandtab |