nvim: yolokai: Sync with nvim-highlite upstream

This commit is contained in:
Sanchayan Maity 2022-01-04 12:24:11 +05:30
parent 25de9376a9
commit 9251900f70

View file

@ -1,10 +1,17 @@
--[[ NOTHING INSIDE THIS FILE NEEDS TO BE EDITED BY THE USER. ]] --[[ NOTHING INSIDE THIS FILE NEEDS TO BE EDITED BY THE USER. ]]
--- A Neovim plugin to create more straightforward syntax for Lua `:map`ping and `:unmap`ping. --- @class Yolokai.Definition
--- @module nvim-cartographer --- @field bg string|table the background color
--- @alias yolokai table --- @field blend number the transparency value
--- @field dark Yolokai.Definition special highlight definition for when `&bg` is 'dark'
--- @field fg string|table the foreground color
--- @field light Yolokai.Definition special highlight definition for when `&bg` is 'light'
--- @field style Yolokai.Style special appearance alterations
--[[/* VARS */]] --- @class Yolokai.Style
--- @field color string|table color of underline or undercurl
--[[/* Vars */]]
--- Which set of colors to use. --- Which set of colors to use.
local _USE_256 = tonumber(vim.go.t_Co) > 255 or string.find(vim.env.TERM, '256') local _USE_256 = tonumber(vim.go.t_Co) > 255 or string.find(vim.env.TERM, '256')
@ -24,17 +31,7 @@ local _TYPE_STRING = 'string'
--- The `table` type. --- The `table` type.
local _TYPE_TABLE = 'table' local _TYPE_TABLE = 'table'
--[[/* HELPER FUNCTIONS */]] --[[/* Helper Functions */]]
--- Filter out information not pertaining to styles
--- @param key string the field from `nvim_get_hl_by_name`
--- @return boolean should_not_filter `true` if the field should not be filtered
local function filter_group_style(key)
return key ~= 'background'
and key ~= 'blend'
and key ~= 'foreground'
and key ~= 'special'
end
--- @param color string|table the color name or definition --- @param color string|table the color name or definition
--- @param index number --- @param index number
@ -51,7 +48,7 @@ end --}}} ‡
--- Take a `command` and add color-specifying components to it. --- Take a `command` and add color-specifying components to it.
--- @param command table the in-progress `:highlight` command --- @param command table the in-progress `:highlight` command
--- @param definition table the definition of the highlight group --- @param definition Yolokai.Definition the definition of the highlight group
local function colorize(command, definition) -- {{{ † local function colorize(command, definition) -- {{{ †
command[#command+1]=' guibg='..get(definition.bg, _PALETTE_HEX)..' guifg='..get(definition.fg, _PALETTE_HEX) command[#command+1]=' guibg='..get(definition.bg, _PALETTE_HEX)..' guifg='..get(definition.fg, _PALETTE_HEX)
..' ctermbg='..get(definition.bg, _PALETTE_CTERM)..' ctermfg='..get(definition.fg, _PALETTE_CTERM) ..' ctermbg='..get(definition.bg, _PALETTE_CTERM)..' ctermfg='..get(definition.fg, _PALETTE_CTERM)
@ -80,34 +77,40 @@ end
local function tohex(rgb) return string.format('#%06x', rgb) end local function tohex(rgb) return string.format('#%06x', rgb) end
--- Create a metatable which prioritizes entries in the `&bg` index of `definition` --- Create a metatable which prioritizes entries in the `&bg` index of `definition`
--- @param definition table the definition of the highlight group --- @param definition Yolokai.Definition the definition of the highlight group
--- @return table --- @return table
local function use_background_with(definition) local function use_background_with(definition)
return setmetatable(definition[vim.go.background], {__index = definition}) return setmetatable(definition[vim.go.background], {__index = definition})
end end
--[[/* MODULE */]] --[[/* Module */]]
--- A Neovim plugin to create more straightforward syntax for Lua `:map`ping and `:unmap`ping.
--- @class Yolokai
local yolokai = {} local yolokai = {}
--- @param group_name string --- @param name string the name of the highlight group
--- @return table definition a nvim-yolokai compliant table describing `group_name` --- @return Yolokai.Definition definition an nvim-highlite compliant table describing the highlight group `name`
function yolokai.group(group_name) function yolokai.group(name)
local no_errors, group_definition = pcall(vim.api.nvim_get_hl_by_name, group_name, vim.go.termguicolors) local no_errors, definition = pcall(vim.api.nvim_get_hl_by_name, name, vim.go.termguicolors)
if not no_errors then group_definition = {} end if not no_errors then definition = {} end
-- the style of the highlight group -- the style of the highlight group
local style = vim.tbl_filter(filter_group_style, vim.tbl_keys(group_definition)) local style = {}
if group_definition.special then for k, v in pairs(definition) do
style.color = tohex(group_definition.special) if k == 'special' then
style.color = tohex(v)
elseif k ~= 'background' and k ~= 'blend' and k ~= 'foreground' then
style[#style+1] = k
end
end end
return return
{ {
fg = group_definition.foreground and tohex(group_definition.foreground) or _NONE, fg = definition.foreground and tohex(definition.foreground) or _NONE,
bg = group_definition.background and tohex(group_definition.background) or _NONE, bg = definition.background and tohex(definition.background) or _NONE,
blend = group_definition.blend, blend = definition.blend,
style = style or _NONE style = style or _NONE
} }
end end
@ -115,16 +118,16 @@ end
-- Generate a `:highlight` command from a group and some definition. -- Generate a `:highlight` command from a group and some definition.
--- Generate and execute `:highlight` command from a group and some definition. --- Generate and execute `:highlight` command from a group and some definition.
--- @param highlight_group string the `{group-name}` argument for `:highlight` --- @param group_name string the `{group-name}` argument for `:highlight`
--- @param definition string|table a link or an attribute map --- @param definition Yolokai.Definition|string a link or an attribute map
function yolokai.highlight(highlight_group, definition) -- {{{ † function yolokai.highlight(group_name, definition) -- {{{ †
if type(definition) == _TYPE_STRING then -- `highlight_group` is a link to another group. if type(definition) == _TYPE_STRING then -- `highlight_group` is a link to another group.
vim.api.nvim_command('hi! link '..highlight_group..' '..definition) vim.api.nvim_command('hi! link '..group_name..' '..definition)
return return
end end
-- The base highlight command -- The base highlight command
local highlight_cmd = {'hi! ', highlight_group} local highlight_cmd = {'hi! ', group_name}
-- 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
@ -192,8 +195,8 @@ return setmetatable(yolokai, {__call = function(self, normal, highlights, termin
self.highlight('Normal', normal) self.highlight('Normal', normal)
-- Highlight everything else. -- Highlight everything else.
for highlight_group, _ in pairs(highlights) do for group_name, _ in pairs(highlights) do
self.highlight(highlight_group, resolve(highlights, highlight_group, false)) self.highlight(group_name, resolve(highlights, group_name, false))
end end
-- Set the terminal highlight colors. -- Set the terminal highlight colors.