From a52f078026b27694d2290e34efa61a6e4a690621 Mon Sep 17 00:00:00 2001 From: Antoine Cotten Date: Sun, 11 Sep 2022 12:43:31 +0200 Subject: [PATCH] feat(utils): support extracting fallback color --- lua/lualine/components/diagnostics/config.lua | 8 ++--- lua/lualine/utils/utils.lua | 21 ++++++----- tests/spec/utils_spec.lua | 35 +++++++++++++++++++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/lua/lualine/components/diagnostics/config.lua b/lua/lualine/components/diagnostics/config.lua index 6b80fff..bfe3544 100644 --- a/lua/lualine/components/diagnostics/config.lua +++ b/lua/lualine/components/diagnostics/config.lua @@ -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' ), diff --git a/lua/lualine/utils/utils.lua b/lua/lualine/utils/utils.lua index 82b9af2..2f74c3e 100644 --- a/lua/lualine/utils/utils.lua +++ b/lua/lualine/utils/utils.lua @@ -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 diff --git a/tests/spec/utils_spec.lua b/tests/spec/utils_spec.lua index c62ae82..cc44d07 100644 --- a/tests/spec/utils_spec.lua +++ b/tests/spec/utils_spec.lua @@ -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',