Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): use neovim builtin to get highlights #632

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 4 additions & 42 deletions lua/modules/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,44 +80,6 @@ local function hexToRgb(c)
return { tonumber(c:sub(2, 3), 16), tonumber(c:sub(4, 5), 16), tonumber(c:sub(6, 7), 16) }
end

---Parse the `style` string into nvim_set_hl options
---@param style string @The style config
---@return table
local function parse_style(style)
if not style or style == "NONE" then
return {}
end

local result = {}
for field in string.gmatch(style, "([^,]+)") do
result[field] = true
end

return result
end

---Wrapper function for nvim_get_hl_by_name
---@param hl_group string @Highlight group name
---@return table
local function get_highlight(hl_group)
local hl = vim.api.nvim_get_hl_by_name(hl_group, true)
if hl.link then
return get_highlight(hl.link)
end

local result = parse_style(hl.style)
result.fg = hl.foreground and string.format("#%06x", hl.foreground)
result.bg = hl.background and string.format("#%06x", hl.background)
result.sp = hl.special and string.format("#%06x", hl.special)
for attr, val in pairs(hl) do
if type(attr) == "string" and attr ~= "foreground" and attr ~= "background" and attr ~= "special" then
result[attr] = val
end
end

return result
end

---Blend foreground with background
---@param foreground string @The foreground color
---@param background string @The background color to blend with
Expand All @@ -143,10 +105,10 @@ end
---@return string
function M.hl_to_rgb(hl_group, use_bg, fallback_hl)
local hex = fallback_hl or "#000000"
local hlexists = pcall(vim.api.nvim_get_hl_by_name, hl_group, true)
local hlexists = pcall(vim.api.nvim_get_hl, 0, { name = hl_group, link = false })

if hlexists then
local result = get_highlight(hl_group)
local result = vim.api.nvim_get_hl(0, { name = hl_group, link = false })
if use_bg then
hex = result.bg and result.bg or "NONE"
else
Expand All @@ -161,12 +123,12 @@ end
---@param name string @Target highlight group name
---@param def table @Attributes to be extended
function M.extend_hl(name, def)
local hlexists = pcall(vim.api.nvim_get_hl_by_name, name, true)
local hlexists = pcall(vim.api.nvim_get_hl, 0, { name = name, link = false })
if not hlexists then
-- Do nothing if highlight group not found
return
end
local current_def = get_highlight(name)
local current_def = vim.api.nvim_get_hl(0, { name = name, link = false })
local combined_def = vim.tbl_deep_extend("force", current_def, def)

vim.api.nvim_set_hl(0, name, combined_def)
Expand Down