From dba4e35903b6c22fda33e5f1f4139b34ced57e7b Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 5 Sep 2022 14:06:03 +0200 Subject: [PATCH] refactor: refactored code to better deal with the day theme --- lua/lightline/colorscheme/tokyonight.lua | 3 +- lua/lualine/themes/tokyonight.lua | 4 +- lua/tokyonight/colors.lua | 12 ++--- lua/tokyonight/config.lua | 59 +++++++++++------------- lua/tokyonight/extra/init.lua | 5 +- lua/tokyonight/theme.lua | 45 ++++++++++++------ lua/tokyonight/util.lua | 23 ++++++--- 7 files changed, 84 insertions(+), 67 deletions(-) diff --git a/lua/lightline/colorscheme/tokyonight.lua b/lua/lightline/colorscheme/tokyonight.lua index 5220f12..dba1c42 100644 --- a/lua/lightline/colorscheme/tokyonight.lua +++ b/lua/lightline/colorscheme/tokyonight.lua @@ -1,5 +1,4 @@ -local config = require("tokyonight.config") -local colors = require("tokyonight.colors").setup(config) +local colors = require("tokyonight.colors").setup({ transform = true }) local tokyonight = {} diff --git a/lua/lualine/themes/tokyonight.lua b/lua/lualine/themes/tokyonight.lua index 88c41ba..29cdffd 100644 --- a/lua/lualine/themes/tokyonight.lua +++ b/lua/lualine/themes/tokyonight.lua @@ -1,6 +1,4 @@ -local config = require("tokyonight.config") -local colors = require("tokyonight.colors").setup(config) -local util = require("tokyonight.util") +local colors = require("tokyonight.colors").setup({ transform = true }) local tokyonight = {} diff --git a/lua/tokyonight/colors.lua b/lua/tokyonight/colors.lua index 58fe6ae..d7c9a5f 100644 --- a/lua/tokyonight/colors.lua +++ b/lua/tokyonight/colors.lua @@ -1,11 +1,11 @@ local util = require("tokyonight.util") +local config = require("tokyonight.config").options local M = {} ----@param config Config ---@return ColorScheme -function M.setup(config) - config = config or require("tokyonight.config") +function M.setup(opts) + opts = opts or {} -- Color Palette ---@class ColorScheme @@ -40,7 +40,7 @@ function M.setup(config) teal = "#1abc9c", red = "#f7768e", red1 = "#db4b4b", - git = { change = "#6183bb", add = "#449dab", delete = "#914c54", conflict = "#bb7a61" }, + git = { change = "#6183bb", add = "#449dab", delete = "#914c54" }, } if config.style == "night" or config.style == "day" or vim.o.background == "light" then colors.bg = "#1a1b26" @@ -86,8 +86,8 @@ function M.setup(config) util.color_overrides(colors, config) - if config.style == "day" or vim.o.background == "light" then - return util.invert_colors(colors) + if opts.transform and (config.style == "day" or vim.o.background == "light") then + util.invert_colors(colors) end return colors diff --git a/lua/tokyonight/config.lua b/lua/tokyonight/config.lua index cb262d7..83821e0 100644 --- a/lua/tokyonight/config.lua +++ b/lua/tokyonight/config.lua @@ -1,40 +1,37 @@ -- shim vim for kitty and other generators vim = vim or { g = {}, o = {} } -local function opt(key, default) - key = "tokyonight_" .. key - if vim.g[key] == nil then - return default - end - if vim.g[key] == 0 then - return false - end - return vim.g[key] -end +local M = {} ---@class Config -local config = { - style = opt("style", "storm"), - dayBrightness = opt("day_brightness", 0.3), - transparent = opt("transparent", false), - commentStyle = opt("italic_comments", true) and "italic" or "NONE", - keywordStyle = opt("italic_keywords", true) and "italic" or "NONE", - functionStyle = opt("italic_functions", false) and "italic" or "NONE", - variableStyle = opt("italic_variables", false) and "italic" or "NONE", - hideInactiveStatusline = opt("hide_inactive_statusline", false), - terminalColors = opt("terminal_colors", true), - sidebars = opt("sidebars", {}), - colors = opt("colors", {}), - dev = opt("dev", false), - darkFloat = opt("dark_float", true), - darkSidebar = opt("dark_sidebar", true), - transparentSidebar = opt("transparent_sidebar", false), - transform_colors = false, - lualineBold = opt("lualine_bold", false), +local defaults = { + style = "storm", + styles = { + comments = "italic", + functions = "NONE", + keywords = "italic", + variables = "NONE", + }, + colors = {}, + darkFloat = true, + darkSidebar = true, + dayBrightness = 0.3, + dev = false, + hideInactiveStatusline = false, + lualineBold = false, + sidebars = {}, + terminalColors = true, + transparent = false, + transparentSidebar = false, } -if config.style == "day" then - vim.o.background = "light" +---@type Config +M.options = {} + +function M.setup(options) + M.options = vim.tbl_deep_extend("force", {}, defaults, options or {}) end -return config +M.setup() + +return M diff --git a/lua/tokyonight/extra/init.lua b/lua/tokyonight/extra/init.lua index 7e33b3a..329b07a 100644 --- a/lua/tokyonight/extra/init.lua +++ b/lua/tokyonight/extra/init.lua @@ -1,6 +1,6 @@ package.path = "./lua/?/init.lua;./lua/?.lua" -local config = require("tokyonight.config") +local config = require("tokyonight.config").options local function write(str, fileName) print("[write] extra/" .. fileName) @@ -32,8 +32,7 @@ for extra, ext in pairs(extras) do local plugin = require("tokyonight.extra." .. extra) for style, style_name in pairs(styles) do config.style = style - config = config or require("tokyonight.config") - local colors = require("tokyonight.colors").setup(config) + local colors = require("tokyonight.colors").setup({ transform = true }) local fname = extra .. "_tokyonight_" .. style .. "." .. ext colors["_upstream_url"] = "https://github.com/folke/tokyonight.nvim/raw/main/extras/" .. fname colors["_style_name"] = "Tokyo Night" .. style_name diff --git a/lua/tokyonight/theme.lua b/lua/tokyonight/theme.lua index 29a7cd5..6c98fa7 100644 --- a/lua/tokyonight/theme.lua +++ b/lua/tokyonight/theme.lua @@ -1,22 +1,31 @@ local util = require("tokyonight.util") local colors = require("tokyonight.colors") +local config = require("tokyonight.config").options local M = {} +-- +---@class Highlight +---@field fg string|nil +---@field bg string|nil +---@field sp string|nil +---@field style string|nil + +---@alias Highlights table ----@param config Config ---@return Theme -function M.setup(config) - config = config or require("tokyonight.config") - +function M.setup() ---@class Theme - local theme = {} - theme.config = config - theme.colors = colors.setup(config) + ---@field base Highlights + ---@field plugins Highlights + local theme = { + config = config, + colors = colors.setup(), + } local c = theme.colors theme.base = { - Comment = { fg = c.comment, style = config.commentStyle }, -- any comment + 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') Cursor = { fg = c.bg, bg = c.fg }, -- character under the cursor @@ -90,15 +99,15 @@ function M.setup(config) -- Boolean = { }, -- a boolean constant: TRUE, false -- Float = { }, -- a floating point constant: 2.3e10 - Identifier = { fg = c.magenta, style = config.variableStyle }, -- (preferred) any variable name - Function = { fg = c.blue, style = config.functionStyle }, -- function name (also: methods for classes) + Identifier = { fg = c.magenta, style = config.styles.variables }, -- (preferred) any variable name + Function = { fg = c.blue, style = config.styles.functions }, -- function name (also: methods for classes) Statement = { fg = c.magenta }, -- (preferred) any statement -- Conditional = { }, -- if, then, else, endif, switch, etc. -- Repeat = { }, -- for, do, while, etc. -- Label = { }, -- case, default, etc. Operator = { fg = c.blue5 }, -- "sizeof", "+", "*", etc. - Keyword = { fg = c.cyan, style = config.keywordStyle }, -- any other keyword + Keyword = { fg = c.cyan, style = config.styles.keywords }, -- any other keyword -- Exception = { }, -- try, catch, throw PreProc = { fg = c.cyan }, -- (preferred) generic Preprocessor @@ -227,8 +236,8 @@ function M.setup(config) -- TSFuncBuiltin = { }; -- For builtin functions: `table.insert` in Lua. -- TSFuncMacro = { }; -- For macro defined fuctions (calls and definitions): each `macro_rules` in Rust. -- TSInclude = { }; -- For includes: `#include` in C, `use` or `extern crate` in Rust, or `require` in Lua. - TSKeyword = { fg = c.purple, style = config.keywordStyle }, -- For keywords that don't fall in previous categories. - TSKeywordFunction = { fg = c.magenta, style = config.functionStyle }, -- For keywords used to define a fuction. + TSKeyword = { fg = c.purple, style = config.styles.keywords }, -- For keywords that don't fall in previous categories. + TSKeywordFunction = { fg = c.magenta, style = config.styles.functions }, -- For keywords used to define a fuction. TSLabel = { fg = c.blue }, -- For labels: `label:` in C and `:label:` in Lua. -- TSMethod = { }; -- For method calls and definitions. -- TSNamespace = { }; -- For identifiers referring to modules and namespaces. @@ -248,7 +257,7 @@ function M.setup(config) -- TSSymbol = { }; -- For identifiers referring to symbols or atoms. -- TSType = { }; -- For types. -- TSTypeBuiltin = { }; -- For builtin types. - TSVariable = { style = config.variableStyle }, -- Any variable name that does not have another highlight. + TSVariable = { style = config.styles.variables }, -- Any variable name that does not have another highlight. TSVariableBuiltin = { fg = c.red }, -- Variable names that are defined by the languages, like `this` or `self`. -- TSTag = { }; -- Tags like html tag names. @@ -510,7 +519,7 @@ function M.setup(config) MiniStarterCurrent = { style = "nocombine" }, MiniStarterFooter = { fg = c.yellow, style = "italic" }, MiniStarterHeader = { fg = c.blue }, - MiniStarterInactive = { fg = c.comment, style = config.commentStyle }, + MiniStarterInactive = { fg = c.comment, style = config.styles.comments }, MiniStarterItem = { fg = c.fg, bg = config.transparent and c.none or c.bg }, MiniStarterItemBullet = { fg = c.border_highlight }, MiniStarterItemPrefix = { fg = c.warning }, @@ -563,6 +572,12 @@ function M.setup(config) theme.plugins.MiniStatuslineInactive = inactive end + 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) + end + return theme end diff --git a/lua/tokyonight/util.lua b/lua/tokyonight/util.lua index 7f8fe60..929ce2c 100644 --- a/lua/tokyonight/util.lua +++ b/lua/tokyonight/util.lua @@ -184,17 +184,29 @@ function util.terminal(colors) end ---@param colors ColorScheme ----@return ColorScheme function util.invert_colors(colors) if type(colors) == "string" then ---@diagnostic disable-next-line: return-type-mismatch return util.invert_color(colors) end - local ret = {} for key, value in pairs(colors) do - ret[key] = util.invert_colors(value) + colors[key] = util.invert_colors(value) + end +end + +---@param hls Highlights +function util.invert_highlights(hls) + for _, hl in pairs(hls) do + if hl.fg then + hl.fg = util.invert_color(hl.fg) + end + if hl.bg then + hl.bg = util.invert_color(hl.bg) + end + if hl.sp then + hl.sp = util.invert_color(hl.sp) + end end - return ret end ---@param theme Theme @@ -203,9 +215,6 @@ function util.load(theme) if vim.g.colors_name then vim.cmd("hi clear") end - -- if vim.fn.exists("syntax_on") then - -- vim.cmd("syntax reset") - -- end vim.o.termguicolors = true vim.g.colors_name = "tokyonight"