Problem: treesitter parses each diff hunk in isolation, so incomplete syntax constructs at hunk boundaries (e.g., a function definition with no body) produce ERROR nodes and drop captures. Solution: read N lines from the on-disk file before/after each hunk and prepend/append them as unmapped padding lines. The line_map guard in highlight_treesitter skips extmarks for unmapped lines, so padding provides syntax context without visual output. Controlled by highlights.context (default 25, 0 to disable). Also applies to the vim syntax fallback path via a leading_offset filter.
3.1 KiB
diffs.nvim
Syntax highlighting for diffs in Neovim
Enhance vim-fugitive and Neovim's built-in diff mode with language-aware
syntax highlighting.
Features
- Treesitter syntax highlighting in
:Gitdiffs and commit views - Diff header highlighting (
diff --git,index,---,+++) :Gdiffsplit/:Gvdiffsplitsyntax through diff backgrounds:Gdiffunified diff against any git revision with syntax highlighting- Fugitive status buffer keymaps (
du/dU) for unified diffs - Background-only diff colors for any
&diffbuffer (:diffthis,vimdiff) - Vim syntax fallback for languages without a treesitter parser
- Hunk header context highlighting (
@@ ... @@ function foo()) - Character-level (intra-line) diff highlighting for changed characters
- Configurable debouncing, max lines, and diff prefix concealment
Requirements
- Neovim 0.9.0+
Installation
Install with your package manager of choice or via luarocks:
luarocks install diffs.nvim
Documentation
:help diffs.nvim
Known Limitations
-
Incomplete syntax context: Treesitter parses each diff hunk in isolation. To improve accuracy,
diffs.nvimreads lines from disk before and after each hunk for parsing context (controlled byhighlights.context, default 25). This resolves most boundary issues. Sethighlights.context = 0to disable. -
Syntax flashing:
diffs.nvimhooks into theFileType fugitiveevent triggered byvim-fugitive, at which point the buffer is preliminarily painted. The buffer is then re-painted afterdebounce_msmilliseconds, causing an unavoidable visual "flash" even whendebounce_ms = 0. -
Conflicting diff plugins:
diffs.nvimmay not interact well with other plugins that modify diff highlighting. Known plugins that may conflict:diffview.nvim- provides its own diff highlighting and conflict resolution UImini.diff- visualizes buffer differences with its own highlighting systemgitsigns.nvim- generally compatible, but both plugins modifying line highlights may produce unexpected resultsgit-conflict.nvim- conflict marker highlighting may overlap withdiffs.nvim
Acknowledgements
vim-fugitivecodediff.nvimdiffview.nvimdifftasticmini.diffgitsigns.nvimgit-conflict.nvim- @phanen - diff header highlighting, unknown filetype fix, shebang/modeline detection