Skip to content

Commit 9e811b6

Browse files
committed
fix(Color): don't use shared global state
1 parent 770d1b5 commit 9e811b6

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

lua/github-theme/init.lua

+22-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function M.compile(force)
5252

5353
local hash = require('github-theme.lib.hash')(dummy) .. (git == -1 and git_path or git)
5454

55+
-- Compile
5556
if force ~= false or cached ~= hash then
5657
require('github-theme.lib.log').clear()
5758
local compiler = require('github-theme.lib.compiler')
@@ -84,15 +85,15 @@ function M.load(opts)
8485
end
8586

8687
local _, compiled_file = config.get_compiled_info(opts)
87-
local f = loadfile(compiled_file)
88+
local compiled_theme = loadfile(compiled_file)
8889

89-
if not did_setup or override.changed_since_last_compile or not f then
90+
if not did_setup or override.changed_since_last_compile or not compiled_theme then
9091
M.setup()
91-
f = loadfile(compiled_file)
92+
compiled_theme = loadfile(compiled_file)
9293
end
9394

9495
---@diagnostic disable-next-line: need-check-nil
95-
f()
96+
compiled_theme()
9697
require('github-theme.autocmds').set_autocmds()
9798
end
9899

@@ -115,8 +116,23 @@ function M.setup(opts)
115116
end
116117
end
117118

118-
M.compile(false)
119-
require('github-theme.util.deprecation').check_deprecation(opts)
119+
M.compile(not not vim.g.github_theme_force_compile)
120+
121+
-- Use our 1 time to check for deprecations the first time `setup()` is called with
122+
-- opts, instead of the first time `setup()` is called at all.
123+
if next(opts) ~= nil then
124+
-- TODO: might be better to call this and emit notices whenever config changes and on
125+
-- 1st load/setup(), while filtering deprecation messages at the msg level instead of
126+
-- globally.
127+
require('github-theme.util.deprecation').check_deprecation(opts)
128+
end
129+
end
130+
131+
-- Mainly for debugging, testing, development, etc.
132+
for _, env in ipairs({ 'GITHUB_THEME_DEBUG', 'GITHUB_THEME_FORCE_COMPILE' }) do
133+
if vim.env[env] then
134+
vim.g[env:lower()] = true
135+
end
120136
end
121137

122138
return M

lua/github-theme/lib/compiler.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ vim.g.colors_name = "%s"
8787

8888
file = io.open(output_file, 'wb')
8989

90-
local f = loadstring(table.concat(lines, '\n'), '=')
91-
if not f then
90+
local dump_theme = loadstring(table.concat(lines, '\n'), 'dump_theme')
91+
if not dump_theme then
9292
local tmpfile = util.join_paths(util.get_tmp_dir(), 'github_theme_error.lua')
9393
require('github-theme.lib.log').error(
9494
fmt(
@@ -109,7 +109,7 @@ Bellow is the error message:
109109
dofile(tmpfile)
110110
end
111111

112-
file:write(f())
112+
file:write(dump_theme())
113113
file:close()
114114
end
115115

test/github-theme/color_spec.lua

+45
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,49 @@ describe('Color', function()
223223
assert.are.same('#3a677d', c:rotate(-15):to_css())
224224
end)
225225
end)
226+
227+
describe('Color global state `Color.{WHITE,BLACK,BG}`', function()
228+
local theme = 'github_dark_dimmed'
229+
before_each(function()
230+
require('github-theme.util.reload')(true)
231+
vim.g.github_theme_force_compile = true
232+
end)
233+
234+
-- See #362
235+
it('should not disrupt palette/spec', function()
236+
local groups_expected = require('github-theme.group').load(theme)
237+
require('github-theme.util.reload')(true)
238+
239+
-- User require()'s the palette because they want to use it prior to loading the
240+
-- colorscheme (e.g. so they can use its values in `setup()` or elsewhere in their
241+
-- vimrc).
242+
require('github-theme.palette').load(theme)
243+
244+
-- Colorscheme is loaded and compiled...
245+
vim.cmd.colorscheme({ args = { theme } })
246+
assert.are_the_same(groups_expected, require('github-theme.group').load(theme))
247+
end)
248+
249+
-- See #362
250+
it('should not be used internally/by us', function()
251+
local C = require('github-theme.color')
252+
253+
-- Prerequisite for the test
254+
assert.is_not_nil(C.BG or C.WHITE or C.BLACK)
255+
256+
local mt = getmetatable(C) or {}
257+
mt.__index = mt.__index or {}
258+
setmetatable(C, mt)
259+
260+
for _, k in ipairs({ 'BG', 'WHITE', 'BLACK' }) do
261+
if C[k] ~= nil then
262+
mt.__index[k] = function()
263+
error(debug.traceback('Color.' .. k .. ' was accessed internally'))
264+
end
265+
end
266+
end
267+
268+
vim.cmd.colorscheme({ args = { theme } })
269+
end)
270+
end)
226271
end)

0 commit comments

Comments
 (0)