From 7b15ed796b3eb98deaab8d45dbfc17070825ba51 Mon Sep 17 00:00:00 2001 From: Webhooked <9132742+webhooked@users.noreply.github.com> Date: Thu, 31 Jul 2025 08:56:33 +0200 Subject: [PATCH] fix: resolve lazy.nvim loading issue and theme priority --- lua/kanso/colors.lua | 12 ++++++++++-- lua/kanso/init.lua | 26 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lua/kanso/colors.lua b/lua/kanso/colors.lua index 95def02..b70e8b3 100644 --- a/lua/kanso/colors.lua +++ b/lua/kanso/colors.lua @@ -169,8 +169,16 @@ local M = {} ---@return { theme: ThemeColors, palette: PaletteColors} function M.setup(opts) opts = opts or {} - local override_colors = opts.colors or require("kanso").config.colors - local theme = opts.theme or require("kanso")._CURRENT_THEME -- WARN: this fails if called before kanso.load() + local kanso = require("kanso") + local override_colors = opts.colors or kanso.config.colors + local theme = opts.theme or kanso._CURRENT_THEME + + -- Fallback: if _CURRENT_THEME is not set yet, try to determine theme from config + if not theme then + theme = kanso.config.background[vim.o.background] or kanso.config.theme or "ink" + -- Store it for future use + kanso._CURRENT_THEME = theme + end if not theme then error( diff --git a/lua/kanso/init.lua b/lua/kanso/init.lua index c08c8a7..36f1cee 100644 --- a/lua/kanso/init.lua +++ b/lua/kanso/init.lua @@ -32,6 +32,9 @@ M.config = { compile = false, } +-- Store default config for comparison +M._DEFAULT_CONFIG = vim.deepcopy(M.config) + local function check_config(_) local err return not err @@ -57,9 +60,28 @@ function M.load(theme) M._EXPLICIT_THEME = theme end - -- Use explicit theme if set, otherwise fall back to background-based selection - theme = M._EXPLICIT_THEME or M.config.background[vim.o.background] or M.config.theme + -- Priority order for theme selection: + -- 1. Explicitly loaded theme variant (e.g., :colorscheme kanso-zen) + -- 2. Theme specified in config if different from default + -- 3. Background-based selection if available + -- 4. Fallback to config theme + if M._EXPLICIT_THEME then + theme = M._EXPLICIT_THEME + elseif M.config.theme ~= M._DEFAULT_CONFIG.theme then + -- User explicitly changed theme in config + theme = M.config.theme + elseif M.config.background and M.config.background[vim.o.background] then + -- Use background-based selection + theme = M.config.background[vim.o.background] + else + -- Use config theme (whether default or user-specified) + theme = M.config.theme + end M._CURRENT_THEME = theme + + -- Ensure the theme is available to colors module before any require() calls + -- This prevents circular dependency issues when loading within lazy.nvim + package.loaded["kanso"] = M if vim.g.colors_name then vim.cmd("hi clear")