Skip to content

Commit 41c2659

Browse files
committed
feat(hooks): add hook system for post-file creation actions
- Implement hooks functionality in `lua/scratch/api.lua` - Add `hooks` field to the configuration in `lua/scratch/config.lua` - Update README.md with an example of using hooks
1 parent f93937f commit 41c2659

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ return {
6464
},
6565
},
6666
},
67+
hooks = {
68+
{
69+
callback = function()
70+
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "hello", "world" })
71+
end,
72+
},
73+
},
6774
})
6875
end,
6976
event = "VeryLazy",

Diff for: lua/scratch/api.lua

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local config = require("scratch.config")
22
local slash = require("scratch.utils").Slash()
33
local utils = require("scratch.utils")
44
local telescope_status, telescope_builtin = pcall(require, "telescope.builtin")
5+
local Hooks = require("scratch.hooks")
56
local MANUAL_INPUT_OPTION = "MANUAL_INPUT"
67

78
---@class Scratch.ActionOpts
@@ -24,6 +25,11 @@ local function create_and_edit_file(abs_path, opts)
2425
else
2526
vim.api.nvim_command(cmd .. " " .. abs_path)
2627
end
28+
29+
local hooks = Hooks.get_hooks(vim.g.scratch_config.hooks, Hooks.trigger_points.AFTER)
30+
for _, hook in ipairs(hooks) do
31+
hook.callback()
32+
end
2733
end
2834

2935
local function write_lines_to_buffer(lines)

Diff for: lua/scratch/config.lua

+2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ local utils = require("scratch.utils")
4343
---@field file_picker? "fzflua" | "telescope" | nil
4444
---@field filetype_details Scratch.FiletypeDetails
4545
---@field localKeys Scratch.LocalKeyConfig[]
46+
---@field hooks Scratch.Hook[]
4647
local default_config = {
4748
scratch_file_dir = vim.fn.stdpath("cache") .. slash .. "scratch.nvim", -- where your scratch files will be put
4849
filetypes = { "lua", "js", "py", "sh" }, -- you can simply put filetype here
4950
window_cmd = "edit", -- 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit'
5051
file_picker = "fzflua",
5152
filetype_details = {},
5253
localKeys = {},
54+
hooks = {},
5355
}
5456

5557
---@type Scratch.Config

Diff for: lua/scratch/hooks.lua

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local M = {}
2+
3+
---@class Scratch.Hook
4+
---@field callback fun(param: table?)
5+
---@field name? string
6+
---@field trigger_point? string
7+
8+
M.trigger_points = {
9+
AFTER = "AFTER",
10+
}
11+
12+
---@param hooks Scratch.Hook[]
13+
---@param target_trigger_point? string
14+
---@return Scratch.Hook[]
15+
M.get_hooks = function(hooks, target_trigger_point)
16+
local matching_hooks = {}
17+
for _, hook in ipairs(hooks) do
18+
local matches = false
19+
local trigger_point = hook.trigger_point or M.trigger_points.AFTER
20+
21+
if trigger_point == target_trigger_point then
22+
matches = true
23+
end
24+
25+
if matches then
26+
table.insert(matching_hooks, hook)
27+
end
28+
end
29+
return matching_hooks
30+
end
31+
32+
return M

0 commit comments

Comments
 (0)