diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-25 05:33:34 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-25 05:35:15 +0200 |
| commit | c22fab35706facfa780b800b4dc72559f0ac6c60 (patch) | |
| tree | f01860df038c4197663e86be3d9f103c058398e4 /plugin | |
| parent | d8654076282df55d425b2e4fa0be9355bde6b928 (diff) | |
| download | nvim-config-c22fab35706facfa780b800b4dc72559f0ac6c60.tar.gz nvim-config-c22fab35706facfa780b800b4dc72559f0ac6c60.zip | |
feat(nvim): opt-in autoreload of buffers changed outside Neovim
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/50-autoreload.lua | 56 | ||||
| -rw-r--r-- | plugin/50-color.lua | 3 |
2 files changed, 58 insertions, 1 deletions
diff --git a/plugin/50-autoreload.lua b/plugin/50-autoreload.lua new file mode 100644 index 0000000..765c137 --- /dev/null +++ b/plugin/50-autoreload.lua @@ -0,0 +1,56 @@ +--[[ 50-autoreload.lua — opt-in reloading of buffers changed outside Neovim. + +When enabled, buffers whose file changed on disk are reloaded silently, in one batch, on focus / idle / +buffer-enter — no per-file change prompt. Conflicts (the buffer was also edited in Neovim) and +deletions still defer to Neovim's usual prompt. Off by default; the state lives in +`vim.g.autoreload` and shows as the `+R` flag in the statusline (see 50-color.lua). + +User commands: + `AutoreloadToggle`: toggle disk-change autoreload for this Neovim instance +]] + +vim.g.autoreload = false + +-- Re-stat every loaded buffer in one pass; the FileChangedShell handler below decides per buffer. +local function check_all_buffers() + if vim.g.autoreload then + vim.cmd.checktime() -- no argument → all loaded buffers at once + end +end + +vim.api.nvim_create_autocmd( + { "FocusGained", "BufEnter", "CursorHold", "CursorHoldI", "TermLeave" }, + { + group = vim.g.dotfiles.augroup, + callback = check_all_buffers, + } +) + +--[[ Decide what happens when Neovim notices a file changed under it. While autoreload is on, a plain +on-disk change reloads the buffer with no prompt (batched, since `checktime` fires this for each +changed buffer in a single pass); conflicts and deletions fall back to `ask`. While off, everything +falls back to `ask`, i.e. Neovim's default handling. See <nvim-help://v:fcs_choice>. ]] +vim.api.nvim_create_autocmd("FileChangedShell", { + group = vim.g.dotfiles.augroup, + callback = function() + if vim.g.autoreload and vim.v.fcs_reason == "changed" then + vim.v.fcs_choice = "reload" + else + vim.v.fcs_choice = "ask" + end + end, +}) + +-- Flip autoreload for this instance; check immediately when turning it on so pending changes catch up. +local function toggle_autoreload() + vim.g.autoreload = not vim.g.autoreload + vim.notify("Buffer autoreload " .. (vim.g.autoreload and "enabled" or "disabled")) + check_all_buffers() + vim.cmd.redrawstatus() +end + +vim.api.nvim_create_user_command( + "AutoreloadToggle", + toggle_autoreload, + { desc = "Toggle buffer autoreload" } +) diff --git a/plugin/50-color.lua b/plugin/50-color.lua index 5eb03b3..ede41ee 100644 --- a/plugin/50-color.lua +++ b/plugin/50-color.lua @@ -84,10 +84,11 @@ local function spell_status() end -- Custom status flags: "+F" when formatting is on for the buffer, "+H" when LSP inlay hints are on, --- followed by the spell flag. +-- "+R" when disk-change autoreload is enabled (see 50-autoreload.lua), followed by the spell flag. local function statuses() return (vim.b.format and "+F" or "") .. (vim.lsp.inlay_hint.is_enabled({ bufnr = 0 }) and "+H" or "") + .. (vim.g.autoreload and "+R" or "") .. (vim.o.spell and spell_status() or "") end |
