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
1 changed files with 39 additions and 36 deletions

View File

@ -1,10 +1,17 @@
--[[ 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.
--- @module nvim-cartographer
--- @alias yolokai table
--- @class Yolokai.Definition
--- @field bg string|table the background color
--- @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.
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.
local _TYPE_TABLE = 'table'
--[[/* 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
--[[/* Helper Functions */]]
--- @param color string|table the color name or definition
--- @param index number
@ -51,7 +48,7 @@ end --}}} ‡
--- Take a `command` and add color-specifying components to it.
--- @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) -- {{{ †
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)
@ -80,34 +77,40 @@ end
local function tohex(rgb) return string.format('#%06x', rgb) end
--- 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
local function use_background_with(definition)
return setmetatable(definition[vim.go.background], {__index = definition})
end
--[[/* MODULE */]]
--[[/* Module */]]
--- A Neovim plugin to create more straightforward syntax for Lua `:map`ping and `:unmap`ping.
--- @class Yolokai
local yolokai = {}
--- @param group_name string
--- @return table definition a nvim-yolokai compliant table describing `group_name`
function yolokai.group(group_name)
local no_errors, group_definition = pcall(vim.api.nvim_get_hl_by_name, group_name, vim.go.termguicolors)
--- @param name string the name of the highlight group
--- @return Yolokai.Definition definition an nvim-highlite compliant table describing the highlight group `name`
function yolokai.group(name)
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
local style = vim.tbl_filter(filter_group_style, vim.tbl_keys(group_definition))
if group_definition.special then
style.color = tohex(group_definition.special)
local style = {}
for k, v in pairs(definition) do
if k == 'special' then
style.color = tohex(v)
elseif k ~= 'background' and k ~= 'blend' and k ~= 'foreground' then
style[#style+1] = k
end
end
return
{
fg = group_definition.foreground and tohex(group_definition.foreground) or _NONE,
bg = group_definition.background and tohex(group_definition.background) or _NONE,
blend = group_definition.blend,
fg = definition.foreground and tohex(definition.foreground) or _NONE,
bg = definition.background and tohex(definition.background) or _NONE,
blend = definition.blend,
style = style or _NONE
}
end
@ -115,16 +118,16 @@ end
-- Generate a `: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 definition string|table a link or an attribute map
function yolokai.highlight(highlight_group, definition) -- {{{ †
--- @param group_name string the `{group-name}` argument for `:highlight`
--- @param definition Yolokai.Definition|string a link or an attribute map
function yolokai.highlight(group_name, definition) -- {{{ †
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
end
-- 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.
if definition[vim.go.background] then
@ -192,8 +195,8 @@ return setmetatable(yolokai, {__call = function(self, normal, highlights, termin
self.highlight('Normal', normal)
-- Highlight everything else.
for highlight_group, _ in pairs(highlights) do
self.highlight(highlight_group, resolve(highlights, highlight_group, false))
for group_name, _ in pairs(highlights) do
self.highlight(group_name, resolve(highlights, group_name, false))
end
-- Set the terminal highlight colors.