irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(parser): dynamic highlighting

kawreauthored
parent 90ba521commit 37dc7af13661d015419e3b69fdf5f9c8c83d16abBrowse files

4 files changed, +50 -37

+2-1lua/leetcode/parser/html.lua
@@ -150,6 +150,7 @@ function Html:handle_link(text, tag_data)
150150 local href = vim.tbl_filter(function(attr)
151151 if attr.name == "href" then return attr end
152152 end, tag_data.attrs)[1] or {}
153+ log.debug(tag_data.attrs)
153154
154155 link = href.value or ""
155156 elseif tag == "img" then
@@ -222,7 +223,7 @@ function Html:node_hl(node, tags, tag_data)
222223 if tag == "a" or tag == "img" then
223224 nui_text = self:handle_link(text, tag_data)
224225 else
225- nui_text = NuiText(text, utils.hi(tags))
226+ nui_text = NuiText(text, utils.hl(tags))
226227 end
227228
228229 self.line:append(nui_text)
+36-32lua/leetcode/parser/utils.lua
@@ -1,5 +1,6 @@
11 local log = require("leetcode.logger")
22
3+---@class lc.Parser.Utils
34 local utils = {}
45
56 local entities = {
@@ -37,29 +38,27 @@ local entities = {
3738 }
3839
3940 local highlights = {
40- [""] = "leetcode_normal",
41- ["strong"] = "leetcode_bold",
42- ["b"] = "leetcode_bold",
43- ["em"] = "leetcode_italic",
44- ["i"] = "leetcode_italic",
45- ["code"] = "leetcode_code",
46- ["example"] = "leetcode_example",
47- ["constraints"] = "leetcode_constraints",
41+ strong = "bold",
42+ b = "bold",
43+ em = "italic",
44+ i = "italic",
45+ code = "code",
46+ example = "example",
47+ constraints = "constraints",
48+ a = "link",
49+ u = "underline",
4850
49- ["pre"] = "Inherit",
50- ["span"] = "Inherit",
51- ["p"] = "Inherit",
52- ["ul"] = "Inherit",
53- ["ol"] = "Inherit",
54- ["li"] = "Inherit",
55- ["font"] = "Inherit",
56- ["sup"] = "Inherit",
57- ["sub"] = "Inherit",
58- ["u"] = "Inherit",
59- ["small"] = "Inherit",
60- ["div"] = "Inherit",
61-
62- ["a"] = "Function",
51+ -- pre = "",
52+ -- span = "",
53+ -- p = "",
54+ -- ul = "",
55+ -- ol = "",
56+ -- li = "",
57+ -- font = "",
58+ -- sup = "",
59+ -- sub = "",
60+ -- small = "",
61+ -- div = "",
6362 }
6463
6564 ---@param entity string
@@ -76,21 +75,26 @@ end
7675
7776 ---@param tags string[]
7877 ---@return string
79-function utils.hi(tags)
80- local tag = tags[1] or ""
78+function utils.hl(tags)
79+ if vim.tbl_isempty(tags) then return "" end
8180
82- if tag == "a" then log.error("WTETWET") end
81+ local name = "leetcode_dyn_" .. table.concat(tags, "_")
82+ if _Lc_dyn_hl[name] then return name end
8383
84- -- if not tag then return highlights["p"] end
85- if highlights[tag] and highlights[tag] ~= "Inherit" then return highlights[tag] end
84+ local default = require("leetcode.theme.default").get()
8685
87- ---Inherit hi from parent tag
88- for _, t in ipairs(tags) do
89- if highlights[t] and highlights[t] ~= "Inherit" then return highlights[t] end
86+ local theme = default["normal"]
87+ for _, tag in ipairs(tags) do
88+ local hl = highlights[tag]
89+ if hl then theme = vim.tbl_deep_extend("force", theme, default[hl]) end
9090 end
9191
92- if not highlights[tag] then log.info("Unknown tag: " .. tag) end
93- return highlights[""]
92+ if pcall(vim.api.nvim_set_hl, 0, name, theme) then
93+ _Lc_dyn_hl[name] = theme
94+ return name
95+ else
96+ return "leetcode_normal"
97+ end
9498 end
9599
96100 return utils
+6-3lua/leetcode/theme/default.lua
@@ -22,6 +22,8 @@ end
2222 ---@return lc.highlights
2323 M.get = function()
2424 return {
25+ [""] = { fg = hl("Normal").fg },
26+
2527 easy = { fg = "#46c6c2" },
2628 medium = { fg = "#fac31d" },
2729 hard = { fg = "#f8615c" },
@@ -33,15 +35,16 @@ M.get = function()
3335
3436 alt = { fg = hl("Comment").fg },
3537 normal = { fg = hl("Delimiter").fg },
36- italic = { fg = hl("Delimiter").fg, italic = true },
37- bold = { bold = true },
38+ italic = { fg = hl("Normal").fg, italic = true },
39+ bold = { fg = hl("Normal").fg, bold = true },
40+ underline = { underline = true },
3841
3942 code = { fg = hl("Type").fg, bg = hl("Normal").bg },
4043 example = { fg = hl("DiagnosticHint").fg },
4144 constraints = { fg = hl("DiagnosticInfo").fg },
4245 indent = { fg = hl("Comment").fg },
4346 link = { fg = hl("Function").fg, underline = true },
44- list = { fg = hl("Statement").fg },
47+ list = { fg = hl("SpecialChar").fg },
4548 }
4649 end
4750
+6-1lua/leetcode/theme/init.lua
@@ -4,6 +4,9 @@ local config = require("leetcode.config")
44 ---@class lc.Theme
55 local theme = {}
66
7+---@type table<string, table>
8+_Lc_dyn_hl = {}
9+
710 function theme.load_devicons()
811 vim.tbl_map(function(l)
912 local icon, color = devicons.get_icon_color(l.ft)
@@ -16,6 +19,8 @@ function theme.load_devicons()
1619 end
1720
1821 function theme.load()
22+ _Lc_dyn_hl = {}
23+
1924 local default = require("leetcode.theme.default").get()
2025 for key, t in pairs(default) do
2126 key = "leetcode_" .. key
@@ -33,7 +38,7 @@ function theme.setup()
3338 vim.api.nvim_create_autocmd("ColorScheme", {
3439 group = vim.api.nvim_create_augroup("lc.colorscheme_sync", {}),
3540 desc = "Colorscheme Synchronizer",
36- callback = function() theme.load() end,
41+ callback = theme.load,
3742 })
3843
3944 theme.load()