A Neovim plugin enabling you to solve LeetCode problems.
feat: option to apply custom theme values (#65)
6 files changed, +72 -24
+21-0README.md
| @@ -157,6 +157,9 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat | ||
| 157 | 157 | focus_result = "L", ---@type string |
| 158 | 158 | }, |
| 159 | 159 | |
| 160 | + ---@type lc.highlights | |
| 161 | + theme = {}, | |
| 162 | + | |
| 160 | 163 | ---@type boolean |
| 161 | 164 | image_support = false, |
| 162 | 165 | } |
| @@ -297,6 +300,24 @@ hooks = { | ||
| 297 | 300 | }, |
| 298 | 301 | ``` |
| 299 | 302 | |
| 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 | + | |
| 300 | 321 | ### image support |
| 301 | 322 | |
| 302 | 323 | Whether to render question description images using [image.nvim] |
+21-0README.zh.md
| @@ -159,6 +159,9 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1 | ||
| 159 | 159 | focus_result = "L", ---@type string |
| 160 | 160 | }, |
| 161 | 161 | |
| 162 | + ---@type lc.highlights | |
| 163 | + theme = {}, | |
| 164 | + | |
| 162 | 165 | ---@type boolean |
| 163 | 166 | image_support = false, |
| 164 | 167 | } |
| @@ -296,6 +299,24 @@ hooks = { | ||
| 296 | 299 | }, |
| 297 | 300 | ``` |
| 298 | 301 | |
| 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 | + | |
| 299 | 320 | ### image support |
| 300 | 321 | |
| 301 | 322 | 是否使用 [image.nvim] 渲染问题描述中的图片 |
+1-0lua/leetcode/config/init.lua
| @@ -20,6 +20,7 @@ local config = { | ||
| 20 | 20 | lang = "cpp", |
| 21 | 21 | version = "1.0.1", |
| 22 | 22 | storage = {}, ---@type table<string, Path> |
| 23 | + theme = {}, ---@type lc.highlights | |
| 23 | 24 | |
| 24 | 25 | translator = false, |
| 25 | 26 |
+4-1lua/leetcode/config/template.lua
| @@ -122,8 +122,11 @@ local M = { | ||
| 122 | 122 | focus_result = "L", ---@type string |
| 123 | 123 | }, |
| 124 | 124 | |
| 125 | + ---@type lc.highlights | |
| 126 | + theme = {}, | |
| 127 | + | |
| 125 | 128 | ---@type boolean |
| 126 | - image_support = false, -- setting this to `true` will disable question description wrap | |
| 129 | + image_support = false, | |
| 127 | 130 | } |
| 128 | 131 | |
| 129 | 132 | return M |
+2-1lua/leetcode/theme/default.lua
| @@ -22,7 +22,8 @@ local function hl(name) | ||
| 22 | 22 | return highlight |
| 23 | 23 | end |
| 24 | 24 | |
| 25 | ----@alias lc.highlights table<string, table> | |
| 25 | +---@alias lc.highlights table<string, vim.api.keyset.highlight> | |
| 26 | + | |
| 26 | 27 | ---@return lc.highlights |
| 27 | 28 | M.get = function() |
| 28 | 29 | return { |
+23-22lua/leetcode/theme/init.lua
| @@ -1,13 +1,15 @@ | ||
| 1 | 1 | local devicons_ok, devicons = pcall(require, "nvim-web-devicons") |
| 2 | 2 | local log = require("leetcode.logger") |
| 3 | 3 | local config = require("leetcode.config") |
| 4 | +local default = require("leetcode.theme.default") | |
| 4 | 5 | |
| 5 | 6 | ---@class lc.Theme |
| 6 | -local theme = {} | |
| 7 | +local Theme = {} | |
| 7 | 8 | |
| 8 | 9 | ---@type table<string, string[]> |
| 9 | 10 | local dynamic_hls = {} |
| 10 | 11 | |
| 12 | +-- by default tags use `normal` theme | |
| 11 | 13 | local highlights = { |
| 12 | 14 | strong = "bold", |
| 13 | 15 | b = "bold", |
| @@ -40,7 +42,7 @@ local highlights = { | ||
| 40 | 42 | -- div = "", |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | -function theme.load_devicons() | |
| 45 | +function Theme.load_devicons() | |
| 44 | 46 | ---@param l lc.language |
| 45 | 47 | vim.tbl_map(function(l) |
| 46 | 48 | local icon, color = devicons.get_icon_color(l.ft) |
| @@ -56,16 +58,16 @@ function theme.load_devicons() | ||
| 56 | 58 | end, config.langs) |
| 57 | 59 | end |
| 58 | 60 | |
| 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) | |
| 61 | 63 | |
| 62 | - for key, t in pairs(defaults) do | |
| 64 | + for key, t in pairs(config.theme) do | |
| 63 | 65 | key = "leetcode_" .. key |
| 64 | 66 | vim.api.nvim_set_hl(0, key, t) |
| 65 | 67 | end |
| 66 | 68 | |
| 67 | 69 | if devicons_ok then |
| 68 | - theme.load_devicons() | |
| 70 | + Theme.load_devicons() | |
| 69 | 71 | end |
| 70 | 72 | |
| 71 | 73 | ---@param lang lc.language |
| @@ -77,17 +79,17 @@ function theme.load() | ||
| 77 | 79 | return lang |
| 78 | 80 | end, config.langs) |
| 79 | 81 | |
| 80 | - theme.load_dynamic(defaults) | |
| 82 | + Theme.load_dynamic() | |
| 81 | 83 | end |
| 82 | 84 | |
| 83 | -function theme.load_dynamic(defaults) | |
| 85 | +function Theme.load_dynamic() | |
| 84 | 86 | for name, tags in pairs(dynamic_hls) do |
| 85 | - theme.create_dynamic(name, tags, defaults) | |
| 87 | + Theme.create_dynamic(name, tags) | |
| 86 | 88 | end |
| 87 | 89 | end |
| 88 | 90 | |
| 89 | 91 | ---@param tags string[] |
| 90 | -function theme.get_dynamic(tags) | |
| 92 | +function Theme.get_dynamic(tags) | |
| 91 | 93 | if vim.tbl_isempty(tags) then |
| 92 | 94 | return "leetcode_normal" |
| 93 | 95 | end |
| @@ -97,26 +99,25 @@ function theme.get_dynamic(tags) | ||
| 97 | 99 | return name |
| 98 | 100 | end |
| 99 | 101 | |
| 100 | - return theme.create_dynamic(name, tags) | |
| 102 | + return Theme.create_dynamic(name, tags) | |
| 101 | 103 | end |
| 102 | 104 | |
| 103 | 105 | ---@param name string |
| 104 | 106 | ---@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 | |
| 108 | 109 | |
| 109 | - local tbl = defaults["normal"] | |
| 110 | + local tbl = theme["normal"] | |
| 110 | 111 | for _, tag in ipairs(tags) do |
| 111 | 112 | local hl = highlights[tag] |
| 112 | 113 | if hl then |
| 113 | - tbl = vim.tbl_extend("force", tbl, defaults[hl]) | |
| 114 | + tbl = vim.tbl_extend("force", tbl, theme[hl]) | |
| 114 | 115 | end |
| 115 | 116 | end |
| 116 | 117 | |
| 117 | 118 | 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 | |
| 120 | 121 | end |
| 121 | 122 | end |
| 122 | 123 | |
| @@ -128,14 +129,14 @@ function theme.create_dynamic(name, tags, defaults) | ||
| 128 | 129 | end |
| 129 | 130 | end |
| 130 | 131 | |
| 131 | -function theme.setup() | |
| 132 | +function Theme.setup() | |
| 132 | 133 | vim.api.nvim_create_autocmd("ColorScheme", { |
| 133 | 134 | group = vim.api.nvim_create_augroup("lc.colorscheme_sync", {}), |
| 134 | 135 | desc = "Colorscheme Synchronizer", |
| 135 | - callback = theme.load, | |
| 136 | + callback = Theme.load, | |
| 136 | 137 | }) |
| 137 | 138 | |
| 138 | - theme.load() | |
| 139 | + Theme.load() | |
| 139 | 140 | end |
| 140 | 141 | |
| 141 | -return theme | |
| 142 | +return Theme |