diff --git a/lua/lualine.lua b/lua/lualine.lua index 248aeaf..d6438da 100644 --- a/lua/lualine.lua +++ b/lua/lualine.lua @@ -179,7 +179,7 @@ local function setup_theme() end local theme = get_theme_from_config() modules.highlight.create_highlight_groups(theme) - vim.cmd [[autocmd lualine ColorScheme * lua require'lualine.utils.utils'.reload_highlights() + vim.cmd [[autocmd lualine ColorScheme * lua require'lualine'.setup() autocmd lualine OptionSet background lua require'lualine'.setup()]] end diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua index 31f7f65..e46d8cf 100644 --- a/lua/lualine/components/filetype.lua +++ b/lua/lualine/components/filetype.lua @@ -40,7 +40,7 @@ function FileType:apply_icon() .section) local icon_highlight = self.options.self.section .. '_' .. icon_highlight_group - if not modules.utils.highlight_exists(icon_highlight .. '_normal') then + if not modules.highlight.highlight_exists(icon_highlight .. '_normal') then icon_highlight = modules.highlight.create_component_highlight_group( {fg = highlight_color}, icon_highlight_group, self.options) diff --git a/lua/lualine/highlight.lua b/lua/lualine/highlight.lua index 0966279..2e2befa 100644 --- a/lua/lualine/highlight.lua +++ b/lua/lualine/highlight.lua @@ -7,10 +7,27 @@ local modules = lualine_require.lazy_require{ utils = 'lualine.utils.utils', color_utils = 'lualine.utils.color_utils', } + local section_highlight_map = {x = 'c', y = 'b', z = 'a'} local active_theme = nil local create_cterm_colors = false +-- table to store the highlight names created by lualine +local loaded_highlights = {} + +-- determine if an highlight exist and isn't cleared +function M.highlight_exists(highlight_name) + return loaded_highlights[highlight_name] or false +end + +-- clears loaded_highlights table and highlights +local function clear_highlights() + for highlight_name, _ in pairs(loaded_highlights) do + vim.cmd('highlight clear ' .. highlight_name) + loaded_highlights[highlight_name] = nil + end +end + local function sanitize_color(color) if type(color) == 'string' then if color:sub(1,1) == '#' then return color end -- RGB value @@ -23,7 +40,7 @@ local function sanitize_color(color) end end -function M.highlight(name, foreground, background, gui, link, reload) +function M.highlight(name, foreground, background, gui, link) local command = {'highlight!'} if link and #link > 0 then vim.list_extend(command, {'link', name, link}) @@ -51,13 +68,11 @@ function M.highlight(name, foreground, background, gui, link, reload) end end vim.cmd(table.concat(command, ' ')) - if not reload then - modules.utils.save_highlight(name, {name, foreground, background, gui, link, true}) - end + loaded_highlights[name] = true end function M.create_highlight_groups(theme) - modules.utils.clear_highlights() + clear_highlights() active_theme = theme create_cterm_colors = not vim.go.termguicolors for mode, sections in pairs(theme) do @@ -131,9 +146,9 @@ end -- to retrive highlight group function M.create_component_highlight_group(color, highlight_tag, options) local tag_id = 0 - while (modules.utils.highlight_exists(table.concat( + while (M.highlight_exists(table.concat( {'lualine', highlight_tag, 'no_mode'}, '_')) - or (options.self.section and modules.utils.highlight_exists(table.concat( + or (options.self.section and M.highlight_exists(table.concat( {options.self.section, highlight_tag, 'normal'}, '_'))) ) do highlight_tag = highlight_tag .. '_' .. tostring(tag_id) @@ -199,7 +214,7 @@ function M.component_format_highlight(highlight_name) else highlight_group = highlight_group .. '_inactive' end - if modules.utils.highlight_exists(highlight_group) then + if M.highlight_exists(highlight_group) then return '%#' .. highlight_group .. '#' else return '%#' .. highlight_name .. '_normal#' @@ -208,7 +223,7 @@ end function M.format_highlight(is_focused, highlight_group) if highlight_group > 'lualine_c' - and not modules.utils.highlight_exists(highlight_group .. '_normal') then + and not M.highlight_exists(highlight_group .. '_normal') then highlight_group = 'lualine_' .. section_highlight_map[highlight_group:match( 'lualine_(.)')] @@ -219,7 +234,7 @@ function M.format_highlight(is_focused, highlight_group) else highlight_name = append_mode(highlight_group) end - if modules.utils.highlight_exists(highlight_name) then + if M.highlight_exists(highlight_name) then return '%#' .. highlight_name .. '#' end return '%#' .. highlight_group .. '_normal#' @@ -237,7 +252,7 @@ function M.get_transitional_highlights(left_hl, right_hl) -- construct the name of hightlight group local highlight_name = table.concat({'lualine_transitional',left_hl,'to',right_hl}, '_') - if not modules.utils.highlight_exists(highlight_name) then + if not M.highlight_exists(highlight_name) then -- Create the highlight_group if needed -- Get colors from highlights local fg = modules.utils.extract_highlight_colors(left_hl, 'bg') diff --git a/lua/lualine/utils/utils.lua b/lua/lualine/utils/utils.lua index 6ba068a..77d10c3 100644 --- a/lua/lualine/utils/utils.lua +++ b/lua/lualine/utils/utils.lua @@ -19,34 +19,6 @@ function M.extract_highlight_colors(color_group, scope) return color end --- table to store the highlight names created by lualine -M.loaded_highlights = {} - --- sets loaded_highlights table -function M.save_highlight(highlight_name, highlight_args) - M.loaded_highlights[highlight_name] = highlight_args -end - -function M.reload_highlights() - local highlight = require('lualine.highlight') - for _, highlight_args in pairs(M.loaded_highlights) do - highlight.highlight(unpack(highlight_args)) - end -end - --- determine if an highlight exist and isn't cleared -function M.highlight_exists(highlight_name) - return M.loaded_highlights[highlight_name] and true or false -end - --- clears loaded_highlights table and highlights -function M.clear_highlights() - for highlight_name, _ in pairs(M.loaded_highlights) do - vim.cmd('highlight clear ' .. highlight_name) - M.loaded_highlights[highlight_name] = nil - end -end - -- remove empty strings from list function M.list_shrink(list) local new_list = {} diff --git a/lua/tests/spec/utils_spec.lua b/lua/tests/spec/utils_spec.lua index 0e90f84..263904e 100644 --- a/lua/tests/spec/utils_spec.lua +++ b/lua/tests/spec/utils_spec.lua @@ -1,38 +1,11 @@ local helpers = require 'tests.helpers' local eq = assert.are.same -local meths = helpers.meths local build_component_opts = helpers.build_component_opts describe('Utils', function() local utils = require('lualine.utils.utils') - it('can save and restore highlights', function() - local hl1 = {'hl1', '#122233', '#445566', 'italic', nil, true} - utils.save_highlight(hl1[1], hl1) - -- highlight loaded in loaded_highlights table - eq(utils.loaded_highlights[hl1[1]], hl1) - -- highlight exists works properly - eq(utils.highlight_exists('hl1'), true) - eq(utils.highlight_exists('hl2'), false) - -- highlights can be restored - -- hl doesn't exist - assert.has_error(function() meths.get_hl_by_name('hl1', true) end, - 'Invalid highlight name: hl1') - utils.reload_highlights() - -- Now hl1 is created - eq(meths.get_hl_by_name('hl1', true), { - foreground = tonumber(hl1[2]:sub(2, #hl1[2]), 16), -- convert rgb -> int - background = tonumber(hl1[3]:sub(2, #hl1[3]), 16), -- convert rgb -> int - italic = true - }) - -- highlights can be cleared - utils.clear_highlights() - eq(utils.highlight_exists('hl1'), false) - -- highlight group has been cleared - eq(meths.get_hl_by_name('hl1', true), {[true] = 6}) - end) - it('can retrive highlight groups', function() local hl2 = {fg = '#aabbcc', bg = '#889977', reverse = true} -- handles non existing hl groups