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