No description
Find a file
Barrett Ruth e7d56e3bbe
Some checks are pending
luarocks / quality (push) Waiting to run
luarocks / publish (push) Blocked by required conditions
feat(highlight): wire highlights.context into treesitter pipeline (#151)
## Problem

`highlights.context.enabled` and `highlights.context.lines` were
defined, validated, and range-checked but never read during
highlighting. Hunks inside incomplete constructs (e.g., a table literal
or function body whose opening is beyond the hunk's own context lines)
parsed incorrectly because treesitter had no surrounding code.

## Solution

`compute_hunk_context` in `init.lua` reads the working tree file using
the hunk's `@@ +start,count @@` line numbers to collect up to `lines`
(default 25) surrounding code lines in each direction. Files are read
once via `io.open` and cached across hunks in the same file.
`highlight_treesitter` in `highlight.lua` accepts an optional context
parameter that prepends/appends context lines to the parse string and
offsets capture rows by the prefix count, so extmarks only land on
actual hunk lines. Wired through `highlight_hunk` for the two
code-language treesitter calls (not headers, not `highlight_text`, not
vim syntax).

Closes #148.
2026-03-05 11:14:31 -05:00
.github ci: migrate to nix 2026-02-23 18:14:05 -05:00
doc feat(highlight): wire highlights.context into treesitter pipeline (#151) 2026-03-05 11:14:31 -05:00
lua/diffs feat(highlight): wire highlights.context into treesitter pipeline (#151) 2026-03-05 11:14:31 -05:00
plugin feat: add neogit support (#117) 2026-02-14 17:12:01 -05:00
scripts refactor(highlight): unified per-line extmark builder (#144) 2026-03-05 09:01:22 -05:00
spec feat(highlight): wire highlights.context into treesitter pipeline (#151) 2026-03-05 11:14:31 -05:00
.busted feat(test): testing infrastructure 2026-02-01 23:09:05 -05:00
.editorconfig feat: initial setup files 2026-02-01 16:30:24 -05:00
.gitignore ci: migrate to nix 2026-02-23 18:14:05 -05:00
.luarc.json refactor(highlight): unified per-line extmark builder (#144) 2026-03-05 09:01:22 -05:00
.pre-commit-config.yaml fix: use yaml, not yml YAML file extension 2026-02-02 22:14:04 -05:00
.prettierignore feat: initial setup files 2026-02-01 16:30:24 -05:00
.prettierrc fix: use yaml, not yml YAML file extension 2026-02-02 22:14:04 -05:00
.styluaignore refactor(highlight): unified per-line extmark builder (#144) 2026-03-05 09:01:22 -05:00
diffs.nvim-scm-1.rockspec feat: rename everything 2026-02-02 22:09:13 -05:00
flake.lock performance improvements (#116) 2026-02-12 16:59:13 -05:00
flake.nix refactor(highlight): unified per-line extmark builder (#144) 2026-03-05 09:01:22 -05:00
LICENSE feat: initial setup files 2026-02-01 16:30:24 -05:00
README.md feat: enable vim syntax fallback by default (#152) 2026-03-05 11:13:28 -05:00
selene.toml ci: migrate to nix 2026-02-23 18:14:05 -05:00
stylua.toml feat: initial setup files 2026-02-01 16:30:24 -05:00
vim.yaml ci: add bit luajit global 2026-02-23 18:18:30 -05:00

diffs.nvim

Syntax highlighting for diffs in Neovim

Enhance vim-fugitive, Neogit, and Neovim's built-in diff mode with language-aware syntax highlighting.

Features

  • Treesitter syntax highlighting in vim-fugitive, Neogit, and diff filetype
  • Character-level intra-line diff highlighting (with optional vscode-diff FFI backend for word-level accuracy)
  • :Gdiff unified diff against any revision
  • Inline merge conflict detection, highlighting, and resolution
  • Email quoting/patch syntax support (> diff ...)
  • Vim syntax fallback
  • Configurable highlighiting blend & priorities
  • Context-inclusive, high-accuracy highlights

Requirements

  • Neovim 0.9.0+

Installation

Install with your package manager of choice or via luarocks:

luarocks install diffs.nvim

Documentation

:help diffs.nvim

FAQ

Q: How do I install with lazy.nvim?

{
  'barrettruth/diffs.nvim',
  init = function()
    vim.g.diffs = {
      ...
    }
  end,
}

Do not lazy load diffs.nvim with event, lazy, ft, config, or keys to control loading - diffs.nvim lazy-loads itself.

Q: Does diffs.nvim support vim-fugitive/Neogit?

Yes. Enable it in your config:

vim.g.diffs = {
  fugitive = true,
  neogit = true,
}

See the documentation for more information.

Known Limitations

  • Incomplete syntax context: Treesitter parses each diff hunk in isolation. Context lines within the hunk provide syntactic context for the parser. In rare cases, hunks that start or end mid-expression may produce imperfect highlights due to treesitter error recovery.

  • Syntax "flashing": diffs.nvim hooks into the FileType fugitive event triggered by vim-fugitive, at which point the buffer is preliminarily painted. The decoration provider applies highlights on the next redraw cycle, causing a brief visual "flash".

  • Cold Start: Treesitter grammar loading (~10ms) and query compilation (~4ms) are one-time costs per language per Neovim session. Each language pays this cost on first encounter, which may cause a brief stutter when a diff containing a new language first enters the viewport.

  • Vim syntax fallback is deferred: The vim syntax fallback (for languages without a treesitter parser) cannot run inside the decoration provider's redraw cycle due to Neovim's restriction on buffer mutations. Vim syntax highlights for these hunks appear slightly delayed.

  • Conflicting diff plugins: diffs.nvim may 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 UI
    • mini.diff - visualizes buffer differences with its own highlighting system
    • gitsigns.nvim - generally compatible, but both plugins modifying line highlights may produce unexpected results
    • git-conflict.nvim - diffs.nvim now includes built-in conflict resolution; disable one or the other to avoid overlap

Acknowledgements