Skip to content

Commit

Permalink
fix(Color): don't use shared global state
Browse files Browse the repository at this point in the history
  • Loading branch information
tmillr committed Aug 8, 2024
1 parent 770d1b5 commit 9e811b6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
28 changes: 22 additions & 6 deletions lua/github-theme/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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

Expand All @@ -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
6 changes: 3 additions & 3 deletions lua/github-theme/lib/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -109,7 +109,7 @@ Bellow is the error message:
dofile(tmpfile)
end

file:write(f())
file:write(dump_theme())
file:close()
end

Expand Down
45 changes: 45 additions & 0 deletions test/github-theme/color_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9e811b6

Please sign in to comment.