A Neovim plugin enabling you to solve LeetCode problems.
refactor: hooks order
6 files changed, +13 -12
+2-0lua/leetcode-ui/popup/console/testcase.lua
| @@ -127,6 +127,8 @@ function Testcase:reset() | ||
| 127 | 127 | end |
| 128 | 128 | |
| 129 | 129 | function Testcase:append(input) |
| 130 | + -- pcall(vim.cmd.undojoin) | |
| 131 | + | |
| 130 | 132 | local s = vim.split(input, "\n", { trimempty = true }) |
| 131 | 133 | |
| 132 | 134 | local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, true) |
+3-3lua/leetcode-ui/question.lua
| @@ -47,7 +47,7 @@ function Question:reset_lines() | ||
| 47 | 47 | local new_lines = self:snippet(true) or "" |
| 48 | 48 | |
| 49 | 49 | vim.schedule(function() -- |
| 50 | - log.info("Previous code found, resetting…\nTo undo, simply press `u`") | |
| 50 | + log.info("Previous code found and reseted\nTo undo, simply press `u`") | |
| 51 | 51 | end) |
| 52 | 52 | |
| 53 | 53 | self:set_lines(new_lines) |
| @@ -105,7 +105,7 @@ function Question:open_buffer(existed, loaded) | ||
| 105 | 105 | end |
| 106 | 106 | |
| 107 | 107 | if not loaded then |
| 108 | - utils.exec_hook("question_enter", self) | |
| 108 | + utils.exec_hooks("question_enter", self) | |
| 109 | 109 | self:autocmds() |
| 110 | 110 | end |
| 111 | 111 | end |
| @@ -183,7 +183,7 @@ local group = vim.api.nvim_create_augroup("leetcode_questions", { clear = true } | ||
| 183 | 183 | function Question:autocmds() |
| 184 | 184 | vim.api.nvim_create_autocmd("WinClosed", { |
| 185 | 185 | group = group, |
| 186 | - buffer = self.bufnr, | |
| 186 | + pattern = tostring(self.winid), | |
| 187 | 187 | callback = function() self:unmount() end, |
| 188 | 188 | }) |
| 189 | 189 | end |
+1-1lua/leetcode.lua
| @@ -58,7 +58,7 @@ function leetcode.start(on_vimenter) | ||
| 58 | 58 | Menu():mount() |
| 59 | 59 | |
| 60 | 60 | local utils = require("leetcode.utils") |
| 61 | - utils.exec_hook("enter") | |
| 61 | + utils.exec_hooks("enter") | |
| 62 | 62 | |
| 63 | 63 | return true |
| 64 | 64 | end |
+1-1lua/leetcode/api/interpreter.lua
| @@ -50,7 +50,7 @@ function interpreter.listener(id, callback) | ||
| 50 | 50 | item = interpreter:handle_item(item) |
| 51 | 51 | callback(item) |
| 52 | 52 | else -- still judging |
| 53 | - local intervals = config.auth.is_premium and { 500, 500 } or { 500, 1000 } | |
| 53 | + local intervals = config.auth.is_premium and { 450, 450 } or { 450, 900 } | |
| 54 | 54 | local interval = item.sate == "STARTED" and intervals[2] or intervals[1] |
| 55 | 55 | callback(nil, item.state) |
| 56 | 56 | vim.defer_fn(listen, interval) |
+2-2lua/leetcode/config/hooks.lua
| @@ -2,11 +2,11 @@ | ||
| 2 | 2 | local hooks = {} |
| 3 | 3 | |
| 4 | 4 | hooks["question_enter"] = { |
| 5 | - function(q) | |
| 5 | + vim.schedule_wrap(function(q) | |
| 6 | 6 | -- https://github.com/kawre/leetcode.nvim/issues/14 |
| 7 | 7 | if q.lang ~= "rust" then return end |
| 8 | 8 | pcall(function() require("rust-tools.standalone").start_standalone_client() end) |
| 9 | - end, | |
| 9 | + end), | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | return hooks |
+4-5lua/leetcode/utils.lua
| @@ -74,22 +74,21 @@ end | ||
| 74 | 74 | ---@param event lc.hook |
| 75 | 75 | ---@return fun()[]|nil |
| 76 | 76 | function utils.get_hooks(event) |
| 77 | - local defaults = config.hooks[event] or {} | |
| 78 | 77 | local fns = config.user.hooks[event] |
| 79 | - | |
| 80 | 78 | if not fns then return end |
| 81 | 79 | |
| 82 | 80 | if type(fns) == "function" then fns = { fns } end |
| 83 | - return vim.list_extend(defaults, fns) | |
| 81 | + | |
| 82 | + return vim.list_extend(fns, config.hooks[event] or {}) | |
| 84 | 83 | end |
| 85 | 84 | |
| 86 | 85 | ---@param event lc.hook |
| 87 | -function utils.exec_hook(event, ...) | |
| 86 | +function utils.exec_hooks(event, ...) | |
| 88 | 87 | local fns = utils.get_hooks(event) |
| 89 | 88 | if not fns then return log.error("unknown hook event: " .. event) end |
| 90 | 89 | |
| 91 | 90 | for i, fn in ipairs(fns) do |
| 92 | - local ok, msg = pcall(vim.schedule_wrap(fn), ...) | |
| 91 | + local ok, msg = pcall(fn, ...) | |
| 93 | 92 | if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end |
| 94 | 93 | end |
| 95 | 94 | end |