-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(config): refactored config module
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
Showing
3 changed files
with
122 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |