robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
4ed844be59
master
1--- Provides 'autoread' via OS filewatchers: watches 'autoread' buffer files for external changes 2--- using vim._watch. Complements the existing FocusGained/:checktime approach. 3 4local uv =vim . uv 5local watch =vim . _watch 6local nvim_on =require ( 'vim._core.util' ). nvim_on 7 8local M ={} 9 10local debounce_ms =100 11--- @type table<integer, fun()> bufnr -> cancel function 12local watchers ={} 13--- @type table<integer, uv.uv_timer_t> bufnr -> debounce timer 14local timers ={} 15--- @type table<integer, true> bufnr -> true. Tracks pending autoreads (debounce window, or :checktime in flight), 16--- so we can surface activity via the 'busy' flag. 17local pending ={} 18--- @type table<integer, true> bufnr -> true. Tracks which `pending` buffers have set 'busy'. 19local pending_busy ={} 20 21--- @private 22--- Test-only: override the debounce window so tests can run faster. 23--- @param ms integer 24function M . _set_debounce ( ms ) 25debounce_ms =ms 26end 27 28--- @private 29--- @param bufnr integer 30--- @return boolean 31function M . _is_watching ( bufnr ) 32return watchers [ bufnr ] ~= nil 33end 34 35--- Sets the 'busy' option on a `pending` buffer. Idempotent: if `pending` and `pending_busy` 36--- already agree, it's a no-op. Must run on main thread. 37--- 38--- @param bufnr integer 39local function sync_busy ( bufnr ) 40if not vim . api . nvim_buf_is_valid ( bufnr ) then 41pending_busy [ bufnr ] =nil 42return 43end 44local want =pending [ bufnr ] ~= nil 45local have =pending_busy [ bufnr ] ~= nil 46if want == have then 47return 48end 49vim . bo [ bufnr ]. busy =math . max ( 0 , vim . bo [ bufnr ]. busy + ( want and 1 or - 1 )) 50pending_busy [ bufnr ] =want or nil 51end 52 53--- Sends `pending` state for `bufnr`. 54--- 55--- @param bufnr integer 56--- @param is_pending boolean 57local function set_pending ( bufnr , is_pending ) 58pending [ bufnr ] =is_pending or nil 59vim . schedule ( function () 60sync_busy ( bufnr ) 61end ) 62end 63 64--- Returns the effective 'autoread' value for a buffer. 65--- 'autoread' is global-local: vim.bo[bufnr].autoread is nil when not set locally, 66--- so we must fall back to the global value. 67--- @param bufnr integer 68--- @return boolean 69local function buf_autoread ( bufnr ) 70local local_val =vim . bo [ bufnr ]. autoread 71if local_val ~= nil then 72return local_val 73end 74return vim . go . autoread 75end 76 77--- Returns true if the buffer should be watched. 78--- @param bufnr integer 79--- @return boolean 80local function should_watch ( bufnr ) 81if not vim . api . nvim_buf_is_loaded ( bufnr ) then 82return false 83end 84-- Skip special buffers (terminal, help, quickfix, etc.) 85if vim . bo [ bufnr ]. buftype ~= '' then 86return false 87end 88-- Must have a file name that exists on disk 89local name =vim . api . nvim_buf_get_name ( bufnr ) 90if name == '' or not uv . fs_stat ( name ) then 91return false 92end 93if not buf_autoread ( bufnr ) then 94return false 95end 96return true 97end 98 99--- Stops and cleans up the watcher for a buffer. 100--- @param bufnr integer 101local function stop_watcher ( bufnr ) 102set_pending ( bufnr , false ) 103local cancel =watchers [ bufnr ] 104if cancel then 105cancel () 106watchers [ bufnr ] =nil 107end 108local timer =timers [ bufnr ] 109if timer then 110timer : stop () 111timer : close () 112timers [ bufnr ] =nil 113end 114end 115 116--- Ensures the buffer has an active file watcher if appropriate, or stops 117--- an existing one if the buffer should no longer be watched. 118--- @param bufnr integer 119local function ensure_watcher ( bufnr ) 120stop_watcher ( bufnr ) 121 122if not should_watch ( bufnr ) then 123return 124end 125 126local name =vim . api . nvim_buf_get_name ( bufnr ) 127local timer =assert ( uv . new_timer ()) 128timers [ bufnr ] =timer 129 130local cancel =watch . watch ( name , {} , function ( _ , change_type ) 131-- Set the 'busy' buffer option for the duration of the pending cycle. This is a small, "best 132-- effort" UX hint, not intended to be noticeable except when filewatcher activity is "noisy". 133set_pending ( bufnr , true ) 134-- Debounce: restart the same timer on each event, so only the last 135-- event in a rapid series (e.g. truncate + write) triggers checktime. 136timer : start ( debounce_ms , 0 , function () 137vim . schedule ( function () 138sync_busy ( bufnr ) 139if not vim . api . nvim_buf_is_loaded ( bufnr ) or not buf_autoread ( bufnr ) then 140set_pending ( bufnr , false ) 141return 142end 143 144-- :checktime may throw if file was deleted (E211), or if reload triggers a buggy autocmd. 145local ok , err =pcall ( vim . cmd . checktime , bufnr ) ---@type any, any 146local file_missing =tostring ( err ): find ( 'E211:' , 1 , true ) 147 148set_pending ( bufnr , false ) 149-- Update the watcher if it's now stale: "rename" events (watcher pointing to old inode), or 150-- file deleted between event-and-:checktime. 151if change_type ~= watch . FileChangeType . Changed or file_missing then 152ensure_watcher ( bufnr ) 153end 154if not ok and not file_missing then 155vim . api . nvim_echo ( { 156{ ( 'autoread: :checktime failed for buffer %d: %s' ): format ( bufnr , err ) } , 157} , true , { err =true } ) 158end 159end ) 160end ) 161end ) 162 163watchers [ bufnr ] =cancel 164end 165 166function M . enable () 167local group =vim . api . nvim_create_augroup ( 'nvim.autoread' , { clear =true } ) 168 169-- (Re)start watcher when a file is loaded or written. 170nvim_on ( { 'BufReadPost' , 'BufWritePost' } , group , function ( args ) 171ensure_watcher ( args . buf ) 172end ) 173 174-- Stop watcher when buffer is unloaded or wiped out. 175nvim_on ( { 'BufUnload' , 'BufWipeout' } , group , function ( args ) 176stop_watcher ( args . buf ) 177end ) 178 179-- Clean up all watchers on exit to avoid dangling handles in the event loop. 180nvim_on ( 'VimLeavePre' , group , function () 181for bufnr in pairs ( watchers ) do 182stop_watcher ( bufnr ) 183end 184end ) 185 186-- React to 'autoread' option changes. 187nvim_on ( 'OptionSet' , group , { pattern ='autoread' } , function () 188if vim . v . option_type == 'global' then 189for _ , bufnr in ipairs ( vim . api . nvim_list_bufs ()) do 190ensure_watcher ( bufnr ) 191end 192else 193ensure_watcher ( vim . api . nvim_get_current_buf ()) 194end 195end ) 196 197-- Attach to buffers that were already loaded before enable() ran. 198for _ , bufnr in ipairs ( vim . api . nvim_list_bufs ()) do 199ensure_watcher ( bufnr ) 200end 201end 202 203return M