From 2b54a9f46ac41f2ba5bf7fdd9c615b9ad21f4272 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 19 Mar 2026 21:57:32 -0400 Subject: [PATCH] fix(init): recover deferred syntax when tick changes during ft retry Problem: when `did_filetype()` blocks filetype detection (e.g. `.sh`, `.bash`), `ensure_cache` schedules a retry via `vim.schedule` that calls `invalidate_cache` (setting `tick=-1`) then `redraw!`. The deferred syntax callback from `on_win` then sees a stale tick and aborts, leaving hunks without treesitter syntax highlighting. Solution: instead of aborting when the tick changes, fall back to the current cache's hunks that have a resolved `lang` field. This allows the deferred syntax pass to apply treesitter highlighting even after the ft retry invalidation cycle. --- lua/diffs/init.lua | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 585f187..57364d0 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -1000,13 +1000,25 @@ local function init() return end local cur = hunk_cache[bufnr] - if not cur or cur.tick ~= tick then + if not cur then + return + end + local hunks_to_hl = deferred_syntax + if cur.tick ~= tick then dbg( - 'deferred syntax stale: cur.tick=%s captured=%d', - cur and tostring(cur.tick) or 'nil', + 'deferred syntax tick changed: cur.tick=%s captured=%d, using current hunks', + tostring(cur.tick), tick ) - return + hunks_to_hl = {} + for _, hunk in ipairs(cur.hunks or {}) do + if hunk.lang then + hunks_to_hl[#hunks_to_hl + 1] = hunk + end + end + if #hunks_to_hl == 0 then + return + end end local t1 = config.debug and vim.uv.hrtime() or nil local syntax_opts = { @@ -1014,7 +1026,7 @@ local function init() highlights = config.highlights, syntax_only = true, } - for _, hunk in ipairs(deferred_syntax) do + for _, hunk in ipairs(hunks_to_hl) do highlight.highlight_hunk(bufnr, ns, hunk, syntax_opts) end if t1 then