irongit

A Neovim plugin enabling you to solve LeetCode problems.

fix: improvements

kawreauthored
parent 2f9b199commit ddb0fb61edc164672ed281df6ca17509df778754Browse files

15 files changed, +89 -121

+1-14lua/leetcode-menu/init.lua
@@ -11,16 +11,6 @@ local log = require("leetcode.logger")
1111 local menu = {} ---@diagnostic disable-line
1212 menu.__index = menu
1313
14----@type table<integer, lc.Question>
15-_Lc_questions = {}
16-
17----@type integer
18-_Lc_curr_question = 0
19-
20----@type lc-menu
21-_Lc_Menu = {} ---@diagnostic disable-line
22-_Lc_MenuTabPage = 0
23-
2414 local function tbl_keys(t)
2515 local keys = vim.tbl_keys(t)
2616 if not keys then return end
@@ -104,10 +94,8 @@ end
10494 function menu:init()
10595 local bufnr = vim.api.nvim_get_current_buf()
10696 local winid = vim.api.nvim_get_current_win()
107- local tabpage = vim.api.nvim_get_current_tabpage()
108- _Lc_MenuTabPage = tabpage
10997
110- utils.apply_opt_local({
98+ utils.apply_opt_local(winid, {
11199 buflisted = false,
112100 matchpairs = "",
113101 swapfile = false,
@@ -134,7 +122,6 @@ function menu:init()
134122 local obj = setmetatable({
135123 bufnr = bufnr,
136124 winid = winid,
137- tabpage = tabpage,
138125 layout = loading,
139126 cursor = {
140127 idx = 1,
+4-4lua/leetcode-menu/utils.lua
@@ -2,12 +2,12 @@ local log = require("leetcode.logger")
22
33 local utils = {}
44
5-function utils.apply_opt_local(options)
6- local bufnr = vim.api.nvim_get_current_buf()
7- log.debug("applying `opt_local` to bufnr: " .. bufnr)
5+function utils.apply_opt_local(winid, options)
6+ if not winid then return end
87
8+ log.debug("applying `opt_local` to winid: " .. winid)
99 for key, value in pairs(options) do
10- vim.api.nvim_set_option_value(key, value, { scope = "local" })
10+ vim.api.nvim_set_option_value(key, value, { win = winid, scope = "local" })
1111 end
1212 end
1313
+2-2lua/leetcode/api/auth.lua
@@ -17,13 +17,13 @@ end
1717
1818 local usr_fields = [[
1919 id: userId
20+ name: username
2021 is_signed_in: isSignedIn
2122 is_premium: isPremium
22- name: username
2323 is_verified: isVerified
2424 ]]
2525
26----@return lc.UserAuth
26+---@return lc.UserStatus
2727 function M.user()
2828 local query = string.format(
2929 [[
+10-0lua/leetcode/api/types.lua
@@ -311,3 +311,13 @@
311311 ---@field _ lc.Interpreter.item
312312
313313 ---@alias lc.submission lc.Interpreter.Response.submission
314+
315+--------------------------------------------
316+--- UserStatus
317+--------------------------------------------
318+---@class lc.UserStatus
319+---@field name string
320+---@field is_signed_in boolean
321+---@field is_premium boolean
322+---@field is_verified boolean
323+---@field id integer
+5-2lua/leetcode/command.lua
@@ -66,7 +66,7 @@ end
6666
6767 ---Merge configurations into default configurations and set it as user configurations.
6868 ---
69----@return lc.UserAuth | nil
69+---@return lc.UserStatus | nil
7070 function cmd.authenticate() require("leetcode.api.auth").user() end
7171
7272 ---Merge configurations into default configurations and set it as user configurations.
@@ -90,7 +90,10 @@ function cmd.random_question()
9090 end
9191 end
9292
93-function cmd.menu() vim.api.nvim_set_current_tabpage(_Lc_MenuTabPage) end
93+function cmd.menu()
94+ local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, _Lc_Menu.winid)
95+ if ok then vim.api.nvim_set_current_tabpage(tabp) end
96+end
9497
9598 ---@param layout layouts
9699 function cmd.menu_layout(layout) _Lc_Menu:set_layout(layout) end
+9-7lua/leetcode/config/init.lua
@@ -1,5 +1,11 @@
11 local template = require("leetcode.config.template")
22
3+---@type lc.Question[]
4+_Lc_questions = {}
5+
6+---@type lc-menu
7+_Lc_Menu = {} ---@diagnostic disable-line
8+
39 ---@class lc.Settings
410 local config = {
511 default = template,
@@ -14,14 +20,10 @@ local config = {
1420
1521 langs = require("leetcode.config.langs"),
1622 icons = require("leetcode.config.icons"),
17-}
1823
19----@class lc.UserAuth
20----@field name string
21----@field is_signed_in boolean
22----@field is_premium boolean
23----@field id integer
24-config.auth = {}
24+ ---@diagnostic disable-next-line
25+ auth = {}, ---@type lc.UserStatus
26+}
2527
2628 ---Merge configurations into default configurations and set it as user configurations.
2729 ---
+3-0lua/leetcode/parser/init.lua
@@ -1,3 +1,6 @@
1+---TODO: 2659
2+---TODO: https://leetcode.com/problems/make-array-empty/
3+
14 local Text = require("leetcode-ui.component.text")
25 local utils = require("leetcode.parser.utils")
36 local log = require("leetcode.logger")
+9-3lua/leetcode/pickers/question-tabs.lua
@@ -31,12 +31,18 @@ local function display_difficulty(q)
3131 }
3232
3333 local lang = utils.get_lang(q.lang)
34- return { lang.short, hl[q.q.difficulty] }
34+ return { lang.icon }
3535 end
3636
3737 ---@param question lc.Cache.Question
3838 local function display_question(question)
39- local index = { question.frontend_id .. ".", "leetcode_normal" }
39+ local hl = {
40+ ["Easy"] = "leetcode_easy",
41+ ["Medium"] = "leetcode_medium",
42+ ["Hard"] = "leetcode_hard",
43+ }
44+
45+ local index = { question.frontend_id .. ".", hl[question.difficulty] }
4046 local title = { question.title }
4147
4248 return unpack({ index, title })
@@ -46,7 +52,7 @@ local displayer = entry_display.create({
4652 separator = " ",
4753 items = {
4854 { width = 1 },
49- { width = 5 },
55+ { width = 1 },
5056 { width = 5 },
5157 { remaining = true },
5258 },
+9-5lua/leetcode/theme/default.lua
@@ -1,3 +1,5 @@
1+local M = {}
2+
13 ---Get highlight group value by name.
24 ---
35 ---@param name string String for looking up highlight group.
@@ -17,8 +19,8 @@ local function hl(name)
1719 end
1820
1921 ---@alias lc.highlights table<string, table>
20----@type lc.highlights
21-return function()
22+---@return lc.highlights
23+M.get = function()
2224 return {
2325 easy = { fg = "#46c6c2" },
2426 medium = { fg = "#fac31d" },
@@ -29,11 +31,11 @@ return function()
2931 hint = { fg = hl("DiagnosticHint").fg },
3032 error = { fg = hl("DiagnosticError").fg },
3133
32- bold = { bold = true },
34+ alt = { fg = hl("Comment").fg },
35+ normal = { fg = hl("FloatTitle").fg },
3336 italic = { fg = hl("FloatTitle").fg, italic = true },
37+ bold = { bold = true },
3438
35- normal = { fg = hl("FloatTitle").fg },
36- alt = { fg = hl("Comment").fg },
3739 code = { fg = hl("Type").fg, bg = hl("Normal").bg },
3840 example = { fg = hl("DiagnosticHint").fg },
3941 constraints = { fg = hl("DiagnosticInfo").fg },
@@ -42,3 +44,5 @@ return function()
4244 list = { fg = hl("Tag").fg },
4345 }
4446 end
47+
48+return M
+1-1lua/leetcode/theme/init.lua
@@ -2,7 +2,7 @@
22 local theme = {}
33
44 function theme.load()
5- local default = require("leetcode.theme.default")()
5+ local default = require("leetcode.theme.default").get()
66 for key, t in pairs(default) do
77 key = "leetcode_" .. key
88 vim.api.nvim_set_hl(0, key, t)
+3-3lua/leetcode/ui/description.lua
@@ -32,10 +32,10 @@ function description:mount()
3232 self.visible = true
3333 self:populate()
3434 self.split:mount()
35- self:draw()
3635
3736 local utils = require("leetcode-menu.utils")
38- utils.apply_opt_local({
37+ utils.apply_opt_local(self.split.winid, {
38+ winhighlight = "Normal:NormalFloat,FloatBorder:FloatBorder",
3939 buflisted = false,
4040 matchpairs = "",
4141 swapfile = false,
@@ -54,9 +54,9 @@ function description:mount()
5454 list = false,
5555 spell = false,
5656 signcolumn = "no",
57- winhighlight = "Normal:NormalFloat,FloatBorder:FloatBorder",
5857 })
5958
59+ self:draw()
6060 self:autocmds()
6161 return self
6262 end
+4-5lua/leetcode/ui/hints.lua
@@ -11,14 +11,14 @@ local Hints = {}
1111 Hints.__index = Hints
1212
1313 function Hints:mount()
14- log.debug(self)
1514 self.popup:mount()
15+
1616 local utils = require("leetcode-menu.utils")
17- utils.apply_opt_local({ wrap = true })
17+ utils.apply_opt_local(self.popup.winid, { wrap = true })
1818
1919 local NuiTree = require("nui.tree")
20-
2120 local nodes = {}
21+
2222 for i, hint in ipairs(self.hints) do
2323 local node = NuiTree.Node(
2424 { text = NuiText("󰛨 Hint " .. i, "leetcode_hint") },
@@ -39,11 +39,10 @@ function Hints:mount()
3939 nodes = nodes,
4040 prepare_node = function(node)
4141 local line = NuiLine()
42-
4342 line:append(string.rep(" ", node:get_depth() - 1))
4443
4544 if node:has_children() then
46- line:append(node:is_expanded() and " " or " ", "SpecialChar")
45+ line:append(node:is_expanded() and " " or " ", "leetcode_list")
4746 line:append(node.text)
4847 else
4948 if type(node.text) == "string" then
+0-40lua/leetcode/ui/init.lua
@@ -18,44 +18,4 @@ end
1818
1919 function Ui.input(opts, callback) vim.ui.input(opts, callback) end
2020
21---@param question lc.Problem
22-function Ui.open_qot()
23- local qot = api_problems.question_of_today()
24-
25- local popup = Popup({
26- position = "50%",
27- size = {
28- width = 80,
29- height = 40,
30- },
31- enter = true,
32- focusable = true,
33- zindex = 50,
34- relative = "editor",
35- border = {
36- padding = {
37- top = 2,
38- bottom = 2,
39- left = 3,
40- right = 3,
41- },
42- style = "rounded",
43- text = {
44- top = " I am top title ",
45- top_align = "center",
46- bottom = "I am bottom title",
47- bottom_align = "left",
48- },
49- },
50- buf_options = {
51- modifiable = true,
52- readonly = false,
53- },
54- win_options = {
55- winblend = 10,
56- winhighlight = "Normal:Normal,FloatBorder:FloatBorder",
57- },
58- })
59-end
60-
6121 return Ui
+19-27lua/leetcode/ui/question.lua
@@ -52,8 +52,8 @@ function Question:mount()
5252 vim.cmd("$tabe " .. self.file:absolute())
5353
5454 self.bufnr = vim.api.nvim_get_current_buf()
55- _Lc_curr_question = self.bufnr
56- _Lc_questions[_Lc_curr_question] = self
55+ self.winid = vim.api.nvim_get_current_win()
56+ table.insert(_Lc_questions, self)
5757
5858 self.description = Description:init(self)
5959 self.console = Console:init(self)
@@ -64,31 +64,23 @@ function Question:mount()
6464 end
6565
6666 function Question:autocmds()
67- local group_id = vim.api.nvim_create_augroup("leetcode_questions", { clear = true })
68- vim.api.nvim_create_autocmd("TabEnter", {
69- group = group_id,
70- callback = function()
71- local questions = utils.curr_question_tabs()
72-
73- local tabpage = vim.api.nvim_get_current_tabpage()
74- for _, q in ipairs(questions) do
75- if q.tabpage == tabpage then _Lc_curr_question = q.question.bufnr end
76- end
77- end,
78- })
79-
80- local q_group_id = vim.api.nvim_create_augroup("leetcode_question", {})
81- vim.api.nvim_create_autocmd("BufWinEnter", {
82- group = q_group_id,
83- buffer = self.bufnr,
84- callback = function() self.description.split:show() end,
85- })
86-
87- vim.api.nvim_create_autocmd("BufWinLeave", {
88- group = q_group_id,
89- buffer = self.bufnr,
90- callback = function() self.description.split:hide() end,
91- })
67+ -- local group_id = vim.api.nvim_create_augroup("leetcode_questions", { clear = true })
68+
69+ -- local q_group_id = vim.api.nvim_create_augroup("leetcode_question", {})
70+ -- vim.api.nvim_create_autocmd("BufWinEnter", {
71+ -- group = q_group_id,
72+ -- buffer = self.bufnr,
73+ -- callback = function()
74+ -- log.info("wtf")
75+ -- self.description.split:show()
76+ -- end,
77+ -- })
78+ --
79+ -- vim.api.nvim_create_autocmd("BufWinLeave", {
80+ -- group = q_group_id,
81+ -- buffer = self.bufnr,
82+ -- callback = function() self.description.split:hide() end,
83+ -- })
9284 end
9385
9486 function Question:handle_mount()
+10-8lua/leetcode/utils.lua
@@ -68,19 +68,21 @@ function utils.curr_question_tabs()
6868 ---@type lc.Question.Tab[]
6969 local questions = {}
7070
71- for _, tabp in ipairs(vim.api.nvim_list_tabpages()) do
72- local bufs = vim.fn.tabpagebuflist(tabp)
73-
74- for _, bufnr in ipairs(bufs) do
75- if _Lc_questions[bufnr] then
76- table.insert(questions, { tabpage = tabp, question = _Lc_questions[bufnr] })
77- end
78- end
71+ for _, q in ipairs(_Lc_questions) do
72+ local tabp = utils.question_tabp(q)
73+ if tabp then table.insert(questions, { tabpage = tabp, question = q }) end
7974 end
8075
8176 return questions
8277 end
8378
79+---@param q lc.Question
80+---@return integer|nil
81+function utils.question_tabp(q)
82+ local ok, tabp = pcall(vim.api.nvim_win_get_tabpage, q.winid)
83+ if ok then return tabp end
84+end
85+
8486 ---@return lc.Question
8587 function utils.curr_question()
8688 local tabp = vim.api.nvim_get_current_tabpage()