Skip to content

Commit

Permalink
refactor(overrides): cleanup override.lua (#348)
Browse files Browse the repository at this point in the history
- Move the store into the module's metatable at `__index`.

- Setting a key to a falsy value resets the overrides under that key.

- Don't allow `has_override` to be modified externally.

- D.R.Y.
  • Loading branch information
tmillr authored Jul 13, 2024
1 parent e682243 commit c23f71b
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions lua/github-theme/override.lua
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
local collect = require('github-theme.lib.collect')
local M = {}

local store = {
palettes = {},
specs = {},
groups = {},
has_override = false,
}

local function reset()
store.palettes = {}
store.specs = {}
store.groups = {}
store.has_override = false
function M.reset()
getmetatable(M).__index =
{ palettes = {}, specs = {}, groups = {}, has_override = false }
return M
end

local function hash()
local hash = require('github-theme.lib.hash')(store)
return hash and hash or 0
function M.hash()
return require('github-theme.lib.hash')(getmetatable(M).__index) or 0
end

local function check_link(tbl)
for _, style in pairs(tbl) do
for _, opts in pairs(style) do
for _, theme in pairs(tbl) do
for _, opts in pairs(theme) do
opts.link = opts.link or ''
end
end
end

return setmetatable({ reset = reset, hash = hash }, {
__index = function(_, value)
if store[value] then
return store[value]
end
end,

__newindex = function(_, key, value)
if store[key] then
if key == 'groups' then
check_link(value or {})
setmetatable(M, {
__newindex = function(self, k, v)
local store = getmetatable(self).__index
if type(store[k]) == 'table' then
if not v then
store[k] = {}
return
end
if k == 'groups' then
check_link(v)
end
store[key] = collect.deep_extend(store[key], value or {})
store[k] = collect.deep_extend(store[k], v)
store.has_override = true
end
end,
})

return M.reset()

0 comments on commit c23f71b

Please sign in to comment.