irongit

A Neovim plugin enabling you to solve LeetCode problems.

refactor: hooks order

kawreauthored
parent ece6c71commit 05a8e733aa3f2266642e30ca1e6dc9ce3a178a1dBrowse files

6 files changed, +13 -12

+2-0lua/leetcode-ui/popup/console/testcase.lua
@@ -127,6 +127,8 @@ function Testcase:reset()
127127 end
128128
129129 function Testcase:append(input)
130+ -- pcall(vim.cmd.undojoin)
131+
130132 local s = vim.split(input, "\n", { trimempty = true })
131133
132134 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()
4747 local new_lines = self:snippet(true) or ""
4848
4949 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`")
5151 end)
5252
5353 self:set_lines(new_lines)
@@ -105,7 +105,7 @@ function Question:open_buffer(existed, loaded)
105105 end
106106
107107 if not loaded then
108- utils.exec_hook("question_enter", self)
108+ utils.exec_hooks("question_enter", self)
109109 self:autocmds()
110110 end
111111 end
@@ -183,7 +183,7 @@ local group = vim.api.nvim_create_augroup("leetcode_questions", { clear = true }
183183 function Question:autocmds()
184184 vim.api.nvim_create_autocmd("WinClosed", {
185185 group = group,
186- buffer = self.bufnr,
186+ pattern = tostring(self.winid),
187187 callback = function() self:unmount() end,
188188 })
189189 end
+1-1lua/leetcode.lua
@@ -58,7 +58,7 @@ function leetcode.start(on_vimenter)
5858 Menu():mount()
5959
6060 local utils = require("leetcode.utils")
61- utils.exec_hook("enter")
61+ utils.exec_hooks("enter")
6262
6363 return true
6464 end
+1-1lua/leetcode/api/interpreter.lua
@@ -50,7 +50,7 @@ function interpreter.listener(id, callback)
5050 item = interpreter:handle_item(item)
5151 callback(item)
5252 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 }
5454 local interval = item.sate == "STARTED" and intervals[2] or intervals[1]
5555 callback(nil, item.state)
5656 vim.defer_fn(listen, interval)
+2-2lua/leetcode/config/hooks.lua
@@ -2,11 +2,11 @@
22 local hooks = {}
33
44 hooks["question_enter"] = {
5- function(q)
5+ vim.schedule_wrap(function(q)
66 -- https://github.com/kawre/leetcode.nvim/issues/14
77 if q.lang ~= "rust" then return end
88 pcall(function() require("rust-tools.standalone").start_standalone_client() end)
9- end,
9+ end),
1010 }
1111
1212 return hooks
+4-5lua/leetcode/utils.lua
@@ -74,22 +74,21 @@ end
7474 ---@param event lc.hook
7575 ---@return fun()[]|nil
7676 function utils.get_hooks(event)
77- local defaults = config.hooks[event] or {}
7877 local fns = config.user.hooks[event]
79-
8078 if not fns then return end
8179
8280 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 {})
8483 end
8584
8685 ---@param event lc.hook
87-function utils.exec_hook(event, ...)
86+function utils.exec_hooks(event, ...)
8887 local fns = utils.get_hooks(event)
8988 if not fns then return log.error("unknown hook event: " .. event) end
9089
9190 for i, fn in ipairs(fns) do
92- local ok, msg = pcall(vim.schedule_wrap(fn), ...)
91+ local ok, msg = pcall(fn, ...)
9392 if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end
9493 end
9594 end