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() function M:create_option_highlights()
-- set custom highlights -- set custom highlights
if self.options.color then if self.options.color then
self.options.color_highlight = highlight.create_component_highlight_group( self.options.color_highlight = self:create_hl(self.options.color)
self.options.color,
self.options.component_name,
self.options,
false
)
end end
-- setup icon highlight -- setup icon highlight
if type(self.options.icon) == 'table' and self.options.icon.color then 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_highlight = self:create_hl(self.options.icon.color)
self.options.icon.color,
self.options.component_name .. 'icon',
self.options,
false
)
end end
end end
@ -100,7 +90,7 @@ end
function M:apply_highlights(default_highlight) function M:apply_highlights(default_highlight)
if self.options.color_highlight then if self.options.color_highlight then
local hl_fmt 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 self.status = hl_fmt .. self.status
end end
if type(self.options.separator) ~= 'table' and self.status:find('%%#') then if type(self.options.separator) ~= 'table' and self.status:find('%%#') then
@ -127,7 +117,7 @@ function M:apply_icon()
end end
if self.options.icon_color_highlight then if self.options.icon_color_highlight then
self.status = table.concat { self.status = table.concat {
highlight.component_format_highlight(self.options.icon_color_highlight), self:format_hl(self.options.icon_color_highlight),
icon, icon,
self:get_default_hl(), self:get_default_hl(),
' ', ' ',
@ -187,7 +177,7 @@ end
function M:get_default_hl() function M:get_default_hl()
if self.options.color_highlight then 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 elseif self.default_hl then
return self.default_hl return self.default_hl
else else
@ -195,12 +185,31 @@ function M:get_default_hl()
end end
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 -- luacheck: push no unused args
---actual function that updates a component. Must be overwritten with component functionality ---actual function that updates a component. Must be overwritten with component functionality
function M:update_status(is_focused) end function M:update_status(is_focused) end
-- luacheck: pop -- luacheck: pop
---driver code of the class ---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) function M:draw(default_highlight, is_focused)
self.status = '' self.status = ''
self.applied_separator = '' 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) self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
if self.options.component_name == 'buffers' then if self.options.component_name == 'buffers' then
self.highlights = { self.highlights = {
active = highlight.create_component_highlight_group( active = self:create_hl(self.options.buffers_color.active, 'active'),
self.options.buffers_color.active, inactive = self:create_hl(self.options.buffers_color.inactive, 'inactive'),
'buffers_active',
self.options,
false
),
inactive = highlight.create_component_highlight_group(
self.options.buffers_color.inactive,
'buffers_inactive',
self.options,
false
),
} }
end end
end end

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,5 @@
local Window = require('lualine.components.windows.window') local Window = require('lualine.components.windows.window')
local M = require('lualine.components.buffers'):extend() local M = require('lualine.components.buffers'):extend()
local highlight = require('lualine.highlight')
local default_options = { local default_options = {
disabled_filetypes = {}, 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.options.buffers_color = nil -- this is the default value of colors generated by parent bufferes component.
self.highlights = { self.highlights = {
active = highlight.create_component_highlight_group( active = self:create_hl(self.options.windows_color.active, 'active'),
self.options.windows_color.active, inactive = self:create_hl(self.options.windows_color.inactive, 'inactive'),
'windows_active',
self.options,
false
),
inactive = highlight.create_component_highlight_group(
self.options.windows_color.inactive,
'windows_inactive',
self.options,
false
),
} }
end 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(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
assert.stub(hl.create_component_highlight_group).was_called_with( assert.stub(hl.create_component_highlight_group).was_called_with(
{ fg = '#000' }, { fg = '#000' },
'test_highlight_group', 'filetype_test_highlight_group',
opts, opts,
false false
) )