diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index e34a7971..627b54cf 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -97,6 +97,12 @@ 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) + self.lists = {} +end + function Harpoon:sync() local key = self.config.settings.key() self:_for_each_list(function(list, _, list_name) @@ -152,22 +158,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" }, { - group = HarpoonGroup, - pattern = "*", - callback = function(ev) - 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() + vim.api.nvim_create_autocmd( + { "BufLeave", "VimLeavePre", "DirChanged" }, + { + group = HarpoonGroup, + pattern = "*", + callback = function(ev) + if ev.event == "DirChanged" then + self:reset() 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