robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear , eq , neq =n . clear , t . eq , t . neq 6local command =n . command 7local exec_capture =n . exec_capture 8local api =n . api 9local fn =n . fn 10local pcall_err =t . pcall_err 11local ok =t . ok 12local assert_alive =n . assert_alive 13 14describe ( 'API: set highlight' , function () 15local highlight_color ={ 16fg =tonumber ( '0xff0000' ), 17bg =tonumber ( '0x0032aa' ), 18ctermfg =8 , 19ctermbg =15 , 20} 21local highlight1 ={ 22bg =highlight_color . bg , 23fg =highlight_color . fg , 24bold =true , 25italic =true , 26} 27local highlight2_config ={ 28ctermbg =highlight_color . ctermbg , 29ctermfg =highlight_color . ctermfg , 30underline =true , 31reverse =true , 32} 33local highlight2_result ={ 34ctermbg =highlight_color . ctermbg , 35ctermfg =highlight_color . ctermfg , 36underline =true , 37reverse =true , 38} 39local highlight3_config ={ 40bg =highlight_color . bg , 41fg =highlight_color . fg , 42ctermbg =highlight_color . ctermbg , 43ctermfg =highlight_color . ctermfg , 44bold =true , 45italic =true , 46reverse =true , 47underdashed =true , 48strikethrough =true , 49altfont =true , 50dim =true , 51blink =true , 52conceal =true , 53overline =true , 54cterm ={ 55italic =true , 56reverse =true , 57strikethrough =true , 58altfont =true , 59dim =true , 60blink =true , 61conceal =true , 62overline =true , 63nocombine =true , 64} , 65} 66local highlight3_result_gui ={ 67bg =highlight_color . bg , 68fg =highlight_color . fg , 69bold =true , 70italic =true , 71reverse =true , 72underdashed =true , 73strikethrough =true , 74altfont =true , 75dim =true , 76blink =true , 77conceal =true , 78overline =true , 79} 80local highlight3_result_cterm ={ 81ctermbg =highlight_color . ctermbg , 82ctermfg =highlight_color . ctermfg , 83italic =true , 84reverse =true , 85strikethrough =true , 86altfont =true , 87dim =true , 88blink =true , 89conceal =true , 90overline =true , 91nocombine =true , 92} 93 94local function get_ns () 95local ns =api . nvim_create_namespace ( 'Test_set_hl' ) 96api . nvim_set_hl_ns ( ns ) 97return ns 98end 99 100---@param expect table<string, any> 101---@param result table<string, any> 102---@param cterm? boolean 103local function match ( expect , result , cterm ) 104for k , v in pairs ( expect ) do 105eq ( v , cterm and result . cterm [ k ] or result [ k ]) 106end 107end 108 109before_each ( clear ) 110 111it ( 'validation' , function () 112eq ( 113"Invalid 'blend': out of range" , 114pcall_err ( api . nvim_set_hl , 0 , 'Test_hl3' , { fg ='#FF00FF' , blend =999 } ) 115) 116eq ( 117"Invalid 'blend': expected Integer, got Array" , 118pcall_err ( api . nvim_set_hl , 0 , 'Test_hl3' , { fg ='#FF00FF' , blend ={} } ) 119) 120-- 'url' is rejected. #38162 121eq ( "Invalid key: 'url'" , pcall_err ( api . nvim_set_hl , 0 , 'Test' , { url ='https://example.com' } )) 122assert_alive () 123end ) 124 125it ( 'can set gui highlight' , function () 126local ns =get_ns () 127api . nvim_set_hl ( ns , 'Test_hl' , highlight1 ) 128match ( highlight1 , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 129end ) 130 131it ( 'can set cterm highlight' , function () 132local ns =get_ns () 133api . nvim_set_hl ( ns , 'Test_hl' , highlight2_config ) 134match ( highlight2_result , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 135end ) 136 137it ( 'can set empty cterm attr' , function () 138local ns =get_ns () 139api . nvim_set_hl ( ns , 'Test_hl' , { cterm ={} } ) 140eq ( {} , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 141end ) 142 143it ( 'cterm attr defaults to gui attr' , function () 144local ns =get_ns () 145api . nvim_set_hl ( ns , 'Test_hl' , highlight1 ) 146match ( { 147bold =true , 148italic =true , 149} , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 150end ) 151 152it ( 'can overwrite attr for cterm #test' , function () 153local ns =get_ns () 154api . nvim_set_hl ( ns , 'Test_hl' , highlight3_config ) 155match ( highlight3_result_gui , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 156match ( highlight3_result_cterm , api . nvim_get_hl ( ns , { name ='Test_hl' } ), true ) 157end ) 158 159it ( 'only allows one underline attribute #22371' , function () 160local ns =get_ns () 161api . nvim_set_hl ( ns , 'Test_hl' , { 162underdouble =true , 163underdotted =true , 164cterm ={ 165underline =true , 166undercurl =true , 167} , 168} ) 169local result =api . nvim_get_hl ( ns , { name ='Test_hl' } ) 170match ( { undercurl =true } , result , true ) 171match ( { underdotted =true } , result ) 172end ) 173 174it ( 'can set all underline cterm attributes #31385' , function () 175local ns =get_ns () 176local attrs ={ 'underline' , 'undercurl' , 'underdouble' , 'underdotted' , 'underdashed' } 177for _ , attr in ipairs ( attrs ) do 178api . nvim_set_hl ( ns , 'Test_' .. attr , { cterm ={ [ attr ] =true } } ) 179match ( { [ attr ] =true } , api . nvim_get_hl ( ns , { name ='Test_' .. attr } ), true ) 180end 181end ) 182 183it ( 'can set a highlight in the global namespace' , function () 184api . nvim_set_hl ( 0 , 'Test_hl' , highlight2_config ) 185eq ( 186'Test_hl xxx cterm=underline,reverse ctermfg=8 ctermbg=15 gui=underline,reverse' , 187exec_capture ( 'highlight Test_hl' ) 188) 189 190api . nvim_set_hl ( 0 , 'Test_hl' , { background =highlight_color . bg } ) 191eq ( 'Test_hl xxx guibg=#0032aa' , exec_capture ( 'highlight Test_hl' )) 192 193api . nvim_set_hl ( 0 , 'Test_hl2' , highlight3_config ) 194eq ( 195'Test_hl2 xxx cterm=italic,reverse,strikethrough,altfont,dim,blink,conceal,overline,nocombine ctermfg=8 ctermbg=15 gui=bold,underdashed,italic,reverse,strikethrough,altfont,dim,blink,conceal,overline guifg=#ff0000 guibg=#0032aa' , 196exec_capture ( 'highlight Test_hl2' ) 197) 198 199-- Colors are stored with the name they are defined, but 200-- with canonical casing 201api . nvim_set_hl ( 0 , 'Test_hl3' , { bg ='reD' , fg ='bLue' } ) 202eq ( 'Test_hl3 xxx guifg=Blue guibg=Red' , exec_capture ( 'highlight Test_hl3' )) 203end ) 204 205it ( 'can modify a highlight in the global namespace' , function () 206api . nvim_set_hl ( 0 , 'Test_hl3' , { bg ='red' , fg ='blue' } ) 207eq ( 'Test_hl3 xxx guifg=Blue guibg=Red' , exec_capture ( 'highlight Test_hl3' )) 208 209api . nvim_set_hl ( 0 , 'Test_hl3' , { bg ='red' } ) 210eq ( 'Test_hl3 xxx guibg=Red' , exec_capture ( 'highlight Test_hl3' )) 211 212api . nvim_set_hl ( 0 , 'Test_hl3' , { ctermbg =9 , ctermfg =12 } ) 213eq ( 'Test_hl3 xxx ctermfg=12 ctermbg=9' , exec_capture ( 'highlight Test_hl3' )) 214 215api . nvim_set_hl ( 0 , 'Test_hl3' , { ctermbg ='red' , ctermfg ='blue' } ) 216eq ( 'Test_hl3 xxx ctermfg=12 ctermbg=9' , exec_capture ( 'highlight Test_hl3' )) 217 218api . nvim_set_hl ( 0 , 'Test_hl3' , { ctermbg =9 } ) 219eq ( 'Test_hl3 xxx ctermbg=9' , exec_capture ( 'highlight Test_hl3' )) 220 221eq ( 222"Invalid highlight color: 'redd'" , 223pcall_err ( api . nvim_set_hl , 0 , 'Test_hl3' , { fg ='redd' } ) 224) 225 226eq ( 227"Invalid highlight color: 'bleu'" , 228pcall_err ( api . nvim_set_hl , 0 , 'Test_hl3' , { ctermfg ='bleu' } ) 229) 230 231api . nvim_set_hl ( 0 , 'Test_hl3' , { fg ='#FF00FF' } ) 232eq ( 'Test_hl3 xxx guifg=#ff00ff' , exec_capture ( 'highlight Test_hl3' )) 233 234eq ( 235"Invalid highlight color: '#FF00FF'" , 236pcall_err ( api . nvim_set_hl , 0 , 'Test_hl3' , { ctermfg ='#FF00FF' } ) 237) 238 239for _ , fg_val in ipairs { nil , 'NONE' , 'nOnE' , '' , - 1 } do 240api . nvim_set_hl ( 0 , 'Test_hl3' , { fg =fg_val } ) 241eq ( 'Test_hl3 xxx cleared' , exec_capture ( 'highlight Test_hl3' )) 242end 243 244api . nvim_set_hl ( 0 , 'Test_hl3' , { fg ='#FF00FF' , blend =50 } ) 245eq ( 'Test_hl3 xxx guifg=#ff00ff blend=50' , exec_capture ( 'highlight Test_hl3' )) 246end ) 247 248it ( "correctly sets 'Normal' internal properties" , function () 249-- Normal has some special handling internally. #18024 250api . nvim_set_hl ( 0 , 'Normal' , { fg ='#000083' , bg ='#0000F3' } ) 251eq ( { fg =131 , bg =243 } , api . nvim_get_hl ( 0 , { name ='Normal' } )) 252end ) 253 254it ( 'does not segfault on invalid group name #20009' , function () 255eq ( 256'Vim:E5248: Invalid character in group name' , 257pcall_err ( api . nvim_set_hl , 0 , 'foo bar' , { bold =true } ) 258) 259assert_alive () 260end ) 261 262it ( 'can be silenced if there are too many groups #38930' , function () 263local n_groups =vim . tbl_count ( api . nvim_get_hl ( 0 , {} )) 264local has_fail =false 265for i =n_groups + 1 , 20000 do 266local _ , msg =pcall ( api . nvim_set_hl , 0 , 'New_' .. i , { fg ='#000000' } ) 267local is_fail =type ( msg ) == 'string' 268and msg : find ( 'Too many highlight and syntax groups$' ) ~= nil 269has_fail =has_fail or is_fail 270end 271eq ( '' , exec_capture ( 'messages' )) 272eq ( true , has_fail ) 273end ) 274 275it ( 'update=true sets only specified keys' , function () 276api . nvim_set_hl ( 0 , 'TestGroup' , { fg ='#ff0000' , bg ='#0000ff' , bold =true } ) 277api . nvim_set_hl ( 0 , 'TestGroup' , { bg ='#00ff00' , update =true } ) 278local hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 279eq ( tonumber ( '0xff0000' ), hl . fg ) 280eq ( tonumber ( '0x00ff00' ), hl . bg ) 281eq ( true , hl . bold ) 282 283api . nvim_set_hl ( 0 , 'TestGroup' , { bold =false , update =true } ) 284hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 285eq ( nil , hl . bold ) 286eq ( tonumber ( '0xff0000' ), hl . fg ) 287 288api . nvim_set_hl ( 0 , 'TestGroup' , { italic =true } ) 289 290hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 291eq ( nil , hl . fg ) 292eq ( nil , hl . bg ) 293eq ( true , hl . italic ) 294 295local ns =api . nvim_create_namespace ( 'test' ) 296api . nvim_set_hl ( ns , 'TestGroup' , { fg ='#ff0000' , italic =true } ) 297api . nvim_set_hl ( ns , 'TestGroup' , { fg ='#00ff00' , update =true } ) 298hl =api . nvim_get_hl ( ns , { name ='TestGroup' } ) 299eq ( tonumber ( '0x00ff00' ), hl . fg ) 300eq ( true , hl . italic ) 301 302api . nvim_set_hl ( 0 , 'NamedColor' , { fg ='red' , bg ='blue' } ) 303api . nvim_set_hl ( 0 , 'LinkedGroup' , { link ='NamedColor' } ) 304api . nvim_set_hl ( 0 , 'LinkedGroup' , { bold =true , fg ='green' , update =true } ) 305hl =api . nvim_get_hl ( 0 , { name ='LinkedGroup' } ) 306eq ( nil , hl . link ) 307eq ( true , hl . bold ) 308eq ( 309'LinkedGroup xxx cterm=bold gui=bold guifg=Green guibg=Blue' , 310n . exec_capture ( 'hi LinkedGroup' ) 311) 312api . nvim_set_hl ( 0 , 'LinkedGroup' , { bg ='#121314' , update =true } ) 313eq ( 314'LinkedGroup xxx cterm=bold gui=bold guifg=Green guibg=#121314' , 315n . exec_capture ( 'hi LinkedGroup' ) 316) 317 318-- underline style flags: false must not corrupt other styles 319local unders ={ 'underline' , 'undercurl' , 'underdouble' , 'underdotted' , 'underdashed' } 320for _ , a in ipairs ( unders ) do 321for _ , b in ipairs ( unders ) do 322if a ~= b then 323api . nvim_set_hl ( 0 , 'TestGroup' , { [ a ] =true , [ b ] =false } ) 324hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 325eq ( true , hl [ a ]) 326eq ( nil , hl [ b ]) 327end 328end 329end 330api . nvim_set_hl ( 0 , 'TestGroup' , { underdouble =true , fg ='#ff0000' , bold =true } ) 331api . nvim_set_hl ( 0 , 'TestGroup' , { fg ='#00ff00' , update =true } ) 332hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 333eq ( true , hl . underdouble ) 334eq ( true , hl . bold ) 335eq ( 65280 , hl . fg ) 336 337api . nvim_set_hl ( 0 , 'TestGroup' , { underdashed =true , update =true } ) 338hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 339eq ( true , hl . underdashed ) 340eq ( nil , hl . underdouble ) 341 342api . nvim_set_hl ( 0 , 'TestGroup' , { underdouble =true , bold =true } ) 343api . nvim_set_hl ( 0 , 'TestGroup' , { underdashed =false , update =true } ) 344hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 345eq ( true , hl . underdouble ) 346api . nvim_set_hl ( 0 , 'TestGroup' , { underdouble =false , update =true } ) 347hl =api . nvim_get_hl ( 0 , { name ='TestGroup' } ) 348eq ( nil , hl . underdouble ) 349eq ( true , hl . bold ) 350end ) 351 352it ( 'can set font' , function () 353local ns =api . nvim_create_namespace ( 'test_font' ) 354api . nvim_set_hl ( ns , 'TestFont' , { fg ='#ff0000' , font ='Courier New 10' } ) 355local hl =api . nvim_get_hl ( ns , { name ='TestFont' } ) 356eq ( 'Courier New 10' , hl . font ) 357eq ( 16711680 , hl . fg ) 358 359-- update=true keeps the font when it isn't re-specified 360api . nvim_set_hl ( ns , 'TestFont' , { italic =true , update =true } ) 361eq ( 'Courier New 10' , api . nvim_get_hl ( ns , { name ='TestFont' } ). font ) 362 363api . nvim_set_hl ( ns , 'TestFont' , { font ='Monaco' } ) 364hl =api . nvim_get_hl ( ns , { name ='TestFont' } ) 365eq ( 'Monaco' , hl . font ) 366 367-- "NONE" or an empty string clears the font 368for _ , v in ipairs ( { 'NONE' , '' } ) do 369api . nvim_set_hl ( ns , 'TestFont' , { font ='Monaco' } ) 370api . nvim_set_hl ( ns , 'TestFont' , { font =v } ) 371eq ( nil , api . nvim_get_hl ( ns , { name ='TestFont' } ). font ) 372end 373 374-- a font-only highlight (no other attrs) must not be dropped 375api . nvim_set_hl ( 0 , 'TestFontGlobal' , { font ='JetBrains Mono' } ) 376eq ( 'JetBrains Mono' , api . nvim_get_hl ( 0 , { name ='TestFontGlobal' } ). font ) 377end ) 378 379it ( 'higher-priority overlapping extmark font wins' , function () 380api . nvim_set_hl ( 0 , 'FontLo' , { fg ='#ffffff' , font ='Courier New 10' } ) 381api . nvim_set_hl ( 0 , 'FontHi' , { fg ='#ff00ff' , font ='Monaco' } ) 382local ns =api . nvim_create_namespace ( 'test_font_combine' ) 383api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'hello' } ) 384api . nvim_buf_set_extmark ( 0 , ns , 0 , 0 , { end_col =1 , hl_group ='FontLo' , priority =100 } ) 385api . nvim_buf_set_extmark ( 0 , ns , 0 , 0 , { end_col =1 , hl_group ='FontHi' , priority =200 } ) 386command ( 'redraw' ) 387local cell =api . nvim__inspect_cell ( 1 , 0 , 0 )[ 2 ] 388eq ( 16711935 , cell . foreground ) 389eq ( 'Monaco' , cell . font ) 390end ) 391end ) 392 393describe ( 'API: get highlight' , function () 394local highlight_color ={ 395fg =tonumber ( '0xff0000' ), 396bg =tonumber ( '0x0032aa' ), 397ctermfg =8 , 398ctermbg =15 , 399} 400local highlight1 ={ 401bg =highlight_color . bg , 402fg =highlight_color . fg , 403bold =true , 404italic =true , 405cterm ={ bold =true , italic =true } , 406} 407local highlight2 ={ 408ctermbg =highlight_color . ctermbg , 409ctermfg =highlight_color . ctermfg , 410underline =true , 411reverse =true , 412cterm ={ underline =true , reverse =true } , 413} 414local highlight3_config ={ 415bg =highlight_color . bg , 416fg =highlight_color . fg , 417ctermbg =highlight_color . ctermbg , 418ctermfg =highlight_color . ctermfg , 419bold =true , 420italic =true , 421reverse =true , 422underdashed =true , 423strikethrough =true , 424altfont =true , 425dim =true , 426blink =true , 427conceal =true , 428overline =true , 429cterm ={ 430italic =true , 431reverse =true , 432strikethrough =true , 433altfont =true , 434dim =true , 435blink =true , 436conceal =true , 437overline =true , 438nocombine =true , 439} , 440} 441local highlight3_result ={ 442bg =highlight_color . bg , 443fg =highlight_color . fg , 444ctermbg =highlight_color . ctermbg , 445ctermfg =highlight_color . ctermfg , 446bold =true , 447italic =true , 448reverse =true , 449underdashed =true , 450strikethrough =true , 451altfont =true , 452dim =true , 453blink =true , 454conceal =true , 455overline =true , 456cterm ={ 457italic =true , 458nocombine =true , 459reverse =true , 460strikethrough =true , 461altfont =true , 462dim =true , 463blink =true , 464conceal =true , 465overline =true , 466} , 467} 468 469local function get_ns () 470-- Test namespace filtering behavior 471local ns2 =api . nvim_create_namespace ( 'Another_namespace' ) 472api . nvim_set_hl ( ns2 , 'Test_hl' , { ctermfg =23 } ) 473api . nvim_set_hl ( ns2 , 'Test_another_hl' , { link ='Test_hl' } ) 474api . nvim_set_hl ( ns2 , 'Test_hl_link' , { link ='Test_another_hl' } ) 475api . nvim_set_hl ( ns2 , 'Test_another_hl_link' , { link ='Test_hl_link' } ) 476 477local ns =api . nvim_create_namespace ( 'Test_set_hl' ) 478api . nvim_set_hl_ns ( ns ) 479 480return ns 481end 482 483before_each ( clear ) 484 485it ( 'validation' , function () 486eq ( 487"Invalid 'name': expected String, got Integer" , 488pcall_err ( api . nvim_get_hl , 0 , { name =177 } ) 489) 490eq ( 'Highlight id out of bounds' , pcall_err ( api . nvim_get_hl , 0 , { name ='Test set hl' } )) 491end ) 492 493it ( 'nvim_get_hl with create flag' , function () 494eq ( {} , api . nvim_get_hl ( 0 , { name ='Foo' , create =false } )) 495eq ( 0 , fn . hlexists ( 'Foo' )) 496api . nvim_get_hl ( 0 , { name ='Bar' , create =true } ) 497eq ( 1 , fn . hlexists ( 'Bar' )) 498api . nvim_get_hl ( 0 , { name ='FooBar' } ) 499eq ( 1 , fn . hlexists ( 'FooBar' )) 500end ) 501 502it ( 'can get all highlights in current namespace' , function () 503local ns =get_ns () 504api . nvim_set_hl ( ns , 'Test_hl' , { bg ='#B4BEFE' } ) 505api . nvim_set_hl ( ns , 'Test_hl_link' , { link ='Test_hl' } ) 506eq ( { 507Test_hl ={ 508bg =11845374 , 509} , 510Test_hl_link ={ 511link ='Test_hl' , 512} , 513} , api . nvim_get_hl ( ns , {} )) 514end ) 515 516it ( 'can get gui highlight' , function () 517local ns =get_ns () 518api . nvim_set_hl ( ns , 'Test_hl' , highlight1 ) 519eq ( highlight1 , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 520end ) 521 522it ( 'can get cterm highlight' , function () 523local ns =get_ns () 524api . nvim_set_hl ( ns , 'Test_hl' , highlight2 ) 525eq ( highlight2 , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 526end ) 527 528it ( 'can get empty cterm attr' , function () 529local ns =get_ns () 530api . nvim_set_hl ( ns , 'Test_hl' , { cterm ={} } ) 531eq ( {} , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 532end ) 533 534it ( 'cterm attr defaults to gui attr' , function () 535local ns =get_ns () 536api . nvim_set_hl ( ns , 'Test_hl' , highlight1 ) 537eq ( highlight1 , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 538end ) 539 540it ( 'can overwrite attr for cterm' , function () 541local ns =get_ns () 542api . nvim_set_hl ( ns , 'Test_hl' , highlight3_config ) 543eq ( highlight3_result , api . nvim_get_hl ( ns , { name ='Test_hl' } )) 544end ) 545 546it ( 'only allows one underline attribute #22371' , function () 547local ns =get_ns () 548api . nvim_set_hl ( ns , 'Test_hl' , { 549underdouble =true , 550underdotted =true , 551cterm ={ 552underline =true , 553undercurl =true , 554} , 555} ) 556eq ( 557{ underdotted =true , cterm ={ undercurl =true } } , 558api . nvim_get_hl ( ns , { name ='Test_hl' } ) 559) 560end ) 561 562it ( 'can get a highlight in the global namespace' , function () 563api . nvim_set_hl ( 0 , 'Test_hl' , highlight2 ) 564eq ( highlight2 , api . nvim_get_hl ( 0 , { name ='Test_hl' } )) 565 566api . nvim_set_hl ( 0 , 'Test_hl' , { background =highlight_color . bg } ) 567eq ( { 568bg =12970 , 569} , api . nvim_get_hl ( 0 , { name ='Test_hl' } )) 570 571api . nvim_set_hl ( 0 , 'Test_hl2' , highlight3_config ) 572eq ( highlight3_result , api . nvim_get_hl ( 0 , { name ='Test_hl2' } )) 573 574-- Colors are stored with the name they are defined, but 575-- with canonical casing 576api . nvim_set_hl ( 0 , 'Test_hl3' , { bg ='reD' , fg ='bLue' } ) 577eq ( { 578bg =16711680 , 579fg =255 , 580} , api . nvim_get_hl ( 0 , { name ='Test_hl3' } )) 581end ) 582 583it ( 'nvim_get_hl by id' , function () 584local hl_id =api . nvim_get_hl_id_by_name ( 'NewHighlight' ) 585 586command ( 587'hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold' 588) 589eq ( { 590fg =16711680 , 591bg =16776960 , 592sp =255 , 593bold =true , 594ctermbg =10 , 595cterm ={ underline =true } , 596} , api . nvim_get_hl ( 0 , { id =hl_id } )) 597 598-- Test 0 argument 599eq ( 'Highlight id out of bounds' , pcall_err ( api . nvim_get_hl , 0 , { id =0 } )) 600 601eq ( 602"Invalid 'id': expected Integer, got String" , 603pcall_err ( api . nvim_get_hl , 0 , { id ='Test_set_hl' } ) 604) 605 606-- Test all highlight properties. 607command ( 608'hi NewHighlight gui=underline,bold,italic,reverse,strikethrough,altfont,dim,blink,conceal,overline,nocombine' 609) 610eq ( { 611fg =16711680 , 612bg =16776960 , 613sp =255 , 614altfont =true , 615blink =true , 616bold =true , 617conceal =true , 618dim =true , 619italic =true , 620nocombine =true , 621overline =true , 622reverse =true , 623strikethrough =true , 624underline =true , 625ctermbg =10 , 626cterm ={ underline =true } , 627} , api . nvim_get_hl ( 0 , { id =hl_id } )) 628 629-- Test undercurl 630command ( 'hi NewHighlight gui=undercurl' ) 631eq ( { 632fg =16711680 , 633bg =16776960 , 634sp =255 , 635undercurl =true , 636ctermbg =10 , 637cterm ={ underline =true } , 638} , api . nvim_get_hl ( 0 , { id =hl_id } )) 639end ) 640 641it ( 'can correctly detect links' , function () 642command ( 'hi String guifg=#a6e3a1 ctermfg=NONE' ) 643command ( 'hi link @string string' ) 644command ( 'hi link @string.cpp @string' ) 645eq ( { fg =10937249 } , api . nvim_get_hl ( 0 , { name ='String' } )) 646eq ( { link ='String' } , api . nvim_get_hl ( 0 , { name ='@string' } )) 647eq ( { fg =10937249 } , api . nvim_get_hl ( 0 , { name ='@string.cpp' , link =false } )) 648end ) 649 650it ( 'can get all attributes for a linked group' , function () 651command ( 'hi Bar guifg=red' ) 652command ( 'hi Foo guifg=#00ff00 gui=bold,underline' ) 653command ( 'hi! link Foo Bar' ) 654eq ( 655{ link ='Bar' , fg =tonumber ( '00ff00' , 16 ), bold =true , underline =true } , 656api . nvim_get_hl ( 0 , { name ='Foo' , link =true } ) 657) 658end ) 659 660it ( 'can set link as well as other attributes' , function () 661command ( 'hi Bar guifg=red' ) 662local hl ={ link ='Bar' , fg =tonumber ( '00ff00' , 16 ), bold =true , cterm ={ bold =true } } 663api . nvim_set_hl ( 0 , 'Foo' , hl ) 664eq ( hl , api . nvim_get_hl ( 0 , { name ='Foo' , link =true } )) 665end ) 666 667it ( 'link_global resolves in global namespace' , function () 668local ns =api . nvim_create_namespace ( 'hl_test' ) 669api . nvim_set_hl ( 0 , 'GlTarget' , { fg ='#ff0000' } ) 670api . nvim_set_hl ( ns , 'GlSource' , { link_global =fn . hlID ( 'GlTarget' ) } ) 671eq ( { link ='GlTarget' } , api . nvim_get_hl ( ns , { name ='GlSource' } )) 672 673-- when both link and link_global are given, link_global wins 674api . nvim_set_hl ( 0 , 'OtherTarget' , { fg ='#00ff00' } ) 675api . nvim_set_hl ( 676ns , 677'GlSource' , 678{ link =fn . hlID ( 'OtherTarget' ), link_global =fn . hlID ( 'GlTarget' ) } 679) 680eq ( { link ='GlTarget' } , api . nvim_get_hl ( ns , { name ='GlSource' } )) 681end ) 682 683it ( "doesn't contain unset groups" , function () 684local id =api . nvim_get_hl_id_by_name '@foobar.hubbabubba' 685ok ( id > 0 ) 686 687local data =api . nvim_get_hl ( 0 , {} ) 688eq ( nil , data [ '@foobar.hubbabubba' ]) 689eq ( nil , data [ '@foobar' ]) 690 691command 'hi @foobar.hubbabubba gui=bold' 692data =api . nvim_get_hl ( 0 , {} ) 693eq ( { bold =true } , data [ '@foobar.hubbabubba' ]) 694eq ( nil , data [ '@foobar' ]) 695 696-- @foobar.hubbabubba was explicitly cleared and thus shows up 697-- but @foobar was never touched, and thus doesn't 698command 'hi clear @foobar.hubbabubba' 699data =api . nvim_get_hl ( 0 , {} ) 700eq ( {} , data [ '@foobar.hubbabubba' ]) 701eq ( nil , data [ '@foobar' ]) 702end ) 703 704it ( 'should return default flag' , function () 705api . nvim_set_hl ( 0 , 'Tried' , { fg ='#00ff00' , default =true } ) 706eq ( { fg =tonumber ( '00ff00' , 16 ), default =true } , api . nvim_get_hl ( 0 , { name ='Tried' } )) 707end ) 708 709it ( 'should not output empty gui and cterm #23474' , function () 710api . nvim_set_hl ( 0 , 'Foo' , { default =true } ) 711api . nvim_set_hl ( 0 , 'Bar' , { default =true , fg ='#ffffff' } ) 712api . nvim_set_hl ( 0 , 'FooBar' , { default =true , fg ='#ffffff' , cterm ={ bold =true } } ) 713api . nvim_set_hl ( 7140 , 715'FooBarA' , 716{ default =true , fg ='#ffffff' , cterm ={ bold =true , italic =true } } 717) 718 719eq ( 'Foo xxx cleared' , exec_capture ( 'highlight Foo' )) 720eq ( { default =true } , api . nvim_get_hl ( 0 , { name ='Foo' } )) 721eq ( 'Bar xxx guifg=#ffffff' , exec_capture ( 'highlight Bar' )) 722eq ( 'FooBar xxx cterm=bold guifg=#ffffff' , exec_capture ( 'highlight FooBar' )) 723eq ( 'FooBarA xxx cterm=bold,italic guifg=#ffffff' , exec_capture ( 'highlight FooBarA' )) 724end ) 725 726it ( 'can override exist highlight group by force #20323' , function () 727local white =tonumber ( 'ffffff' , 16 ) 728local green =tonumber ( '00ff00' , 16 ) 729api . nvim_set_hl ( 0 , 'Foo' , { fg =white } ) 730api . nvim_set_hl ( 0 , 'Foo' , { fg =green , force =true } ) 731eq ( { fg =green } , api . nvim_get_hl ( 0 , { name ='Foo' } )) 732api . nvim_set_hl ( 0 , 'Bar' , { link ='Comment' , default =true } ) 733api . nvim_set_hl ( 0 , 'Bar' , { link ='Foo' , default =true , force =true } ) 734eq ( { link ='Foo' , default =true } , api . nvim_get_hl ( 0 , { name ='Bar' } )) 735end ) 736 737it ( 'round-trips fg_indexed/bg_indexed through nvim_get_hl' , function () 738api . nvim_set_hl ( 0 , 'Test_idx' , { 739fg ='#cc0000' , 740bg ='#0000cc' , 741ctermfg =1 , 742ctermbg =4 , 743fg_indexed =true , 744bg_indexed =true , 745} ) 746local hl =api . nvim_get_hl ( 0 , { name ='Test_idx' } ) 747eq ( true , hl . fg_indexed ) 748eq ( true , hl . bg_indexed ) 749eq ( tonumber ( '0xcc0000' ), hl . fg ) 750eq ( tonumber ( '0x0000cc' ), hl . bg ) 751eq ( 1 , hl . ctermfg ) 752eq ( 4 , hl . ctermbg ) 753 754api . nvim_set_hl ( 0 , 'Test_idx' , { fg_indexed =false , update =true } ) 755eq ( nil , api . nvim_get_hl ( 0 , { name ='Test_idx' } ). fg_indexed ) 756eq ( true , api . nvim_get_hl ( 0 , { name ='Test_idx' } ). bg_indexed ) 757eq ( tonumber ( '0xcc0000' ), api . nvim_get_hl ( 0 , { name ='Test_idx' } ). fg ) 758end ) 759end ) 760 761describe ( 'API: set/get highlight namespace' , function () 762before_each ( clear ) 763 764it ( 'set/get highlight namespace' , function () 765eq ( 0 , api . nvim_get_hl_ns ( {} )) 766local ns =api . nvim_create_namespace ( '' ) 767api . nvim_set_hl_ns ( ns ) 768eq ( ns , api . nvim_get_hl_ns ( {} )) 769end ) 770 771it ( 'set/get window highlight namespace' , function () 772eq ( - 1 , api . nvim_get_hl_ns ( { winid =0 } )) 773local ns =api . nvim_create_namespace ( '' ) 774api . nvim_win_set_hl_ns ( 0 , ns ) 775eq ( ns , api . nvim_get_hl_ns ( { winid =0 } )) 776end ) 777 778it ( 'setting namespace takes priority over &winhighlight' , function () 779local win =api . nvim_get_current_win () 780eq ( - 1 , api . nvim_get_hl_ns ( { winid =win } )) 781command ( 'set winhighlight=Visual:Search' ) 782local winhl_ns =api . nvim_get_hl_ns ( { winid =win } ) 783neq ( 0 , winhl_ns ) 784eq ( 'Search' , api . nvim_get_hl ( winhl_ns , { name ='Visual' } ). link ) 785n . insert ( 'foobar' ) 786local ns =api . nvim_create_namespace ( '' ) 787neq ( winhl_ns , ns ) 788api . nvim_win_set_hl_ns ( win , ns ) 789eq ( ns , api . nvim_get_hl_ns ( { winid =win } )) 790command ( 'enew' ) -- Switching buffer keeps namespace. #30904 791eq ( ns , api . nvim_get_hl_ns ( { winid =win } )) 792command ( 'set winhighlight=' ) 793eq ( ns , api . nvim_get_hl_ns ( { winid =win } )) 794-- Setting 'winhighlight' changes its namespace even when not using it. 795command ( 'set winhighlight=Visual:IncSearch' ) 796eq ( 'IncSearch' , api . nvim_get_hl ( winhl_ns , { name ='Visual' } ). link ) 797eq ( ns , api . nvim_get_hl_ns ( { winid =win } )) 798-- Setting 'winhighlight' works with global namespace. #37865 799api . nvim_win_set_hl_ns ( win , 0 ) 800eq ( 0 , api . nvim_get_hl_ns ( { winid =win } )) 801command ( 'set winhighlight=Visual:IncSearch' ) 802eq ( 'IncSearch' , api . nvim_get_hl ( winhl_ns , { name ='Visual' } ). link ) 803eq ( winhl_ns , api . nvim_get_hl_ns ( { winid =win } )) 804command ( 'set winhighlight=' ) 805eq ( 0 , api . nvim_get_hl_ns ( { winid =win } )) 806-- 'winhighlight' keeps the same namespace. 807api . nvim_win_set_hl_ns ( win , winhl_ns ) 808eq ( winhl_ns , api . nvim_get_hl_ns ( { winid =win } )) 809command ( 'set winhighlight=Visual:Substitute' ) 810eq ( 'Substitute' , api . nvim_get_hl ( winhl_ns , { name ='Visual' } ). link ) 811eq ( winhl_ns , api . nvim_get_hl_ns ( { winid =win } )) 812end ) 813end )