Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add before operation hook #113

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local M = {}
local git_worktree_root = nil
local current_worktree_path = nil
local on_change_callbacks = {}
local before_change_callbacks = {}

M.setup_git_info = function()
local cwd = vim.loop.cwd()
Expand Down Expand Up @@ -136,6 +137,14 @@ local function on_tree_change_handler(op, metadata)
end
end

local function emit_before_change(op, metadata)
status:next_status(string.format("Running before %s callbacks", op))
for idx = 1, #before_change_callbacks do
before_change_callbacks[idx](op, metadata)
end
end


local function emit_on_change(op, metadata)
-- TODO: We don't have a way to async update what is running
status:next_status(string.format("Running post %s callbacks", op))
Expand Down Expand Up @@ -385,7 +394,9 @@ local function create_worktree(path, branch, upstream, found_branch)
end

M.create_worktree = function(path, branch, upstream)
status:reset(8)
status:reset(9)

emit_before_change(Enum.Operations.Create, { path = path, prev_path = vim.loop.cwd() })

if upstream == nil then
if has_origin() then
Expand All @@ -408,7 +419,10 @@ M.create_worktree = function(path, branch, upstream)
end

M.switch_worktree = function(path)
status:reset(2)
status:reset(3)

emit_before_change(Enum.Operations.Switch, { path = path, prev_path = vim.loop.cwd() })

M.setup_git_info()
has_worktree(path, function(found)

Expand All @@ -429,7 +443,10 @@ M.delete_worktree = function(path, force, opts)
opts = {}
end

status:reset(2)
status:reset(3)

emit_before_change(Enum.Operations.Delete, { path = vim.loop.cwd() })

M.setup_git_info()
has_worktree(path, function(found)
if not found then
Expand Down Expand Up @@ -512,8 +529,13 @@ M.on_tree_change = function(cb)
table.insert(on_change_callbacks, cb)
end

M.before_tree_change = function(cb)
table.insert(before_change_callbacks , cb)
end

M.reset = function()
on_change_callbacks = {}
before_change_callbacks = {}
end

M.get_root = function()
Expand Down