Skip to content

Commit 5020d1a

Browse files
authored
refactor: simplify set themes function and types (#74)
1 parent bd19abd commit 5020d1a

File tree

6 files changed

+38
-117
lines changed

6 files changed

+38
-117
lines changed

lua/one_monokai/colors.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
---@class palette
1+
---@class colors
22
local colors = {}
33

4-
---@type palette
4+
---@type colors
55
local defaults = {
66
fg = "#abb2bf",
77
bg = "#282c34",

lua/one_monokai/config.lua

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
local config = {}
22

3-
---@alias colors table<string,string> #colors table
4-
---
5-
---@class config
6-
---@field transparent boolean #whether to enable transparent background
7-
---@field colors colors #custom colors
8-
---@field themes fun(colors:colors):table #custom highlight groups
9-
---@field italics boolean #whether to italicize some highlight groups
10-
config.default = {
3+
---@class options
4+
---@field transparent boolean #Whether to enable transparent background
5+
---@field colors colors #Custom colors
6+
---@field themes fun(colors:colors):groups #Custom highlight groups
7+
---@field italics boolean #Whether to italicize some highlight groups
8+
local defaults = {
119
transparent = false,
1210
colors = {},
1311
themes = function(_)
@@ -16,17 +14,16 @@ config.default = {
1614
italics = true,
1715
}
1816

19-
---@type config
20-
config.extended = vim.deepcopy(config.default)
17+
config.options = vim.deepcopy(defaults)
2118

22-
---Extend default with user config
23-
---@param user_config config
24-
function config:extend(user_config)
25-
self.extended = vim.tbl_deep_extend("force", self.extended, user_config or {})
19+
---Extend default with user's config
20+
---@param opts options
21+
function config:extend(opts)
22+
self.options = vim.tbl_deep_extend("force", self.options, opts or {})
2623
end
2724

2825
return setmetatable(config, {
2926
__index = function(_, key)
30-
return config.extended[key]
27+
return config.options[key]
3128
end,
3229
})

lua/one_monokai/init.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
local M = {}
22

3-
local set = vim.cmd
4-
local config = require "one_monokai.config"
5-
local themes = require "one_monokai.themes"
3+
function M.setup(opts)
4+
local set = vim.cmd
5+
local config = require "one_monokai.config"
6+
local themes = require "one_monokai.themes"
67

7-
function M.setup(user_config)
88
if vim.g.colors_name then
99
set.hi "clear"
1010
end
@@ -17,11 +17,10 @@ function M.setup(user_config)
1717
vim.o.termguicolors = true
1818
vim.g.colors_name = "one_monokai"
1919

20-
-- load user config and themes
21-
config:extend(user_config)
20+
config:extend(opts)
2221
themes.load()
2322

24-
if user_config then
23+
if opts then
2524
set.colorscheme "one_monokai"
2625
end
2726
end

lua/one_monokai/themes/highlight.lua

Lines changed: 0 additions & 43 deletions
This file was deleted.

lua/one_monokai/themes/init.lua

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
local themes = {}
22

3-
local config = require "one_monokai.config"
4-
local highlight = require "one_monokai.themes.highlight"
5-
local logs = require "one_monokai.logs"
3+
---Set highlight groups
4+
---@param groups groups
5+
local function set_highlight(groups)
6+
local logs = require "one_monokai.logs"
7+
local set_hl = vim.api.nvim_set_hl
8+
9+
for name, attrs in pairs(groups) do
10+
local status_ok, err = pcall(set_hl, 0, name, attrs)
11+
12+
if not status_ok then
13+
logs.error.notify(("themes(%s): %s"):format(name, err), 0)
14+
end
15+
end
16+
end
617

718
function themes.load()
19+
local config = require "one_monokai.config"
820
local colors = require "one_monokai.colors"
9-
local groups = require "one_monokai.themes.groups"
10-
11-
local default = highlight:new(groups)
12-
local user_themes = config.themes(colors)
21+
local default = require "one_monokai.themes.groups"
1322

14-
-- set default if user has no custom themes
15-
if vim.tbl_isempty(user_themes) then
16-
default:set()
17-
18-
return
19-
end
20-
21-
-- extend default with user config
22-
local extended = default:extend(user_themes)
23-
local set_theme_ok, err = pcall(function()
24-
extended:set()
25-
end)
26-
27-
if not set_theme_ok then
28-
default:set()
29-
30-
logs.error.notify(err)
31-
end
23+
set_highlight(default)
24+
set_highlight(config.themes(colors))
3225
end
3326

3427
return themes

tests/spec/highlight_spec.lua

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)