diff --git a/lua/nvim-tree/explorer/watch.lua b/lua/nvim-tree/explorer/watch.lua index 449b9a50e47..d42a47d5cf2 100644 --- a/lua/nvim-tree/explorer/watch.lua +++ b/lua/nvim-tree/explorer/watch.lua @@ -77,7 +77,7 @@ function M.create_watcher(absolute_path) end M.uid = M.uid + 1 - return Watcher:new(absolute_path, callback, { + return Watcher:new(absolute_path, nil, callback, { context = "explorer:watch:" .. absolute_path .. ":" .. M.uid, }) end diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 8eeeb22e8c1..e16169503fb 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -11,6 +11,16 @@ local M = { cwd_to_project_root = {}, } +-- Files under .git that should result in a reload when changed. +-- Utilities (like watchman) can also write to this directory (often) and aren't useful for us. +local WATCHED_FILES = { + "FETCH_HEAD", -- remote ref + "HEAD", -- local ref + "HEAD.lock", -- HEAD will not always be updated e.g. revert + "config", -- user config + "index", -- staging area +} + function M.reload() if not M.config.git.enable then return {} @@ -149,7 +159,7 @@ function M.load_project_status(cwd) end) end - watcher = Watcher:new(utils.path_join { project_root, ".git" }, callback, { + watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, { project_root = project_root, }) end diff --git a/lua/nvim-tree/watcher.lua b/lua/nvim-tree/watcher.lua index 972147dfbd1..da8e64774d1 100644 --- a/lua/nvim-tree/watcher.lua +++ b/lua/nvim-tree/watcher.lua @@ -58,7 +58,7 @@ function Event:start() else log.line("watcher", "event_cb '%s' '%s'", self._path, filename) for _, listener in ipairs(self._listeners) do - listener() + listener(filename) end end end) @@ -101,14 +101,15 @@ function Event:destroy(message) Event._events[self._path] = nil end -function Watcher:new(path, callback, data) - log.line("watcher", "Watcher:new '%s'", path) +function Watcher:new(path, files, callback, data) + log.line("watcher", "Watcher:new '%s' %s", path, vim.inspect(files)) local w = setmetatable(data, Watcher) w._event = Event._events[path] or Event:new(path) w._listener = nil w._path = path + w._files = files w._callback = callback if not w._event then @@ -123,8 +124,10 @@ function Watcher:new(path, callback, data) end function Watcher:start() - self._listener = function() - self._callback(self) + self._listener = function(filename) + if not self._files or vim.tbl_contains(self._files, filename) then + self._callback(self) + end end self._event:add(self._listener)