nvim: yolokai: Sync with upstream

This commit is contained in:
Sanchayan Maity 2023-01-04 13:54:10 +05:30
parent f68aed5238
commit 8b6cf82abe
Signed by: sanchayanmaity
GPG key ID: 6F6A0609C12038F3

View file

@ -1,12 +1,12 @@
--[[ NOTHING INSIDE THIS FILE NEEDS TO BE EDITED BY THE USER. ]] --[[ NOTHING INSIDE THIS FILE NEEDS TO BE EDITED BY THE USER. ]]
--- @class Yolokai.Definition --- @class Yolokai.Definition
--- @field bg string|table the background color --- @field bg? string|table the background color
--- @field blend number the transparency value --- @field blend? number the transparency value
--- @field dark Yolokai.Definition special highlight definition for when `&bg` is 'dark' --- @field dark? Yolokai.Definition special highlight definition for when `&bg` is 'dark'
--- @field fg string|table the foreground color --- @field fg? string|table the foreground color
--- @field light Yolokai.Definition special highlight definition for when `&bg` is 'light' --- @field light? Yolokai.Definition special highlight definition for when `&bg` is 'light'
--- @field style Yolokai.Style special appearance alterations --- @field style? Yolokai.Style special appearance alterations
--- @class Yolokai.Style --- @class Yolokai.Style
--- @field color string|table color of underline or undercurl --- @field color string|table color of underline or undercurl
@ -23,20 +23,21 @@ local _PALETTE_CTERM = _USE_256 and 2 or 3
local _PALETTE_HEX = 1 local _PALETTE_HEX = 1
--- The `string` type. --- The `string` type.
local _TYPE_STRING = 'string' local _TYPE_STRING = type ''
--- The `table` type. --- The `table` type.
local _TYPE_TABLE = 'table' local _TYPE_TABLE = type {}
--[[/* Helper Functions */]] --[[/* Helper Functions */]]
--- @param color string|table the color name or definition --- @param color? string|table the color name or definition
--- @param index number --- @param index number
--- @return number|string color_value a hex, 16-bit, or ANSI color. "NONE" by default --- @return nil|number|string color_value a hex, 16-bit, or ANSI color. "NONE" by default
local function get(color, index) -- {{{ † local function get(color, index) -- {{{ †
if type(color) == _TYPE_TABLE and color[index] then if color and color[index] then
return color[index] return color[index]
elseif type(color) == _TYPE_STRING then elseif type(color) == _TYPE_STRING then
--- @diagnostic disable-next-line:return-type-mismatch (we test for `color == string`, which is a subtype of `(number|string)?`
return color return color
end end
end --}}} ‡ end --}}} ‡
@ -59,7 +60,7 @@ end
local yolokai = {} local yolokai = {}
--- @param name string the name of the highlight group --- @param name string the name of the highlight group
--- @return Yolokai.Definition definition an nvim-highlite compliant table describing the highlight group `name` --- @return Yolokai.Definition definition an nvim-yolokai compliant table describing the highlight group `name`
function yolokai.group(name) function yolokai.group(name)
local no_errors, definition = pcall(vim.api.nvim_get_hl_by_name, name, vim.go.termguicolors) local no_errors, definition = pcall(vim.api.nvim_get_hl_by_name, name, vim.go.termguicolors)
@ -96,6 +97,7 @@ function yolokai.highlight(group_name, definition) -- {{{ †
else else
-- Take care of special instructions for certain background colors. -- Take care of special instructions for certain background colors.
if definition[vim.go.background] then if definition[vim.go.background] then
--- @diagnostic disable-next-line: param-type-mismatch (`str.dark` and `str.light` are `nil`)
definition = use_background_with(definition) definition = use_background_with(definition)
end end
@ -109,7 +111,9 @@ function yolokai.highlight(group_name, definition) -- {{{ †
local style = definition.style local style = definition.style
if type(style) == _TYPE_TABLE then if type(style) == _TYPE_TABLE then
--- @diagnostic disable-next-line:param-type-mismatch (we check `type(style) == 'table'` right above this)
for _, option in ipairs(style) do highlight[option] = true end for _, option in ipairs(style) do highlight[option] = true end
--- @diagnostic disable-next-line:need-check-nil (we check `type(style) == 'table'` right above this)
highlight.special = get(style.color, _PALETTE_HEX) highlight.special = get(style.color, _PALETTE_HEX)
elseif style then elseif style then
highlight[style] = true highlight[style] = true
@ -137,16 +141,16 @@ return setmetatable(yolokai, {__call = function(self, normal, highlights, termin
--- @param resolve_links boolean whether to translate highlight links into full values --- @param resolve_links boolean whether to translate highlight links into full values
--- @returns the value at `tbl[key]`, when highlight links and embedded functions have been accounted for. --- @returns the value at `tbl[key]`, when highlight links and embedded functions have been accounted for.
local function resolve(tbl, key, resolve_links) local function resolve(tbl, key, resolve_links)
local value = tbl[key] local original_value = tbl[key]
local value_type = type(value) local original_value_type = type(original_value)
if value_type == 'function' then -- call and cache the result; next time, if it isn't a function this step will be skipped if original_value_type == 'function' then -- call and cache the result; next time, if it isn't a function this step will be skipped
tbl[key] = value(setmetatable({}, tbl[key] = original_value(setmetatable({},
{ {
__index = function(_, inner_key) return resolve(tbl, inner_key, true) end __index = function(_, inner_key) return resolve(tbl, inner_key, true) end
})) }))
elseif resolve_links and value_type == _TYPE_STRING and not string.find(value, '^#') then elseif resolve_links and original_value_type == _TYPE_STRING and not string.find(original_value, '^#') then
return resolve(tbl, value, resolve_links) return resolve(tbl, original_value, resolve_links)
end end
return tbl[key] return tbl[key]
@ -160,7 +164,6 @@ return setmetatable(yolokai, {__call = function(self, normal, highlights, termin
-- replace the colors_name -- replace the colors_name
vim.g.colors_name = color_name vim.g.colors_name = color_name
color_name = nil
-- If we aren't using hex nor 256 colorsets. -- If we aren't using hex nor 256 colorsets.
if not (vim.go.termguicolors or _USE_256) then vim.go.t_Co = '16' end if not (vim.go.termguicolors or _USE_256) then vim.go.t_Co = '16' end