irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(cmd): `Leet last_submit`

kawreauthored
parent 76ab4a8commit fadcc49c19e05fe93a711ef61d1c7d4a20265c2fBrowse files

6 files changed, +57 -15

+6-2README.md
@@ -300,14 +300,18 @@ image_support = false,
300300
301301 - `submit` submit currently opened question
302302
303-- `open` opens the current question in a default browser
304-
305303 - `random` opens a random question
306304
307305 - `daily` opens the question of today
308306
309307 - `list` opens a problemlist picker
310308
309+- `open` opens the current question in a default browser
310+
311+- `reset` reset current question to default code definition
312+
313+- `last_submit` retrieve last submitted code for the current question
314+
311315 - `desc` toggle question description
312316
313317 - `toggle` same as `Leet desc`
+6-2README.zh.md
@@ -302,14 +302,18 @@ image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行
302302
303303 - `submit` 提交当前打开的问题
304304
305-- `open` 在默认浏览器中打开当前问题。
306-
307305 - `random` 打开一个随机问题
308306
309307 - `daily` 打开今天的问题
310308
311309 - `list` 打开问题列表选择器
312310
311+- `open` 在默认浏览器中打开当前问题
312+
313+- `reset` 还原到默认的代码模版
314+
315+- `last_submit` 检索上次提交的代码,用于当前问题
316+
313317 - `desc` 切换问题描述
314318
315319 - `toggle` 与 `Leet desc` 相同
+6-7lua/leetcode-ui/question.lua
@@ -23,12 +23,7 @@ function Question:get_snippet()
2323 local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1]
2424 if not snip then return end
2525
26- local lang = utils.get_lang(self.lang)
27- snip.code = (snip.code or ""):gsub("\r\n", "\n")
28-
29- return self:injector(
30- ("%s @leet start\n%s\n%s @leet end"):format(lang.comment, snip.code, lang.comment)
31- )
26+ return self:injector(snip.code or "")
3227 end
3328
3429 function Question:create_file()
@@ -54,9 +49,10 @@ function Question:create_file()
5449 return self.file:absolute()
5550 end
5651
57----@private
5852 ---@param code string
5953 function Question:injector(code)
54+ code = code:gsub("\r\n", "\n")
55+
6056 local injector = config.user.injector
6157
6258 local inject = injector[self.lang]
@@ -80,8 +76,11 @@ function Question:injector(code)
8076 end
8177 end
8278
79+ local lang = utils.get_lang(self.lang)
8380 return norm_inject(inject.before, true) --
81+ .. ("%s @leet start\n"):format(lang.comment)
8482 .. code
83+ .. ("\n%s @leet end"):format(lang.comment)
8584 .. norm_inject(inject.after, false)
8685 end
8786
+9-0lua/leetcode/api/question.lua
@@ -2,6 +2,7 @@ local utils = require("leetcode.api.utils")
22 local log = require("leetcode.logger")
33 local queries = require("leetcode.api.queries")
44 local problemlist = require("leetcode.cache.problemlist")
5+local urls = require("leetcode.api.urls")
56
67 local question = {}
78
@@ -69,4 +70,12 @@ function question.random(filters)
6970 return q
7071 end
7172
73+---@param qid integer
74+---@param lang lc.lang
75+---@param cb function
76+function question.latest_submission(qid, lang, cb)
77+ local url = urls.latest_submission:format(qid, lang)
78+ utils.get(url, { callback = cb })
79+end
80+
7281 return question
+1-0lua/leetcode/api/urls.lua
@@ -13,5 +13,6 @@ urls.interpret = "/problems/%s/interpret_solution/"
1313 urls.submit = "/problems/%s/submit/"
1414 urls.run = "/problems/%s/interpret_solution/"
1515 urls.check = "/submissions/detail/%s/check/"
16+urls.latest_submission = "/submissions/latest/?qid=%s&lang=%s"
1617
1718 return urls
+29-4lua/leetcode/command/init.lua
@@ -275,13 +275,38 @@ function cmd.reset()
275275 if not q then return end
276276
277277 local snip = q:get_snippet()
278- if not snip then return end
279-
280- if vim.api.nvim_buf_is_valid(q.bufnr) then
278+ if snip and vim.api.nvim_buf_is_valid(q.bufnr) then
281279 vim.api.nvim_buf_set_lines(q.bufnr, 0, -1, false, vim.split(snip, "\n"))
282280 end
283281 end
284282
283+function cmd.last_submit()
284+ local utils = require("leetcode.utils")
285+ utils.auth_guard()
286+ local q = utils.curr_question()
287+ if not q then return end
288+
289+ local question_api = require("leetcode.api.question")
290+ question_api.latest_submission(q.q.id, q.lang, function(res, err) --
291+ if err then
292+ if err.status == 404 then
293+ log.error("You haven't submitted any code!")
294+ else
295+ log.err(err)
296+ end
297+
298+ return
299+ end
300+
301+ if type(res) == "table" and res.code and vim.api.nvim_buf_is_valid(q.bufnr) then
302+ local code = q:injector(res.code)
303+ vim.api.nvim_buf_set_lines(q.bufnr, 0, -1, false, vim.split(code, "\n"))
304+ else
305+ log.error("Something went wrong")
306+ end
307+ end)
308+end
309+
285310 function cmd.fix()
286311 require("leetcode.cache.cookie").delete()
287312 require("leetcode.cache.problemlist").delete()
@@ -411,7 +436,7 @@ cmd.commands = {
411436 yank = { cmd.yank },
412437 open = { cmd.open },
413438 reset = { cmd.reset },
414- last_submission = { cmd.last_submission },
439+ last_submit = { cmd.last_submit },
415440
416441 list = {
417442 cmd.problems,