From 2d4a1111088bbb39a068c10394fa56bac34ddfd0 Mon Sep 17 00:00:00 2001 From: Farhan Date: Tue, 31 Oct 2023 16:20:25 +0700 Subject: [PATCH 1/3] add `before_change_callbacks` table --- lua/git-worktree/init.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index 0fb5295..d556923 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -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() @@ -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)) @@ -514,6 +523,7 @@ end M.reset = function() on_change_callbacks = {} + before_change_callbacks = {} end M.get_root = function() From ffc3cd5851a7025d65c5105244a1b8c8c2e9f9a7 Mon Sep 17 00:00:00 2001 From: Farhan Date: Tue, 31 Oct 2023 16:21:56 +0700 Subject: [PATCH 2/3] add `before_tree_change` function --- lua/git-worktree/init.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index d556923..bfb7eb9 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -521,6 +521,10 @@ 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 = {} From 6978d34613eecb98ad3351cafba52be72573779f Mon Sep 17 00:00:00 2001 From: Farhan Date: Tue, 31 Oct 2023 16:22:29 +0700 Subject: [PATCH 3/3] run `before_change_callbacks` on every operation --- lua/git-worktree/init.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index bfb7eb9..da8c3ed 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -394,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 @@ -417,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) @@ -438,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