fix(explorer): refresh git status on all tabs when needed. Closes #2348

This commit is contained in:
Folke Lemaitre 2025-10-25 18:37:56 +02:00
parent 235d99330b
commit 1472211f9c
No known key found for this signature in database
GPG key ID: 9B52594D560070AB
2 changed files with 45 additions and 27 deletions

View file

@ -59,44 +59,53 @@ function M.refresh()
100,
0,
vim.schedule_wrap(function()
local picker = Snacks.picker.get({ source = "explorer" })[1]
if picker and not picker.closed and Tree:is_dirty(picker:cwd(), picker.opts) then
if not picker.list.target then
picker.list:set_target()
local pickers = Snacks.picker.get({ source = "explorer", tab = false })
for _, picker in ipairs(pickers) do
if picker and not picker.closed and Tree:is_dirty(picker:cwd(), picker.opts) then
if not picker.list.target then
picker.list:set_target()
end
vim.schedule(function()
picker:find()
end)
end
vim.schedule(function()
picker:find()
end)
end
end)
)
end
---@param cwd string
function M.watch(cwd)
function M.watch()
-- Track used watches
local used = {} ---@type table<string, boolean>
-- Watch git index
local root = Snacks.git.get_root(cwd)
if root then
used[root .. "/.git"] = true
M.start(root .. "/.git", function(file)
if vim.fs.basename(file) == "index" then
Git.refresh(root)
M.refresh()
local pickers = Snacks.picker.get({ source = "explorer", tab = false })
local cwds = {} ---@type table<string, boolean>
for _, picker in ipairs(pickers) do
cwds[picker:cwd()] = true
end
for cwd in pairs(cwds) do
-- Watch git index
local root = Snacks.git.get_root(cwd)
if root then
used[root .. "/.git"] = true
M.start(root .. "/.git", function(file)
if vim.fs.basename(file) == "index" then
Git.refresh(root)
M.refresh()
end
end)
end
-- Watch open directories
Tree:walk(Tree:find(cwd), function(node)
if node.dir and node.open then
used[node.path] = true
M.start(node.path)
end
end)
end
-- Watch open directories
Tree:walk(Tree:find(cwd), function(node)
if node.dir and node.open then
used[node.path] = true
M.start(node.path)
end
end)
-- Stop unused watches
for path in pairs(M._watches) do
if not used[path] then

View file

@ -50,7 +50,9 @@ function State.new(picker)
if opts.watch then
local on_close = picker.opts.on_close
picker.opts.on_close = function(p)
require("snacks.explorer.watch").abort()
vim.schedule(function()
require("snacks.explorer.watch").watch()
end)
if on_close then
on_close(p)
end
@ -65,6 +67,13 @@ function State.new(picker)
end
end)
picker.list.win:on("TabEnter", function(_, ev)
local p = ref()
if p and p:on_current_tab() then
Actions.update(p)
end
end)
picker.list.win:on("WinEnter", function(_, ev)
local p = ref()
if p then
@ -137,7 +146,7 @@ end
function State:setup(ctx)
local opts = ctx.picker.opts --[[@as snacks.picker.explorer.Config]]
if opts.watch then
require("snacks.explorer.watch").watch(ctx.filter.cwd)
require("snacks.explorer.watch").watch()
end
return not ctx.filter:is_empty()
end