irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(theme): `ColorScheme` autocmd

kawreauthored
parent fe143d9commit 183ea3e76a05f40c6ca065851113b42e2ec379feBrowse files

17 files changed, +143 -83

+1-1lua/leetcode-menu/components/footer.lua
@@ -21,7 +21,7 @@ function footer:init(opts)
2121
2222 if config.auth.is_signed_in then
2323 local line = NuiLine()
24- line:append("Signed in as: ", "Comment")
24+ line:append("Signed in as: ", "leetcode_alt")
2525 line:append(config.auth.name)
2626 text:append(line)
2727 end
+1-1lua/leetcode-menu/components/notifications.lua
@@ -10,7 +10,7 @@ setmetatable(notifications, Text)
1010 function notifications:init(opts)
1111 opts = vim.tbl_deep_extend("force", {
1212 position = "center",
13- hl = "LeetCodeInfo",
13+ hl = "leetcode_info",
1414 }, opts or {})
1515
1616 local text = Text:init({
+1-1lua/leetcode-ui/component/button.lua
@@ -32,7 +32,7 @@ function button:init(text, sc, on_press, expandable)
3232 local line = NuiLine()
3333 line:append(txt)
3434 line:append(padding)
35- line:append(sc, "LeetCodeInfo")
35+ line:append(sc, "leetcode_info")
3636
3737 local obj = setmetatable({
3838 opts = opts,
+9-20lua/leetcode.lua
@@ -16,26 +16,15 @@ local function should_start()
1616 end
1717
1818 local function setup_highlights()
19- vim.api.nvim_set_hl(0, "LeetCodeEasy", { fg = "#46c6c2" })
20- vim.api.nvim_set_hl(0, "LeetCodeMedium", { fg = "#fac31d" })
21- vim.api.nvim_set_hl(0, "LeetCodeHard", { fg = "#f8615c" })
22-
23- vim.api.nvim_set_hl(0, "LeetCodeInfo", { link = "DiagnosticInfo" })
24- vim.api.nvim_set_hl(0, "LeetCodeHint", { link = "DiagnosticHint" })
25- vim.api.nvim_set_hl(0, "LeetCodeError", { link = "DiagnosticError" })
26- vim.api.nvim_set_hl(0, "LeetCodeOk", { link = "DiagnosticOk" })
27-
28- local normal = vim.api.nvim_get_hl(0, { name = "FloatTitle" })
29- vim.api.nvim_set_hl(0, "LeetCodeNormal", normal)
30- vim.api.nvim_set_hl(0, "LeetCodeItalic", vim.tbl_extend("force", normal, { italic = true }))
31- vim.api.nvim_set_hl(0, "LeetCodeBold", { bold = true })
32-
33- vim.api.nvim_set_hl(0, "LeetCodeCode", { link = "Type" })
34- vim.api.nvim_set_hl(0, "LeetCodeExample", { link = "LeetCodeHint" })
35- vim.api.nvim_set_hl(0, "LeetCodeConstraints", { link = "LeetCodeInfo" })
36- vim.api.nvim_set_hl(0, "LeetCodeIndent", { link = "Comment" })
37- vim.api.nvim_set_hl(0, "LeetCodeList", { link = "Tag" })
38- vim.api.nvim_set_hl(0, "LeetCodeLink", { link = "Function" })
19+ local theme = require("leetcode.theme")
20+
21+ vim.api.nvim_create_autocmd("ColorScheme", {
22+ desc = "Colorscheme Synchronizer",
23+ group = vim.api.nvim_create_augroup("lc.colorscheme_sync", {}),
24+ callback = function() theme.load() end,
25+ })
26+
27+ theme.load()
3928 end
4029
4130 local function setup_cmds()
+8-8lua/leetcode/parser/init.lua
@@ -135,14 +135,14 @@ function parser:handle_list(tags)
135135 text = string.format("%s%d. ", leftpad, self.ol_count[ol_c])
136136 end
137137
138- self.line:append(text, "LeetCodeList")
138+ self.line:append(text, "leetcode_list")
139139 end
140140
141141 ---@param text string
142142 function parser:handle_indent(text)
143143 if self.line:content() ~= "" then return text end
144144
145- self.line:append("\t▎\t", "LeetCodeIndent")
145+ self.line:append("\t▎\t", "leetcode_indent")
146146 end
147147
148148 ---@private
@@ -160,9 +160,9 @@ function parser:handle_link(text, tag_data)
160160 end
161161 if not href then return line end
162162
163- line:append("[", "LeetCodeIndent")
164- line:append(text, "LeetCodeLink")
165- line:append(string.format("](%s)", href), "LeetCodeIndent")
163+ line:append("[", "leetcode_indent")
164+ line:append(text, "leetcode_link")
165+ line:append(string.format("](%s)", href), "leetcode_indent")
166166
167167 return line
168168 end
@@ -180,9 +180,9 @@ function parser:handle_img(tag_data)
180180 if not src then return end
181181 alt = alt or ""
182182
183- line:append(string.format("[%s](", alt ~= "" and alt or "img"), "LeetCodeIndent")
184- line:append(src, "LeetCodeLink")
185- line:append(")", "LeetCodeIndent")
183+ line:append(string.format("[%s](", alt ~= "" and alt or "img"), "leetcode_indent")
184+ line:append(src, "leetcode_link")
185+ line:append(")", "leetcode_indent")
186186
187187 self.text:append(line)
188188 end
+8-8lua/leetcode/parser/utils.lua
@@ -36,14 +36,14 @@ local entities = {
3636 }
3737
3838 local highlights = {
39- [""] = "LeetCodeNormal",
40- ["strong"] = "LeetCodeBold",
41- ["b"] = "LeetCodeBold",
42- ["em"] = "LeetCodeItalic",
43- ["i"] = "LeetCodeItalic",
44- ["code"] = "LeetCodeCode",
45- ["example"] = "LeetCodeExample",
46- ["constraints"] = "LeetCodeConstraints",
39+ [""] = "leetcode_normal",
40+ ["strong"] = "leetcode_bold",
41+ ["b"] = "leetcode_bold",
42+ ["em"] = "leetcode_italic",
43+ ["i"] = "leetcode_italic",
44+ ["code"] = "leetcode_code",
45+ ["example"] = "leetcode_example",
46+ ["constraints"] = "leetcode_constraints",
4747
4848 ["pre"] = "Inherit",
4949 ["span"] = "Inherit",
+1-1lua/leetcode/pickers/language.lua
@@ -21,7 +21,7 @@ local function lang_formatter(snippet) return string.format("%s %s", snippet.t.l
2121
2222 ---@param snippet lc.QuestionCodeSnippet
2323 local function dislay_icon(snippet)
24- local hl = "LeetCodeLang_" .. snippet.t.slug
24+ local hl = "leetcode_lang_" .. snippet.t.slug
2525 vim.api.nvim_set_hl(0, hl, { fg = snippet.t.color })
2626
2727 return { snippet.t.icon, hl }
+8-2lua/leetcode/pickers/question-tabs.lua
@@ -24,13 +24,19 @@ local function display_current(entry)
2424 end
2525
2626 local function display_difficulty(q)
27+ local hl = {
28+ ["Easy"] = "leetcode_easy",
29+ ["Medium"] = "leetcode_medium",
30+ ["Hard"] = "leetcode_hard",
31+ }
32+
2733 local lang = utils.get_lang(q.lang)
28- return { lang.short, "LeetCode" .. q.q.difficulty }
34+ return { lang.short, hl[q.q.difficulty] }
2935 end
3036
3137 ---@param question lc.Cache.Question
3238 local function display_question(question)
33- local index = { question.frontend_id .. ".", "LeetCodeNormal" }
39+ local index = { question.frontend_id .. ".", "leetcode_normal" }
3440 local title = { question.title }
3541
3642 return unpack({ index, title })
+11-9lua/leetcode/pickers/question.lua
@@ -20,22 +20,24 @@ end
2020
2121 ---@param question lc.Cache.Question
2222 local function display_difficulty(question)
23- local hi = {
24- ["Easy"] = { "E", "LeetCodeEasy" },
25- ["Medium"] = { "M", "LeetCodeMedium" },
26- ["Hard"] = { "H", "LeetCodeHard" },
23+ local hl = {
24+ ["Easy"] = "leetcode_easy",
25+ ["Medium"] = "leetcode_medium",
26+ ["Hard"] = "leetcode_hard",
2727 }
2828
29- return { "󱓻", "LeetCode" .. question.difficulty }
29+ return { "󱓻", hl[question.difficulty] }
3030 end
3131
3232 ---@param question lc.Cache.Question
3333 local function display_user_status(question)
34- if question.paid_only and not config.auth.is_premium then return { "", "LeetCodeMedium" } end
34+ if question.paid_only and not config.auth.is_premium then
35+ return { "", "leetcode_medium" }
36+ end
3537
3638 local user_status = {
37- ac = { "", "LeetCodeEasy" },
38- notac = { "󱎖", "LeetCodeMedium" },
39+ ac = { "", "leetcode_easy" },
40+ notac = { "󱎖", "leetcode_medium" },
3941 }
4042
4143 if question.status == vim.NIL then return { "" } end
@@ -44,7 +46,7 @@ end
4446
4547 ---@param question lc.Cache.Question
4648 local function display_question(question)
47- local index = { question.frontend_id .. ".", "LeetCodeNormal" }
49+ local index = { question.frontend_id .. ".", "leetcode_normal" }
4850 local title = { question.title }
4951
5052 return unpack({ index, title })
+44-0lua/leetcode/theme/default.lua
@@ -0,0 +1,44 @@
1+---Get highlight group value by name.
2+---
3+---@param name string String for looking up highlight group.
4+---@return table
5+local function hl(name)
6+ local highlight = vim.api.nvim_get_hl(0, { name = name, link = true })
7+ setmetatable(highlight, {
8+ __index = function(self, key)
9+ if key == "bg" then return self.background end
10+ if key == "fg" then return self.foreground end
11+
12+ return nil
13+ end,
14+ })
15+
16+ return highlight
17+end
18+
19+---@alias lc.Themes table<string, table>
20+---@type lc.Themes
21+return function()
22+ return {
23+ easy = { fg = "#46c6c2" },
24+ medium = { fg = "#fac31d" },
25+ hard = { fg = "#f8615c" },
26+
27+ ok = { fg = hl("DiagnosticOk").fg },
28+ info = { fg = hl("DiagnosticInfo").fg },
29+ hint = { fg = hl("DiagnosticHint").fg },
30+ error = { fg = hl("DiagnosticError").fg },
31+
32+ bold = { bold = true },
33+ italic = { fg = hl("FloatTitle").fg, italic = true },
34+
35+ normal = { fg = hl("FloatTitle").fg },
36+ alt = { fg = hl("Comment").fg },
37+ code = { fg = hl("Type").fg },
38+ example = { fg = hl("DiagnosticHint").fg },
39+ constraints = { fg = hl("DiagnosticInfo").fg },
40+ indent = { fg = hl("Comment").fg },
41+ link = { fg = hl("Function").fg },
42+ list = { fg = hl("Tag").fg },
43+ }
44+end
+12-0lua/leetcode/theme/init.lua
@@ -0,0 +1,12 @@
1+---@class lc.Theme
2+local theme = {}
3+
4+function theme.load()
5+ local default = require("leetcode.theme.default")()
6+ for key, t in pairs(default) do
7+ key = "leetcode_" .. key
8+ vim.api.nvim_set_hl(0, key, t)
9+ end
10+end
11+
12+return theme
+4-4lua/leetcode/ui/console/components/case.lua
@@ -18,22 +18,22 @@ function case:init(index, input, output, expected, passed)
1818
1919 local match = passed or (output == expected)
2020 local icon = match and "" or ""
21- local hi = match and "LeetCodeOk" or "LeetCodeError"
21+ local hi = match and "leetcode_ok" or "leetcode_error"
2222 local title_line = NuiLine():append(icon .. " Case " .. index, hi)
2323
2424 local input_line = NuiLine()
2525 input_line:append("Input: ")
26- input_line:append(input, "Comment")
26+ input_line:append(input, "leetcode_alt")
2727 table.insert(t, input_line)
2828
2929 local output_line = NuiLine()
3030 output_line:append("Output: ")
31- output_line:append(output, "Comment")
31+ output_line:append(output, "leetcode_alt")
3232 table.insert(t, output_line)
3333
3434 local expected_line = NuiLine()
3535 expected_line:append("Expected: ")
36- expected_line:append(expected, "Comment")
36+ expected_line:append(expected, "leetcode_alt")
3737 table.insert(t, expected_line)
3838
3939 return Pre:init(title_line, t)
+2-2lua/leetcode/ui/console/components/pre.lua
@@ -17,7 +17,7 @@ function pre:init(title, lines)
1717 local text = Text:init({ lines = { title, NuiLine() } })
1818
1919 for _, line in ipairs(lines) do
20- local new_line = NuiLine({ NuiText("\t▎\t", "LeetCodeIndent") })
20+ local new_line = NuiLine({ NuiText("\t▎\t", "leetcode_indent") })
2121 new_line:append(line)
2222 text:append(new_line)
2323 end
@@ -33,7 +33,7 @@ function pre:init(title, lines)
3333 -- table.insert(t, NuiLine())
3434 -- local match = output == expected
3535 -- local icon = match and "" or ""
36- -- local hi = match and "LeetCodeOk" or "LeetCodeError"
36+ -- local hi = match and "leetcode_ok" or "leetcode_error"
3737 -- local index_line = NuiLine():append(icon .. " Case " .. index, hi)
3838 -- table.insert(t, index_line)
3939 --
+1-1lua/leetcode/ui/console/components/stdout.lua
@@ -19,7 +19,7 @@ function stdout:init(output)
1919 table.insert(t, NuiLine():append(output_list[i]))
2020 end
2121
22- return Pre:init(NuiLine():append(" Stdout", "Comment"), t)
22+ return Pre:init(NuiLine():append(" Stdout", "leetcode_alt"), t)
2323 end
2424
2525 return stdout
+16-16lua/leetcode/ui/console/result.lua
@@ -29,7 +29,7 @@ end
2929 function result:set_popup_border_hi(hi) self.popup.border:set_highlight(hi) end
3030
3131 function result:handle_accepted(item)
32- local function perc_hi(perc) return perc >= 50 and "LeetCodeOk" or "LeetCodeError" end
32+ local function perc_hi(perc) return perc >= 50 and "leetcode_ok" or "leetcode_error" end
3333 local group = Group:init({ opts = { spacing = 2 } })
3434
3535 local header = Text:init()
@@ -40,7 +40,7 @@ function result:handle_accepted(item)
4040 local status_runtime = NuiLine()
4141 local runtime_ms = item.display_runtime or vim.split(item.status_runtime, " ")[1] or "NIL"
4242 status_runtime:append(runtime_ms)
43- status_runtime:append(" ms", "Comment")
43+ status_runtime:append(" ms", "leetcode_alt")
4444
4545 local perc_runtime = NuiLine()
4646 perc_runtime:append(
@@ -49,7 +49,7 @@ function result:handle_accepted(item)
4949 )
5050 perc_runtime:append("of users with " .. item.pretty_lang)
5151
52- local runtime = Pre:init(NuiText("󰓅 Runtime", "LeetCodeNormal"), {
52+ local runtime = Pre:init(NuiText("󰓅 Runtime", "leetcode_normal"), {
5353 status_runtime,
5454 perc_runtime,
5555 })
@@ -60,7 +60,7 @@ function result:handle_accepted(item)
6060 if item.status_memory == "0B" then item.status_memory = "0 MB" end
6161 local s_mem = vim.split(item.status_memory, " ")
6262 status_memory:append(s_mem[1] .. " ")
63- status_memory:append(s_mem[2], "Comment")
63+ status_memory:append(s_mem[2], "leetcode_alt")
6464
6565 local perc_mem = NuiLine()
6666 perc_mem:append(
@@ -69,7 +69,7 @@ function result:handle_accepted(item)
6969 )
7070 perc_mem:append("of users with " .. item.pretty_lang)
7171
72- local memory = Pre:init(NuiText("󰍛 Memory", "LeetCodeNormal"), {
72+ local memory = Pre:init(NuiText("󰍛 Memory", "leetcode_normal"), {
7373 status_memory,
7474 perc_mem,
7575 })
@@ -90,7 +90,7 @@ function result:handle_runtime(item) -- status code = 10
9090 local h = NuiLine()
9191 h:append(item._.title, item._.hl)
9292 h:append(" | ")
93- h:append("Runtime: " .. item.status_runtime, "Comment")
93+ h:append("Runtime: " .. item.status_runtime, "leetcode_alt")
9494 header:append(h)
9595 group:append(header)
9696
@@ -122,7 +122,7 @@ function result:handle_submission_error(item) -- status code = 11
122122 local header = NuiLine()
123123 header:append(item._.title, item._.hl)
124124 header:append(" | ")
125- header:append(passed_testcases(item), "Comment")
125+ header:append(passed_testcases(item), "leetcode_alt")
126126 group:append(Text:init({ lines = { header } }))
127127
128128 local text = Case:init(
@@ -153,10 +153,10 @@ function result:handle_limit_exceeded(item) -- status code = 14
153153
154154 if item._.submission then
155155 local last_testcase = NuiLine()
156- last_testcase:append(item.last_testcase:gsub("\n", " "), "LeetCodeIndent")
156+ last_testcase:append(item.last_testcase:gsub("\n", " "), "leetcode_indent")
157157
158158 local pre_header = NuiLine()
159- pre_header:append(" Last Executed Input", "LeetCodeNormal")
159+ pre_header:append(" Last Executed Input", "leetcode_normal")
160160
161161 local last_exec = Pre:init(pre_header, { last_testcase })
162162 group:append(last_exec)
@@ -184,21 +184,21 @@ function result:handle_runtime_error(item) -- status code = 15
184184
185185 if item._.submission then
186186 header:append(" | ")
187- header:append(passed_testcases(item), "Comment")
187+ header:append(passed_testcases(item), "leetcode_alt")
188188 end
189189
190190 local t = {}
191191 for line in vim.gsplit(item.full_runtime_error, "\n") do
192- table.insert(t, NuiLine():append(line, "LeetCodeError"))
192+ table.insert(t, NuiLine():append(line, "leetcode_error"))
193193 end
194194 group:append(Pre:init(header, t))
195195
196196 if item._.submission then
197197 local pre_header = NuiLine()
198- pre_header:append(" Last Executed Input", "LeetCodeNormal")
198+ pre_header:append(" Last Executed Input", "leetcode_normal")
199199
200200 local last_testcase = NuiLine()
201- last_testcase:append(item.last_testcase:gsub("\n", " "), "LeetCodeIndent")
201+ last_testcase:append(item.last_testcase:gsub("\n", " "), "leetcode_indent")
202202
203203 local last_exec = Pre:init(pre_header, { last_testcase })
204204 group:append(last_exec)
@@ -228,12 +228,12 @@ function result:handle_compile_error(item) -- status code = 20
228228 header:append(item._.title, item._.hl)
229229 if item._.submission then
230230 header:append(" | ")
231- header:append(passed_testcases(item), "Comment")
231+ header:append(passed_testcases(item), "leetcode_alt")
232232 end
233233
234234 local t = {}
235235 for line in vim.gsplit(item.full_compile_error, "\n") do
236- table.insert(t, NuiLine():append(line, "LeetCodeError"))
236+ table.insert(t, NuiLine():append(line, "leetcode_error"))
237237 end
238238
239239 group:append(Pre:init(header, t))
@@ -254,7 +254,7 @@ function result:handle_item(item)
254254 if item.submission_id then
255255 submission = not item.submission_id:find("runcode") and true or false
256256 end
257- local hl = success and "LeetCodeOk" or "LeetCodeError"
257+ local hl = success and "leetcode_ok" or "leetcode_error"
258258
259259 item._ = {
260260 title = " " .. item.status_msg,
+14-7lua/leetcode/ui/description.lua
@@ -88,7 +88,7 @@ function description:populate()
8888 local q = self.parent.q
8989
9090 local linkline = NuiLine()
91- linkline:append("https://leetcode.com/problems/" .. q.title_slug .. "/", "Comment")
91+ linkline:append("https://leetcode.com/problems/" .. q.title_slug .. "/", "leetcode_alt")
9292
9393 local titleline = NuiLine()
9494 titleline:append(" " .. q.frontend_id .. ". " .. q.title .. " ", "")
@@ -97,18 +97,25 @@ function description:populate()
9797 statsline:append(
9898 q.difficulty,
9999 ({
100- ["Easy"] = "LeetCodeEasy",
101- ["Medium"] = "LeetCodeMedium",
102- ["Hard"] = "LeetCodeHard",
100+ ["Easy"] = "leetcode_easy",
101+ ["Medium"] = "leetcode_medium",
102+ ["Hard"] = "leetcode_hard",
103103 })[q.difficulty]
104104 )
105105 statsline:append(" | ")
106106
107- statsline:append(q.likes .. "  ", "Comment")
108- statsline:append(q.dislikes .. " ", "Comment")
107+ statsline:append(q.likes .. "  ", "leetcode_alt")
108+ statsline:append(q.dislikes .. " ", "leetcode_alt")
109109 statsline:append(" | ")
110110
111- statsline:append(string.format("%s of %s", q.stats.acRate, q.stats.totalSubmission), "Comment")
111+ statsline:append(
112+ string.format("%s of %s", q.stats.acRate, q.stats.totalSubmission),
113+ "leetcode_alt"
114+ )
115+ if not vim.tbl_isempty(q.hints) then
116+ statsline:append(" | ")
117+ statsline:append("Hints", "leetcode_alt")
118+ end
112119
113120 local titlecomp = Text:init({
114121 lines = { titleline },
+2-2lua/leetcode/ui/hints.lua
@@ -21,7 +21,7 @@ function Hints:mount()
2121 local nodes = {}
2222 for i, hint in ipairs(self.hints) do
2323 local node = NuiTree.Node(
24- { text = NuiText("󰛨 Hint " .. i, "LeetCodeHint") },
24+ { text = NuiText("󰛨 Hint " .. i, "leetcode_hint") },
2525 { NuiTree.Node({ text = hint }) }
2626 )
2727
@@ -30,7 +30,7 @@ function Hints:mount()
3030 if vim.tbl_isempty(nodes) then
3131 table.insert(
3232 nodes,
33- NuiTree.Node({ text = NuiText(" No hints available", "LeetCodeError") })
33+ NuiTree.Node({ text = NuiText(" No hints available", "leetcode_error") })
3434 )
3535 end
3636