irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(config): custom hooks

kawreauthored
parent 315001acommit 05806f6b5199687243d8d47118fd32b5e97dd942Browse files

5 files changed, +45 -12

+3-0lua/leetcode.lua
@@ -52,6 +52,9 @@ function leetcode.start()
5252
5353 leetcode.setup_cmds()
5454
55+ local utils = require("leetcode.utils")
56+ utils.exec_hooks("LeetEnter")
57+
5558 local theme = require("leetcode.theme")
5659 theme.setup()
5760
+10-10lua/leetcode/api/utils.lua
@@ -49,6 +49,7 @@ function utils.curl(method, url, params)
4949 if type(params.body) == "table" then params.body = vim.json.encode(params.body) end
5050
5151 local tries = params.retry
52+
5253 if params.callback then
5354 local cb = vim.schedule_wrap(params.callback)
5455 params.callback = function(out, _)
@@ -64,15 +65,15 @@ function utils.curl(method, url, params)
6465
6566 curl[method](url, params)
6667 else
67- local res, err
68-
69- repeat
70- local out = curl[method](url, params)
71- res, err = utils.handle_res(out)
72- tries = tries - 1
73- until not (res and not err) or tries > 0
74-
75- return res, err
68+ local out = curl[method](url, params)
69+ local res, err = utils.handle_res(out)
70+
71+ if err and tries > 0 then
72+ params_cpy.retry = tries - 1
73+ utils.curl(method, url, params_cpy)
74+ else
75+ return res, err
76+ end
7677 end
7778 end
7879
@@ -99,7 +100,6 @@ function utils.handle_res(out)
99100 res = utils.decode(out.body)
100101 end
101102
102- -- if err then log.debug(err) end
103103 return res, err
104104 end
105105
+10-0lua/leetcode/config/template.lua
@@ -35,6 +35,10 @@
3535 ---| "com"
3636 ---| "cn"
3737
38+---@alias lc.hook
39+---| "LeetEnter"
40+---| "LeetQuestionEnter"
41+
3842 ---@class lc.UserConfig
3943 local M = {
4044 ---@type lc.domain
@@ -81,6 +85,12 @@ local M = {
8185
8286 width = "40%", ---@type string | integer
8387 },
88+
89+ ---@type table<lc.hook, function[]>
90+ hooks = {
91+ LeetEnter = {},
92+ LeetQuestionEnter = {},
93+ },
8494 }
8595
8696 return M
+8-2lua/leetcode/ui/question.lua
@@ -50,6 +50,10 @@ function Question:mount()
5050 vim.api.nvim_set_current_dir(config.home:absolute())
5151 vim.cmd("$tabe " .. self.file:absolute())
5252
53+ utils.exec_hooks("LeetQuestionEnter", {
54+ lang = self.lang,
55+ })
56+
5357 -- https://github.com/kawre/leetcode.nvim/issues/14
5458 if self.lang == "rust" then
5559 pcall(function() require("rust-tools.standalone").start_standalone_client() end)
@@ -92,14 +96,16 @@ function Question:init(problem)
9296 return log.warn("Question is for premium users only")
9397 end
9498
95- local lang = utils.get_lang(is_sql(q) and config.sql or config.lang)
99+ -- local lang = utils.get_lang(is_sql(q) and config.sql or config.lang)
96100
97101 self = setmetatable({
98102 q = q,
99- lang = lang.slug,
100103 cache = problem,
104+ is_sql = q.meta_data.database and true or false,
101105 }, self)
102106
107+ self.lang = self.is_sql and config.sql or config.lang
108+
103109 return self:handle_mount()
104110 end
105111
+14-0lua/leetcode/utils.lua
@@ -111,4 +111,18 @@ function utils.get_lang(slug)
111111 return vim.tbl_filter(function(l) return l.slug == slug end, config.langs)[1] or {}
112112 end
113113
114+---@param event lc.hook
115+function utils.exec_hooks(event, ...)
116+ local log = require("leetcode.logger")
117+ local fns = config.user.hooks[event]
118+ if not fns then log.error("Unknown hook event: " .. event) end
119+
120+ for i, fn in ipairs(fns) do
121+ local ok, msg = pcall(fn, ...)
122+ if not ok then
123+ log.error(("Error executing hook index `%d` in `%s` event: %s"):format(i, event, msg))
124+ end
125+ end
126+end
127+
114128 return utils