robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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