fix: simplifies highlights complex code (#1291)

This commit is contained in:
yetone
2025-02-18 10:25:38 +08:00
committed by GitHub
parent b20b2caf22
commit 7f74d9f105
3 changed files with 64 additions and 65 deletions

View File

@@ -20,10 +20,10 @@ local Highlights = {
}
Highlights.conflict = {
CURRENT = { name = "AvanteConflictCurrent", bg = "#562C30", bold = true }, -- #405d7e
CURRENT_LABEL = { name = "AvanteConflictCurrentLabel", link = "CURRENT", shade = 60 },
CURRENT = { name = "AvanteConflictCurrent", bg = "#562C30", bold = true },
CURRENT_LABEL = { name = "AvanteConflictCurrentLabel", shade_link = "AvanteConflictCurrent", shade = 30 },
INCOMING = { name = "AvanteConflictIncoming", bg = 3229523, bold = true }, -- #314753
INCOMING_LABEL = { name = "AvanteConflictIncomingLabel", link = "INCOMING", shade = 60 },
INCOMING_LABEL = { name = "AvanteConflictIncomingLabel", shade_link = "AvanteConflictIncoming", shade = 30 },
}
--- helper
@@ -55,41 +55,59 @@ M.setup = function()
end)
end
M.conflict_highlights()
M.setup_conflict_highlights()
end
---@param opts? AvanteConflictHighlights
M.conflict_highlights = function(opts)
opts = opts or Config.diff.highlights
M.setup_conflict_highlights = function()
local custom_hls = Config.highlights.diff
local get_highlights = function(key, hl)
local cl = api.nvim_get_hl(0, { name = opts[key:lower()] })
return cl ~= nil and cl or api.nvim_get_hl(0, { name = hl.name })
local get_bg = function(hl_name)
local hl = api.nvim_get_hl(0, { name = hl_name })
return hl.bg
end
local get_default_colors = function(key, hl)
--- We will first check for user custom highlight. Then fallback to default name highlight.
return get_highlights(key, hl).bg or hl.bg
end
local get_shade = function(hl)
local color = get_default_colors(hl.link, Highlights.conflict[hl.link])
return H.shade_color(color, hl.shade)
local get_bold = function(hl_name)
local hl = api.nvim_get_hl(0, { name = hl_name })
return hl.bold
end
vim.iter(Highlights.conflict):each(function(key, hl)
if not has_set_colors(hl.name) then
if hl.link ~= nil then
api.nvim_set_hl(0, hl.name, { bg = get_shade(hl), default = true })
else
local bold = get_highlights(key, hl).bold
api.nvim_set_hl(
0,
hl.name,
{ bg = get_default_colors(key, hl), default = true, bold = bold ~= nil and bold or hl.bold }
)
end
--- set none shade linked highlights first
if hl.shade_link ~= nil and hl.shade ~= nil then return end
if has_set_colors(hl.name) then return end
local bg = hl.bg
local bold = hl.bold
local custom_hl_name = custom_hls[key:lower()]
if custom_hl_name ~= nil then
bg = get_bg(custom_hl_name) or hl.bg
bold = get_bold(custom_hl_name) or hl.bold
end
api.nvim_set_hl(0, hl.name, { bg = bg, default = true, bold = bold })
end)
vim.iter(Highlights.conflict):each(function(key, hl)
--- only set shade linked highlights
if hl.shade_link == nil or hl.shade == nil then return end
if has_set_colors(hl.name) then return end
local bg
local bold = hl.bold
local custom_hl_name = custom_hls[key:lower()]
if custom_hl_name ~= nil then
bg = get_bg(custom_hl_name)
bold = get_bold(custom_hl_name) or hl.bold
else
local link_bg = get_bg(hl.shade_link)
bg = H.shade_color(link_bg, hl.shade)
end
api.nvim_set_hl(0, hl.name, { bg = bg, default = true, bold = bold })
end)
end
@@ -128,7 +146,7 @@ H.alter = function(attr, percent) return math.floor(attr * (100 + percent) / 100
---@param percent number
---@return string
H.shade_color = function(color, percent)
percent = vim.opt.background:get() == "light" and percent / 10 or percent
percent = vim.opt.background:get() == "light" and percent / 5 or percent
local rgb = H.decode_24bit_rgb(color)
if not rgb.r or not rgb.g or not rgb.b then return "NONE" end
local r, g, b = H.alter(rgb.r, percent), H.alter(rgb.g, percent), H.alter(rgb.b, percent)