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