feat: use vim.api.nvim_set_hl instead of ":hi"

This commit is contained in:
Folke Lemaitre
2022-09-05 16:56:09 +02:00
parent 28b790dbd6
commit 426aed0670

View File

@@ -1,12 +1,8 @@
local hsluv = require("tokyonight.hsluv") local M = {}
local util = {} M.bg = "#000000"
M.fg = "#ffffff"
util.colorsUsed = {} M.day_brightness = 0.3
util.bg = "#000000"
util.fg = "#ffffff"
util.day_brightness = 0.3
---@param c string ---@param c string
local function hexToRgb(c) local function hexToRgb(c)
@@ -29,83 +25,38 @@ function M.blend(fg, bg, alpha)
return string.format("#%02x%02x%02x", blendChannel(1), blendChannel(2), blendChannel(3)) return string.format("#%02x%02x%02x", blendChannel(1), blendChannel(2), blendChannel(3))
end end
function util.darken(hex, amount, bg) function M.darken(hex, amount, bg)
return util.blend(hex, bg or util.bg, math.abs(amount)) return M.blend(hex, bg or M.bg, math.abs(amount))
end end
function util.lighten(hex, amount, fg) function M.lighten(hex, amount, fg)
return util.blend(hex, fg or util.fg, math.abs(amount)) return M.blend(hex, fg or M.fg, math.abs(amount))
end end
function util.brighten(color, percentage) function M.invert_color(color)
local hsl = hsluv.hex_to_hsluv(color) local hsluv = require("tokyonight.hsluv")
local larpSpace = 100 - hsl[3]
if percentage < 0 then
larpSpace = hsl[3]
end
hsl[3] = hsl[3] + larpSpace * percentage
return hsluv.hsluv_to_hex(hsl)
end
function util.invert_color(color)
if color ~= "NONE" then if color ~= "NONE" then
local hsl = hsluv.hex_to_hsluv(color) local hsl = hsluv.hex_to_hsluv(color)
hsl[3] = 100 - hsl[3] hsl[3] = 100 - hsl[3]
if hsl[3] < 40 then if hsl[3] < 40 then
hsl[3] = hsl[3] + (100 - hsl[3]) * util.day_brightness hsl[3] = hsl[3] + (100 - hsl[3]) * M.day_brightness
end end
return hsluv.hsluv_to_hex(hsl) return hsluv.hsluv_to_hex(hsl)
end end
return color return color
end end
-- local ns = vim.api.nvim_create_namespace("tokyonight") function M.highlight(group, color)
function util.highlight(group, color) local hl = { fg = color.fg, bg = color.bg, sp = color.sp, link = color.link }
if color.fg then if color.style and color.style:lower() ~= "none" then
util.colorsUsed[color.fg] = true for s in string.gmatch(color.style, "([^,]+)") do
end hl[s] = true
if color.bg then
util.colorsUsed[color.bg] = true
end
if color.sp then
util.colorsUsed[color.sp] = true
end
local style = color.style and "gui=" .. color.style or "gui=NONE"
local fg = color.fg and "guifg=" .. color.fg or "guifg=NONE"
local bg = color.bg and "guibg=" .. color.bg or "guibg=NONE"
local sp = color.sp and "guisp=" .. color.sp or ""
local hl = "highlight " .. group .. " " .. style .. " " .. fg .. " " .. bg .. " " .. sp
if color.link then
vim.cmd("highlight! link " .. group .. " " .. color.link)
else
-- local data = {}
-- if color.fg then data.foreground = color.fg end
-- if color.bg then data.background = color.bg end
-- if color.sp then data.special = color.sp end
-- if color.style then data[color.style] = true end
-- vim.api.nvim_set_hl(ns, group, data)
vim.cmd(hl)
end
end
function util.debug(colors)
colors = colors or require("tokyonight.colors")
-- Dump unused colors
for name, color in pairs(colors) do
if type(color) == "table" then
util.debug(color)
else
if util.colorsUsed[color] == nil then
print("not used: " .. name .. " : " .. color)
end
end end
end end
vim.api.nvim_set_hl(0, group, hl)
end end
--- Delete the autocmds when the theme changes to something else --- Delete the autocmds when the theme changes to something else
function util.onColorScheme() function M.onColorScheme()
if vim.g.colors_name ~= "tokyonight" then if vim.g.colors_name ~= "tokyonight" then
vim.cmd([[autocmd! TokyoNight]]) vim.cmd([[autocmd! TokyoNight]])
vim.cmd([[augroup! TokyoNight]]) vim.cmd([[augroup! TokyoNight]])
@@ -113,7 +64,7 @@ function util.onColorScheme()
end end
---@param config Config ---@param config Config
function util.autocmds(config) function M.autocmds(config)
vim.cmd([[augroup TokyoNight]]) vim.cmd([[augroup TokyoNight]])
vim.cmd([[ autocmd!]]) vim.cmd([[ autocmd!]])
vim.cmd([[ autocmd ColorScheme * lua require("tokyonight.util").onColorScheme()]]) vim.cmd([[ autocmd ColorScheme * lua require("tokyonight.util").onColorScheme()]])
@@ -136,20 +87,20 @@ end
-- --
---@param str string template string ---@param str string template string
---@param table table key value pairs to replace in the string ---@param table table key value pairs to replace in the string
function util.template(str, table) function M.template(str, table)
return (str:gsub("($%b{})", function(w) return (str:gsub("($%b{})", function(w)
return table[w:sub(3, -2)] or w return table[w:sub(3, -2)] or w
end)) end))
end end
function util.syntax(syntax) function M.syntax(syntax)
for group, colors in pairs(syntax) do for group, colors in pairs(syntax) do
util.highlight(group, colors) M.highlight(group, colors)
end end
end end
---@param colors ColorScheme ---@param colors ColorScheme
function util.terminal(colors) function M.terminal(colors)
-- dark -- dark
vim.g.terminal_color_0 = colors.black vim.g.terminal_color_0 = colors.black
vim.g.terminal_color_8 = colors.terminal_black vim.g.terminal_color_8 = colors.terminal_black
@@ -179,33 +130,33 @@ function util.terminal(colors)
end end
---@param colors ColorScheme ---@param colors ColorScheme
function util.invert_colors(colors) function M.invert_colors(colors)
if type(colors) == "string" then if type(colors) == "string" then
---@diagnostic disable-next-line: return-type-mismatch ---@diagnostic disable-next-line: return-type-mismatch
return util.invert_color(colors) return M.invert_color(colors)
end end
for key, value in pairs(colors) do for key, value in pairs(colors) do
colors[key] = util.invert_colors(value) colors[key] = M.invert_colors(value)
end end
end end
---@param hls Highlights ---@param hls Highlights
function util.invert_highlights(hls) function M.invert_highlights(hls)
for _, hl in pairs(hls) do for _, hl in pairs(hls) do
if hl.fg then if hl.fg then
hl.fg = util.invert_color(hl.fg) hl.fg = M.invert_color(hl.fg)
end end
if hl.bg then if hl.bg then
hl.bg = util.invert_color(hl.bg) hl.bg = M.invert_color(hl.bg)
end end
if hl.sp then if hl.sp then
hl.sp = util.invert_color(hl.sp) hl.sp = M.invert_color(hl.sp)
end end
end end
end end
---@param theme Theme ---@param theme Theme
function util.load(theme) function M.load(theme)
-- only needed to clear when not the default colorscheme -- only needed to clear when not the default colorscheme
if vim.g.colors_name then if vim.g.colors_name then
vim.cmd("hi clear") vim.cmd("hi clear")
@@ -215,22 +166,24 @@ function util.load(theme)
vim.g.colors_name = "tokyonight" vim.g.colors_name = "tokyonight"
-- vim.api.nvim__set_hl_ns(ns) -- vim.api.nvim__set_hl_ns(ns)
-- load base theme -- load base theme
util.syntax(theme.base) M.syntax(theme.base)
util.syntax(theme.plugins) M.syntax(theme.plugins)
-- vim.api.nvim_set_hl_ns(M.ns)
if theme.config.terminalColors then if theme.config.terminalColors then
util.terminal(theme.colors) M.terminal(theme.colors)
end end
util.autocmds(theme.config) M.autocmds(theme.config)
vim.defer_fn(function() vim.defer_fn(function()
util.syntax(theme.defer) M.syntax(theme.defer)
end, 100) end, 100)
end end
---@param config Config ---@param config Config
---@param colors ColorScheme ---@param colors ColorScheme
function util.color_overrides(colors, config) function M.color_overrides(colors, config)
if type(config.colors) == "table" then if type(config.colors) == "table" then
for key, value in pairs(config.colors) do for key, value in pairs(config.colors) do
if not colors[key] then if not colors[key] then
@@ -239,7 +192,7 @@ function util.color_overrides(colors, config)
-- Patch: https://github.com/ful1e5/onedark.nvim/issues/6 -- Patch: https://github.com/ful1e5/onedark.nvim/issues/6
if type(colors[key]) == "table" then if type(colors[key]) == "table" then
util.color_overrides(colors[key], { colors = value }) M.color_overrides(colors[key], { colors = value })
else else
if value:lower() == "none" then if value:lower() == "none" then
-- set to none -- set to none
@@ -259,4 +212,4 @@ function util.color_overrides(colors, config)
end end
end end
return util return M