feat: add support for diff and other filetypes

This commit is contained in:
Barrett Ruth 2026-02-12 18:04:47 -05:00
parent 9a0b812f69
commit 3990014a93
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
4 changed files with 40 additions and 4 deletions

View file

@ -53,6 +53,7 @@ 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' },
highlights = {
background = true,
gutter = true,
@ -115,6 +116,17 @@ 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
vim.g.diffs = {
filetypes = {
'fugitive', 'git', 'gitcommit', 'diff',
},
}
<
{highlights} (table, default: see below)
Controls which highlight features are enabled.
See |diffs.Highlights| for fields.
@ -588,8 +600,8 @@ refresh({bufnr}) *diffs.refresh()*
IMPLEMENTATION *diffs-implementation*
Summary / commit detail views: ~
1. `FileType fugitive` or `FileType git` (for `fugitive://` buffers)
triggers |diffs.attach()|
1. `FileType` autocmd for configured filetypes (see {filetypes}) 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 @@`)
3. For each hunk:

View file

@ -56,6 +56,7 @@
---@class diffs.Config
---@field debug boolean|string
---@field hide_prefix boolean
---@field filetypes string[]
---@field highlights diffs.Highlights
---@field fugitive diffs.FugitiveConfig
---@field conflict diffs.ConflictConfig
@ -107,6 +108,7 @@ end
local default_config = {
debug = false,
hide_prefix = false,
filetypes = { 'fugitive', 'git', 'gitcommit' },
highlights = {
background = true,
gutter = true,
@ -402,6 +404,7 @@ local function init()
'boolean or string (file path)',
},
hide_prefix = { opts.hide_prefix, 'boolean', true },
filetypes = { opts.filetypes, 'table', true },
highlights = { opts.highlights, 'table', true },
})

View file

@ -151,6 +151,11 @@ function M.parse_buffer(bufnr)
local file_new_start = nil
---@type integer?
local file_new_count = nil
---@type integer?
local old_remaining = nil
---@type integer?
local new_remaining = nil
local is_unified_diff = false
local function flush_hunk()
if hunk_start and #hunk_lines > 0 then
@ -183,11 +188,15 @@ function M.parse_buffer(bufnr)
file_old_count = nil
file_new_start = nil
file_new_count = nil
old_remaining = nil
new_remaining = nil
end
for i, line in ipairs(lines) do
local filename = line:match('^[MADRCU%?!]%s+(.+)$') or line:match('^diff %-%-git a/.+ b/(.+)$')
local diff_git_file = line:match('^diff %-%-git a/.+ b/(.+)$')
local filename = line:match('^[MADRCU%?!]%s+(.+)$') or diff_git_file
if filename then
is_unified_diff = diff_git_file ~= nil
flush_hunk()
current_filename = filename
local cache_key = (repo_root or '') .. '\0' .. filename
@ -223,6 +232,8 @@ function M.parse_buffer(bufnr)
file_old_count = tonumber(hc) or 1
file_new_start = tonumber(hs2)
file_new_count = tonumber(hc2) or 1
old_remaining = file_old_count
new_remaining = file_new_count
end
else
local hs2, hc2 = line:match('%+(%d+),?(%d*) @@')
@ -243,6 +254,16 @@ function M.parse_buffer(bufnr)
local prefix = line:sub(1, 1)
if prefix == ' ' or prefix == '+' or prefix == '-' then
table.insert(hunk_lines, line)
if old_remaining and (prefix == ' ' or prefix == '-') then
old_remaining = old_remaining - 1
end
if new_remaining and (prefix == ' ' or prefix == '+') then
new_remaining = new_remaining - 1
end
elseif line == '' and is_unified_diff and old_remaining and old_remaining > 0 and new_remaining and new_remaining > 0 then
table.insert(hunk_lines, ' ')
old_remaining = old_remaining - 1
new_remaining = new_remaining - 1
elseif
line == ''
or line:match('^[MADRC%?!]%s+')

View file

@ -6,7 +6,7 @@ vim.g.loaded_diffs = 1
require('diffs.commands').setup()
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'fugitive', 'git', 'gitcommit' },
pattern = (vim.g.diffs or {}).filetypes or { 'fugitive', 'git', 'gitcommit' },
callback = function(args)
local diffs = require('diffs')
if args.match == 'git' and not diffs.is_fugitive_buffer(args.buf) then