feat(utils): support extracting fallback color

This commit is contained in:
Antoine Cotten 2022-09-11 12:43:31 +02:00 committed by Shadman
parent 05f850d25f
commit a52f078026
3 changed files with 51 additions and 13 deletions

View File

@ -26,28 +26,28 @@ function M.apply_default_colors(opts)
local default_diagnostics_color = { local default_diagnostics_color = {
error = { error = {
fg = utils.extract_color_from_hllist( fg = utils.extract_color_from_hllist(
'fg', { 'fg', 'sp' },
{ 'DiagnosticError', 'LspDiagnosticsDefaultError', 'DiffDelete' }, { 'DiagnosticError', 'LspDiagnosticsDefaultError', 'DiffDelete' },
'#e32636' '#e32636'
), ),
}, },
warn = { warn = {
fg = utils.extract_color_from_hllist( fg = utils.extract_color_from_hllist(
'fg', { 'fg', 'sp' },
{ 'DiagnosticWarn', 'LspDiagnosticsDefaultWarning', 'DiffText' }, { 'DiagnosticWarn', 'LspDiagnosticsDefaultWarning', 'DiffText' },
'#ffa500' '#ffa500'
), ),
}, },
info = { info = {
fg = utils.extract_color_from_hllist( fg = utils.extract_color_from_hllist(
'fg', { 'fg', 'sp' },
{ 'DiagnosticInfo', 'LspDiagnosticsDefaultInformation', 'Normal' }, { 'DiagnosticInfo', 'LspDiagnosticsDefaultInformation', 'Normal' },
'#ffffff' '#ffffff'
), ),
}, },
hint = { hint = {
fg = utils.extract_color_from_hllist( fg = utils.extract_color_from_hllist(
'fg', { 'fg', 'sp' },
{ 'DiagnosticHint', 'LspDiagnosticsDefaultHint', 'DiffChange' }, { 'DiagnosticHint', 'LspDiagnosticsDefaultHint', 'DiffChange' },
'#273faf' '#273faf'
), ),

View File

@ -36,23 +36,26 @@ end
--- retrieves color value from highlight group name in syntax_list --- retrieves color value from highlight group name in syntax_list
--- first present highlight is returned --- first present highlight is returned
---@param scope string ---@param scope string|table
---@param syntaxlist table ---@param syntaxlist table
---@param default string ---@param default string
---@return string|nil ---@return string|nil
function M.extract_color_from_hllist(scope, syntaxlist, default) function M.extract_color_from_hllist(scope, syntaxlist, default)
scope = type(scope) == 'string' and { scope } or scope
for _, highlight_name in ipairs(syntaxlist) do for _, highlight_name in ipairs(syntaxlist) do
if vim.fn.hlexists(highlight_name) ~= 0 then if vim.fn.hlexists(highlight_name) ~= 0 then
local color = M.extract_highlight_colors(highlight_name) local color = M.extract_highlight_colors(highlight_name)
for _, sc in ipairs(scope) do
if color.reverse then if color.reverse then
if scope == 'bg' then if sc == 'bg' then
scope = 'fg' sc = 'fg'
else else
scope = 'bg' sc = 'bg'
end end
end end
if color[scope] then if color[sc] then
return color[scope] return color[sc]
end
end end
end end
end end

View File

@ -24,6 +24,41 @@ describe('Utils', function()
vim.cmd('hi clear hl2') vim.cmd('hi clear hl2')
end) 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() it('can shrink list with holes', function()
local list_with_holes = { local list_with_holes = {
'2', '2',