feat(utils): support extracting fallback color
This commit is contained in:
parent
05f850d25f
commit
a52f078026
|
@ -26,28 +26,28 @@ function M.apply_default_colors(opts)
|
|||
local default_diagnostics_color = {
|
||||
error = {
|
||||
fg = utils.extract_color_from_hllist(
|
||||
'fg',
|
||||
{ 'fg', 'sp' },
|
||||
{ 'DiagnosticError', 'LspDiagnosticsDefaultError', 'DiffDelete' },
|
||||
'#e32636'
|
||||
),
|
||||
},
|
||||
warn = {
|
||||
fg = utils.extract_color_from_hllist(
|
||||
'fg',
|
||||
{ 'fg', 'sp' },
|
||||
{ 'DiagnosticWarn', 'LspDiagnosticsDefaultWarning', 'DiffText' },
|
||||
'#ffa500'
|
||||
),
|
||||
},
|
||||
info = {
|
||||
fg = utils.extract_color_from_hllist(
|
||||
'fg',
|
||||
{ 'fg', 'sp' },
|
||||
{ 'DiagnosticInfo', 'LspDiagnosticsDefaultInformation', 'Normal' },
|
||||
'#ffffff'
|
||||
),
|
||||
},
|
||||
hint = {
|
||||
fg = utils.extract_color_from_hllist(
|
||||
'fg',
|
||||
{ 'fg', 'sp' },
|
||||
{ 'DiagnosticHint', 'LspDiagnosticsDefaultHint', 'DiffChange' },
|
||||
'#273faf'
|
||||
),
|
||||
|
|
|
@ -36,23 +36,26 @@ end
|
|||
|
||||
--- retrieves color value from highlight group name in syntax_list
|
||||
--- first present highlight is returned
|
||||
---@param scope string
|
||||
---@param scope string|table
|
||||
---@param syntaxlist table
|
||||
---@param default string
|
||||
---@return string|nil
|
||||
function M.extract_color_from_hllist(scope, syntaxlist, default)
|
||||
scope = type(scope) == 'string' and { scope } or scope
|
||||
for _, highlight_name in ipairs(syntaxlist) do
|
||||
if vim.fn.hlexists(highlight_name) ~= 0 then
|
||||
local color = M.extract_highlight_colors(highlight_name)
|
||||
if color.reverse then
|
||||
if scope == 'bg' then
|
||||
scope = 'fg'
|
||||
else
|
||||
scope = 'bg'
|
||||
for _, sc in ipairs(scope) do
|
||||
if color.reverse then
|
||||
if sc == 'bg' then
|
||||
sc = 'fg'
|
||||
else
|
||||
sc = 'bg'
|
||||
end
|
||||
end
|
||||
if color[sc] then
|
||||
return color[sc]
|
||||
end
|
||||
end
|
||||
if color[scope] then
|
||||
return color[scope]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,6 +24,41 @@ describe('Utils', function()
|
|||
vim.cmd('hi clear hl2')
|
||||
end)
|
||||
|
||||
it('can extract individual highlight color', function()
|
||||
local fg_clr = '#aabbcc'
|
||||
local bg_clr = '#889977'
|
||||
local sp_clr = '#997788'
|
||||
local def_clr = '#ff0000'
|
||||
local hl_std = { fg = fg_clr, bg = bg_clr }
|
||||
local hl_rvs = { fg = fg_clr, bg = bg_clr, reverse = true }
|
||||
local hl_ul = { sp = sp_clr, undercurl = true }
|
||||
local hl_ul_rvs = { fg = fg_clr, bg = bg_clr, sp = sp_clr, reverse = true, undercurl = true }
|
||||
-- create highlights
|
||||
vim.cmd(string.format('hi hl_std guifg=%s guibg=%s', hl_std.fg, hl_std.bg))
|
||||
vim.cmd(string.format('hi hl_rvs guifg=%s guibg=%s gui=reverse', hl_rvs.fg, hl_rvs.bg))
|
||||
vim.cmd(string.format('hi hl_ul guisp=%s gui=undercurl', hl_ul.sp))
|
||||
vim.cmd(string.format('hi hl_ul_rvs guifg=%s guibg=%s guisp=%s gui=reverse,undercurl', hl_ul_rvs.fg, hl_ul_rvs.bg, hl_ul_rvs.sp))
|
||||
-- Can extract color from primary highlight group
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_std','hl_ul'}, def_clr), fg_clr)
|
||||
-- Can extract color from fallback highlight group
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_noexist','hl_std'}, def_clr), fg_clr)
|
||||
-- Can fall back to default color on nonexistent color
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_ul'}, def_clr), def_clr)
|
||||
-- Can fall back to default color on nonexistent highlight group
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_noexist'}, def_clr), def_clr)
|
||||
-- Can extract fallback color
|
||||
eq(utils.extract_color_from_hllist({'fg','sp'}, {'hl_ul'}, def_clr), sp_clr)
|
||||
-- Can extract reverse color
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_rvs'}, def_clr), bg_clr)
|
||||
-- Can extract fallback reverse color
|
||||
eq(utils.extract_color_from_hllist({'sp','fg'}, {'hl_rvs'}, def_clr), bg_clr)
|
||||
-- clear highlights
|
||||
vim.cmd('hi clear hl_std')
|
||||
vim.cmd('hi clear hl_rvs')
|
||||
vim.cmd('hi clear hl_ul')
|
||||
vim.cmd('hi clear hl_ul_rvs')
|
||||
end)
|
||||
|
||||
it('can shrink list with holes', function()
|
||||
local list_with_holes = {
|
||||
'2',
|
||||
|
|
Loading…
Reference in New Issue