A Neovim plugin enabling you to solve LeetCode problems.
fix: crash on `q_close_all`
5 files changed, +54 -21
+2-2lua/leetcode/api/utils.lua
| @@ -203,8 +203,8 @@ end | ||
| 203 | 203 | ---@param titles { questionId: integer, title: string }[] |
| 204 | 204 | function utils.translate_titles(problems, titles) |
| 205 | 205 | local map = {} |
| 206 | - for _, t in ipairs(titles) do | |
| 207 | - map[t.questionId] = t.title | |
| 206 | + for _, title in ipairs(titles) do | |
| 207 | + map[title.questionId] = title.title | |
| 208 | 208 | end |
| 209 | 209 | |
| 210 | 210 | return vim.tbl_map(function(p) |
+3-3lua/leetcode/cache/cookie.lua
| @@ -30,7 +30,7 @@ function Cookie.set(str) | ||
| 30 | 30 | local _, aerr = auth_api.user() |
| 31 | 31 | |
| 32 | 32 | if aerr then |
| 33 | - Cookie.delete() | |
| 33 | + require("leetcode.command").delete_cookie() | |
| 34 | 34 | return false |
| 35 | 35 | else |
| 36 | 36 | return true |
| @@ -55,13 +55,13 @@ function Cookie.get() | ||
| 55 | 55 | |
| 56 | 56 | local contents = file:read() |
| 57 | 57 | if not contents or type(contents) ~= "string" then |
| 58 | - Cookie.delete() | |
| 58 | + require("leetcode.command").delete_cookie() | |
| 59 | 59 | return |
| 60 | 60 | end |
| 61 | 61 | |
| 62 | 62 | local cookie = Cookie.parse(contents) |
| 63 | 63 | if not cookie then |
| 64 | - Cookie.delete() | |
| 64 | + require("leetcode.command").delete_cookie() | |
| 65 | 65 | return |
| 66 | 66 | end |
| 67 | 67 |
+22-8lua/leetcode/command/init.lua
| @@ -2,6 +2,7 @@ local log = require("leetcode.logger") | ||
| 2 | 2 | local arguments = require("leetcode.command.arguments") |
| 3 | 3 | local config = require("leetcode.config") |
| 4 | 4 | local t = require("leetcode.translator") |
| 5 | +local event = require("nui.utils.autocmd").event | |
| 5 | 6 | |
| 6 | 7 | ---@class lc.Commands |
| 7 | 8 | local cmd = {} |
| @@ -12,10 +13,16 @@ function cmd.deprecate(old_name, new_name) | ||
| 12 | 13 | log.warn(("`%s` is deprecated, use `%s` instead."):format(old_name, new_name)) |
| 13 | 14 | end |
| 14 | 15 | |
| 15 | -function cmd.cache_update() require("leetcode.cache").update() end | |
| 16 | +function cmd.cache_update() | |
| 17 | + require("leetcode.utils").auth_guard() | |
| 18 | + | |
| 19 | + require("leetcode.cache").update() | |
| 20 | +end | |
| 16 | 21 | |
| 17 | 22 | ---@param options table<string, string[]> |
| 18 | 23 | function cmd.problems(options) |
| 24 | + require("leetcode.utils").auth_guard() | |
| 25 | + | |
| 19 | 26 | local async = require("plenary.async") |
| 20 | 27 | local problems = require("leetcode.cache.problemlist") |
| 21 | 28 | |
| @@ -65,8 +72,10 @@ function cmd.cookie_prompt(cb) | ||
| 65 | 72 | end, |
| 66 | 73 | }) |
| 67 | 74 | |
| 68 | - input:map("n", { "<Esc>", "q" }, function() input:unmount() end) | |
| 69 | 75 | input:mount() |
| 76 | + | |
| 77 | + input:map("n", { "<Esc>", "q" }, function() input:unmount() end) | |
| 78 | + input:on(event.BufLeave, function() input:unmount() end) | |
| 70 | 79 | end |
| 71 | 80 | |
| 72 | 81 | function cmd.sign_out() |
| @@ -78,6 +87,7 @@ end | ||
| 78 | 87 | |
| 79 | 88 | ---Sign out |
| 80 | 89 | function cmd.delete_cookie() |
| 90 | + config.auth = {} | |
| 81 | 91 | local cookie = require("leetcode.cache.cookie") |
| 82 | 92 | cookie.delete() |
| 83 | 93 | end |
| @@ -97,12 +107,12 @@ function cmd.expire() | ||
| 97 | 107 | |
| 98 | 108 | cmd.cookie_prompt(function(success) |
| 99 | 109 | if success then |
| 100 | - if vim.api.nvim_tabpage_is_valid(tabp) then vim.cmd.tabnext(tabp) end | |
| 110 | + if vim.api.nvim_tabpage_is_valid(tabp) then pcall(vim.cmd.tabnext, tabp) end | |
| 101 | 111 | log.info("Successful re-login") |
| 102 | 112 | else |
| 103 | 113 | cmd.delete_cookie() |
| 104 | 114 | cmd.menu_layout("signin") |
| 105 | - cmd.q_close_all() | |
| 115 | + vim.schedule(cmd.q_close_all) | |
| 106 | 116 | end |
| 107 | 117 | end) |
| 108 | 118 | end |
| @@ -111,6 +121,8 @@ end | ||
| 111 | 121 | --- |
| 112 | 122 | --@param theme lc-db.Theme |
| 113 | 123 | function cmd.qot() |
| 124 | + require("leetcode.utils").auth_guard() | |
| 125 | + | |
| 114 | 126 | local problems = require("leetcode.api.problems") |
| 115 | 127 | local Question = require("leetcode.ui.question") |
| 116 | 128 | |
| @@ -122,16 +134,16 @@ function cmd.qot() | ||
| 122 | 134 | end |
| 123 | 135 | |
| 124 | 136 | function cmd.random_question() |
| 137 | + require("leetcode.utils").auth_guard() | |
| 138 | + | |
| 125 | 139 | local problems = require("leetcode.cache.problemlist") |
| 126 | 140 | local question = require("leetcode.api.question") |
| 127 | 141 | |
| 128 | 142 | local q, err = question.random() |
| 129 | 143 | if err then return log.err(err) end |
| 130 | 144 | |
| 131 | - if q then | |
| 132 | - local item = problems.get_by_title_slug(q.title_slug) or {} | |
| 133 | - require("leetcode.ui.question"):init(item) | |
| 134 | - end | |
| 145 | + local item = problems.get_by_title_slug(q.title_slug) or {} | |
| 146 | + require("leetcode.ui.question"):init(item) | |
| 135 | 147 | end |
| 136 | 148 | |
| 137 | 149 | function cmd.menu() |
| @@ -175,12 +187,14 @@ end | ||
| 175 | 187 | |
| 176 | 188 | function cmd.q_run() |
| 177 | 189 | local utils = require("leetcode.utils") |
| 190 | + utils.auth_guard() | |
| 178 | 191 | local q = utils.curr_question() |
| 179 | 192 | if q then q.console:run() end |
| 180 | 193 | end |
| 181 | 194 | |
| 182 | 195 | function cmd.q_submit() |
| 183 | 196 | local utils = require("leetcode.utils") |
| 197 | + utils.auth_guard() | |
| 184 | 198 | local q = utils.curr_question() |
| 185 | 199 | if q then q.console:submit() end |
| 186 | 200 | end |
+17-8lua/leetcode/config/template.lua
| @@ -28,6 +28,15 @@ | ||
| 28 | 28 | ---| "LeetEnter" |
| 29 | 29 | ---| "LeetQuestionNew" |
| 30 | 30 | |
| 31 | +---@alias lc.size | |
| 32 | +---| string | |
| 33 | +---| number | |
| 34 | +---| { width: string | number, height: string | number } | |
| 35 | + | |
| 36 | +---@alias lc.position "top" | "right" | "bottom" | "left" | |
| 37 | + | |
| 38 | +---@alias lc.direction "col" | "row" | |
| 39 | + | |
| 31 | 40 | ---@class lc.UserConfig |
| 32 | 41 | local M = { |
| 33 | 42 | ---@type string |
| @@ -55,28 +64,28 @@ local M = { | ||
| 55 | 64 | console = { |
| 56 | 65 | open_on_runcode = true, ---@type boolean |
| 57 | 66 | |
| 58 | - dir = "row", ---@type "col" | "row" | |
| 67 | + dir = "row", ---@type lc.direction | |
| 59 | 68 | |
| 60 | - size = { | |
| 61 | - width = "90%", ---@type string | integer | |
| 62 | - height = "75%", ---@type string | integer | |
| 69 | + size = { ---@type lc.size | |
| 70 | + width = "90%", | |
| 71 | + height = "75%", | |
| 63 | 72 | }, |
| 64 | 73 | |
| 65 | 74 | result = { |
| 66 | - size = "60%", ---@type string | integer | |
| 75 | + size = "60%", ---@type lc.size | |
| 67 | 76 | }, |
| 68 | 77 | |
| 69 | 78 | testcase = { |
| 70 | 79 | virt_text = true, ---@type boolean |
| 71 | 80 | |
| 72 | - size = "40%", ---@type string | integer | |
| 81 | + size = "40%", ---@type lc.size | |
| 73 | 82 | }, |
| 74 | 83 | }, |
| 75 | 84 | |
| 76 | 85 | description = { |
| 77 | - position = "left", ---@type "top" | "right" | "bottom" | "left" | |
| 86 | + position = "left", ---@type lc.position | |
| 78 | 87 | |
| 79 | - width = "40%", ---@type string | integer | |
| 88 | + width = "40%", ---@type lc.size | |
| 80 | 89 | }, |
| 81 | 90 | |
| 82 | 91 | hooks = { |
+10-0lua/leetcode/utils.lua
| @@ -138,4 +138,14 @@ function utils.translate(content, translated_content) | ||
| 138 | 138 | end |
| 139 | 139 | end |
| 140 | 140 | |
| 141 | +function utils.auth_guard() | |
| 142 | + -- if not config.auth.is_signed_in then | |
| 143 | + -- local log = require("leetcode.logger") | |
| 144 | + -- log.error("User not logged-in") | |
| 145 | + -- return false | |
| 146 | + -- end | |
| 147 | + -- require("leetcode.logger").info(config.auth) | |
| 148 | + assert(config.auth.is_signed_in, "User not logged-in") | |
| 149 | +end | |
| 150 | + | |
| 141 | 151 | return utils |