Skip to content

Commit

Permalink
chore(config): refactored config module
Browse files Browse the repository at this point in the history
The config module has been rewritten and a new library called `collect`
has been added. This library provides utilities for setting options and
overriding options in the config module.

This also addresses #238 if it's caused by the one-level override of
user config.
  • Loading branch information
ful1e5 committed Apr 7, 2023
1 parent 88d4373 commit 0e4887c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 21 deletions.
43 changes: 28 additions & 15 deletions lua/github-theme/config.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
local M = {}
local types = require('github-theme.types')
local collect = require('github-theme.lib.collect')

M.default = {
colors = {},
comment_style = types.gt.HighlightStyle.Italic,
dark_float = false,
dark_sidebar = true,
dev = false,
function_style = types.gt.HighlightStyle.None,
local M = { has_options = true }

local defaults = {
theme_style = 'dark',
hide_end_of_buffer = true,
hide_inactive_statusline = true,
keyword_style = types.gt.HighlightStyle.Italic,
msg_area_style = types.gt.HighlightStyle.None,
comment_style = 'italic',
function_style = 'NONE',
keyword_style = 'Italic',
msg_area_style = 'NONE',
variable_style = 'NONE',
transparent = false,
dark_float = false,
dark_sidebar = true,
sidebars = {},
colors = {},
overrides = function()
return {}
end,
sidebars = {},
theme_style = types.gt.ThemeStyle.Dark,
transparent = false,
variable_style = types.gt.HighlightStyle.None,
dev = false,
}

M.options = collect.deep_copy(defaults)

function M.set_options(opts)
opts = opts or {}
M.options = collect.deep_extend(M.options, opts)
M.has_options = true
end

function M.reset()
M.options = collect.deep_copy(defaults)
end

return M
15 changes: 9 additions & 6 deletions lua/github-theme/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ local theme = require('github-theme.theme')
local util = require('github-theme.util')
local dep = require('github-theme.util.deprecation')

M.setup = function(user_config)
local c = config.default
if user_config then
c = vim.tbl_deep_extend('force', config.default, user_config or {})
local did_setup = false

M.setup = function(opts)
did_setup = true

if opts then
config.set_options(opts)
end

dep.check_deprecation(c)
dep.check_deprecation(config.options)

-- Load colorscheme
util.load(theme.setup(c))
util.load(theme.setup(config.options))
end

return M
85 changes: 85 additions & 0 deletions lua/github-theme/lib/collect.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
local M = {}

--- Checks if a list-like (vector) table contains `value`.
---@param t table Table to check
---@param value any Value to compare
---@returns true if `t` contains `value`
function M.contains(t, value)
for _, v in ipairs(t) do
if v == value then
return true
end
end
return false
end

--- Merges recursively two or more map-like tables.
---@param ... any Two or more map-like tables.
---@return table Merged table
function M.deep_extend(...)
local lhs = {}
for _, rhs in ipairs({ ... }) do
for k, v in pairs(rhs) do
if type(lhs[k]) == 'table' and type(v) == 'table' then
lhs[k] = M.deep_extend(lhs[k], v)
else
lhs[k] = v
end
end
end

return lhs
end

---Apply a function to each value in a table.
---@param tbl table
---@param f function (value)
function M.map(tbl, f)
local t = {}
for k, v in pairs(tbl) do
t[k] = f(v)
end
return t
end

---Filter table based on function
---@param tbl table Table to be filtered
---@param func function Function to apply filter
---@return table
function M.filter(tbl, func)
local result = {}
for _, item in ipairs(tbl) do
if func(item) then
table.insert(result, item)
end
end
return M(result)
end

---Apply function to each element of table
---@param tbl Table A list of elements
---@param func function to be applied
function M.each(tbl, func)
for _, item in ipairs(tbl) do
func(item)
end
end

function M.deep_copy(obj, seen)
if type(obj) ~= 'table' then
return obj
end
if seen and seen[obj] then
return seen[obj]
end

local s = seen or {}
local res = {}
s[obj] = res
for k, v in pairs(obj) do
res[M.deep_copy(k, s)] = M.deep_copy(v, s)
end
return setmetatable(res, getmetatable(obj))
end

return M

0 comments on commit 0e4887c

Please sign in to comment.