From db7749e5925ddb028acb051d6fde7a5d5e611794 Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Tue, 2 Jul 2024 15:34:25 -0400 Subject: [PATCH 1/3] fix: properly handle directory changes This change clears out the in-memory harpoon data and re-read it from the disk whenever a `DirChanged` event is triggered. This makes sure the in memory data always reflects the harpoon lists for the current working directory. --- lua/harpoon/init.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index e34a7971..a5154b28 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -97,6 +97,11 @@ function Harpoon:_for_each_list(cb) end end +---Resets harpoon data by reading from disk +function Harpoon:reset() + self.data = Data.Data:new(self.config) +end + function Harpoon:sync() local key = self.config.settings.key() self:_for_each_list(function(list, _, list_name) @@ -152,10 +157,13 @@ function Harpoon.setup(self, partial_config) ---TODO: should we go through every seen list and update its config? if self.hooks_setup == false then - vim.api.nvim_create_autocmd({ "BufLeave", "VimLeavePre" }, { + vim.api.nvim_create_autocmd({ "BufLeave", "VimLeavePre", "DirChanged" }, { group = HarpoonGroup, pattern = "*", callback = function(ev) + if ev.event == "DirChanged" then + self:reset() + end self:_for_each_list(function(list, config) local fn = config[ev.event] if fn ~= nil then From e58645da8cbb03c96dc601c1fd6a29c4a5c7d6b1 Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Tue, 2 Jul 2024 15:45:07 -0400 Subject: [PATCH 2/3] make pr-ready --- lua/harpoon/init.lua | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index a5154b28..cfa45c34 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -157,25 +157,28 @@ function Harpoon.setup(self, partial_config) ---TODO: should we go through every seen list and update its config? if self.hooks_setup == false then - vim.api.nvim_create_autocmd({ "BufLeave", "VimLeavePre", "DirChanged" }, { - group = HarpoonGroup, - pattern = "*", - callback = function(ev) - if ev.event == "DirChanged" then - self:reset() - end - self:_for_each_list(function(list, config) - local fn = config[ev.event] - if fn ~= nil then - fn(ev, list) + vim.api.nvim_create_autocmd( + { "BufLeave", "VimLeavePre", "DirChanged" }, + { + group = HarpoonGroup, + pattern = "*", + callback = function(ev) + if ev.event == "DirChanged" then + self:reset() end - - if ev.event == "VimLeavePre" then - self:sync() - end - end) - end, - }) + self:_for_each_list(function(list, config) + local fn = config[ev.event] + if fn ~= nil then + fn(ev, list) + end + + if ev.event == "VimLeavePre" then + self:sync() + end + end) + end, + } + ) self.hooks_setup = true end From 09e58f92339b7af3fd4f1d9c18a29448e8504282 Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Sun, 21 Jul 2024 16:36:36 -0400 Subject: [PATCH 3/3] clear harpoon lists on directory change --- lua/harpoon/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index cfa45c34..627b54cf 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -100,6 +100,7 @@ end ---Resets harpoon data by reading from disk function Harpoon:reset() self.data = Data.Data:new(self.config) + self.lists = {} end function Harpoon:sync()