Skip to content

Commit

Permalink
feat(cmd): Leet last_submit
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 15, 2024
1 parent 76ab4a8 commit fadcc49
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,18 @@ image_support = false,

- `submit` submit currently opened question

- `open` opens the current question in a default browser

- `random` opens a random question

- `daily` opens the question of today

- `list` opens a problemlist picker

- `open` opens the current question in a default browser

- `reset` reset current question to default code definition

- `last_submit` retrieve last submitted code for the current question

- `desc` toggle question description

- `toggle` same as `Leet desc`
Expand Down
8 changes: 6 additions & 2 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,18 @@ image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行

- `submit` 提交当前打开的问题

- `open` 在默认浏览器中打开当前问题。

- `random` 打开一个随机问题

- `daily` 打开今天的问题

- `list` 打开问题列表选择器

- `open` 在默认浏览器中打开当前问题

- `reset` 还原到默认的代码模版

- `last_submit` 检索上次提交的代码,用于当前问题

- `desc` 切换问题描述

- `toggle``Leet desc` 相同
Expand Down
13 changes: 6 additions & 7 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ function Question:get_snippet()
local snip = vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1]
if not snip then return end

local lang = utils.get_lang(self.lang)
snip.code = (snip.code or ""):gsub("\r\n", "\n")

return self:injector(
("%s @leet start\n%s\n%s @leet end"):format(lang.comment, snip.code, lang.comment)
)
return self:injector(snip.code or "")
end

function Question:create_file()
Expand All @@ -54,9 +49,10 @@ function Question:create_file()
return self.file:absolute()
end

---@private
---@param code string
function Question:injector(code)
code = code:gsub("\r\n", "\n")

local injector = config.user.injector

local inject = injector[self.lang]
Expand All @@ -80,8 +76,11 @@ function Question:injector(code)
end
end

local lang = utils.get_lang(self.lang)
return norm_inject(inject.before, true) --
.. ("%s @leet start\n"):format(lang.comment)
.. code
.. ("\n%s @leet end"):format(lang.comment)
.. norm_inject(inject.after, false)
end

Expand Down
9 changes: 9 additions & 0 deletions lua/leetcode/api/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local utils = require("leetcode.api.utils")
local log = require("leetcode.logger")
local queries = require("leetcode.api.queries")
local problemlist = require("leetcode.cache.problemlist")
local urls = require("leetcode.api.urls")

local question = {}

Expand Down Expand Up @@ -69,4 +70,12 @@ function question.random(filters)
return q
end

---@param qid integer
---@param lang lc.lang
---@param cb function
function question.latest_submission(qid, lang, cb)
local url = urls.latest_submission:format(qid, lang)
utils.get(url, { callback = cb })
end

return question
1 change: 1 addition & 0 deletions lua/leetcode/api/urls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ urls.interpret = "/problems/%s/interpret_solution/"
urls.submit = "/problems/%s/submit/"
urls.run = "/problems/%s/interpret_solution/"
urls.check = "/submissions/detail/%s/check/"
urls.latest_submission = "/submissions/latest/?qid=%s&lang=%s"

return urls
33 changes: 29 additions & 4 deletions lua/leetcode/command/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,38 @@ function cmd.reset()
if not q then return end

local snip = q:get_snippet()
if not snip then return end

if vim.api.nvim_buf_is_valid(q.bufnr) then
if snip and vim.api.nvim_buf_is_valid(q.bufnr) then
vim.api.nvim_buf_set_lines(q.bufnr, 0, -1, false, vim.split(snip, "\n"))
end
end

function cmd.last_submit()
local utils = require("leetcode.utils")
utils.auth_guard()
local q = utils.curr_question()
if not q then return end

local question_api = require("leetcode.api.question")
question_api.latest_submission(q.q.id, q.lang, function(res, err) --
if err then
if err.status == 404 then
log.error("You haven't submitted any code!")
else
log.err(err)
end

return
end

if type(res) == "table" and res.code and vim.api.nvim_buf_is_valid(q.bufnr) then
local code = q:injector(res.code)
vim.api.nvim_buf_set_lines(q.bufnr, 0, -1, false, vim.split(code, "\n"))
else
log.error("Something went wrong")
end
end)
end

function cmd.fix()
require("leetcode.cache.cookie").delete()
require("leetcode.cache.problemlist").delete()
Expand Down Expand Up @@ -411,7 +436,7 @@ cmd.commands = {
yank = { cmd.yank },
open = { cmd.open },
reset = { cmd.reset },
last_submission = { cmd.last_submission },
last_submit = { cmd.last_submit },

list = {
cmd.problems,
Expand Down

0 comments on commit fadcc49

Please # to comment.