enhace: add create&format_hl to make hl easier for componemet

This commit is contained in:
shadmansaleh 2022-04-15 00:30:47 +06:00
parent 63f74ac069
commit 13ead784c4
8 changed files with 42 additions and 104 deletions

View File

@ -51,21 +51,11 @@ end
function M:create_option_highlights()
-- set custom highlights
if self.options.color then
self.options.color_highlight = highlight.create_component_highlight_group(
self.options.color,
self.options.component_name,
self.options,
false
)
self.options.color_highlight = self:create_hl(self.options.color)
end
-- setup icon highlight
if type(self.options.icon) == 'table' and self.options.icon.color then
self.options.icon_color_highlight = highlight.create_component_highlight_group(
self.options.icon.color,
self.options.component_name .. 'icon',
self.options,
false
)
self.options.icon_color_highlight = self:create_hl(self.options.icon.color)
end
end
@ -100,7 +90,7 @@ end
function M:apply_highlights(default_highlight)
if self.options.color_highlight then
local hl_fmt
hl_fmt, M.color_fn_cache = highlight.component_format_highlight(self.options.color_highlight)
hl_fmt, M.color_fn_cache = self:format_hl(self.options.color_highlight)
self.status = hl_fmt .. self.status
end
if type(self.options.separator) ~= 'table' and self.status:find('%%#') then
@ -127,7 +117,7 @@ function M:apply_icon()
end
if self.options.icon_color_highlight then
self.status = table.concat {
highlight.component_format_highlight(self.options.icon_color_highlight),
self:format_hl(self.options.icon_color_highlight),
icon,
self:get_default_hl(),
' ',
@ -187,7 +177,7 @@ end
function M:get_default_hl()
if self.options.color_highlight then
return highlight.component_format_highlight(self.options.color_highlight)
return self:format_hl(self.options.color_highlight)
elseif self.default_hl then
return self.default_hl
else
@ -195,12 +185,31 @@ function M:get_default_hl()
end
end
---create a lualine highlight for color
---@param color table|string|function defined color for hl
---@param hint string|nil hint for hl name
---@return table an identifier to later retrive the hl for application
function M:create_hl(color, hint)
hint = hint and self.options.component_name .. '_' .. hint or self.options.component_name
return highlight.create_component_highlight_group(color, hint, self.options, false)
end
---Get stl formated hl group for hl_token
---@param hl_token table indentifier received from create_hl or create_component_highlight_group
---@return string stl formated hl group for hl_token
function M:format_hl(hl_token)
return highlight.component_format_highlight(hl_token)
end
-- luacheck: push no unused args
---actual function that updates a component. Must be overwritten with component functionality
function M:update_status(is_focused) end
-- luacheck: pop
---driver code of the class
---@param default_highlight string default hl group of section where component resides
---@param is_focused boolean|number whether drawing for active or inactive statusline.
---@return string stl formated rendering string for component
function M:draw(default_highlight, is_focused)
self.status = ''
self.applied_separator = ''

View File

@ -51,18 +51,8 @@ function M:init(options)
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
if self.options.component_name == 'buffers' then
self.highlights = {
active = highlight.create_component_highlight_group(
self.options.buffers_color.active,
'buffers_active',
self.options,
false
),
inactive = highlight.create_component_highlight_group(
self.options.buffers_color.inactive,
'buffers_inactive',
self.options,
false
),
active = self:create_hl(self.options.buffers_color.active, 'active'),
inactive = self:create_hl(self.options.buffers_color.inactive, 'inactive'),
}
end
end

View File

@ -31,30 +31,10 @@ function M:init(options)
-- Initialize highlight groups
if self.options.colored then
self.highlight_groups = {
error = modules.highlight.create_component_highlight_group(
self.options.diagnostics_color.error,
'diagnostics_error',
self.options,
false
),
warn = modules.highlight.create_component_highlight_group(
self.options.diagnostics_color.warn,
'diagnostics_warn',
self.options,
false
),
info = modules.highlight.create_component_highlight_group(
self.options.diagnostics_color.info,
'diagnostics_info',
self.options,
false
),
hint = modules.highlight.create_component_highlight_group(
self.options.diagnostics_color.hint,
'diagnostics_hint',
self.options,
false
),
error = self:create_hl(self.options.diagnostics_color.error, 'error'),
warn = self:create_hl(self.options.diagnostics_color.warn, 'warn'),
info = self:create_hl(self.options.diagnostics_color.info, 'info'),
hint = self:create_hl(self.options.diagnostics_color.hint, 'hint'),
}
end
@ -107,7 +87,7 @@ function M:update_status()
if self.options.colored then
local colors, bgs = {}, {}
for name, hl in pairs(self.highlight_groups) do
colors[name] = modules.highlight.component_format_highlight(hl)
colors[name] = self:format_hl(hl)
bgs[name] = modules.utils.extract_highlight_colors(colors[name]:match('%%#(.-)#'), 'bg')
end
local previous_section, padding

View File

@ -49,24 +49,9 @@ function M:init(options)
-- create highlights and save highlight_name in highlights table
if self.options.colored then
self.highlights = {
added = modules.highlight.create_component_highlight_group(
self.options.diff_color.added,
'diff_added',
self.options,
false
),
modified = modules.highlight.create_component_highlight_group(
self.options.diff_color.modified,
'diff_modified',
self.options,
false
),
removed = modules.highlight.create_component_highlight_group(
self.options.diff_color.removed,
'diff_removed',
self.options,
false
),
added = self:create_hl(self.options.diff_color.added, 'added'),
modified = self:create_hl(self.options.diff_color.modified, 'modified'),
removed = self:create_hl(self.options.diff_color.removed, 'removed'),
}
end
modules.git_diff.init(self.options)
@ -83,7 +68,7 @@ function M:update_status(is_focused)
if self.options.colored then
-- load the highlights and store them in colors table
for name, highlight_name in pairs(self.highlights) do
colors[name] = modules.highlight.component_format_highlight(highlight_name)
colors[name] = self:format_hl(highlight_name)
end
end

View File

@ -41,16 +41,11 @@ function M:apply_icon()
local default_highlight = self:get_default_hl()
local icon_highlight = self.icon_hl_cache[highlight_color]
if not icon_highlight or not modules.highlight.highlight_exists(icon_highlight.name .. '_normal') then
icon_highlight = modules.highlight.create_component_highlight_group(
{ fg = highlight_color },
icon_highlight_group,
self.options,
false
)
icon_highlight = self:create_hl({ fg = highlight_color }, icon_highlight_group)
self.icon_hl_cache[highlight_color] = icon_highlight
end
icon = modules.highlight.component_format_highlight(icon_highlight) .. icon .. default_highlight
icon = self:format_hl(icon_highlight) .. icon .. default_highlight
end
end
else

View File

@ -41,18 +41,8 @@ function M:init(options)
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
-- stylua: ignore
self.highlights = {
active = highlight.create_component_highlight_group(
self.options.tabs_color.active,
'tabs_active',
self.options,
false
),
inactive = highlight.create_component_highlight_group(
self.options.tabs_color.inactive,
'tabs_inactive',
self.options,
false
),
active = self:create_hl( self.options.tabs_color.active, 'active'),
inactive = self:create_hl( self.options.tabs_color.inactive, 'inactive'),
}
end

View File

@ -1,6 +1,5 @@
local Window = require('lualine.components.windows.window')
local M = require('lualine.components.buffers'):extend()
local highlight = require('lualine.highlight')
local default_options = {
disabled_filetypes = {},
@ -16,18 +15,8 @@ function M:init(options)
self.options.buffers_color = nil -- this is the default value of colors generated by parent bufferes component.
self.highlights = {
active = highlight.create_component_highlight_group(
self.options.windows_color.active,
'windows_active',
self.options,
false
),
inactive = highlight.create_component_highlight_group(
self.options.windows_color.inactive,
'windows_inactive',
self.options,
false
),
active = self:create_hl(self.options.windows_color.active, 'active'),
inactive = self:create_hl(self.options.windows_color.inactive, 'inactive'),
}
end

View File

@ -311,7 +311,7 @@ describe('Filetype component', function()
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
assert.stub(hl.create_component_highlight_group).was_called_with(
{ fg = '#000' },
'test_highlight_group',
'filetype_test_highlight_group',
opts,
false
)