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