robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Test for syntax and syntax iskeyword option 2 3source check.vim 4CheckFeature syntax 5 6source view_util.vim 7source screendump.vim 8 9func GetSyntaxItem ( pat ) 10let c = '' 11let a = [ 'a' , getreg ( 'a' ), getregtype ( 'a' )] 120 13redraw ! 14call search ( a: pat , 'W' ) 15let synid = synID ( line ( '.' ), col ( '.' ), 1 ) 16while synid == synID ( line ( '.' ), col ( '.' ), 1 ) 17norm ! v"ay 18" stop at whitespace 19if @a=~# '\s' 20break 21endif 22let c .= @a 23norm ! l 24endw 25call call ( 'setreg' , a ) 260 27return c 28endfunc 29 30func AssertHighlightGroups ( lnum , startcol , expected , trans = 1 , msg = "" ) 31" Assert that the characters starting at a given (line, col) 32" sequentially match the expected highlight groups. 33" If groups are provided as a string, each character is assumed to be a 34" group and spaces represent no group, useful for visually describing tests. 35let l: expectedGroups = type ( a: expected ) == v: t_string 36 \? a: expected ->split ( '\zs' ) ->map ({ _ , v ->trim ( v )}) 37 \: a: expected 38let l: errors = 0 39let l: msg = ( a: msg ->empty () ? "" : a: msg .. ": " ) 40 \.. "Wrong highlight group at " .. a: lnum .. "," 41 42for l: i in range ( a: startcol , a: startcol + l: expectedGroups ->len () - 1 ) 43let l: errors += synID ( a: lnum , l: i , a: trans ) 44 \ ->synIDattr ( "name" ) 45 \ ->assert_equal ( l: expectedGroups [ l: i - 1 ], 46 \l: msg .. l: i ) 47endfor 48endfunc 49 50func Test_syn_iskeyword () 51new 52call setline ( 1 , [ 53 \'CREATE TABLE FOOBAR(' , 54 \' DLTD_BY VARCHAR2(100)' , 55 \');' , 56 \'' ]) 57 58syntax on 59set ft = sql 60syn match SYN /C\k\+\> / 61hi link SYN ErrorMsg 62call assert_equal ( 'DLTD_BY' , GetSyntaxItem ( 'DLTD' )) 63/ \<D\k\+\> / : norm ! ygn 64call assert_equal ( 'DLTD_BY' , @0) 65redir @c 66syn iskeyword 67redir END 68call assert_equal ( "\nsyntax iskeyword not set" , @c) 69 70syn iskeyword @,48-57,_,192-255 71redir @c 72syn iskeyword 73redir END 74call assert_equal ( "\nsyntax iskeyword @,48-57,_,192-255" , @c) 75 76call assert_fails ( "syntax iskeyword invalid_value" , "E474:" ) 77redir @c 78syn iskeyword 79redir END 80" did not change 81call assert_equal ( "\nsyntax iskeyword @,48-57,_,192-255" , @c) 82 83setlocal isk -= _ 84call assert_equal ( 'DLTD_BY' , GetSyntaxItem ( 'DLTD' )) 85/ \<D\k\+\> / : norm ! ygn 86let b2 = @0 87call assert_equal ( 'DLTD' , @0) 88 89syn iskeyword clear 90redir @c 91syn iskeyword 92redir END 93call assert_equal ( "\nsyntax iskeyword not set" , @c) 94 95quit ! 96endfunc 97 98func Test_syntax_after_reload () 99split Xsomefile 100call setline ( 1 , [ 'hello' , 'there' ]) 101w ! 102only ! 103setl filetype = hello 104au FileType hello let g: gotit = 1 105call assert_false ( exists ( 'g:gotit' )) 106edit other 107buf Xsomefile 108call assert_equal ( 'hello' , & filetype ) 109call assert_true ( exists ( 'g:gotit' )) 110call delete ( 'Xsomefile' ) 111endfunc 112 113func Test_syntax_filetype_syntax () 114set filetype = syntax 115syntax on 116syntax off 117set filetype = syntax = 118endfunc 119 120func Test_syntime () 121CheckFeature profile 122 123syntax on 124syntime on 125let a = execute ( 'syntime report' ) 126call assert_equal ( "\nNo Syntax items defined for this buffer" , a ) 127 128let a = execute ( 'syntime clear' ) 129call assert_equal ( "\nNo Syntax items defined for this buffer" , a ) 130 131view ../memfile_test.c 132setfiletype cpp 133redraw 134let a = execute ( 'syntime report' ) 135call assert_match ( '^ TOTAL *COUNT *MATCH *SLOWEST *AVERAGE *NAME *PATTERN' , a ) 136call assert_match ( ' \d*\.\d* \+[^0]\d* .* cppRawString ' , a ) 137call assert_match ( ' \d*\.\d* \+[^0]\d* .* cppNumber ' , a ) 138 139syntime off 140syntime clear 141let a = execute ( 'syntime report' ) 142call assert_match ( '^ TOTAL *COUNT *MATCH *SLOWEST *AVERAGE *NAME *PATTERN' , a ) 143call assert_notmatch ( '.* cppRawString *' , a ) 144call assert_notmatch ( '.* cppNumber*' , a ) 145call assert_notmatch ( '[1-9]' , a ) 146 147call assert_fails ( 'syntime abc' , 'E475' ) 148 149syntax clear 150let a = execute ( 'syntime report' ) 151call assert_equal ( "\nNo Syntax items defined for this buffer" , a ) 152 153bd 154endfunc 155 156func Test_syntime_completion () 157if ! has ( 'profile' ) 158return 159endif 160 161call feedkeys ( ":syntime \<C-A>\<C-B>\"\<CR>" , 'tx' ) 162call assert_equal ( '"syntime clear off on report' , @:) 163endfunc 164 165func Test_syntax_list () 166syntax on 167let a = execute ( 'syntax list' ) 168call assert_equal ( "\nNo Syntax items defined for this buffer" , a ) 169 170view ../memfile_test.c 171setfiletype c 172 173let a = execute ( 'syntax list' ) 174call assert_match ( 'cInclude*' , a ) 175call assert_match ( 'cDefine' , a ) 176 177let a = execute ( 'syntax list cDefine' ) 178call assert_notmatch ( 'cInclude*' , a ) 179call assert_match ( 'cDefine' , a ) 180call assert_match ( ' links to Macro$' , a ) 181 182call assert_fails ( 'syntax list ABCD' , 'E28:' ) 183call assert_fails ( 'syntax list @ABCD' , 'E392:' ) 184 185syntax clear 186let a = execute ( 'syntax list' ) 187call assert_equal ( "\nNo Syntax items defined for this buffer" , a ) 188 189syntax keyword Type int containedin = g1 skipwhite skipempty skipnl nextgroup = Abc 190let exp = "Type xxx containedin=g1 nextgroup=Abc skipnl skipwhite skipempty int" 191call assert_equal ( exp , split ( execute ( "syntax list" ), "\n" )[ 1 ]) 192 193bd 194endfunc 195 196func Test_syntax_completion () 197call feedkeys ( ":syn \<C-A>\<C-B>\"\<CR>" , 'tx' ) 198call assert_equal ( '"syn case clear cluster conceal enable foldlevel include iskeyword keyword list manual match off on region reset spell sync' , @:) 199 200call feedkeys ( ":syn case \<C-A>\<C-B>\"\<CR>" , 'tx' ) 201call assert_equal ( '"syn case ignore match' , @:) 202 203call feedkeys ( ":syn spell \<C-A>\<C-B>\"\<CR>" , 'tx' ) 204call assert_equal ( '"syn spell default notoplevel toplevel' , @:) 205 206call feedkeys ( ":syn sync \<C-A>\<C-B>\"\<CR>" , 'tx' ) 207call assert_equal ( '"syn sync ccomment clear fromstart linebreaks= linecont lines= match maxlines= minlines= region' , @:) 208 209" Check that clearing "Aap" avoids it showing up before Boolean. 210hi @Aap ctermfg = blue 211call feedkeys ( ":syn list \<C-A>\<C-B>\"\<CR>" , 'tx' ) 212call assert_match ( '^"syn list @Aap @attribute @attribute.builtin @boolean @character ' , @:) 213hi clear @Aap 214 215call feedkeys ( ":syn list \<C-A>\<C-B>\"\<CR>" , 'tx' ) 216call assert_match ( '^"syn list @attribute @attribute.builtin @boolean @character ' , @:) 217 218call feedkeys ( ":syn match \<C-A>\<C-B>\"\<CR>" , 'tx' ) 219call assert_match ( '^"syn match @attribute @attribute.builtin @boolean @character ' , @:) 220 221syn cluster Aax contains = Aap 222call feedkeys ( ":syn list @A\<C-A>\<C-B>\"\<CR>" , 'tx' ) 223call assert_match ( '^"syn list @Aax' , @:) 224endfunc 225 226func Test_echohl_completion () 227call feedkeys ( ":echohl no\<C-A>\<C-B>\"\<CR>" , 'tx' ) 228" call assert_equal('"echohl NonText Normal none', @:) 229call assert_equal ( '"echohl NonText Normal NormalFloat NormalNC none' , @:) 230endfunc 231 232func Test_syntax_arg_skipped () 233syn clear 234syntax case ignore 235if 0 236syntax case match 237endif 238call assert_match ( 'case ignore' , execute ( 'syntax case' )) 239 240syn keyword Foo foo 241call assert_match ( 'Foo' , execute ( 'syntax' )) 242syn clear 243call assert_match ( 'case match' , execute ( 'syntax case' )) 244call assert_notmatch ( 'Foo' , execute ( 'syntax' )) 245 246if has ( 'conceal' ) 247syn clear 248syntax concealon 249if 0 250syntax concealoff 251endif 252call assert_match ( 'conceal on' , execute ( 'syntax conceal' )) 253syn clear 254call assert_match ( 'conceal off' , execute ( 'syntax conceal' )) 255endif 256 257syntax concealon 258syntax concealoff 259call assert_match ( 'conceal off' , execute ( 'syntax conceal' )) 260 261syntax region Bar start = /< /end = /> / 262if 0 263syntax region NotTest start = /< /end = /> /contains = @Spell 264endif 265call assert_match ( 'Bar' , execute ( 'syntax' )) 266call assert_notmatch ( 'NotTest' , execute ( 'syntax' )) 267call assert_notmatch ( 'Spell' , execute ( 'syntax' )) 268 269hi Foo ctermfg = blue 270let a = execute ( 'hi Foo' ) 271if 0 272syntax rest 273endif 274call assert_equal ( a , execute ( 'hi Foo' )) 275hi clear Bar 276hi clear Foo 277 278set ft = tags 279syn off 280if 0 281syntax enable 282endif 283call assert_match ( 'No Syntax items defined' , execute ( 'syntax' )) 284syntax enable 285call assert_match ( 'tagComment' , execute ( 'syntax' )) 286set ft = 287 288syn clear 289if 0 290syntax include @Spell nothing 291endif 292call assert_notmatch ( 'Spell' , execute ( 'syntax' )) 293 294syn clear 295syn iskeyword 48-57,$,_ 296call assert_match ( '48-57,$,_' , execute ( 'syntax iskeyword' )) 297if 0 298syn clear 299syn iskeyword clear 300endif 301call assert_match ( '48-57,$,_' , execute ( 'syntax iskeyword' )) 302syn iskeyword clear 303call assert_match ( 'not set' , execute ( 'syntax iskeyword' )) 304syn iskeyword 48-57,$,_ 305syn clear 306call assert_match ( 'not set' , execute ( 'syntax iskeyword' )) 307 308syn clear 309syn keyword Foo foo 310if 0 311syn keyword NotAdded bar 312endif 313call assert_match ( 'Foo' , execute ( 'syntax' )) 314call assert_notmatch ( 'NotAdded' , execute ( 'highlight' )) 315 316syn clear 317syn keyword Foo foo 318call assert_match ( 'Foo' , execute ( 'syntax' )) 319call assert_match ( 'Foo' , execute ( 'syntax list' )) 320call assert_notmatch ( 'Foo' , execute ( 'if 0 | syntax | endif' )) 321call assert_notmatch ( 'Foo' , execute ( 'if 0 | syntax list | endif' )) 322 323syn clear 324syn match Fopi /asdf / 325if 0 326syn match Fopx /asdf / 327endif 328call assert_match ( 'Fopi' , execute ( 'syntax' )) 329call assert_notmatch ( 'Fopx' , execute ( 'syntax' )) 330 331syn clear 332syn spell toplevel 333call assert_match ( 'spell toplevel' , execute ( 'syntax spell' )) 334if 0 335syn spell notoplevel 336endif 337call assert_match ( 'spell toplevel' , execute ( 'syntax spell' )) 338syn spell notoplevel 339call assert_match ( 'spell notoplevel' , execute ( 'syntax spell' )) 340syn spell default 341call assert_match ( 'spell default' , execute ( 'syntax spell' )) 342 343syn clear 344if 0 345syntax cluster Spell 346endif 347call assert_notmatch ( 'Spell' , execute ( 'syntax' )) 348 349syn clear 350syn keyword Foo foo 351syn sync ccomment 352syn sync maxlines= 5 353if 0 354syn sync maxlines= 11 355endif 356call assert_match ( 'on C-style comments' , execute ( 'syntax sync' )) 357call assert_match ( 'maximal 5 lines' , execute ( 'syntax sync' )) 358syn syncclear 359if 0 360syn sync ccomment 361endif 362call assert_notmatch ( 'on C-style comments' , execute ( 'syntax sync' )) 363syn sync fromstart 364call assert_match ( 'syncing starts at the first line' , execute ( 'syntax sync' )) 365 366syn clear 367endfunc 368 369" Check for an error. Used when multiple errors are thrown and we are checking 370" for an earliest error. 371func AssertFails ( cmd , errcode ) 372let save_exception = '' 373try 374exe a: cmd 375catch 376let save_exception = v: exception 377endtry 378call assert_match ( a: errcode , save_exception ) 379endfunc 380 381func Test_syntax_invalid_arg () 382call assert_fails ( 'syntax case asdf' , 'E390:' ) 383if has ( 'conceal' ) 384call assert_fails ( 'syntax conceal asdf' , 'E390:' ) 385endif 386call assert_fails ( 'syntax spell asdf' , 'E390:' ) 387call assert_fails ( 'syntax clear @ABCD' , 'E391:' ) 388call assert_fails ( 'syntax include random_file' , 'E484:' ) 389call assert_fails ( 'syntax include <afile>' , 'E495:' ) 390call assert_fails ( 'syntax sync x' , 'E404:' ) 391call assert_fails ( 'syntax keyword Abc a[' , 'E789:' ) 392call assert_fails ( 'syntax keyword Abc a[bc]d' , 'E890:' ) 393call assert_fails ( 'syntax cluster Abc add=A add=' , 'E406:' ) 394 395" Test for too many \z\( and unmatched \z\( 396" Not able to use assert_fails() here because both E50:/E879: and E475: 397" messages are emitted. 398set regexpengine = 1 399call AssertFails ( "syntax region MyRegion start='\\z\\(' end='\\*/'" , 'E52:' ) 400 401let cmd = "syntax region MyRegion start='" 402let cmd ..= repeat ( "\\z\\(.\\)" , 10 ) .. "' end='\*/'" 403call AssertFails ( cmd , 'E50:' ) 404 405set regexpengine = 2 406call AssertFails ( "syntax region MyRegion start='\\z\\(' end='\\*/'" , 'E54:' ) 407 408let cmd = "syntax region MyRegion start='" 409let cmd ..= repeat ( "\\z\\(.\\)" , 10 ) .. "' end='\*/'" 410call AssertFails ( cmd , 'E879:' ) 411set regexpengine & 412 413call AssertFails ( 'syntax keyword cMyItem grouphere G1' , 'E393:' ) 414call AssertFails ( 'syntax sync match Abc grouphere MyItem "abc"' , 'E394:' ) 415call AssertFails ( 'syn keyword Type contains int' , 'E395:' ) 416call assert_fails ( 'syntax include @Xxx' , 'E397:' ) 417call AssertFails ( 'syntax region X start' , 'E398:' ) 418call assert_fails ( 'syntax region X start="{"' , 'E399:' ) 419call AssertFails ( 'syntax cluster contains=Abc' , 'E400:' ) 420call AssertFails ( "syntax match Character /'.'" , 'E401:' ) 421call AssertFails ( "syntax match Character /'.'/a" , 'E402:' ) 422call assert_fails ( 'syntax sync linecont /\%(/' , 'E53:' ) 423call assert_fails ( 'syntax sync linecont /pat' , 'E404:' ) 424call assert_fails ( 'syntax sync linecont' , 'E404:' ) 425call assert_fails ( 'syntax sync linecont /pat1/ linecont /pat2/' , 'E403:' ) 426call assert_fails ( 'syntax sync minlines=a' , 'E404:' ) 427call AssertFails ( 'syntax match ABC /x/ contains=' , 'E406:' ) 428call AssertFails ( "syntax match Character contains /'.'/" , 'E405:' ) 429call AssertFails ( 'syntax match ccFoo "Foo" nextgroup=ALLBUT,F' , 'E407:' ) 430call AssertFails ( 'syntax region Block start="{" contains=F,ALLBUT' , 'E408:' ) 431call AssertFails ( "syntax match Characters contains=a.*x /'.'/" , 'E409:' ) 432call assert_fails ( 'syntax match Search /abc/ contains=ALLBUT,/\%(/' , 'E53:' ) 433endfunc 434 435func Test_syn_sync () 436syntax region HereGroup start = /this /end = /that / 437syntax syncmatch SyncHere grouphereHereGroup "pattern " 438call assert_match ( 'SyncHere' , execute ( 'syntax sync' )) 439syn syncclear 440call assert_notmatch ( 'SyncHere' , execute ( 'syntax sync' )) 441 442syn sync minlines= 10 443syntax cluster xmlStuff contains = xmlGroup1 , xmlGroup2 444syntax region xmlComment start = /<!-- /end = /--> /keepend contains = @Spell 445syntax syncmatch xmlSync1 groupherexmlComment /<!--/ 446syntax syncmatch xmlSync2 groupthereNONE /-->/ 447syntax cluster xmlAll contains = ALL 448let out = execute ( 'syntax sync' ) 449call assert_match ( 'xmlSync1' , out ) 450call assert_match ( 'xmlSync2' , out ) 451call assert_match ( 'grouphere xmlComment' , out ) 452call assert_match ( 'groupthere NONE' , out ) 453call assert_notmatch ( 'xmlStuff' , out ) 454call assert_notmatch ( 'xmlAll' , out ) 455call assert_notmatch ( 'cluster' , out ) 456call assert_notmatch ( 'keepend' , out ) 457" should output 4 lines: 1 header + 3 syn sync lines 458call assert_equal ( 4 , len ( split ( out , '\n' ))) 459 460syn clear 461endfunc 462 463func Test_syn_clear () 464syntax keyword Foo foo 465syntax keyword Bar tar 466call assert_match ( 'Foo' , execute ( 'syntax' )) 467call assert_match ( 'Bar' , execute ( 'syntax' )) 468call assert_equal ( 'Foo' , synIDattr ( hlID ( "Foo" ), "name" )) 469syn clear Foo 470call assert_notmatch ( 'Foo' , execute ( 'syntax' )) 471call assert_match ( 'Bar' , execute ( 'syntax' )) 472call assert_equal ( 'Foo' , synIDattr ( hlID ( "Foo" ), "name" )) 473syn clear Foo Bar 474call assert_notmatch ( 'Foo' , execute ( 'syntax' )) 475call assert_notmatch ( 'Bar' , execute ( 'syntax' )) 476hi clear Foo 477call assert_equal ( 'Foo' , synIDattr ( hlID ( "Foo" ), "name" )) 478hi clear Bar 479call assert_fails ( 'syntax clear invalid_syngroup' , 'E28:' ) 480endfunc 481 482func Test_invalid_name () 483syn clear 484syn keyword Nop yes 485call assert_fails ( "syntax keyword Wr\x17ong bar" , 'E669:' ) 486syntax keyword @Wrong bar 487call assert_fails ( "syntax keyword @#Wrong bar" , 'E5248:' ) 488syn clear 489hi clear Nop 490hi clear @Wrong 491endfunc 492 493func Test_ownsyntax () 494new XfooOwnSyntax 495call setline ( 1 , '#define FOO' ) 496syntax on 497set filetype = c 498 499ownsyntax perl 500" this should not crash 501set 502 503call assert_equal ( 'perlComment' , synIDattr ( synID ( line ( '.' ), col ( '.' ), 1 ), 'name' )) 504call assert_equal ( 'c' , b: current_syntax ) 505call assert_equal ( 'perl' , w: current_syntax ) 506 507" A new split window should have the original syntax. 508split 509call assert_equal ( 'cDefine' , synIDattr ( synID ( line ( '.' ), col ( '.' ), 1 ), 'name' )) 510call assert_equal ( 'c' , b: current_syntax ) 511call assert_equal ( 0 , exists ( 'w:current_syntax' )) 512 513wincmd x 514call assert_equal ( 'perlComment' , synIDattr ( synID ( line ( "." ), col ( "." ), 1 ), "name" )) 515 516syntax off 517set filetype & 518 %bw ! 519endfunc 520 521func Test_ownsyntax_completion () 522call feedkeys ( ":ownsyntax java\<C-A>\<C-B>\"\<CR>" , 'tx' ) 523call assert_equal ( '"ownsyntax java javacc javascript javascriptreact' , @:) 524endfunc 525 526func Test_highlight_invalid_arg () 527if has ( 'gui_running' ) 528call assert_fails ( 'hi XXX guifg=xxx' , 'E254:' ) 529endif 530call assert_fails ( 'hi DoesNotExist' , 'E411:' ) 531call assert_fails ( 'hi link' , 'E412:' ) 532call assert_fails ( 'hi link a' , 'E412:' ) 533call assert_fails ( 'hi link a b c' , 'E413:' ) 534call assert_fails ( 'hi XXX =' , 'E415:' ) 535call assert_fails ( 'hi XXX cterm' , 'E416:' ) 536call assert_fails ( 'hi XXX cterm=' , 'E417:' ) 537call assert_fails ( 'hi XXX cterm=DoesNotExist' , 'E418:' ) 538call assert_fails ( 'hi XXX ctermfg=DoesNotExist' , 'E421:' ) 539call assert_fails ( 'hi XXX xxx=White' , 'E423:' ) 540endfunc 541 542func Test_conceal () 543if ! has ( 'conceal' ) 544return 545endif 546 547new 548call setline ( 1 , [ '' , '123456' ]) 549syn match test23 "23 "conceal cchar = X 550syn match test45 "45 "conceal 551 552set conceallevel = 0 553call assert_equal ( '123456 ' , ScreenLines ( 2 , 7 )[ 0 ]) 554call assert_equal ([[ 0 , '' , 0 ], [ 0 , '' , 0 ], [ 0 , '' , 0 ], [ 0 , '' , 0 ], [ 0 , '' , 0 ], [ 0 , '' , 0 ]], map ( range ( 1 , 6 ), 'synconcealed(2, v:val)' )) 555 556set conceallevel = 1 557call assert_equal ( '1X 6 ' , ScreenLines ( 2 , 7 )[ 0 ]) 558call assert_equal ([[ 0 , '' , 0 ], [ 1 , 'X' , 1 ], [ 1 , 'X' , 1 ], [ 1 , ' ' , 2 ], [ 1 , ' ' , 2 ], [ 0 , '' , 0 ]], map ( range ( 1 , 6 ), 'synconcealed(2, v:val)' )) 559 560set conceallevel = 1 561set listchars = conceal:Y 562call assert_equal ([[ 0 , '' , 0 ], [ 1 , 'X' , 1 ], [ 1 , 'X' , 1 ], [ 1 , 'Y' , 2 ], [ 1 , 'Y' , 2 ], [ 0 , '' , 0 ]], map ( range ( 1 , 6 ), 'synconcealed(2, v:val)' )) 563call assert_equal ( '1XY6 ' , ScreenLines ( 2 , 7 )[ 0 ]) 564 565set conceallevel = 2 566call assert_match ( '1X6 ' , ScreenLines ( 2 , 7 )[ 0 ]) 567call assert_equal ([[ 0 , '' , 0 ], [ 1 , 'X' , 1 ], [ 1 , 'X' , 1 ], [ 1 , '' , 2 ], [ 1 , '' , 2 ], [ 0 , '' , 0 ]], map ( range ( 1 , 6 ), 'synconcealed(2, v:val)' )) 568 569set conceallevel = 3 570call assert_match ( '16 ' , ScreenLines ( 2 , 7 )[ 0 ]) 571call assert_equal ([[ 0 , '' , 0 ], [ 1 , '' , 1 ], [ 1 , '' , 1 ], [ 1 , '' , 2 ], [ 1 , '' , 2 ], [ 0 , '' , 0 ]], map ( range ( 1 , 6 ), 'synconcealed(2, v:val)' )) 572 573call AssertFails ( "syntax match Entity '&' conceal cchar=\<Tab>" , 'E844:' ) 574 575syn clear 576set conceallevel & 577bw ! 578endfunc 579 580func Test_bg_detection () 581CheckNotGui 582 583" auto-detection of &bg, make sure it isn't set anywhere before this test 584hi Normal ctermbg= 0 585call assert_equal ( 'dark' , & bg ) 586hi Normal ctermbg= 4 587call assert_equal ( 'dark' , & bg ) 588hi Normal ctermbg= 12 589call assert_equal ( 'light' , & bg ) 590hi Normal ctermbg= 15 591call assert_equal ( 'light' , & bg ) 592 593" manually-set &bg takes precedence over auto-detection 594set bg = light 595hi Normal ctermbg= 4 596call assert_equal ( 'light' , & bg ) 597set bg = dark 598hi Normal ctermbg= 12 599call assert_equal ( 'dark' , & bg ) 600 601hi Normal ctermbg = NONE 602endfunc 603 604func Test_syntax_hangs () 605if ! has ( 'reltime' ) || ! has ( 'float' ) || ! has ( 'syntax' ) 606return 607endif 608 609" This pattern takes a long time to match, it should timeout. 610new 611call setline ( 1 , [ 'aaa' , repeat ( 'abc ' , 1000 ), 'ccc' ]) 612let start = reltime () 613set nolazyredraw redrawtime = 101 614syn match Error /\%#=1a * . * X\@<=b * / 615redraw 616let elapsed = reltimefloat ( reltime ( start )) 617call assert_true ( elapsed > 0.1 ) 618call assert_true ( elapsed < 1.0 ) 619 620" second time syntax HL is disabled 621let start = reltime () 622redraw 623let elapsed = reltimefloat ( reltime ( start )) 624call assert_true ( elapsed < 0.1 ) 625 626" after CTRL-L the timeout flag is reset 627let start = reltime () 628exe "normal \<C-L>" 629redraw 630let elapsed = reltimefloat ( reltime ( start )) 631call assert_true ( elapsed > 0.1 ) 632call assert_true ( elapsed < 1.0 ) 633 634set redrawtime & 635bwipe ! 636endfunc 637 638func Test_synstack_synIDtrans () 639new 640setfiletype c 641syntax on 642call setline ( 1 , ' /* A comment with a TODO */' ) 643 644call assert_equal ([], synstack ( 1 , 1 )) 645 646norm f/ 647eval synstack ( line ( "." ), col ( "." )) ->map ( 'synIDattr(v:val, "name")' ) ->assert_equal ([ 'cComment' , 'cCommentStart' ]) 648eval synstack ( line ( "." ), col ( "." )) ->map ( 'synIDattr(synIDtrans(v:val), "name")' ) ->assert_equal ([ 'Comment' , 'Comment' ]) 649 650norm fA 651call assert_equal ([ 'cComment' ], map ( synstack ( line ( "." ), col ( "." )), 'synIDattr(v:val, "name")' )) 652call assert_equal ([ 'Comment' ], map ( synstack ( line ( "." ), col ( "." )), 'synIDattr(synIDtrans(v:val), "name")' )) 653 654norm fT 655call assert_equal ([ 'cComment' , 'cTodo' ], map ( synstack ( line ( "." ), col ( "." )), 'synIDattr(v:val, "name")' )) 656call assert_equal ([ 'Comment' , 'Todo' ], map ( synstack ( line ( "." ), col ( "." )), 'synIDattr(synIDtrans(v:val), "name")' )) 657 658call assert_fails ( "let n=synIDtrans([])" , 'E745:' ) 659 660syn clear 661bw ! 662endfunc 663 664" Check highlighting for a small piece of C code with a screen dump. 665func Test_syntax_c () 666CheckScreendump 667CheckRunVimInTerminal 668call writefile ([ 669 \'/* comment line at the top */' , 670 \'int main(int argc, char **argv) { // another comment' , 671 \'#if 0' , 672 \' int not_used;' , 673 \'#else' , 674 \' int used;' , 675 \'#endif' , 676 \' printf("Just an example piece of C code\n");' , 677 \' return 0x0ff;' , 678 \'}' , 679 \"\t\t " , 680 \' static void' , 681 \'myFunction(const double count, struct nothing, long there) {' , 682 \"\t// 123: nothing to endif here" , 683 \"\tfor (int i = 0; i < count; ++i) {" , 684 \"\t break;" , 685 \"\t}" , 686 \"\tNote: asdf" , 687 \'}' , 688 \], 'Xtest.c' , 'D' ) 689 690" This makes the default for 'background' use "dark", check that the 691" response to t_RB corrects it to "light". 692let $ COLORFGBG = '15;0' 693 694let buf = RunVimInTerminal ( 'Xtest.c' , {}) 695call term_sendkeys ( buf , ":syn keyword Search Note\r" ) 696call VerifyScreenDump ( buf , 'Test_syntax_c_01' , {}) 697call StopVimInTerminal ( buf ) 698 699let $ COLORFGBG = '' 700endfun 701 702" Test \z(...) along with \z1 703func Test_syn_zsub () 704new 705syntax on 706call setline ( 1 , 'xxx start foo xxx not end foo xxx end foo xxx' ) 707let l: expected = ' ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ' 708 709for l: re in [ 0 , 1 , 2 ] 710" Example taken from :help :syn-ext-match 711syntax region Z start = "start \z(\I\i * \) "skip = "not end \z1 "end = "end \z1 " 712eval AssertHighlightGroups ( 1 , 1 , l: expected , 1 , 'regexp=' .. l: re ) 713syntax clear Z 714endfor 715 716set re & 717bw ! 718endfunc 719 720func s: SynDumpBuffer ( ft , lines ) 721new 722syntax on 723execute 'setfiletype ' .. a: ft 724call setline ( 1 , a: lines ) 725let result = [] 726for lnum in range ( 1 , line ( '$' )) 727for col in range ( 1 , max ([ 1 , len ( getline ( lnum ))])) 728call add ( result , synIDattr ( synID ( lnum , col , 1 ), 'name' )) 729endfor 730endfor 731bwipe ! 732return result 733endfunc 734 735" The in_id_list() cache is a speed optimization only: highlighting must be 736" identical with the cache on (default) and off. Compare full-buffer synID() 737" output both ways across filetypes that use "contains"/cluster lists. 738func Test_syntax_idlist_cache_unchanged () 739throw 'Skipped: Nvim does not support test_override()' 740let samples = { 741 \'c' : [ '#define M 0x1F' , "char c = '\\n';" , 742 \'int f(int a) { return a + 0xAB; }' ], 743 \'vim' : [ 'func <SID>Foo() abort' , ' let x = [1, 2] + {"k": 0z1f}' , 744 \'endfunc' ], 745 \'python' : [ 'class C:' , ' def m(self): return {1: "s", 2: r"\d"}' ], 746 \'sh' : [ 'case $x in' , ' a) echo "${y:-0xAB}";;' , 'esac' ], 747 \} 748for ft in sort ( keys ( samples )) 749call test_override ( 'syn_idlist_cache' , 0 ) 750let l: on = s: SynDumpBuffer ( ft , samples [ ft ]) 751call test_override ( 'syn_idlist_cache' , 1 ) 752let l: off = s: SynDumpBuffer ( ft , samples [ ft ]) 753call test_override ( 'ALL' , 0 ) 754call assert_equal ( l: off , l: on , 'filetype ' .. ft ) 755endfor 756endfunc 757 758" Using \z() in a region with NFA failing should not crash. 759func Test_syn_wrong_z_one () 760new 761call setline ( 1 , [ 'just some text' , 'with foo and bar to match with' ]) 762syn region FooBar start = "foo\z(. * \)bar "end = "\z1 " 763" call test_override("nfa_fail", 1) 764redraw ! 765redraw ! 766" call test_override("ALL", 0) 767bwipe ! 768endfunc 769 770func Test_syntax_after_bufdo () 771call writefile ([ '/* aaa comment */' ], 'Xaaa.c' , 'D' ) 772call writefile ([ '/* bbb comment */' ], 'Xbbb.c' , 'D' ) 773call writefile ([ '/* ccc comment */' ], 'Xccc.c' , 'D' ) 774call writefile ([ '/* ddd comment */' ], 'Xddd.c' , 'D' ) 775 776let bnr = bufnr ( '%' ) 777new Xaaa.c 778badd Xbbb.c 779badd Xccc.c 780badd Xddd.c 781exe "bwipe " . bnr 782let l = [] 783bufdo call add(l, bufnr('%')) 784call assert_equal ( 4 , len ( l )) 785 786syntax on 787 788" This used to only enable syntax HL in the last buffer. 789bufdo tab split 790tabrewind 791for tab in range ( 1 , 4 ) 792norm fm 793call assert_equal ([ 'cComment' ], map ( synstack ( line ( "." ), col ( "." )), 'synIDattr(v:val, "name")' )) 794tabnext 795endfor 796 797bwipe ! Xaaa.c 798bwipe ! Xbbb.c 799bwipe ! Xccc.c 800bwipe ! Xddd.c 801syntax off 802endfunc 803 804func Test_syntax_foldlevel () 805new 806call setline ( 1 , [ 807 \'void f(int a)' , 808 \'{' , 809 \' if (a == 1) {' , 810 \' a = 0;' , 811 \' } else if (a == 2) {' , 812 \' a = 1;' , 813 \' } else {' , 814 \' a = 2;' , 815 \' }' , 816 \' if (a > 0) {' , 817 \' if (a == 1) {' , 818 \' a = 0;' , 819 \' } /* missing newline */ } /* end of outer if */ else {' , 820 \' a = 1;' , 821 \' }' , 822 \' if (a == 1)' , 823 \' {' , 824 \' a = 0;' , 825 \' }' , 826 \' else if (a == 2)' , 827 \' {' , 828 \' a = 1;' , 829 \' }' , 830 \' else' , 831 \' {' , 832 \' a = 2;' , 833 \' }' , 834 \'}' , 835 \]) 836setfiletype c 837syntax on 838set foldmethod = syntax 839 840call assert_fails ( 'syn foldlevel start start' , 'E390' ) 841call assert_fails ( 'syn foldlevel not_an_option' , 'E390' ) 842 843set foldlevel = 1 844 845syn foldlevel start 846redir @c 847syn foldlevel 848redir END 849call assert_equal ( "\nsyntax foldlevel start" , @c) 850syn sync fromstart 851call assert_match ( 'from the first line$' , execute ( 'syn sync' )) 852let a = map ( range ( 3 , 9 ), 'foldclosed(v:val)' ) 853call assert_equal ([ 3 , 3 , 3 , 3 , 3 , 3 , 3 ], a ) " attached cascade folds together 854let a = map ( range ( 10 , 15 ), 'foldclosed(v:val)' ) 855call assert_equal ([ 10 , 10 , 10 , 10 , 10 , 10 ], a ) " over-attached 'else' hidden 856let a = map ( range ( 16 , 27 ), 'foldclosed(v:val)' ) 857let unattached_results = [ -1 , 17 , 17 , 17 , -1 , 21 , 21 , 21 , -1 , 25 , 25 , 25 ] 858call assert_equal ( unattached_results , a ) " unattached cascade folds separately 859 860syn foldlevel minimum 861redir @c 862syn foldlevel 863redir END 864call assert_equal ( "\nsyntax foldlevel minimum" , @c) 865syn sync fromstart 866let a = map ( range ( 3 , 9 ), 'foldclosed(v:val)' ) 867call assert_equal ([ 3 , 3 , 5 , 5 , 7 , 7 , 7 ], a ) " attached cascade folds separately 868let a = map ( range ( 10 , 15 ), 'foldclosed(v:val)' ) 869call assert_equal ([ 10 , 10 , 10 , 13 , 13 , 13 ], a ) " over-attached 'else' visible 870let a = map ( range ( 16 , 27 ), 'foldclosed(v:val)' ) 871call assert_equal ( unattached_results , a ) " unattached cascade folds separately 872 873set foldlevel = 2 874 875syn foldlevel start 876syn sync fromstart 877let a = map ( range ( 11 , 14 ), 'foldclosed(v:val)' ) 878call assert_equal ([ 11 , 11 , 11 , -1 ], a ) " over-attached 'else' hidden 879 880syn foldlevel minimum 881syn sync fromstart 882let a = map ( range ( 11 , 14 ), 'foldclosed(v:val)' ) 883call assert_equal ([ 11 , 11 , -1 , -1 ], a ) " over-attached 'else' visible 884 885quit ! 886endfunc 887 888func Test_search_syntax_skip () 889new 890let lines =<< trim END 891 892/* This is VIM */ 893Another Text for VIM 894let a = "VIM" 895END 896call setline ( 1 , lines ) 897syntax on 898syntax match Comment "^/\*. * \*/ " 899syntax match String '". * " ' 900 901" Skip argument using string evaluation. 9021 903call search ( 'VIM' , 'w' , '' , 0 , 'synIDattr(synID(line("."), col("."), 1), "name") =~? "comment"' ) 904call assert_equal ( 'Another Text for VIM' , getline ( '.' )) 905 9061 907call search ( 'VIM' , 'cw' , '' , 0 , 'synIDattr(synID(line("."), col("."), 1), "name") !~? "string"' ) 908call assert_equal ( ' let a = "VIM"' , getline ( '.' )) 909 910" Skip argument using Lambda. 9111 912call search ( 'VIM' , 'w' , '' , 0 , { ->synIDattr ( synID ( line ( "." ), col ( "." ), 1 ), "name" ) =~? "comment" }) 913call assert_equal ( 'Another Text for VIM' , getline ( '.' )) 914 9151 916call search ( 'VIM' , 'cw' , '' , 0 , { ->synIDattr ( synID ( line ( "." ), col ( "." ), 1 ), "name" ) !~? "string" }) 917call assert_equal ( ' let a = "VIM"' , getline ( '.' )) 918 919" Skip argument using funcref. 920func InComment () 921return synIDattr ( synID ( line ( "." ), col ( "." ), 1 ), "name" ) =~? "comment" 922endfunc 923func NotInString () 924return synIDattr ( synID ( line ( "." ), col ( "." ), 1 ), "name" ) !~? "string" 925endfunc 926 9271 928call search ( 'VIM' , 'w' , '' , 0 , function ( 'InComment' )) 929call assert_equal ( 'Another Text for VIM' , getline ( '.' )) 930 9311 932call search ( 'VIM' , 'cw' , '' , 0 , function ( 'NotInString' )) 933call assert_equal ( ' let a = "VIM"' , getline ( '.' )) 934 935delfunc InComment 936delfunc NotInString 937bwipe ! 938endfunc 939 940func Test_syn_contained_transparent () 941" Comments starting with "Regression:" show the result when the highlighting 942" span of the containing item is assigned to the contained region. 943syntax on 944 945let l: case = "Transparent region contained in region" 946new 947syntax region X start = /\[ /end = /\] /contained transparent 948syntax region Y start = /( /end = /) /contains = X 949 950call setline ( 1 , "==(--[~~]--)==" ) 951let l: expected = " YYYYYYYYYY " 952eval AssertHighlightGroups ( 1 , 1 , l: expected , 1 , l: case ) 953syntax clear Y X 954bw ! 955 956let l: case = "Transparent region extends region" 957new 958syntax region X start = /\[ /end = /\] /contained transparent 959syntax region Y start = /( /end = /) /end = /e /contains = X 960 961call setline ( 1 , "==(--[~~e~~]--)==" ) 962let l: expected = " YYYYYYYYYYYYY " 963" Regression: " YYYYYYY YYY " 964eval AssertHighlightGroups ( 1 , 1 , l: expected , 1 , l: case ) 965syntax clear Y X 966bw ! 967 968let l: case = "Nested transparent regions extend region" 969new 970syntax region X start = /\[ /end = /\] /contained transparent 971syntax region Y start = /( /end = /) /end = /e /contains = X 972 973call setline ( 1 , "==(--[~~e~~[~~e~~]~~e~~]--)==" ) 974let l: expected = " YYYYYYYYYYYYYYYYYYYYYYYYY " 975" Regression: " YYYYYYY YYYYYYYYY " 976eval AssertHighlightGroups ( 1 , 1 , l: expected , 1 , l: case ) 977syntax clear Y X 978bw ! 979 980let l: case = "Transparent region contained in match" 981new 982syntax region X start = /\[ /end = /\] /contained transparent 983syntax match Y /(.\{-}) /contains = X 984 985call setline ( 1 , "==(--[~~]--)==" ) 986let l: expected = " YYYYYYYYYY " 987eval AssertHighlightGroups ( 1 , 1 , l: expected , 1 , l: case ) 988syntax clear Y X 989bw ! 990 991let l: case = "Transparent region extends match" 992new 993syntax region X start = /\[ /end = /\] /contained transparent 994syntax match Y /(.\{-} [ e) ] /contains = X 995 996call setline ( 1 , "==(--[~~e~~]--)==" ) 997let l: expected = " YYYYYYYYYY " 998" Regression: " YYYYYYY " 999eval AssertHighlightGroups ( 1 , 1 , l: expected , 1 , l: case ) 1000syntax clear Y X 1001bw ! 1002 1003let l: case = "Nested transparent regions extend match" 1004new 1005syntax region X start = /\[ /end = /\] /contained transparent 1006syntax match Y /(.\{-} [ e) ] /contains = X 1007 1008call setline ( 1 , "==(--[~~e~~[~~e~~]~~e~~]--)==" ) 1009let l: expected = " YYYYYYYYYYYYYYYYYYYYYY " 1010" Regression: " YYYYYYY YYYYYY " 1011eval AssertHighlightGroups ( 1 , 1 , l: expected , 1 , l: case ) 1012syntax clear Y X 1013bw ! 1014endfunc 1015 1016func Test_syn_include_contains_TOP () 1017let l: case = "TOP in included syntax refers to top level of that included syntax" 1018new 1019syntax include @INCLUDED syntax/c.vim 1020syntax region FencedCodeBlockC start = /```c /end = /``` /contains = @INCLUDED 1021 1022call setline ( 1 , [ '```c' , '#if 0' , 'int' , '#else' , 'int' , '#endif' , '```' ]) 1023let l: expected = [ "cCppOutIf2" ] 1024eval AssertHighlightGroups ( 3 , 1 , l: expected , 1 ) 1025" cCppOutElse has contains=TOP 1026let l: expected = [ "cType" ] 1027eval AssertHighlightGroups ( 5 , 1 , l: expected , 1 , l: case ) 1028syntax clear 1029bw ! 1030endfunc 1031 1032func Test_syn_include_contains_TOP_excluding () 1033new 1034syntax include @INCLUDED syntax/c.vim 1035syntax region FencedCodeBlockC start = /```c /end = /``` /contains = @INCLUDED 1036 1037call setline ( 1 , [ '```c' , '#if 0' , 'int' , '#else' , 'int' , '#if' , '#endif' , '```' ]) 1038let l: expected = [ "cCppOutElse" , "cConditional" ] 1039eval AssertHighlightGroups ( 6 , 1 , l: expected , 1 ) 1040syntax clear 1041bw ! 1042endfunc 1043 1044" This was using freed memory 1045func Test_WinEnter_synstack_synID () 1046autocmd WinEnter * call synstack ( line ( "." ), col ( "." )) 1047autocmd WinEnter * call synID ( line ( '.' ), col ( '.' ) - 1 , 1 ) 1048call setline ( 1 , 'aaaaa' ) 1049normal ! $ 1050new 1051close 1052 1053au ! WinEnter 1054bw ! 1055endfunc 1056 1057" This was going beyond the end of the "foo" line 1058func Test_syn_sync_grouphere_shorter_next_line () 1059let lines =<< trim END 1060if [[ "$var1" == 1 ]]; then 1061foo 1062else 1063bar 1064fi 1065END 1066let lines = [ 'a' ] ->repeat ( 50 ) + lines + [ 'a' ] ->repeat ( 48 ) 1067 106820 new 1069call setline ( 1 , lines ) 1070syn region shIf transparent 1071 \start = "\<if\_s "skip = +-fi\> +end = "\<;\_s * then\> "end = "\<fi\> " 1072syn sync minlines= 20 maxlines=40 1073syn syncmatch shIfSync grouphereshIf "\<if\> " 1074redraw ! 1075 1076normal ! G 1077" Should not go beyond end of line 1078redraw ! 1079 1080bw ! 1081endfunc 1082 1083" vim: shiftwidth=2 sts=2 expandtab