Skip to content

Commit 65f2d1e

Browse files
committed
feat: option to apply custom theme values (#65)
1 parent 7cec0d3 commit 65f2d1e

File tree

6 files changed

+72
-24
lines changed

6 files changed

+72
-24
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
157157
focus_result = "L", ---@type string
158158
},
159159

160+
---@type lc.highlights
161+
theme = {},
162+
160163
---@type boolean
161164
image_support = false,
162165
}
@@ -297,6 +300,24 @@ hooks = {
297300
},
298301
```
299302

303+
### theme
304+
305+
Override the [default theme](./lua/leetcode/theme/default.lua).
306+
307+
Each value is the same type as val parameter in `:help nvim_set_hl`
308+
309+
```lua
310+
---@type lc.highlights
311+
theme = {
312+
["alt"] = {
313+
bg = "#FFFFFF",
314+
},
315+
["normal"] = {
316+
fg = "#EA4AAA",
317+
},
318+
},
319+
```
320+
300321
### image support
301322

302323
Whether to render question description images using [image.nvim]

README.zh.md

+21
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1
159159
focus_result = "L", ---@type string
160160
},
161161

162+
---@type lc.highlights
163+
theme = {},
164+
162165
---@type boolean
163166
image_support = false,
164167
}
@@ -296,6 +299,24 @@ hooks = {
296299
},
297300
```
298301

302+
### theme
303+
304+
覆盖[默认主题](./lua/leetcode/theme/default.lua)
305+
306+
每个值都与 `:help nvim_set_hl` 中的val参数相同类型
307+
308+
```lua
309+
---@type lc.highlights
310+
theme = {
311+
["alt"] = {
312+
bg = "#FFFFFF",
313+
},
314+
["normal"] = {
315+
fg = "#EA4AAA",
316+
},
317+
},
318+
```
319+
299320
### image support
300321

301322
是否使用 [image.nvim] 渲染问题描述中的图片

lua/leetcode/config/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local config = {
2020
lang = "cpp",
2121
version = "1.0.1",
2222
storage = {}, ---@type table<string, Path>
23+
theme = {}, ---@type lc.highlights
2324

2425
translator = false,
2526

lua/leetcode/config/template.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ local M = {
122122
focus_result = "L", ---@type string
123123
},
124124

125+
---@type lc.highlights
126+
theme = {},
127+
125128
---@type boolean
126-
image_support = false, -- setting this to `true` will disable question description wrap
129+
image_support = false,
127130
}
128131

129132
return M

lua/leetcode/theme/default.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ local function hl(name)
2222
return highlight
2323
end
2424

25-
---@alias lc.highlights table<string, table>
25+
---@alias lc.highlights table<string, vim.api.keyset.highlight>
26+
2627
---@return lc.highlights
2728
M.get = function()
2829
return {

lua/leetcode/theme/init.lua

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
local devicons_ok, devicons = pcall(require, "nvim-web-devicons")
22
local log = require("leetcode.logger")
33
local config = require("leetcode.config")
4+
local default = require("leetcode.theme.default")
45

56
---@class lc.Theme
6-
local theme = {}
7+
local Theme = {}
78

89
---@type table<string, string[]>
910
local dynamic_hls = {}
1011

12+
-- by default tags use `normal` theme
1113
local highlights = {
1214
strong = "bold",
1315
b = "bold",
@@ -40,7 +42,7 @@ local highlights = {
4042
-- div = "",
4143
}
4244

43-
function theme.load_devicons()
45+
function Theme.load_devicons()
4446
---@param l lc.language
4547
vim.tbl_map(function(l)
4648
local icon, color = devicons.get_icon_color(l.ft)
@@ -56,16 +58,16 @@ function theme.load_devicons()
5658
end, config.langs)
5759
end
5860

59-
function theme.load()
60-
local defaults = require("leetcode.theme.default").get()
61+
function Theme.load()
62+
config.theme = vim.tbl_extend("force", default.get(), config.user.theme)
6163

62-
for key, t in pairs(defaults) do
64+
for key, t in pairs(config.theme) do
6365
key = "leetcode_" .. key
6466
vim.api.nvim_set_hl(0, key, t)
6567
end
6668

6769
if devicons_ok then
68-
theme.load_devicons()
70+
Theme.load_devicons()
6971
end
7072

7173
---@param lang lc.language
@@ -77,17 +79,17 @@ function theme.load()
7779
return lang
7880
end, config.langs)
7981

80-
theme.load_dynamic(defaults)
82+
Theme.load_dynamic()
8183
end
8284

83-
function theme.load_dynamic(defaults)
85+
function Theme.load_dynamic()
8486
for name, tags in pairs(dynamic_hls) do
85-
theme.create_dynamic(name, tags, defaults)
87+
Theme.create_dynamic(name, tags)
8688
end
8789
end
8890

8991
---@param tags string[]
90-
function theme.get_dynamic(tags)
92+
function Theme.get_dynamic(tags)
9193
if vim.tbl_isempty(tags) then
9294
return "leetcode_normal"
9395
end
@@ -97,26 +99,25 @@ function theme.get_dynamic(tags)
9799
return name
98100
end
99101

100-
return theme.create_dynamic(name, tags)
102+
return Theme.create_dynamic(name, tags)
101103
end
102104

103105
---@param name string
104106
---@param tags string[]
105-
---@param defaults? table
106-
function theme.create_dynamic(name, tags, defaults)
107-
defaults = defaults or require("leetcode.theme.default").get()
107+
function Theme.create_dynamic(name, tags)
108+
local theme = config.theme
108109

109-
local tbl = defaults["normal"]
110+
local tbl = theme["normal"]
110111
for _, tag in ipairs(tags) do
111112
local hl = highlights[tag]
112113
if hl then
113-
tbl = vim.tbl_extend("force", tbl, defaults[hl])
114+
tbl = vim.tbl_extend("force", tbl, theme[hl])
114115
end
115116
end
116117

117118
if tbl.italic or tbl.bold then
118-
if tbl.fg == defaults["normal"].fg then
119-
tbl.fg = defaults[""].fg
119+
if tbl.fg == theme["normal"].fg then
120+
tbl.fg = theme[""].fg
120121
end
121122
end
122123

@@ -128,14 +129,14 @@ function theme.create_dynamic(name, tags, defaults)
128129
end
129130
end
130131

131-
function theme.setup()
132+
function Theme.setup()
132133
vim.api.nvim_create_autocmd("ColorScheme", {
133134
group = vim.api.nvim_create_augroup("lc.colorscheme_sync", {}),
134135
desc = "Colorscheme Synchronizer",
135-
callback = theme.load,
136+
callback = Theme.load,
136137
})
137138

138-
theme.load()
139+
Theme.load()
139140
end
140141

141-
return theme
142+
return Theme

0 commit comments

Comments
 (0)