Skip to content

Commit

Permalink
feat: option to apply custom theme values (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Mar 3, 2024
1 parent 7cec0d3 commit 65f2d1e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat
focus_result = "L", ---@type string
},

---@type lc.highlights
theme = {},

---@type boolean
image_support = false,
}
Expand Down Expand Up @@ -297,6 +300,24 @@ hooks = {
},
```

### theme

Override the [default theme](./lua/leetcode/theme/default.lua).

Each value is the same type as val parameter in `:help nvim_set_hl`

```lua
---@type lc.highlights
theme = {
["alt"] = {
bg = "#FFFFFF",
},
["normal"] = {
fg = "#EA4AAA",
},
},
```

### image support

Whether to render question description images using [image.nvim]
Expand Down
21 changes: 21 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1
focus_result = "L", ---@type string
},

---@type lc.highlights
theme = {},

---@type boolean
image_support = false,
}
Expand Down Expand Up @@ -296,6 +299,24 @@ hooks = {
},
```

### theme

覆盖[默认主题](./lua/leetcode/theme/default.lua)

每个值都与 `:help nvim_set_hl` 中的val参数相同类型

```lua
---@type lc.highlights
theme = {
["alt"] = {
bg = "#FFFFFF",
},
["normal"] = {
fg = "#EA4AAA",
},
},
```

### image support

是否使用 [image.nvim] 渲染问题描述中的图片
Expand Down
1 change: 1 addition & 0 deletions lua/leetcode/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local config = {
lang = "cpp",
version = "1.0.1",
storage = {}, ---@type table<string, Path>
theme = {}, ---@type lc.highlights

translator = false,

Expand Down
5 changes: 4 additions & 1 deletion lua/leetcode/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ local M = {
focus_result = "L", ---@type string
},

---@type lc.highlights
theme = {},

---@type boolean
image_support = false, -- setting this to `true` will disable question description wrap
image_support = false,
}

return M
3 changes: 2 additions & 1 deletion lua/leetcode/theme/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ local function hl(name)
return highlight
end

---@alias lc.highlights table<string, table>
---@alias lc.highlights table<string, vim.api.keyset.highlight>

---@return lc.highlights
M.get = function()
return {
Expand Down
45 changes: 23 additions & 22 deletions lua/leetcode/theme/init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local devicons_ok, devicons = pcall(require, "nvim-web-devicons")
local log = require("leetcode.logger")
local config = require("leetcode.config")
local default = require("leetcode.theme.default")

---@class lc.Theme
local theme = {}
local Theme = {}

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

-- by default tags use `normal` theme
local highlights = {
strong = "bold",
b = "bold",
Expand Down Expand Up @@ -40,7 +42,7 @@ local highlights = {
-- div = "",
}

function theme.load_devicons()
function Theme.load_devicons()
---@param l lc.language
vim.tbl_map(function(l)
local icon, color = devicons.get_icon_color(l.ft)
Expand All @@ -56,16 +58,16 @@ function theme.load_devicons()
end, config.langs)
end

function theme.load()
local defaults = require("leetcode.theme.default").get()
function Theme.load()
config.theme = vim.tbl_extend("force", default.get(), config.user.theme)

for key, t in pairs(defaults) do
for key, t in pairs(config.theme) do
key = "leetcode_" .. key
vim.api.nvim_set_hl(0, key, t)
end

if devicons_ok then
theme.load_devicons()
Theme.load_devicons()
end

---@param lang lc.language
Expand All @@ -77,17 +79,17 @@ function theme.load()
return lang
end, config.langs)

theme.load_dynamic(defaults)
Theme.load_dynamic()
end

function theme.load_dynamic(defaults)
function Theme.load_dynamic()
for name, tags in pairs(dynamic_hls) do
theme.create_dynamic(name, tags, defaults)
Theme.create_dynamic(name, tags)
end
end

---@param tags string[]
function theme.get_dynamic(tags)
function Theme.get_dynamic(tags)
if vim.tbl_isempty(tags) then
return "leetcode_normal"
end
Expand All @@ -97,26 +99,25 @@ function theme.get_dynamic(tags)
return name
end

return theme.create_dynamic(name, tags)
return Theme.create_dynamic(name, tags)
end

---@param name string
---@param tags string[]
---@param defaults? table
function theme.create_dynamic(name, tags, defaults)
defaults = defaults or require("leetcode.theme.default").get()
function Theme.create_dynamic(name, tags)
local theme = config.theme

local tbl = defaults["normal"]
local tbl = theme["normal"]
for _, tag in ipairs(tags) do
local hl = highlights[tag]
if hl then
tbl = vim.tbl_extend("force", tbl, defaults[hl])
tbl = vim.tbl_extend("force", tbl, theme[hl])
end
end

if tbl.italic or tbl.bold then
if tbl.fg == defaults["normal"].fg then
tbl.fg = defaults[""].fg
if tbl.fg == theme["normal"].fg then
tbl.fg = theme[""].fg
end
end

Expand All @@ -128,14 +129,14 @@ function theme.create_dynamic(name, tags, defaults)
end
end

function theme.setup()
function Theme.setup()
vim.api.nvim_create_autocmd("ColorScheme", {
group = vim.api.nvim_create_augroup("lc.colorscheme_sync", {}),
desc = "Colorscheme Synchronizer",
callback = theme.load,
callback = Theme.load,
})

theme.load()
Theme.load()
end

return theme
return Theme

0 comments on commit 65f2d1e

Please sign in to comment.