From a7e293a0242c5b590265396068ba46894788577f Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Sat, 27 Jan 2024 13:34:32 -0800 Subject: [PATCH] feat: allow custom highlights and colors per theme #67 --- README.md | 23 +++++++++++++++++++++++ doc/onenord.nvim.txt | 22 ++++++++++++++++++++++ lua/onenord/colors/init.lua | 13 ++++++++++--- lua/onenord/util.lua | 24 +++++++++++++++++++++--- 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 110cae2..387766f 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,29 @@ require("onenord").setup({ }) ``` +If you use the `light` and `dark` keys, the override will be specific to those themes, otherwise they apply to both: + +```lua +local colors = require("onenord.colors").load() + +require("onenord").setup({ + custom_highlights = { + light = { + ["@constructor"] = { fg = colors.dark_blue }, -- only applies in light theme + }, + }, + custom_colors = { + blue = "#0000ff", -- applies in both themes + light = { + red = "#000000", -- only applies in light theme + }, + dark = { + red = "#ffffff", -- only applies in dark theme + }, + }, +}) +``` + You can also use the OneNord color palette for other plugins using `local colors = require("onenord.colors").load()`! ## Integrations diff --git a/doc/onenord.nvim.txt b/doc/onenord.nvim.txt index 6c62eef..2303a22 100644 --- a/doc/onenord.nvim.txt +++ b/doc/onenord.nvim.txt @@ -146,6 +146,28 @@ Here is an example of overwriting the default highlight groups and colors: }) < +If you use the `light` and `dark` keys, the override will be specific to those themes, otherwise they apply to both: +>lua + local colors = require("onenord.colors").load() + + require("onenord").setup({ + custom_highlights = { + light = { + ["@constructor"] = { fg = colors.dark_blue }, -- only applies in light theme + }, + }, + custom_colors = { + blue = "#0000ff", -- applies in both themes + light = { + red = "#000000", -- only applies in light theme + }, + dark = { + red = "#ffffff", -- only applies in dark theme + }, + }, + }) +< + You can also use the onenord color palette for other plugins using `local colors = require("onenord.colors").load()`! ================================================================================ diff --git a/lua/onenord/colors/init.lua b/lua/onenord/colors/init.lua index 881d186..017337e 100644 --- a/lua/onenord/colors/init.lua +++ b/lua/onenord/colors/init.lua @@ -1,7 +1,8 @@ local light_colors = require("onenord.colors.onenordlight") local dark_colors = require("onenord.colors.onenord") +local util = require("onenord.util") -local function load() +local function get_theme() local theme = require("onenord.config").options.theme -- if style is set, it takes priority @@ -10,8 +11,14 @@ local function load() theme = vim.o.background end + return theme +end + +local function load() + local theme = get_theme() + local base_colors = theme == "light" and light_colors or dark_colors - return vim.tbl_deep_extend("force", base_colors, require("onenord.config").options.custom_colors) + return util.deep_extend_by_theme(base_colors, require("onenord.config").options.custom_colors) end -return { load = load } +return { get_theme = get_theme, load = load } diff --git a/lua/onenord/util.lua b/lua/onenord/util.lua index 545a8ea..f0a7e56 100644 --- a/lua/onenord/util.lua +++ b/lua/onenord/util.lua @@ -17,6 +17,26 @@ function util.highlight(group, colors) end end +function util.deep_extend_by_theme(base, custom) + local color_theme = require("onenord.colors").get_theme() + + local dark_specific = custom["dark"] + local light_specific = custom["light"] + custom["light"] = nil + custom["dark"] = nil + + base = vim.tbl_deep_extend("force", base, custom) + if color_theme == "light" and light_specific then + base = vim.tbl_deep_extend("force", base, light_specific) + elseif color_theme == "dark" and dark_specific then + base = vim.tbl_deep_extend("force", base, dark_specific) + end + + custom["light"] = light_specific + custom["dark"] = dark_specific + return base +end + -- Load the theme function util.load(colors, exec_autocmd) local config = require("onenord.config").options @@ -34,10 +54,8 @@ function util.load(colors, exec_autocmd) vim.g.colors_name = "onenord" -- Load highlights - colors = vim.tbl_deep_extend("force", colors, config.custom_colors) local base_highlights = theme.highlights(colors, config) - - local highlights = vim.tbl_deep_extend("force", base_highlights, config.custom_highlights) + local highlights = util.deep_extend_by_theme(base_highlights, config.custom_highlights) for group, color in pairs(highlights) do util.highlight(group, color)