feat: callbacks to easily override (or add) colors and highlights

This commit is contained in:
Folke Lemaitre
2022-09-05 20:32:31 +02:00
parent ec13e0f44f
commit 12649d34fe
5 changed files with 71 additions and 91 deletions

View File

@@ -1,4 +1,5 @@
local colors = require("tokyonight.colors").setup({ transform = true }) local colors = require("tokyonight.colors").setup({ transform = true })
local config = require("tokyonight.config").options
local tokyonight = {} local tokyonight = {}
@@ -34,7 +35,7 @@ tokyonight.inactive = {
c = { bg = colors.bg_statusline, fg = colors.fg_gutter }, c = { bg = colors.bg_statusline, fg = colors.fg_gutter },
} }
if vim.g.tokyonight_lualine_bold then if config.lualine_bold then
for _, mode in pairs(tokyonight) do for _, mode in pairs(tokyonight) do
mode.a.gui = "bold" mode.a.gui = "bold"
end end

View File

@@ -47,7 +47,7 @@ function M.setup(opts)
colors.bg_dark = "#16161e" colors.bg_dark = "#16161e"
end end
util.bg = colors.bg util.bg = colors.bg
util.day_brightness = config.dayBrightness util.day_brightness = config.day_brightness
colors.diff = { colors.diff = {
add = util.darken(colors.green2, 0.15), add = util.darken(colors.green2, 0.15),
@@ -72,8 +72,13 @@ function M.setup(opts)
colors.bg_statusline = colors.bg_dark colors.bg_statusline = colors.bg_dark
-- Sidebar and Floats are configurable -- Sidebar and Floats are configurable
colors.bg_sidebar = (config.transparentSidebar and colors.none) or config.darkSidebar and colors.bg_dark or colors.bg colors.bg_sidebar = config.styles.sidebars == "transparent" and colors.none
colors.bg_float = config.darkFloat and colors.bg_dark or colors.bg or config.styles.sidebars == "dark" and colors.bg_dark
or colors.bg
colors.bg_float = config.styles.floats == "transparent" and colors.none
or config.styles.floats == "dark" and colors.bg_dark
or colors.bg
colors.bg_visual = util.darken(colors.blue0, 0.7) colors.bg_visual = util.darken(colors.blue0, 0.7)
colors.bg_search = colors.blue0 colors.bg_search = colors.blue0
@@ -84,8 +89,7 @@ function M.setup(opts)
colors.info = colors.blue2 colors.info = colors.blue2
colors.hint = colors.teal colors.hint = colors.teal
util.color_overrides(colors, config) config.on_colors(colors)
if opts.transform and (config.style == "day" or vim.o.background == "light") then if opts.transform and (config.style == "day" or vim.o.background == "light") then
util.invert_colors(colors) util.invert_colors(colors)
end end

View File

@@ -2,24 +2,35 @@ local M = {}
---@class Config ---@class Config
local defaults = { local defaults = {
style = "storm", style = "storm", -- The theme comes in three styles, `storm`, a darker variant `night` and `day`
transparent = false, -- Enable this to disable setting the background color
terminal_colors = true, -- Configure the colors used when opening a `:terminal` in Neovim |
styles = { styles = {
-- Style to be applied to different syntax groups
-- Value is any valid attr-list value `:help attr-list`
comments = "italic", comments = "italic",
functions = "NONE",
keywords = "italic", keywords = "italic",
functions = "NONE",
variables = "NONE", variables = "NONE",
-- Background styles. Can be "dark", "transparent" or "normal"
sidebars = "dark", -- style for sidebars, see below
floats = "dark", -- style for floating windows
}, },
colors = {}, sidebars = { "qf", "help" }, -- Set a darker background on sidebar-like windows. For example: `["qf", "vista_kind", "terminal", "packer"]` |
darkFloat = true, day_brightness = 0.3, -- Adjusts the brightness of the colors of the **Day** style. Number between 0 and 1, from dull to vibrant colors |
darkSidebar = true, hide_inactive_statusline = false, -- Enabling this option, will hide inactive statuslines and replace them with a thin border instead. Should work with the standard **StatusLine** and **LuaLine**. |
dayBrightness = 0.3, lualine_bold = false, -- When `true`, section headers in the lualine theme will be bold |
dev = false,
hideInactiveStatusline = false, --- You can override specific color groups to use other groups or a hex color |
lualineBold = false, --- fucntion will be called with a ColorScheme table
sidebars = {}, ---@param colors ColorScheme
terminalColors = true, on_colors = function(colors) end,
transparent = false,
transparentSidebar = false, --- You can override specific highlights to use other groups or a hex color |
--- fucntion will be called with a Highlights and ColorScheme table
---@param highlights Highlights
---@param colors ColorScheme
on_highlights = function(highlights, colors) end,
} }
---@type Config ---@type Config

View File

@@ -15,8 +15,7 @@ local M = {}
function M.setup() function M.setup()
local config = require("tokyonight.config").options local config = require("tokyonight.config").options
---@class Theme ---@class Theme
---@field base Highlights ---@field highlights Highlights
---@field plugins Highlights
local theme = { local theme = {
config = config, config = config,
colors = colors.setup(), colors = colors.setup(),
@@ -24,7 +23,9 @@ function M.setup()
local c = theme.colors local c = theme.colors
theme.base = { theme.highlights = {
Foo = { bg = c.magenta2, fg = c.magenta2 },
Comment = { fg = c.comment, style = config.styles.comments }, -- any comment Comment = { fg = c.comment, style = config.styles.comments }, -- any comment
ColorColumn = { bg = c.black }, -- used for the columns set with 'colorcolumn' ColorColumn = { bg = c.black }, -- used for the columns set with 'colorcolumn'
Conceal = { fg = c.dark5 }, -- placeholder characters substituted for concealed text (see 'conceallevel') Conceal = { fg = c.dark5 }, -- placeholder characters substituted for concealed text (see 'conceallevel')
@@ -43,6 +44,7 @@ function M.setup()
-- TermCursorNC= { }, -- cursor in an unfocused terminal -- TermCursorNC= { }, -- cursor in an unfocused terminal
ErrorMsg = { fg = c.error }, -- error messages on the command line ErrorMsg = { fg = c.error }, -- error messages on the command line
VertSplit = { fg = c.border }, -- the column separating vertically split windows VertSplit = { fg = c.border }, -- the column separating vertically split windows
WinSeparator = { fg = c.border, style = "bold" }, -- the column separating vertically split windows
Folded = { fg = c.blue, bg = c.fg_gutter }, -- line used for closed folds Folded = { fg = c.blue, bg = c.fg_gutter }, -- line used for closed folds
FoldColumn = { bg = c.bg, fg = c.comment }, -- 'foldcolumn' FoldColumn = { bg = c.bg, fg = c.comment }, -- 'foldcolumn'
SignColumn = { bg = config.transparent and c.none or c.bg, fg = c.fg_gutter }, -- column where |signs| are displayed SignColumn = { bg = config.transparent and c.none or c.bg, fg = c.fg_gutter }, -- column where |signs| are displayed
@@ -188,26 +190,6 @@ function M.setup()
ALEErrorSign = { fg = c.error }, ALEErrorSign = { fg = c.error },
ALEWarningSign = { fg = c.warning }, ALEWarningSign = { fg = c.warning },
}
if not vim.diagnostic then
local severity_map = {
Error = "Error",
Warn = "Warning",
Info = "Information",
Hint = "Hint",
}
local types = { "Default", "VirtualText", "Underline" }
for _, type in ipairs(types) do
for snew, sold in pairs(severity_map) do
theme.base["LspDiagnostics" .. type .. sold] = {
link = "Diagnostic" .. (type == "Default" and "" or type) .. snew,
}
end
end
end
theme.plugins = {
-- These groups are for the neovim tree-sitter highlights. -- These groups are for the neovim tree-sitter highlights.
-- As of writing, tree-sitter support is a WIP, group names may change. -- As of writing, tree-sitter support is a WIP, group names may change.
@@ -284,9 +266,9 @@ function M.setup()
rainbowcol7 = { fg = c.red }, rainbowcol7 = { fg = c.red },
-- LspTrouble -- LspTrouble
LspTroubleText = { fg = c.fg_dark }, TroubleText = { fg = c.fg_dark },
LspTroubleCount = { fg = c.magenta, bg = c.fg_gutter }, TroubleCount = { fg = c.magenta, bg = c.fg_gutter },
LspTroubleNormal = { fg = c.fg_sidebar, bg = c.bg_sidebar }, TroubleNormal = { fg = c.fg_sidebar, bg = c.bg_sidebar },
-- Illuminate -- Illuminate
illuminatedWord = { bg = c.fg_gutter }, illuminatedWord = { bg = c.fg_gutter },
@@ -325,8 +307,8 @@ function M.setup()
GitSignsDelete = { fg = c.gitSigns.delete }, -- diff mode: Deleted line |diff.txt| GitSignsDelete = { fg = c.gitSigns.delete }, -- diff mode: Deleted line |diff.txt|
-- Telescope -- Telescope
TelescopeBorder = { fg = c.border_highlight, bg = config.transparent and c.bg_float or c.bg }, TelescopeBorder = { fg = c.border_highlight, bg = c.bg_float },
TelescopeNormal = { fg = c.fg, bg = config.transparent and c.bg_float or c.bg }, TelescopeNormal = { fg = c.fg, bg = c.bg_float },
-- NvimTree -- NvimTree
NvimTreeNormal = { fg = c.fg_sidebar, bg = c.bg_sidebar }, NvimTreeNormal = { fg = c.fg_sidebar, bg = c.bg_sidebar },
@@ -555,13 +537,30 @@ function M.setup()
MiniTrailspace = { bg = c.red }, MiniTrailspace = { bg = c.red },
} }
if not vim.diagnostic then
local severity_map = {
Error = "Error",
Warn = "Warning",
Info = "Information",
Hint = "Hint",
}
local types = { "Default", "VirtualText", "Underline" }
for _, type in ipairs(types) do
for snew, sold in pairs(severity_map) do
theme.highlights["LspDiagnostics" .. type .. sold] = {
link = "Diagnostic" .. (type == "Default" and "" or type) .. snew,
}
end
end
end
theme.defer = {} theme.defer = {}
if config.hideInactiveStatusline then if config.hide_inactive_statusline then
local inactive = { style = "underline", bg = c.none, fg = c.bg, sp = c.border } local inactive = { style = "underline", bg = c.none, fg = c.bg, sp = c.border }
-- StatusLineNC -- StatusLineNC
theme.base.StatusLineNC = inactive theme.highlights.StatusLineNC = inactive
-- LuaLine -- LuaLine
for _, section in ipairs({ "a", "b", "c" }) do for _, section in ipairs({ "a", "b", "c" }) do
@@ -569,13 +568,14 @@ function M.setup()
end end
-- mini.statusline -- mini.statusline
theme.plugins.MiniStatuslineInactive = inactive theme.highlights.MiniStatuslineInactive = inactive
end end
config.on_highlights(theme.highlights, theme.colors)
if config.style == "day" or vim.o.background == "light" then if config.style == "day" or vim.o.background == "light" then
util.invert_colors(theme.colors) util.invert_colors(theme.colors)
util.invert_highlights(theme.base) util.invert_highlights(theme.highlights)
util.invert_highlights(theme.plugins)
end end
return theme return theme

View File

@@ -68,9 +68,6 @@ 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()]])
if config.dev then
vim.cmd([[ autocmd BufWritePost */lua/tokyonight/** nested colorscheme tokyonight]])
end
for _, sidebar in ipairs(config.sidebars) do for _, sidebar in ipairs(config.sidebars) do
if sidebar == "terminal" then if sidebar == "terminal" then
vim.cmd([[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]]) vim.cmd([[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
@@ -164,13 +161,11 @@ function M.load(theme)
vim.o.termguicolors = true vim.o.termguicolors = true
vim.g.colors_name = "tokyonight" vim.g.colors_name = "tokyonight"
-- vim.api.nvim__set_hl_ns(ns)
-- load base theme M.syntax(theme.highlights)
M.syntax(theme.base)
M.syntax(theme.plugins)
-- vim.api.nvim_set_hl_ns(M.ns) -- vim.api.nvim_set_hl_ns(M.ns)
if theme.config.terminalColors then if theme.config.terminal_colors then
M.terminal(theme.colors) M.terminal(theme.colors)
end end
@@ -181,35 +176,4 @@ function M.load(theme)
end, 100) end, 100)
end end
---@param config Config
---@param colors ColorScheme
function M.color_overrides(colors, config)
if type(config.colors) == "table" then
for key, value in pairs(config.colors) do
if not colors[key] then
error("Color " .. key .. " does not exist")
end
-- Patch: https://github.com/ful1e5/onedark.nvim/issues/6
if type(colors[key]) == "table" then
M.color_overrides(colors[key], { colors = value })
else
if value:lower() == "none" then
-- set to none
colors[key] = "NONE"
elseif string.sub(value, 1, 1) == "#" then
-- hex override
colors[key] = value
else
-- another group
if not colors[value] then
error("Color " .. value .. " does not exist")
end
colors[key] = colors[value]
end
end
end
end
end
return M return M