irongit

A Neovim plugin enabling you to solve LeetCode problems.

fix: hints text

kawreauthored
parent 8f35246commit d00a7a2edc7202aa0efba259bbac93c86d12b85dBrowse files

7 files changed, +130 -16

+21-0lua/leetcode-ui/group/init.lua
@@ -11,6 +11,27 @@ local log = require("leetcode.logger")
1111 ---@field _ lc.ui.Group.params | lines.params
1212 local Group = Lines:extend("LeetGroup")
1313
14+function Group:lines()
15+ local tbl = {}
16+ for _, item in ipairs(self:contents()) do
17+ self:lines_helper(item, tbl)
18+ end
19+ return tbl
20+end
21+
22+---@private
23+---@param tbl table
24+function Group:lines_helper(item, tbl) --
25+ if item.class and item.class.name == "LeetLine" then
26+ table.insert(tbl, item)
27+ return
28+ end
29+
30+ for _, c in ipairs(item:contents()) do
31+ self:lines_helper(c, tbl)
32+ end
33+end
34+
1435 function Group:contents()
1536 local items = utils.shallowcopy(self._.items)
1637
+1-1lua/leetcode-ui/group/tag/init.lua
@@ -51,7 +51,7 @@ function Tag.normalize(text)
5151 :gsub("\t", "&lctab;")
5252 :gsub("%s", " ")
5353 :gsub("<[^>]*>", function(match) return match:gsub("&nbsp;", " ") end)
54- -- :gsub("<a[^>]*>(.-)</a>", function(match) return match:gsub("&#?%w+;", utils.entity) end)
54+ :gsub("<a[^>]*>(.-)</a>", function(match) return match:gsub("&#?%w+;", utils.entity) end)
5555
5656 log.debug(text)
5757 log.debug(norm:gsub("&lcnl;", "&lcnl;\n"), false)
+45-0lua/leetcode-ui/group/tag/sub.lua
@@ -5,6 +5,51 @@ local log = require("leetcode.logger")
55 ---@class lc.ui.Tag.sub : lc.ui.Tag
66 local Sub = Tag:extend("LeetTagSub")
77
8+local subscript = {
9+ ["0"] = "₀",
10+ ["1"] = "₁",
11+ ["2"] = "₂",
12+ ["3"] = "₃",
13+ ["4"] = "₄",
14+ ["5"] = "₅",
15+ ["6"] = "₆",
16+ ["7"] = "₇",
17+ ["8"] = "₈",
18+ ["9"] = "₉",
19+ ["a"] = "ₐ",
20+ ["b"] = "♭",
21+ ["c"] = "꜀",
22+ ["d"] = "ᑯ",
23+ ["e"] = "ₑ",
24+ ["f"] = "բ",
25+ ["g"] = "₉",
26+ ["h"] = "ₕ",
27+ ["i"] = "ᵢ",
28+ ["j"] = "ⱼ",
29+ ["k"] = "ₖ",
30+ ["l"] = "ₗ",
31+ ["m"] = "ₘ",
32+ ["n"] = "ₙ",
33+ ["o"] = "ₒ",
34+ ["p"] = "ₚ",
35+ ["q"] = "ɋ",
36+ ["r"] = "ᵣ",
37+ ["s"] = "ₛ",
38+ ["t"] = "ₜ",
39+ ["u"] = "ᵤ",
40+ ["v"] = "ᵥ",
41+ ["w"] = "w",
42+ ["x"] = "ₓ",
43+ ["y"] = "y",
44+ ["z"] = "z",
45+}
46+
47+-- function Sub:append(content, highlight)
48+-- content = content:gsub(".", function(match) return subscript[match:lower()] or match end)
49+--
50+-- Sub.super.append(self, content, highlight)
51+-- end
52+
853 function Sub:parse_node()
954 self:append("_")
1055
+45-0lua/leetcode-ui/group/tag/sup.lua
@@ -6,6 +6,51 @@ local log = require("leetcode.logger")
66 ---@class lc.ui.Tag.sup : lc.ui.Tag
77 local Sup = Tag:extend("LeetTagSup")
88
9+local superscript = {
10+ ["0"] = "⁰",
11+ ["1"] = "¹",
12+ ["2"] = "²",
13+ ["3"] = "³",
14+ ["4"] = "⁴",
15+ ["5"] = "⁵",
16+ ["6"] = "⁶",
17+ ["7"] = "⁷",
18+ ["8"] = "⁸",
19+ ["9"] = "⁹",
20+ ["a"] = "ᵃ",
21+ ["b"] = "ᵇ",
22+ ["c"] = "ᶜ",
23+ ["d"] = "ᵈ",
24+ ["e"] = "ᵉ",
25+ ["f"] = "ᶠ",
26+ ["g"] = "ᵍ",
27+ ["h"] = "ʰ",
28+ ["i"] = "ⁱ",
29+ ["j"] = "ʲ",
30+ ["k"] = "ᵏ",
31+ ["l"] = "ˡ",
32+ ["m"] = "ᵐ",
33+ ["n"] = "ⁿ",
34+ ["o"] = "ᵒ",
35+ ["p"] = "ᵖ",
36+ ["q"] = "ᵠ",
37+ ["r"] = "ʳ",
38+ ["s"] = "ˢ",
39+ ["t"] = "ᵗ",
40+ ["u"] = "ᵘ",
41+ ["v"] = "ᵛ",
42+ ["w"] = "ʷ",
43+ ["x"] = "ˣ",
44+ ["y"] = "ʸ",
45+ ["z"] = "ᶻ",
46+}
47+
48+-- function Sup:append(content, highlight)
49+-- content = content:gsub(".", function(match) return superscript[match:lower()] or match end)
50+--
51+-- Sup.super.append(self, content, highlight)
52+-- end
53+
954 function Sup:parse_node()
1055 self:append("^")
1156
+14-13lua/leetcode-ui/popup/info.lua
@@ -4,6 +4,7 @@ local t = require("leetcode.translator")
44 local Line = require("leetcode-ui.line")
55 local NuiTree = require("nui.tree")
66 local SimilarQuestions = require("leetcode-ui.group.similar-questions")
7+local Parser = require("leetcode.parser")
78
89 local config = require("leetcode.config")
910 local utils = require("leetcode.utils")
@@ -13,9 +14,9 @@ local log = require("leetcode.logger")
1314 ---@field popup NuiPopup
1415 ---@field question lc.ui.Question
1516 ---@field hints table[]
16-local InfoPopup = Popup:extend("LeetInfoPopup")
17+local Info = Popup:extend("LeetInfoPopup")
1718
18-function InfoPopup:similar_questions_node()
19+function Info:similar_questions_node()
1920 local nodes = SimilarQuestions:to_nodes(self.question.q.similar)
2021
2122 if not vim.tbl_isempty(nodes) then
@@ -30,9 +31,10 @@ function InfoPopup:similar_questions_node()
3031 end
3132 end
3233
33-function InfoPopup:hints_node()
34+function Info:hints_node()
3435 local hints = {}
3536 for i, hint_txt in ipairs(self.hints) do
37+ log.info(self.hints)
3638 local line = Line()
3739
3840 line:append(tostring(i), "leetcode_list")
@@ -49,7 +51,7 @@ function InfoPopup:hints_node()
4951 end
5052 end
5153
52-function InfoPopup:topics_node()
54+function Info:topics_node()
5355 local topics = {}
5456 for _, topic in ipairs(self.question.q.topic_tags) do
5557 local line = Line()
@@ -67,7 +69,7 @@ function InfoPopup:topics_node()
6769 end
6870 end
6971
70-function InfoPopup:populate()
72+function Info:populate()
7173 local nodes = {
7274 self:hints_node(),
7375 NuiTree.Node({ text = "" }),
@@ -89,9 +91,8 @@ function InfoPopup:populate()
8991 else
9092 if type(node.text) == "string" then
9193 line:append(" ")
92- local parser = require("leetcode.parser")
93- local txt = parser:parse(node.text)
94- if txt:contents()[1] then line:append(txt:contents()[1]) end
94+ local txt = Parser:parse(node.text)
95+ if txt:lines()[1] then line:append(txt:lines()[1]) end
9596 else
9697 line:append(node.text)
9798 end
@@ -119,8 +120,8 @@ function InfoPopup:populate()
119120 end, { noremap = true, nowait = true })
120121 end
121122
122-function InfoPopup:mount()
123- InfoPopup.super.mount(self)
123+function Info:mount()
124+ Info.super.mount(self)
124125
125126 self:populate()
126127
@@ -140,8 +141,8 @@ function InfoPopup:mount()
140141 end
141142
142143 ---@param parent lc.ui.Question
143-function InfoPopup:init(parent)
144- InfoPopup.super.init(self, {
144+function Info:init(parent)
145+ Info.super.init(self, {
145146 position = "50%",
146147 size = {
147148 width = "50%",
@@ -173,6 +174,6 @@ function InfoPopup:init(parent)
173174 end
174175
175176 ---@type fun(parent: lc.ui.Question): lc.ui.InfoPopup
176-local LeetInfoPopup = InfoPopup
177+local LeetInfoPopup = Info
177178
178179 return LeetInfoPopup
+1-0lua/leetcode/theme/default.lua
@@ -60,6 +60,7 @@ M.get = function()
6060 link = { fg = hl("Function").fg, underline = true },
6161 list = { fg = hl("SpecialChar").fg },
6262 ref = { fg = hl("Tag").fg },
63+ su = { fg = hl("Number").fg },
6364
6465 -- calendar
6566 calendar_0 = { fg = "#45475b" },
+3-2lua/leetcode/theme/init.lua
@@ -26,6 +26,9 @@ local highlights = {
2626 explanation = "header",
2727 followup = "followup",
2828
29+ sup = "su",
30+ sub = "su",
31+
2932 -- pre = "",
3033 -- span = "",
3134 -- p = "",
@@ -33,8 +36,6 @@ local highlights = {
3336 -- ol = "",
3437 -- li = "",
3538 -- font = "",
36- sup = "header",
37- sub = "header",
3839 -- small = "",
3940 -- div = "",
4041 }