From 12649d34fec045b6cd92c252a4210cabe78748c8 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 5 Sep 2022 20:32:31 +0200 Subject: [PATCH] feat: callbacks to easily override (or add) colors and highlights --- lua/lualine/themes/tokyonight.lua | 3 +- lua/tokyonight/colors.lua | 14 ++++--- lua/tokyonight/config.lua | 37 +++++++++++------ lua/tokyonight/theme.lua | 66 +++++++++++++++---------------- lua/tokyonight/util.lua | 42 ++------------------ 5 files changed, 71 insertions(+), 91 deletions(-) diff --git a/lua/lualine/themes/tokyonight.lua b/lua/lualine/themes/tokyonight.lua index 29cdffd..8c48cde 100644 --- a/lua/lualine/themes/tokyonight.lua +++ b/lua/lualine/themes/tokyonight.lua @@ -1,4 +1,5 @@ local colors = require("tokyonight.colors").setup({ transform = true }) +local config = require("tokyonight.config").options local tokyonight = {} @@ -34,7 +35,7 @@ tokyonight.inactive = { 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 mode.a.gui = "bold" end diff --git a/lua/tokyonight/colors.lua b/lua/tokyonight/colors.lua index f8461ac..22431d4 100644 --- a/lua/tokyonight/colors.lua +++ b/lua/tokyonight/colors.lua @@ -47,7 +47,7 @@ function M.setup(opts) colors.bg_dark = "#16161e" end util.bg = colors.bg - util.day_brightness = config.dayBrightness + util.day_brightness = config.day_brightness colors.diff = { add = util.darken(colors.green2, 0.15), @@ -72,8 +72,13 @@ function M.setup(opts) colors.bg_statusline = colors.bg_dark -- 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_float = config.darkFloat and colors.bg_dark or colors.bg + colors.bg_sidebar = config.styles.sidebars == "transparent" and colors.none + 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_search = colors.blue0 @@ -84,8 +89,7 @@ function M.setup(opts) colors.info = colors.blue2 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 util.invert_colors(colors) end diff --git a/lua/tokyonight/config.lua b/lua/tokyonight/config.lua index f2b4eff..68a9700 100644 --- a/lua/tokyonight/config.lua +++ b/lua/tokyonight/config.lua @@ -2,24 +2,35 @@ local M = {} ---@class Config 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 = { + -- Style to be applied to different syntax groups + -- Value is any valid attr-list value `:help attr-list` comments = "italic", - functions = "NONE", keywords = "italic", + functions = "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 = {}, - darkFloat = true, - darkSidebar = true, - dayBrightness = 0.3, - dev = false, - hideInactiveStatusline = false, - lualineBold = false, - sidebars = {}, - terminalColors = true, - transparent = false, - transparentSidebar = false, + sidebars = { "qf", "help" }, -- Set a darker background on sidebar-like windows. For example: `["qf", "vista_kind", "terminal", "packer"]` | + day_brightness = 0.3, -- Adjusts the brightness of the colors of the **Day** style. Number between 0 and 1, from dull to vibrant colors | + 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**. | + lualine_bold = false, -- When `true`, section headers in the lualine theme will be bold | + + --- You can override specific color groups to use other groups or a hex color | + --- fucntion will be called with a ColorScheme table + ---@param colors ColorScheme + on_colors = function(colors) end, + + --- 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 diff --git a/lua/tokyonight/theme.lua b/lua/tokyonight/theme.lua index 4b93497..b08bb83 100644 --- a/lua/tokyonight/theme.lua +++ b/lua/tokyonight/theme.lua @@ -15,8 +15,7 @@ local M = {} function M.setup() local config = require("tokyonight.config").options ---@class Theme - ---@field base Highlights - ---@field plugins Highlights + ---@field highlights Highlights local theme = { config = config, colors = colors.setup(), @@ -24,7 +23,9 @@ function M.setup() 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 ColorColumn = { bg = c.black }, -- used for the columns set with 'colorcolumn' 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 ErrorMsg = { fg = c.error }, -- error messages on the command line 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 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 @@ -188,26 +190,6 @@ function M.setup() ALEErrorSign = { fg = c.error }, 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. -- As of writing, tree-sitter support is a WIP, group names may change. @@ -284,9 +266,9 @@ function M.setup() rainbowcol7 = { fg = c.red }, -- LspTrouble - LspTroubleText = { fg = c.fg_dark }, - LspTroubleCount = { fg = c.magenta, bg = c.fg_gutter }, - LspTroubleNormal = { fg = c.fg_sidebar, bg = c.bg_sidebar }, + TroubleText = { fg = c.fg_dark }, + TroubleCount = { fg = c.magenta, bg = c.fg_gutter }, + TroubleNormal = { fg = c.fg_sidebar, bg = c.bg_sidebar }, -- Illuminate illuminatedWord = { bg = c.fg_gutter }, @@ -325,8 +307,8 @@ function M.setup() GitSignsDelete = { fg = c.gitSigns.delete }, -- diff mode: Deleted line |diff.txt| -- Telescope - TelescopeBorder = { fg = c.border_highlight, bg = config.transparent and c.bg_float or c.bg }, - TelescopeNormal = { fg = c.fg, bg = config.transparent and c.bg_float or c.bg }, + TelescopeBorder = { fg = c.border_highlight, bg = c.bg_float }, + TelescopeNormal = { fg = c.fg, bg = c.bg_float }, -- NvimTree NvimTreeNormal = { fg = c.fg_sidebar, bg = c.bg_sidebar }, @@ -555,13 +537,30 @@ function M.setup() 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 = {} - if config.hideInactiveStatusline then + if config.hide_inactive_statusline then local inactive = { style = "underline", bg = c.none, fg = c.bg, sp = c.border } -- StatusLineNC - theme.base.StatusLineNC = inactive + theme.highlights.StatusLineNC = inactive -- LuaLine for _, section in ipairs({ "a", "b", "c" }) do @@ -569,13 +568,14 @@ function M.setup() end -- mini.statusline - theme.plugins.MiniStatuslineInactive = inactive + theme.highlights.MiniStatuslineInactive = inactive end + config.on_highlights(theme.highlights, theme.colors) + if config.style == "day" or vim.o.background == "light" then util.invert_colors(theme.colors) - util.invert_highlights(theme.base) - util.invert_highlights(theme.plugins) + util.invert_highlights(theme.highlights) end return theme diff --git a/lua/tokyonight/util.lua b/lua/tokyonight/util.lua index a713f3d..5f7c203 100644 --- a/lua/tokyonight/util.lua +++ b/lua/tokyonight/util.lua @@ -68,9 +68,6 @@ function M.autocmds(config) vim.cmd([[augroup TokyoNight]]) vim.cmd([[ autocmd!]]) 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 if sidebar == "terminal" then vim.cmd([[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]]) @@ -164,13 +161,11 @@ function M.load(theme) vim.o.termguicolors = true vim.g.colors_name = "tokyonight" - -- vim.api.nvim__set_hl_ns(ns) - -- load base theme - M.syntax(theme.base) - M.syntax(theme.plugins) + + M.syntax(theme.highlights) -- vim.api.nvim_set_hl_ns(M.ns) - if theme.config.terminalColors then + if theme.config.terminal_colors then M.terminal(theme.colors) end @@ -181,35 +176,4 @@ function M.load(theme) end, 100) 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