Skip to content

Commit

Permalink
feat: add patch_func to monkey patch into an existing function
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Sep 19, 2024
1 parent e32c4a9 commit e7d7a35
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lua/astrocore/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,27 @@ function M.file_worktree(file, worktrees)
end
end

--- Monkey patch into an existing function
---
--- Example from `:h vim.paste()`
--- ```lua
--- local patch_func = require("astrocore").patch_func
--- vim.paste = patch_func(vim.paste, function(orig, lines, phase)
--- for i, line in ipairs(lines) do
--- -- Scrub ANSI color codes from paste input.
--- lines[i] = line:gsub('\27%[[0-9;mK]+', '')
--- end
--- return orig(lines, phase)
--- end)
--- ```
---@param orig? function the original function to override, if `nil` is provided then an empty function is passed
---@param override fun(orig:function, ...):... the override function
---@return function the new function with the patch applied
function M.patch_func(orig, override)
if not orig then orig = function() end end
return function(...) return override(orig, ...) end
end

--- Setup and configure AstroCore
---@param opts AstroCoreOpts
---@see astrocore.config
Expand Down

0 comments on commit e7d7a35

Please sign in to comment.