From 9e811b6303703919c89180655aecc0271810855b Mon Sep 17 00:00:00 2001 From: Tyler Miller Date: Wed, 7 Aug 2024 20:30:42 -0700 Subject: [PATCH] fix(Color): don't use shared global state --- lua/github-theme/init.lua | 28 ++++++++++++++----- lua/github-theme/lib/compiler.lua | 6 ++--- test/github-theme/color_spec.lua | 45 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/lua/github-theme/init.lua b/lua/github-theme/init.lua index 143d93f..59cd4d0 100644 --- a/lua/github-theme/init.lua +++ b/lua/github-theme/init.lua @@ -52,6 +52,7 @@ function M.compile(force) local hash = require('github-theme.lib.hash')(dummy) .. (git == -1 and git_path or git) + -- Compile if force ~= false or cached ~= hash then require('github-theme.lib.log').clear() local compiler = require('github-theme.lib.compiler') @@ -84,15 +85,15 @@ function M.load(opts) end local _, compiled_file = config.get_compiled_info(opts) - local f = loadfile(compiled_file) + local compiled_theme = loadfile(compiled_file) - if not did_setup or override.changed_since_last_compile or not f then + if not did_setup or override.changed_since_last_compile or not compiled_theme then M.setup() - f = loadfile(compiled_file) + compiled_theme = loadfile(compiled_file) end ---@diagnostic disable-next-line: need-check-nil - f() + compiled_theme() require('github-theme.autocmds').set_autocmds() end @@ -115,8 +116,23 @@ function M.setup(opts) end end - M.compile(false) - require('github-theme.util.deprecation').check_deprecation(opts) + M.compile(not not vim.g.github_theme_force_compile) + + -- Use our 1 time to check for deprecations the first time `setup()` is called with + -- opts, instead of the first time `setup()` is called at all. + if next(opts) ~= nil then + -- TODO: might be better to call this and emit notices whenever config changes and on + -- 1st load/setup(), while filtering deprecation messages at the msg level instead of + -- globally. + require('github-theme.util.deprecation').check_deprecation(opts) + end +end + +-- Mainly for debugging, testing, development, etc. +for _, env in ipairs({ 'GITHUB_THEME_DEBUG', 'GITHUB_THEME_FORCE_COMPILE' }) do + if vim.env[env] then + vim.g[env:lower()] = true + end end return M diff --git a/lua/github-theme/lib/compiler.lua b/lua/github-theme/lib/compiler.lua index f0c8889..d8204fb 100644 --- a/lua/github-theme/lib/compiler.lua +++ b/lua/github-theme/lib/compiler.lua @@ -87,8 +87,8 @@ vim.g.colors_name = "%s" file = io.open(output_file, 'wb') - local f = loadstring(table.concat(lines, '\n'), '=') - if not f then + local dump_theme = loadstring(table.concat(lines, '\n'), 'dump_theme') + if not dump_theme then local tmpfile = util.join_paths(util.get_tmp_dir(), 'github_theme_error.lua') require('github-theme.lib.log').error( fmt( @@ -109,7 +109,7 @@ Bellow is the error message: dofile(tmpfile) end - file:write(f()) + file:write(dump_theme()) file:close() end diff --git a/test/github-theme/color_spec.lua b/test/github-theme/color_spec.lua index 5878220..a6daafd 100644 --- a/test/github-theme/color_spec.lua +++ b/test/github-theme/color_spec.lua @@ -223,4 +223,49 @@ describe('Color', function() assert.are.same('#3a677d', c:rotate(-15):to_css()) end) end) + + describe('Color global state `Color.{WHITE,BLACK,BG}`', function() + local theme = 'github_dark_dimmed' + before_each(function() + require('github-theme.util.reload')(true) + vim.g.github_theme_force_compile = true + end) + + -- See #362 + it('should not disrupt palette/spec', function() + local groups_expected = require('github-theme.group').load(theme) + require('github-theme.util.reload')(true) + + -- User require()'s the palette because they want to use it prior to loading the + -- colorscheme (e.g. so they can use its values in `setup()` or elsewhere in their + -- vimrc). + require('github-theme.palette').load(theme) + + -- Colorscheme is loaded and compiled... + vim.cmd.colorscheme({ args = { theme } }) + assert.are_the_same(groups_expected, require('github-theme.group').load(theme)) + end) + + -- See #362 + it('should not be used internally/by us', function() + local C = require('github-theme.color') + + -- Prerequisite for the test + assert.is_not_nil(C.BG or C.WHITE or C.BLACK) + + local mt = getmetatable(C) or {} + mt.__index = mt.__index or {} + setmetatable(C, mt) + + for _, k in ipairs({ 'BG', 'WHITE', 'BLACK' }) do + if C[k] ~= nil then + mt.__index[k] = function() + error(debug.traceback('Color.' .. k .. ' was accessed internally')) + end + end + end + + vim.cmd.colorscheme({ args = { theme } }) + end) + end) end)