refactor(nvim): overhaul highlights

This commit is contained in:
Price Hiller 2024-02-20 17:09:02 -06:00
parent dbd8fb2d20
commit eb98162264
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
3 changed files with 305 additions and 238 deletions

View File

@ -40,3 +40,15 @@
!parameter !parameter
contents: (contents) @markup.raw.block contents: (contents) @markup.raw.block
end_name: (expr) @org-block-end-name (#any-of? @org-block-end-name "src" "SRC")) end_name: (expr) @org-block-end-name (#any-of? @org-block-end-name "src" "SRC"))
; Improved timestamp highlights
(entry
timestamp:
(timestamp
("<" @org.timestamp.active.delimiter)
(">" @org.timestamp.active.delimiter)))
(entry
timestamp:
(timestamp
("[" @org.timestamp.inactive.delimiter)
("]" @org.timestamp.inactive.delimiter)))

View File

@ -38,11 +38,12 @@ return {
local palette = require("kanagawa.colors").setup() local palette = require("kanagawa.colors").setup()
local colors = require("kanagawa.colors").setup().palette local colors = require("kanagawa.colors").setup().palette
-- This table was formed as an array to ensure the ordering of the keys. Since some -- HACK: This table was formed as an array to ensure the ordering of the keys. Since some
-- highlights defined below depend on other highlights being correctly set, order is -- highlights defined below depend on other highlights being correctly set, order is
-- very important. Lua does not guarantee hashmaps will be in the order they are -- very important. Lua does not guarantee hashmaps will be in the order they are
-- defined, thus the array. -- defined, thus the array.
---@type { [1]: string, [2]: vim.api.keyset.highlight | fun(): vim.api.keyset.highlight }[] --
---@type { [1]: string, [2]: vim.api.keyset.highlight | fun(): vim.api.keyset.highlight | table<Highlight.Keys, fun(): string | integer> }[]
local extra_hls = { local extra_hls = {
{ "NvimNotifyError", { fg = colors.samuraiRed } }, { "NvimNotifyError", { fg = colors.samuraiRed } },
{ "NvimNotifyWarn", { fg = colors.roninYellow } }, { "NvimNotifyWarn", { fg = colors.roninYellow } },
@ -52,6 +53,10 @@ return {
{ "Winbar", { bg = nil } }, { "Winbar", { bg = nil } },
{ "StatusLineNC", { bg = nil } }, { "StatusLineNC", { bg = nil } },
{ "WinBarNC", { bg = nil } }, { "WinBarNC", { bg = nil } },
{ "DiagnosticSignErrorCul", { link = "DiagnosticSignError" } },
{ "DiagnosticSignWarnCul", { link = "DiagnosticSignWarn" } },
{ "DiagnosticSignInfoCul", { link = "DiagnosticSignInfo" } },
{ "DiagnosticSignHintCul", { link = "DiagnosticSignHint" } },
{ "CursorLineNr", { fg = colors.roninYellow, bg = palette.theme.ui.bg_m1 } }, { "CursorLineNr", { fg = colors.roninYellow, bg = palette.theme.ui.bg_m1 } },
{ "CursorLineFold", { fg = colors.crystalBlue, bg = palette.theme.ui.bg_m1 } }, { "CursorLineFold", { fg = colors.crystalBlue, bg = palette.theme.ui.bg_m1 } },
{ "CursorLineSign", { bg = palette.theme.ui.bg_m1 } }, { "CursorLineSign", { bg = palette.theme.ui.bg_m1 } },
@ -217,23 +222,53 @@ return {
{ "NotificationWarning", { link = "NvimNotifyWarn" } }, { "NotificationWarning", { link = "NvimNotifyWarn" } },
{ "NotificationError", { link = "NvimNotifyError" } }, { "NotificationError", { link = "NvimNotifyError" } },
-- Markup specific -- Markup specific
{ "@markup.raw", { fg = colors.carpYellow, bg = colors.sumiInk2 } }, { "@markup.raw", { fg = colors.carpYellow, bg = colors.sumiInk2 } },
{ "@markup.raw.block", select_hl("@markup.raw", { "fg" }) }, { "@markup.raw.block", select_hl("@markup.raw", { "fg" }) },
{ "@markup.raw.delimiter", { link = "@punctuation.delimiter" } }, { "@markup.raw.delimiter", { link = "@punctuation.delimiter" } },
{ "CodeBlock", function() return { bg = get_hl("@markup.raw")().bg } end }, {
"CodeBlock",
function()
return { bg = get_hl("@markup.raw")().bg }
end,
},
{ "Headline", { bg = colors.sumiInk0 } }, { "Headline", { bg = colors.sumiInk0 } },
{ "@markup.list.checked", { fg = colors.springGreen } }, { "@markup.list.checked", { fg = colors.springGreen } },
{ "@markup.list.indeterminate", { fg = colors.carpYellow } }, { "@markup.list.indeterminate", { fg = colors.carpYellow } },
{ "@markup.list.unchecked", { fg = colors.crystalBlue } }, { "@markup.list.unchecked", { fg = colors.crystalBlue } },
{ "@OrgTSCheckboxChecked.org", { link = "@markup.list.checked" } }, { "@markup.verbatim", { fg = colors.springGreen, bg = colors.sumiInk0 } },
{ "@OrgTSCheckboxHalfChecked.org", { link = "@markup.list.indeterminate" } }, { "@org.verbatim", { link = "@markup.verbatim" } },
{ "@OrgTSCheckbox.org", { link = "@markup.list.unchecked" } }, { "@org.verbatim.delimiter", { link = "@markup.verbatim" } },
{ "org_verbatim", { fg = colors.springGreen, bg = colors.sumiInk0 } }, { "@org.timestamp", { underline = true, italic = false } },
{ "org_code", { link = "@markup.raw" } }, { "@org.timestamp.active", get_hl("@org.timestamp", { fg = colors.springViolet1 }) },
{ "@OrgTSBlock.org", { fg = colors.fujiGray, bold = true, italic = true } }, {
{ "@OrgTSDirective.org", { link = "@OrgTSBlock.org" } }, "@org.timestamp.active.delimiter",
{
fg = function()
return get_hl("@org.timestamp.active")().fg
end,
underline = false,
nocombine = true,
},
},
{
"@org.timestamp.inactive",
get_hl("@org.timestamp", {
fg = function()
return get_hl("@comment")().fg
end,
}),
},
{
"@org.timestamp.inactive.delimiter",
{
fg = function()
return get_hl("@org.timestamp.inactive")().fg
end,
underline = false,
nocombine = true,
},
},
-- Titles/Headlines -- Titles/Headlines
{ "@markup.heading.1", { fg = colors.crystalBlue, bold = true } }, { "@markup.heading.1", { fg = colors.crystalBlue, bold = true } },
@ -267,31 +302,44 @@ return {
vim.iter(extra_hls):enumerate():fold({}, function(t, index, tbl) vim.iter(extra_hls):enumerate():fold({}, function(t, index, tbl)
local hl_name = tbl[1] local hl_name = tbl[1]
local hl_opts = tbl[2] local hl_opts = tbl[2]
local wrapper = function()
if t[hl_name] then if t[hl_name] then
vim.notify(string.format("Duplicate highlight '%s' defined at index '%d'!", hl_name, index), vim.notify(
vim.log.levels.ERROR) string.format("Duplicate highlight '%s' defined at index '%d'!", hl_name, index),
vim.log.levels.ERROR
)
end end
if type(hl_opts) == "function" then if type(hl_opts) == "function" then
hl_opts = hl_opts() hl_opts = hl_opts()
end end
hl_opts = vim.iter(hl_opts):fold({}, function(hl_t, k, v)
if type(v) == "function" then
v = v()
end
hl_t[k] = v
return hl_t
end)
if hl_opts.force == nil then if hl_opts.force == nil then
vim.tbl_deep_extend("force", hl_opts, { force = true }) vim.tbl_deep_extend("force", hl_opts, { force = true })
end end
local success, err = pcall(vim.api.nvim_set_hl, 0, hl_name, hl_opts) vim.api.nvim_set_hl(0, hl_name, hl_opts)
t[hl_name] = true
return t
end
local success, result = pcall(wrapper)
if not success then if not success then
vim.notify( vim.notify(
string.format( string.format(
"Failed to set highlight for '%s', err: '%s' ... Table:\n%s", "Failed to set highlight for '%s', err: '%s' ... Table:\n%s",
hl_name, hl_name,
err, result,
vim.inspect(hl_opts) vim.inspect(hl_opts)
), ),
vim.log.levels.ERROR vim.log.levels.ERROR
) )
end end
t[hl_name] = true return result
return t
end) end)
end, end,
}, },

View File

@ -51,18 +51,6 @@ U.title_case = function(str)
return string.gsub(str, "(%a)([%w_']*)", inner) return string.gsub(str, "(%a)([%w_']*)", inner)
end end
---Get the effective given highlight, optionally overriding some of its fields
---@param name string Highlight name
---@param opts? vim.api.keyset.highlight
---@return fun(): vim.api.keyset.highlight
U.get_hl = function(name, opts)
---@return vim.api.keyset.highlight
return function()
---@diagnostic disable-next-line: return-type-mismatch
return vim.tbl_deep_extend("force", vim.api.nvim_get_hl(0, { name = name, link = false }), opts or {})
end
end
---@alias Highlight.Keys ---@alias Highlight.Keys
---| '"bold"' ---| '"bold"'
---| '"standout"' ---| '"standout"'
@ -95,6 +83,25 @@ end
---| '"force"' ---| '"force"'
---| '"url"' ---| '"url"'
---Get the effective given highlight, optionally overriding some of its fields
---@param name string Highlight name
---@param opts? vim.api.keyset.highlight | table<Highlight.Keys, fun(): string | integer>
---@return fun(): vim.api.keyset.highlight
U.get_hl = function(name, opts)
opts = vim.iter(opts or {}):fold({}, function(t, k, v)
if type(v) == "function" then
v = v()
end
t[k] = v
return t
end)
---@return vim.api.keyset.highlight
return function()
---@diagnostic disable-next-line: return-type-mismatch
return vim.tbl_deep_extend("force", vim.api.nvim_get_hl(0, { name = name, link = false }), opts or {})
end
end
---Get only the specified items from the highlight ---Get only the specified items from the highlight
---@param name string Highlight name ---@param name string Highlight name
---@param fields Highlight.Keys[] ---@param fields Highlight.Keys[]