A Neovim plugin enabling you to solve LeetCode problems.
feat(cmd): `Leet last_submit`
6 files changed, +57 -15
+6-2README.md
| @@ -300,14 +300,18 @@ image_support = false, | ||
| 300 | 300 | |
| 301 | 301 | - `submit` submit currently opened question |
| 302 | 302 | |
| 303 | -- `open` opens the current question in a default browser | |
| 304 | - | |
| 305 | 303 | - `random` opens a random question |
| 306 | 304 | |
| 307 | 305 | - `daily` opens the question of today |
| 308 | 306 | |
| 309 | 307 | - `list` opens a problemlist picker |
| 310 | 308 | |
| 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 | + | |
| 311 | 315 | - `desc` toggle question description |
| 312 | 316 | |
| 313 | 317 | - `toggle` same as `Leet desc` |
+6-2README.zh.md
| @@ -302,14 +302,18 @@ image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行 | ||
| 302 | 302 | |
| 303 | 303 | - `submit` 提交当前打开的问题 |
| 304 | 304 | |
| 305 | -- `open` 在默认浏览器中打开当前问题。 | |
| 306 | - | |
| 307 | 305 | - `random` 打开一个随机问题 |
| 308 | 306 | |
| 309 | 307 | - `daily` 打开今天的问题 |
| 310 | 308 | |
| 311 | 309 | - `list` 打开问题列表选择器 |
| 312 | 310 | |
| 311 | +- `open` 在默认浏览器中打开当前问题 | |
| 312 | + | |
| 313 | +- `reset` 还原到默认的代码模版 | |
| 314 | + | |
| 315 | +- `last_submit` 检索上次提交的代码,用于当前问题 | |
| 316 | + | |
| 313 | 317 | - `desc` 切换问题描述 |
| 314 | 318 | |
| 315 | 319 | - `toggle` 与 `Leet desc` 相同 |
+6-7lua/leetcode-ui/question.lua
| @@ -23,12 +23,7 @@ function Question:get_snippet() | ||
| 23 | 23 | local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] |
| 24 | 24 | if not snip then return end |
| 25 | 25 | |
| 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 "") | |
| 32 | 27 | end |
| 33 | 28 | |
| 34 | 29 | function Question:create_file() |
| @@ -54,9 +49,10 @@ function Question:create_file() | ||
| 54 | 49 | return self.file:absolute() |
| 55 | 50 | end |
| 56 | 51 | |
| 57 | ----@private | |
| 58 | 52 | ---@param code string |
| 59 | 53 | function Question:injector(code) |
| 54 | + code = code:gsub("\r\n", "\n") | |
| 55 | + | |
| 60 | 56 | local injector = config.user.injector |
| 61 | 57 | |
| 62 | 58 | local inject = injector[self.lang] |
| @@ -80,8 +76,11 @@ function Question:injector(code) | ||
| 80 | 76 | end |
| 81 | 77 | end |
| 82 | 78 | |
| 79 | + local lang = utils.get_lang(self.lang) | |
| 83 | 80 | return norm_inject(inject.before, true) -- |
| 81 | + .. ("%s @leet start\n"):format(lang.comment) | |
| 84 | 82 | .. code |
| 83 | + .. ("\n%s @leet end"):format(lang.comment) | |
| 85 | 84 | .. norm_inject(inject.after, false) |
| 86 | 85 | end |
| 87 | 86 |
+9-0lua/leetcode/api/question.lua
| @@ -2,6 +2,7 @@ local utils = require("leetcode.api.utils") | ||
| 2 | 2 | local log = require("leetcode.logger") |
| 3 | 3 | local queries = require("leetcode.api.queries") |
| 4 | 4 | local problemlist = require("leetcode.cache.problemlist") |
| 5 | +local urls = require("leetcode.api.urls") | |
| 5 | 6 | |
| 6 | 7 | local question = {} |
| 7 | 8 | |
| @@ -69,4 +70,12 @@ function question.random(filters) | ||
| 69 | 70 | return q |
| 70 | 71 | end |
| 71 | 72 | |
| 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 | + | |
| 72 | 81 | return question |
+1-0lua/leetcode/api/urls.lua
| @@ -13,5 +13,6 @@ urls.interpret = "/problems/%s/interpret_solution/" | ||
| 13 | 13 | urls.submit = "/problems/%s/submit/" |
| 14 | 14 | urls.run = "/problems/%s/interpret_solution/" |
| 15 | 15 | urls.check = "/submissions/detail/%s/check/" |
| 16 | +urls.latest_submission = "/submissions/latest/?qid=%s&lang=%s" | |
| 16 | 17 | |
| 17 | 18 | return urls |
+29-4lua/leetcode/command/init.lua
| @@ -275,13 +275,38 @@ function cmd.reset() | ||
| 275 | 275 | if not q then return end |
| 276 | 276 | |
| 277 | 277 | 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 | |
| 281 | 279 | vim.api.nvim_buf_set_lines(q.bufnr, 0, -1, false, vim.split(snip, "\n")) |
| 282 | 280 | end |
| 283 | 281 | end |
| 284 | 282 | |
| 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 | + | |
| 285 | 310 | function cmd.fix() |
| 286 | 311 | require("leetcode.cache.cookie").delete() |
| 287 | 312 | require("leetcode.cache.problemlist").delete() |
| @@ -411,7 +436,7 @@ cmd.commands = { | ||
| 411 | 436 | yank = { cmd.yank }, |
| 412 | 437 | open = { cmd.open }, |
| 413 | 438 | reset = { cmd.reset }, |
| 414 | - last_submission = { cmd.last_submission }, | |
| 439 | + last_submit = { cmd.last_submit }, | |
| 415 | 440 | |
| 416 | 441 | list = { |
| 417 | 442 | cmd.problems, |