A Neovim plugin enabling you to solve LeetCode problems.
feat: improve judge
4 files changed, +71 -81
+27-61lua/leetcode/api/interpreter.lua
| @@ -1,21 +1,14 @@ | ||
| 1 | 1 | local log = require("leetcode.logger") |
| 2 | 2 | local urls = require("leetcode.api.urls") |
| 3 | 3 | |
| 4 | +local config = require("leetcode.config") | |
| 4 | 5 | local utils = require("leetcode.api.utils") |
| 5 | -local spinner = require("leetcode.logger.spinner") | |
| 6 | 6 | |
| 7 | 7 | local t = require("leetcode.translator") |
| 8 | 8 | |
| 9 | 9 | ---@class lc.Interpreter |
| 10 | 10 | local interpreter = {} |
| 11 | 11 | |
| 12 | -local check_state = { | |
| 13 | - ["PENDING"] = "Pending…", | |
| 14 | - ["STARTED"] = "Judging…", | |
| 15 | - ["SUCCESS"] = "Finished", | |
| 16 | - ["FAILURE"] = "Failed", -- CODE: 16 | |
| 17 | -} | |
| 18 | - | |
| 19 | 12 | ---@param item lc.interpreter_response |
| 20 | 13 | --- |
| 21 | 14 | ---@return lc.interpreter_response |
| @@ -49,31 +42,19 @@ end | ||
| 49 | 42 | ---@param id string |
| 50 | 43 | ---@param callback function |
| 51 | 44 | function interpreter.listener(id, callback) |
| 52 | - local noti = spinner:init(check_state["PENDING"], "points") | |
| 53 | - | |
| 54 | 45 | local function listen() |
| 55 | 46 | interpreter.check(id, function(item, err) |
| 56 | - if err then | |
| 57 | - callback(false) | |
| 58 | - return noti:stop(err.msg, false) | |
| 59 | - end | |
| 60 | - | |
| 61 | - if item.status_code then | |
| 47 | + if err then -- error | |
| 48 | + callback(nil, nil, err) | |
| 49 | + elseif item.status_code then -- got results | |
| 62 | 50 | item = interpreter:handle_item(item) |
| 63 | - noti:stop(item.status_msg, item._.success) | |
| 64 | - return callback(item) | |
| 51 | + callback(item) | |
| 52 | + else -- still judging | |
| 53 | + local intervals = config.auth.is_premium and { 500, 500 } or { 500, 1000 } | |
| 54 | + local interval = item.sate == "STARTED" and intervals[2] or intervals[1] | |
| 55 | + callback(nil, item.state) | |
| 56 | + vim.defer_fn(listen, interval) | |
| 65 | 57 | end |
| 66 | - | |
| 67 | - local interval = 500 | |
| 68 | - noti:update(check_state[item.state]) | |
| 69 | - if item.state == "PENDING" then | |
| 70 | - noti:change("points") | |
| 71 | - elseif item.state == "STARTED" then | |
| 72 | - noti:change("dot") | |
| 73 | - interval = 1000 | |
| 74 | - end | |
| 75 | - | |
| 76 | - vim.defer_fn(listen, interval) | |
| 77 | 58 | end) |
| 78 | 59 | end |
| 79 | 60 | |
| @@ -85,32 +66,27 @@ end | ||
| 85 | 66 | ---@field typed_code string |
| 86 | 67 | ---@field data_input string |
| 87 | 68 | |
| 88 | ----@param title_slug string | |
| 89 | ----@param body lc.Interpret.body|string | |
| 90 | ----@param callback function | |
| 91 | -function interpreter.interpret_solution(title_slug, body, callback) | |
| 92 | - local url = urls.interpret:format(title_slug) | |
| 93 | - | |
| 94 | - interpreter.fetch(url, { | |
| 95 | - body = body, | |
| 96 | - callback = function(res, success) | |
| 97 | - callback(success) | |
| 98 | - if success then interpreter.listener(res.interpret_id, callback) end | |
| 99 | - end, | |
| 100 | - }) | |
| 101 | -end | |
| 102 | - | |
| 103 | ----@param title_slug string | |
| 69 | +---@param submit boolean | |
| 70 | +---@param q lc.ui.Question | |
| 104 | 71 | ---@param body lc.Interpret.body|string |
| 105 | 72 | ---@param callback function |
| 106 | -function interpreter.submit(title_slug, body, callback) | |
| 107 | - local url = urls.submit:format(title_slug) | |
| 73 | +function interpreter.run(submit, q, body, callback) | |
| 74 | + local url = (submit and urls.submit or urls.interpret):format(q.q.title_slug) | |
| 108 | 75 | |
| 109 | 76 | interpreter.fetch(url, { |
| 110 | 77 | body = body, |
| 111 | - callback = function(res, success) | |
| 112 | - callback(success) | |
| 113 | - if success then interpreter.listener(res.submission_id, callback) end | |
| 78 | + callback = function(res, err) | |
| 79 | + if err then | |
| 80 | + if err.status == 429 then | |
| 81 | + err.msg = "You have attempted to run code too soon" | |
| 82 | + err.lvl = vim.log.levels.WARN | |
| 83 | + end | |
| 84 | + callback(nil, nil, err) | |
| 85 | + else | |
| 86 | + q.console.result:clear() | |
| 87 | + local id = submit and res.submission_id or res.interpret_id | |
| 88 | + interpreter.listener(id, callback) | |
| 89 | + end | |
| 114 | 90 | end, |
| 115 | 91 | }) |
| 116 | 92 | end |
| @@ -127,17 +103,7 @@ end | ||
| 127 | 103 | function interpreter.fetch(url, opts) |
| 128 | 104 | utils.post(url, { |
| 129 | 105 | body = opts.body, |
| 130 | - callback = function(res, err) | |
| 131 | - if err then | |
| 132 | - if err.status == 429 then | |
| 133 | - err.msg = "You have attempted to run code too soon" | |
| 134 | - err.lvl = vim.log.levels.WARN | |
| 135 | - end | |
| 136 | - opts.callback(log.err(err), false) | |
| 137 | - else | |
| 138 | - opts.callback(res, true) | |
| 139 | - end | |
| 140 | - end, | |
| 106 | + callback = opts.callback, | |
| 141 | 107 | }) |
| 142 | 108 | end |
| 143 | 109 |
+0-0lua/leetcode/logger/spinner.lua → lua/leetcode/logger/spinner/init.lua
No content changes (mode or rename only).
+30-0lua/leetcode/logger/spinner/judge.lua
| @@ -0,0 +1,30 @@ | ||
| 1 | +local Spinner = require("leetcode.logger.spinner") | |
| 2 | + | |
| 3 | +---@class lc.Judge : lc.Spinner | |
| 4 | +local Judge = {} | |
| 5 | +Judge.__index = Judge | |
| 6 | +setmetatable(Judge, Spinner) | |
| 7 | + | |
| 8 | +local check_state = { | |
| 9 | + ["PENDING"] = "Pending…", | |
| 10 | + ["STARTED"] = "Judging…", | |
| 11 | + ["SUCCESS"] = "Finished", | |
| 12 | + ["FAILURE"] = "Failed", -- CODE: 16 | |
| 13 | +} | |
| 14 | + | |
| 15 | +function Judge:from_state(state) | |
| 16 | + self:update(check_state[state]) | |
| 17 | + | |
| 18 | + if state == "PENDING" then | |
| 19 | + self:change("points") | |
| 20 | + else | |
| 21 | + self:change("dot") | |
| 22 | + end | |
| 23 | +end | |
| 24 | + | |
| 25 | +function Judge:init() | |
| 26 | + local spinner = Spinner:init(check_state["PENDING"], "points") | |
| 27 | + return setmetatable(spinner, Judge) | |
| 28 | +end | |
| 29 | + | |
| 30 | +return Judge |
+14-20lua/leetcode/runner/init.lua
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | local log = require("leetcode.logger") |
| 2 | 2 | local interpreter = require("leetcode.api.interpreter") |
| 3 | 3 | local config = require("leetcode.config") |
| 4 | +local Judge = require("leetcode.logger.spinner.judge") | |
| 4 | 5 | |
| 5 | 6 | ---@type Path |
| 6 | 7 | local leetbody = config.storage.cache:joinpath("body") |
| @@ -38,30 +39,23 @@ function Runner:handle(submit) | ||
| 38 | 39 | data_input = not submit and table.concat(question.console.testcase:content(), "\n") or nil, |
| 39 | 40 | } |
| 40 | 41 | |
| 41 | - local function callback(item) | |
| 42 | - if type(item) == "boolean" then | |
| 43 | - if item then | |
| 44 | - question.console.result:clear() | |
| 45 | - else | |
| 46 | - self:stop() | |
| 47 | - end | |
| 48 | - else | |
| 49 | - self:callback(item) | |
| 42 | + local judge = Judge:init() | |
| 43 | + local function callback(item, state, err) | |
| 44 | + if err or item then self:stop() end | |
| 45 | + | |
| 46 | + if item then | |
| 47 | + judge:stop(item.status_msg, item._.success) | |
| 48 | + elseif state then | |
| 49 | + judge:from_state(state) | |
| 50 | + elseif err then | |
| 51 | + judge:stop(err.msg or "Something went wrong", false) | |
| 50 | 52 | end |
| 51 | - end | |
| 52 | 53 | |
| 53 | - leetbody:write(vim.json.encode(body), "w") | |
| 54 | - if not submit then | |
| 55 | - interpreter.interpret_solution(question.q.title_slug, leetbody:absolute(), callback) | |
| 56 | - else | |
| 57 | - interpreter.submit(question.q.title_slug, leetbody:absolute(), callback) | |
| 54 | + if item then question.console.result:handle(item) end | |
| 58 | 55 | end |
| 59 | -end | |
| 60 | 56 | |
| 61 | ----@param item lc.interpreter_response | |
| 62 | -function Runner:callback(item) | |
| 63 | - self:stop() | |
| 64 | - self.question.console.result:handle(item) | |
| 57 | + leetbody:write(vim.json.encode(body), "w") | |
| 58 | + interpreter.run(submit, question, leetbody:absolute(), callback) | |
| 65 | 59 | end |
| 66 | 60 | |
| 67 | 61 | ---@param question lc.ui.Question |