Skip to content

Commit

Permalink
perf(highlights)!: improve highlight normalization; privatize highlig…
Browse files Browse the repository at this point in the history
…ht functions; add module-level get_highlights() function
  • Loading branch information
rebelot committed Apr 24, 2022
1 parent 2083ee3 commit 0c91862
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
41 changes: 25 additions & 16 deletions lua/heirline/highlights.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
local M = {}

M.defined_highlights = {}
local defined_highlights = {}

function M.make_hl(hl_name, hl)
vim.api.nvim_set_hl(0, hl_name, hl)
function M.reset_highlights()
defined_highlights = {}
end

function M.reset_highlights()
M.defined_highlights = {}
function M.get_highlights()
return vim.tbl_extend("force", defined_highlights, {})
end

local function make_hl(hl_name, hl)
vim.api.nvim_set_hl(0, hl_name, hl)
end

function M.name_hl(hl)
local function name_hl(hl)
local style = vim.tbl_filter(function(value)
return not vim.tbl_contains({ "background", "bg", "foreground", "fg", "special", "sp" }, value)
return not vim.tbl_contains({ "background", "foreground", "special" }, value)
end, vim.tbl_keys(hl))
return "Stl"
.. (hl.foreground and hl.foreground:gsub("#", "") or "_")
Expand All @@ -21,18 +25,23 @@ function M.name_hl(hl)
.. (hl.special and hl.special:gsub(",", "") or "")
end

local function hex(n)
if n and type(n) == "number" then
return string.format("#%06x", n)
local function hex(val)
if type(val) == "number" then
return string.format("#%06x", val)
else
return val
end
end

local function fixhl(hl)
local function normalize_hl(hl)
local fixed_hl = vim.tbl_extend("force", hl, {})
fixed_hl.foreground = hex(hl.foreground or hl.fg)
fixed_hl.background = hex(hl.background or hl.bg)
fixed_hl.special = hex(hl.special or hl.sp or hl.guisp)
fixed_hl.guisp = nil
fixed_hl.fg = nil
fixed_hl.bg = nil
fixed_hl.sp = nil
fixed_hl.force = nil

if hl.style then
Expand All @@ -48,11 +57,11 @@ function M.eval_hl(hl)
if vim.tbl_isempty(hl) then
return "", ""
end
hl = fixhl(hl)
local hl_name = M.name_hl(hl)
if not M.defined_highlights[hl_name] then
M.make_hl(hl_name, hl)
M.defined_highlights[hl_name] = true
hl = normalize_hl(hl)
local hl_name = name_hl(hl)
if not defined_highlights[hl_name] then
make_hl(hl_name, hl)
defined_highlights[hl_name] = true
end
return "%#" .. hl_name .. "#", "%*"
end
Expand Down
4 changes: 4 additions & 0 deletions lua/heirline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ function M.reset_highlights()
return require("heirline.highlights").reset_highlights()
end

function M.get_highlights()
return require("heirline.highlights").get_highlights()
end

function M.load()
vim.g.qf_disable_statusline = true
vim.cmd("set statusline=%{%v:lua.require'heirline'.eval()%}")
Expand Down

0 comments on commit 0c91862

Please sign in to comment.