A Neovim plugin enabling you to solve LeetCode problems.
feat(parser): dynamic highlighting
4 files changed, +50 -37
+2-1lua/leetcode/parser/html.lua
| @@ -150,6 +150,7 @@ function Html:handle_link(text, tag_data) | ||
| 150 | 150 | local href = vim.tbl_filter(function(attr) |
| 151 | 151 | if attr.name == "href" then return attr end |
| 152 | 152 | end, tag_data.attrs)[1] or {} |
| 153 | + log.debug(tag_data.attrs) | |
| 153 | 154 | |
| 154 | 155 | link = href.value or "" |
| 155 | 156 | elseif tag == "img" then |
| @@ -222,7 +223,7 @@ function Html:node_hl(node, tags, tag_data) | ||
| 222 | 223 | if tag == "a" or tag == "img" then |
| 223 | 224 | nui_text = self:handle_link(text, tag_data) |
| 224 | 225 | else |
| 225 | - nui_text = NuiText(text, utils.hi(tags)) | |
| 226 | + nui_text = NuiText(text, utils.hl(tags)) | |
| 226 | 227 | end |
| 227 | 228 | |
| 228 | 229 | self.line:append(nui_text) |
+36-32lua/leetcode/parser/utils.lua
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | local log = require("leetcode.logger") |
| 2 | 2 | |
| 3 | +---@class lc.Parser.Utils | |
| 3 | 4 | local utils = {} |
| 4 | 5 | |
| 5 | 6 | local entities = { |
| @@ -37,29 +38,27 @@ local entities = { | ||
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | 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", | |
| 48 | 50 | |
| 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 = "", | |
| 63 | 62 | } |
| 64 | 63 | |
| 65 | 64 | ---@param entity string |
| @@ -76,21 +75,26 @@ end | ||
| 76 | 75 | |
| 77 | 76 | ---@param tags string[] |
| 78 | 77 | ---@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 | |
| 81 | 80 | |
| 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 | |
| 83 | 83 | |
| 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() | |
| 86 | 85 | |
| 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 | |
| 90 | 90 | end |
| 91 | 91 | |
| 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 | |
| 94 | 98 | end |
| 95 | 99 | |
| 96 | 100 | return utils |
+6-3lua/leetcode/theme/default.lua
| @@ -22,6 +22,8 @@ end | ||
| 22 | 22 | ---@return lc.highlights |
| 23 | 23 | M.get = function() |
| 24 | 24 | return { |
| 25 | + [""] = { fg = hl("Normal").fg }, | |
| 26 | + | |
| 25 | 27 | easy = { fg = "#46c6c2" }, |
| 26 | 28 | medium = { fg = "#fac31d" }, |
| 27 | 29 | hard = { fg = "#f8615c" }, |
| @@ -33,15 +35,16 @@ M.get = function() | ||
| 33 | 35 | |
| 34 | 36 | alt = { fg = hl("Comment").fg }, |
| 35 | 37 | 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 }, | |
| 38 | 41 | |
| 39 | 42 | code = { fg = hl("Type").fg, bg = hl("Normal").bg }, |
| 40 | 43 | example = { fg = hl("DiagnosticHint").fg }, |
| 41 | 44 | constraints = { fg = hl("DiagnosticInfo").fg }, |
| 42 | 45 | indent = { fg = hl("Comment").fg }, |
| 43 | 46 | link = { fg = hl("Function").fg, underline = true }, |
| 44 | - list = { fg = hl("Statement").fg }, | |
| 47 | + list = { fg = hl("SpecialChar").fg }, | |
| 45 | 48 | } |
| 46 | 49 | end |
| 47 | 50 |
+6-1lua/leetcode/theme/init.lua
| @@ -4,6 +4,9 @@ local config = require("leetcode.config") | ||
| 4 | 4 | ---@class lc.Theme |
| 5 | 5 | local theme = {} |
| 6 | 6 | |
| 7 | +---@type table<string, table> | |
| 8 | +_Lc_dyn_hl = {} | |
| 9 | + | |
| 7 | 10 | function theme.load_devicons() |
| 8 | 11 | vim.tbl_map(function(l) |
| 9 | 12 | local icon, color = devicons.get_icon_color(l.ft) |
| @@ -16,6 +19,8 @@ function theme.load_devicons() | ||
| 16 | 19 | end |
| 17 | 20 | |
| 18 | 21 | function theme.load() |
| 22 | + _Lc_dyn_hl = {} | |
| 23 | + | |
| 19 | 24 | local default = require("leetcode.theme.default").get() |
| 20 | 25 | for key, t in pairs(default) do |
| 21 | 26 | key = "leetcode_" .. key |
| @@ -33,7 +38,7 @@ function theme.setup() | ||
| 33 | 38 | vim.api.nvim_create_autocmd("ColorScheme", { |
| 34 | 39 | group = vim.api.nvim_create_augroup("lc.colorscheme_sync", {}), |
| 35 | 40 | desc = "Colorscheme Synchronizer", |
| 36 | - callback = function() theme.load() end, | |
| 41 | + callback = theme.load, | |
| 37 | 42 | }) |
| 38 | 43 | |
| 39 | 44 | theme.load() |