feat: add neogit support (#117)

## TODO

1. docs (vimdoc + readme) - this is a non-trivial feature
2. push luarocks version

## Problem

diffs.nvim only activates on `fugitive`, `git`, and `gitcommit`
filetypes.
Neogit uses its own custom filetypes (`NeogitStatus`,
`NeogitCommitView`,
`NeogitDiffView`) and doesn't set `b:git_dir`, so the plugin never
attaches
and repo root resolution fails for filetype detection within diff hunks.

## Solution

Two changes:

1. **`lua/diffs/init.lua`** — Add the three Neogit filetypes to the
default
`filetypes` list. The `FileType` autocmd in `plugin/diffs.lua` already
handles them correctly since the `is_fugitive_buffer` guard only applies
   to the `git` filetype.

2. **`lua/diffs/parser.lua`** — Add a CWD-based fallback in
`get_repo_root()`.
After the existing `b:diffs_repo_root` and `b:git_dir` checks, fall back
to
`vim.fn.getcwd()` via `git.get_repo_root()` (already cached). Without
this,
   the parser can't resolve filetypes for files in Neogit buffers.

Neogit's expanded diffs use standard unified diff format, so the parser
handles
them without modification.

Closes #110.
This commit is contained in:
Barrett Ruth 2026-02-14 17:12:01 -05:00 committed by GitHub
parent 5d3bbc3631
commit 3d640c207b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 314 additions and 56 deletions

View file

@ -7,8 +7,8 @@ License: MIT
INTRODUCTION *diffs.nvim*
diffs.nvim adds syntax highlighting to diff views. It overlays language-aware
highlights on top of default diff highlighting in vim-fugitive and Neovim's
built-in diff mode.
highlights on top of default diff highlighting in vim-fugitive, Neogit, and
Neovim's built-in diff mode.
Features: ~
- Syntax highlighting in |:Git| summary diffs and commit detail views
@ -53,7 +53,9 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
vim.g.diffs = {
debug = false,
hide_prefix = false,
filetypes = { 'fugitive', 'git', 'gitcommit' },
fugitive = false,
neogit = false,
extra_filetypes = {},
highlights = {
background = true,
gutter = true,
@ -83,10 +85,6 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
},
overrides = {},
},
fugitive = {
horizontal = 'du',
vertical = 'dU',
},
conflict = {
enabled = true,
disable_diagnostics = true,
@ -116,14 +114,36 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
is also enabled, the overlay inherits the line's
background color.
{filetypes} (table, default: {'fugitive','git','gitcommit'})
List of filetypes that trigger attachment. Add
`'diff'` to enable highlighting in plain `.diff`
and `.patch` files: >lua
{fugitive} (boolean|table, default: false)
Enable vim-fugitive integration. Accepts
`true`, `false`, or a table with sub-options
(see |diffs.FugitiveConfig|). When enabled,
the `fugitive` filetype is active and status
buffer keymaps are registered. >lua
vim.g.diffs = { fugitive = true }
vim.g.diffs = {
filetypes = {
'fugitive', 'git', 'gitcommit', 'diff',
},
fugitive = { horizontal = 'dd' },
}
<
{neogit} (boolean|table, default: false)
Enable Neogit integration. Accepts `true`,
`false`, or `{ enabled = false }`. When
enabled, `NeogitStatus`, `NeogitCommitView`,
and `NeogitDiffView` filetypes are active and
Neogit highlight overrides are applied. See
|diffs-neogit|. >lua
vim.g.diffs = { neogit = false }
<
{extra_filetypes} (table, default: {})
Additional filetypes to attach to, beyond the
built-in `git`, `gitcommit`, and any enabled
integration filetypes. Use this to enable
highlighting in plain `.diff` / `.patch`
files: >lua
vim.g.diffs = {
extra_filetypes = { 'diff' },
}
<
@ -131,10 +151,6 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
Controls which highlight features are enabled.
See |diffs.Highlights| for fields.
{fugitive} (table, default: see below)
Fugitive status buffer keymap options.
See |diffs.FugitiveConfig| for fields.
{conflict} (table, default: see below)
Inline merge conflict resolution options.
See |diffs.ConflictConfig| for fields.
@ -422,12 +438,20 @@ Configuration: ~
>lua
vim.g.diffs = {
fugitive = {
enabled = true, -- false to disable fugitive integration entirely
horizontal = 'du', -- keymap for horizontal split, false to disable
vertical = 'dU', -- keymap for vertical split, false to disable
},
}
<
Fields: ~
{enabled} (boolean, default: false)
Enable fugitive integration. When false, the
`fugitive` filetype is excluded and no status
buffer keymaps are registered. Shorthand:
`fugitive = false` is equivalent to
`fugitive = { enabled = false }`.
{horizontal} (string|false, default: 'du')
Keymap for unified diff in horizontal split.
Set to `false` to disable.
@ -579,6 +603,31 @@ The working file buffer is modified in place; save it when ready.
Phase 1 inline conflict highlights (see |diffs-conflict|) are refreshed
automatically after each resolution.
==============================================================================
NEOGIT *diffs-neogit*
diffs.nvim works with Neogit (https://github.com/NeogitOrg/neogit) out of
the box. Enable Neogit support in your config: >lua
vim.g.diffs = { neogit = true }
<
When a diff is expanded in a Neogit buffer (e.g., via TAB on a file in the
status view), diffs.nvim applies treesitter syntax highlighting and
intra-line diffs to the hunk lines, just as it does for fugitive.
Neogit highlight overrides: ~
On first attach to a Neogit buffer, diffs.nvim overrides Neogit's diff
highlight groups (`NeogitDiffAdd*`, `NeogitDiffDelete*`,
`NeogitDiffContext*`, `NeogitHunkHeader*`, `NeogitDiffHeader*`, etc.) by
setting them to empty (`{}`). This gives diffs.nvim sole control of diff
line visuals. The overrides are reapplied on `ColorScheme` since Neogit
re-defines its groups then. When `neogit = false`, no highlight overrides
are applied.
Deprecated: ~
The `filetypes` config key still works but is deprecated and will be
removed in 0.3.0. Use `fugitive`, `neogit`, and `extra_filetypes` instead.
==============================================================================
API *diffs-api*
@ -600,7 +649,7 @@ refresh({bufnr}) *diffs.refresh()*
IMPLEMENTATION *diffs-implementation*
Summary / commit detail views: ~
1. `FileType` autocmd for configured filetypes (see {filetypes}) triggers
1. `FileType` autocmd for computed filetypes (see |diffs-config|) triggers
|diffs.attach()|. For `git` buffers, only `fugitive://` URIs are attached.
2. The buffer is parsed to detect file headers (`M path/to/file`,
`diff --git a/... b/...`) and hunk headers (`@@ -10,3 +10,4 @@`)