Skip to content

Commit

Permalink
feat(hooks): add hook system for post-file creation actions
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
LintaoAmons committed Oct 8, 2024
1 parent f93937f commit 41c2659
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ return {
},
},
},
hooks = {
{
callback = function()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "hello", "world" })
end,
},
},
})
end,
event = "VeryLazy",
Expand Down
6 changes: 6 additions & 0 deletions lua/scratch/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local config = require("scratch.config")
local slash = require("scratch.utils").Slash()
local utils = require("scratch.utils")
local telescope_status, telescope_builtin = pcall(require, "telescope.builtin")
local Hooks = require("scratch.hooks")
local MANUAL_INPUT_OPTION = "MANUAL_INPUT"

---@class Scratch.ActionOpts
Expand All @@ -24,6 +25,11 @@ local function create_and_edit_file(abs_path, opts)
else
vim.api.nvim_command(cmd .. " " .. abs_path)
end

local hooks = Hooks.get_hooks(vim.g.scratch_config.hooks, Hooks.trigger_points.AFTER)
for _, hook in ipairs(hooks) do
hook.callback()
end
end

local function write_lines_to_buffer(lines)
Expand Down
2 changes: 2 additions & 0 deletions lua/scratch/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ local utils = require("scratch.utils")
---@field file_picker? "fzflua" | "telescope" | nil
---@field filetype_details Scratch.FiletypeDetails
---@field localKeys Scratch.LocalKeyConfig[]
---@field hooks Scratch.Hook[]
local default_config = {
scratch_file_dir = vim.fn.stdpath("cache") .. slash .. "scratch.nvim", -- where your scratch files will be put
filetypes = { "lua", "js", "py", "sh" }, -- you can simply put filetype here
window_cmd = "edit", -- 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit'
file_picker = "fzflua",
filetype_details = {},
localKeys = {},
hooks = {},
}

---@type Scratch.Config
Expand Down
32 changes: 32 additions & 0 deletions lua/scratch/hooks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local M = {}

---@class Scratch.Hook
---@field callback fun(param: table?)
---@field name? string
---@field trigger_point? string

M.trigger_points = {
AFTER = "AFTER",
}

---@param hooks Scratch.Hook[]
---@param target_trigger_point? string
---@return Scratch.Hook[]
M.get_hooks = function(hooks, target_trigger_point)
local matching_hooks = {}
for _, hook in ipairs(hooks) do
local matches = false
local trigger_point = hook.trigger_point or M.trigger_points.AFTER

if trigger_point == target_trigger_point then
matches = true
end

if matches then
table.insert(matching_hooks, hook)
end
end
return matching_hooks
end

return M

0 comments on commit 41c2659

Please sign in to comment.