From 2feb8a86ed2e0f9b5efabe0817eec556a4d1e28a Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Wed, 25 Feb 2026 11:42:59 -0500 Subject: [PATCH] feat(neogit): use new neogit apis for highlight and repo root (#133) ## Problem diffs.nvim was blanking 18 Neogit highlight groups globally on attach to prevent Neogit's `line_hl_group` fg from stomping treesitter syntax. It also fell back to `getcwd()` plus a subprocess call for repo root detection on Neogit buffers, and had no mechanism to refresh the hunk cache when Neogit lazy-loaded new diff sections. ## Solution Adopts three APIs introduced in NeogitOrg/neogit#1897: - Sets `vim.b.neogit_disable_hunk_highlight = true` on the Neogit buffer at attach time. Neogit's `HunkLine` renderer skips all its own highlight logic when this is set, replacing the need to blank 18 hl groups globally and the associated ColorScheme re-application. - Reads `vim.b.neogit_git_dir` in `get_repo_root()` as a reliable fallback between the existing `b:git_dir` check and the `getcwd()` subprocess path. - Registers a buffer-local `User NeogitDiffLoaded` autocmd on attach that calls `M.refresh()` when Neogit lazy-loads a new diff section, keeping the hunk cache in sync. Closes #128 --- lua/diffs/init.lua | 57 +++++++++++++++++--------------------------- lua/diffs/parser.lua | 5 ++++ 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 6195e8e..8d6dffb 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -410,34 +410,6 @@ local function compute_highlight_groups() end end -local neogit_attached = false - -local neogit_hl_groups = { - 'NeogitDiffAdd', - 'NeogitDiffAddCursor', - 'NeogitDiffAddHighlight', - 'NeogitDiffDelete', - 'NeogitDiffDeleteCursor', - 'NeogitDiffDeleteHighlight', - 'NeogitDiffContext', - 'NeogitDiffContextCursor', - 'NeogitDiffContextHighlight', - 'NeogitDiffHeader', - 'NeogitDiffHeaderHighlight', - 'NeogitHunkHeader', - 'NeogitHunkHeaderCursor', - 'NeogitHunkHeaderHighlight', - 'NeogitHunkMergeHeader', - 'NeogitHunkMergeHeaderCursor', - 'NeogitHunkMergeHeaderHighlight', -} - -local function override_neogit_highlights() - for _, name in ipairs(neogit_hl_groups) do - vim.api.nvim_set_hl(0, name, {}) - end -end - local function init() if initialized then return @@ -661,9 +633,6 @@ local function init() vim.api.nvim_create_autocmd('ColorScheme', { callback = function() compute_highlight_groups() - if neogit_attached then - vim.schedule(override_neogit_highlights) - end for bufnr, _ in pairs(attached_buffers) do invalidate_cache(bufnr) end @@ -679,7 +648,6 @@ local function init() ensure_cache(bufnr) local entry = hunk_cache[bufnr] if entry and entry.pending_clear then - vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) entry.highlighted = {} entry.pending_clear = false end @@ -705,6 +673,12 @@ local function init() for i = first, last do if not entry.highlighted[i] then local hunk = entry.hunks[i] + local clear_start = hunk.start_line - 1 + local clear_end = clear_start + #hunk.lines + if hunk.header_start_line then + clear_start = hunk.header_start_line - 1 + end + vim.api.nvim_buf_clear_namespace(bufnr, ns, clear_start, clear_end) highlight.highlight_hunk(bufnr, ns, hunk, fast_hl_opts) entry.highlighted[i] = true count = count + 1 @@ -782,9 +756,19 @@ function M.attach(bufnr) end attached_buffers[bufnr] = true - if not neogit_attached and config.neogit and vim.bo[bufnr].filetype:match('^Neogit') then - neogit_attached = true - vim.schedule(override_neogit_highlights) + local neogit_augroup = nil + if config.neogit and vim.bo[bufnr].filetype:match('^Neogit') then + vim.b[bufnr].neogit_disable_hunk_highlight = true + neogit_augroup = vim.api.nvim_create_augroup('diffs_neogit_' .. bufnr, { clear = true }) + vim.api.nvim_create_autocmd('User', { + pattern = 'NeogitDiffLoaded', + group = neogit_augroup, + callback = function() + if vim.api.nvim_buf_is_valid(bufnr) and attached_buffers[bufnr] then + M.refresh(bufnr) + end + end, + }) end dbg('attaching to buffer %d', bufnr) @@ -796,6 +780,9 @@ function M.attach(bufnr) callback = function() attached_buffers[bufnr] = nil hunk_cache[bufnr] = nil + if neogit_augroup then + pcall(vim.api.nvim_del_augroup_by_id, neogit_augroup) + end end, }) end diff --git a/lua/diffs/parser.lua b/lua/diffs/parser.lua index 9d86cd1..fa1bcc0 100644 --- a/lua/diffs/parser.lua +++ b/lua/diffs/parser.lua @@ -110,6 +110,11 @@ local function get_repo_root(bufnr) return vim.fn.fnamemodify(git_dir, ':h') end + local ok3, neogit_git_dir = pcall(vim.api.nvim_buf_get_var, bufnr, 'neogit_git_dir') + if ok3 and neogit_git_dir then + return vim.fn.fnamemodify(neogit_git_dir, ':h') + end + local cwd = vim.fn.getcwd() local git = require('diffs.git') return git.get_repo_root(cwd .. '/.')