Line data Source code
1 : -- lua/yoda/git_refresh.lua
2 : -- Centralized git refresh coordination (single source of truth)
3 :
4 9 : local M = {}
5 :
6 9 : local gitsigns = require("yoda.integrations.gitsigns")
7 :
8 : -- Recursion guard for FileChangedShell events
9 9 : local file_changed_in_progress = false
10 :
11 : -- ============================================================================
12 : -- Public API
13 : -- ============================================================================
14 :
15 : --- Setup all git refresh autocmds in one place
16 : --- This is the ONLY place that should register git refresh autocmds
17 : --- @param autocmd function vim.api.nvim_create_autocmd
18 : --- @param augroup function vim.api.nvim_create_augroup
19 9 : function M.setup_autocmds(autocmd, augroup)
20 9 : local git_refresh_group = augroup("YodaGitRefresh", { clear = true })
21 :
22 : -- Refresh after saving files
23 18 : autocmd("BufWritePost", {
24 9 : group = git_refresh_group,
25 : desc = "Refresh git signs after buffer is written",
26 : callback = function()
27 : if vim.bo.buftype == "" then
28 : gitsigns.refresh_batched()
29 : end
30 9 : end,
31 : })
32 :
33 : -- Refresh when external file changes detected
34 18 : autocmd("FileChangedShell", {
35 9 : group = git_refresh_group,
36 : desc = "Refresh git signs when files changed externally",
37 : callback = function(args)
38 : -- Prevent recursive FileChangedShell events
39 2 : if file_changed_in_progress then
40 1 : return
41 : end
42 :
43 1 : file_changed_in_progress = true
44 :
45 2 : vim.schedule(function()
46 : if args.buf and vim.api.nvim_buf_is_valid(args.buf) and vim.bo[args.buf].buftype == "" then
47 : gitsigns.refresh_batched()
48 : end
49 : file_changed_in_progress = false
50 1 : end)
51 10 : end,
52 : })
53 :
54 : -- Refresh when Neovim gains focus and check for external file changes
55 18 : autocmd("FocusGained", {
56 9 : group = git_refresh_group,
57 : desc = "Check for external changes and refresh git signs when Neovim gains focus",
58 : callback = function()
59 : if vim.bo.buftype == "" then
60 : pcall(vim.cmd, "checktime")
61 : vim.schedule(function()
62 : gitsigns.refresh_batched()
63 : end)
64 : end
65 9 : end,
66 : })
67 18 : end
68 :
69 9 : return M
|