Skip to content

Commit

Permalink
refactor(hooks)!: rename hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 24, 2024
1 parent 910bff9 commit f10ef24
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat

hooks = {
---@type fun()[]
LeetEnter = {},
["enter"] = {},

---@type fun(question: lc.ui.Question)[]
LeetQuestionNew = {},
["question_enter"] = {},
},

keys = {
Expand Down Expand Up @@ -258,10 +258,10 @@ List of functions that get executed on specified event
```lua
hooks = {
---@type fun()[]
LeetEnter = {},
["enter"] = {},

---@type fun(question: lc.ui.Question)[]
LeetQuestionNew = {},
["question_enter"] = {},
},
```

Expand Down
8 changes: 4 additions & 4 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-1

hooks = {
---@type fun()[]
LeetEnter = {},
["enter"] = {},

---@type fun(question: lc.ui.Question)[]
LeetQuestionNew = {},
["question_enter"] = {},
},

keys = {
Expand Down Expand Up @@ -260,10 +260,10 @@ injector = { ---@type table<lc.lang, lc.inject>
```lua
hooks = {
---@type fun()[]
LeetEnter = {},
["enter"] = {},

---@type fun(question: lc.ui.Question)[]
LeetQuestionNew = {},
["question_enter"] = {},
},
```

Expand Down
4 changes: 2 additions & 2 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function Question:handle_mount()
self.console = Console(self)
self.info = Info(self)

utils.exec_hook("LeetQuestionNew", self)
utils.exec_hook("question_enter", self)

return self
end
Expand Down Expand Up @@ -209,7 +209,7 @@ Question.change_lang = vim.schedule_wrap(function(self, lang)

self.bufnr = new_bufnr
if bufloaded == 0 then --
utils.exec_hook("LeetQuestionNew", self)
utils.exec_hook("question_enter", self)
end
else
log.error("Changing language failed")
Expand Down
2 changes: 1 addition & 1 deletion lua/leetcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function leetcode.start(on_vimenter)
Menu():mount()

local utils = require("leetcode.utils")
utils.exec_hook("LeetEnter")
utils.exec_hook("enter")

return true
end
Expand Down
8 changes: 4 additions & 4 deletions lua/leetcode/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
---| "bash"

---@alias lc.hook
---| "LeetEnter"
---| "LeetQuestionNew"
---| "enter"
---| "question_enter"

---@alias lc.size
---| string
Expand Down Expand Up @@ -99,10 +99,10 @@ local M = {

hooks = {
---@type fun()[]
LeetEnter = {},
["enter"] = {},

---@type fun(question: lc.ui.Question)[]
LeetQuestionNew = {},
["question_open"] = {},
},

keys = {
Expand Down
2 changes: 2 additions & 0 deletions lua/leetcode/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function utils.exec_hook(event, ...)
local fns = config.user.hooks[event]
if not fns then log.error("unknown hook event: " .. event) end

if type(fns) == "function" then fns = { fns } end

for i, fn in ipairs(fns) do
local ok, msg = pcall(vim.schedule_wrap(fn), ...)
if not ok then log.error(("bad hook #%d in `%s` event: %s"):format(i, event, msg)) end
Expand Down

0 comments on commit f10ef24

Please sign in to comment.