Skip to content

Commit

Permalink
feat(highlights): use api nvim_set_hl:
Browse files Browse the repository at this point in the history
- support api hl keys
- will soon deprecate "style" and "guisp" hl keys
  • Loading branch information
rebelot committed Apr 24, 2022
1 parent 0b93d18 commit 2f60696
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
43 changes: 32 additions & 11 deletions lua/heirline/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,52 @@ local M = {}
M.defined_highlights = {}

function M.make_hl(hl_name, hl)
local fg = hl.fg and "guifg=" .. hl.fg .. " " or ""
local bg = hl.bg and "guibg=" .. hl.bg .. " " or ""
local style = hl.style and "gui=" .. hl.style .. " " or ""
local guisp = hl.guisp and "guisp=" .. hl.guisp .. " " or ""
if fg or bg or style or guisp then
vim.cmd("highlight " .. hl_name .. " " .. fg .. bg .. style .. guisp)
end
vim.api.nvim_set_hl(0, hl_name, hl)
end

function M.reset_highlights()
M.defined_highlights = {}
end

function M.name_hl(hl)
local style = vim.tbl_filter(function(value)
return not vim.tbl_contains({ "background", "bg", "foreground", "fg", "special", "sp" }, value)
end, vim.tbl_keys(hl))
return "Stl"
.. (hl.fg and hl.fg:gsub("#", "") or "_")
.. (hl.bg and hl.bg:gsub("#", "") or "_")
.. (hl.style and hl.style:gsub(",", "") or "")
.. (hl.guisp and hl.guisp:gsub(",", "") or "")
.. (hl.foreground and hl.foreground:gsub("#", "") or "_")
.. (hl.background and hl.background:gsub("#", "") or "_")
.. table.concat(style, "")
.. (hl.special and hl.special:gsub(",", "") or "")
end

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

local function fixhl(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.force = nil

if hl.style then
for _, val in ipairs(vim.fn.split(hl.style, ",")) do
fixed_hl[val] = true
end
fixed_hl.style = nil
end
return fixed_hl
end

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)
Expand Down
19 changes: 11 additions & 8 deletions lua/heirline/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ local M = {}
function M.get_highlight(hlname)
local hl = vim.api.nvim_get_hl_by_name(hlname, true)
local t = {}
local hex = function(n)
if n then
return string.format("#%06x", n)
end
end
t.fg = hex(hl.foreground)
t.bg = hex(hl.background)
t.sp = hex(hl.special)
-- local hex = function(n)
-- if n then
-- return string.format("#%06x", n)
-- end
-- end
for k, v in pairs(hl) do
t[k] = v
end
t.fg = hl.foreground
t.bg = hl.background
t.sp = hl.special
t.style = "none,"
if hl.underline then
t.style = t.style .. "underline"
Expand Down

0 comments on commit 2f60696

Please sign in to comment.