feat: add support for diff and other filetypes
This commit is contained in:
parent
9a0b812f69
commit
3990014a93
4 changed files with 40 additions and 4 deletions
|
|
@ -53,6 +53,7 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
|
||||||
vim.g.diffs = {
|
vim.g.diffs = {
|
||||||
debug = false,
|
debug = false,
|
||||||
hide_prefix = false,
|
hide_prefix = false,
|
||||||
|
filetypes = { 'fugitive', 'git', 'gitcommit' },
|
||||||
highlights = {
|
highlights = {
|
||||||
background = true,
|
background = true,
|
||||||
gutter = 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
|
is also enabled, the overlay inherits the line's
|
||||||
background color.
|
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)
|
{highlights} (table, default: see below)
|
||||||
Controls which highlight features are enabled.
|
Controls which highlight features are enabled.
|
||||||
See |diffs.Highlights| for fields.
|
See |diffs.Highlights| for fields.
|
||||||
|
|
@ -588,8 +600,8 @@ refresh({bufnr}) *diffs.refresh()*
|
||||||
IMPLEMENTATION *diffs-implementation*
|
IMPLEMENTATION *diffs-implementation*
|
||||||
|
|
||||||
Summary / commit detail views: ~
|
Summary / commit detail views: ~
|
||||||
1. `FileType fugitive` or `FileType git` (for `fugitive://` buffers)
|
1. `FileType` autocmd for configured filetypes (see {filetypes}) triggers
|
||||||
triggers |diffs.attach()|
|
|diffs.attach()|. For `git` buffers, only `fugitive://` URIs are attached.
|
||||||
2. The buffer is parsed to detect file headers (`M path/to/file`,
|
2. The buffer is parsed to detect file headers (`M path/to/file`,
|
||||||
`diff --git a/... b/...`) and hunk headers (`@@ -10,3 +10,4 @@`)
|
`diff --git a/... b/...`) and hunk headers (`@@ -10,3 +10,4 @@`)
|
||||||
3. For each hunk:
|
3. For each hunk:
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@
|
||||||
---@class diffs.Config
|
---@class diffs.Config
|
||||||
---@field debug boolean|string
|
---@field debug boolean|string
|
||||||
---@field hide_prefix boolean
|
---@field hide_prefix boolean
|
||||||
|
---@field filetypes string[]
|
||||||
---@field highlights diffs.Highlights
|
---@field highlights diffs.Highlights
|
||||||
---@field fugitive diffs.FugitiveConfig
|
---@field fugitive diffs.FugitiveConfig
|
||||||
---@field conflict diffs.ConflictConfig
|
---@field conflict diffs.ConflictConfig
|
||||||
|
|
@ -107,6 +108,7 @@ end
|
||||||
local default_config = {
|
local default_config = {
|
||||||
debug = false,
|
debug = false,
|
||||||
hide_prefix = false,
|
hide_prefix = false,
|
||||||
|
filetypes = { 'fugitive', 'git', 'gitcommit' },
|
||||||
highlights = {
|
highlights = {
|
||||||
background = true,
|
background = true,
|
||||||
gutter = true,
|
gutter = true,
|
||||||
|
|
@ -402,6 +404,7 @@ local function init()
|
||||||
'boolean or string (file path)',
|
'boolean or string (file path)',
|
||||||
},
|
},
|
||||||
hide_prefix = { opts.hide_prefix, 'boolean', true },
|
hide_prefix = { opts.hide_prefix, 'boolean', true },
|
||||||
|
filetypes = { opts.filetypes, 'table', true },
|
||||||
highlights = { opts.highlights, 'table', true },
|
highlights = { opts.highlights, 'table', true },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,11 @@ function M.parse_buffer(bufnr)
|
||||||
local file_new_start = nil
|
local file_new_start = nil
|
||||||
---@type integer?
|
---@type integer?
|
||||||
local file_new_count = nil
|
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()
|
local function flush_hunk()
|
||||||
if hunk_start and #hunk_lines > 0 then
|
if hunk_start and #hunk_lines > 0 then
|
||||||
|
|
@ -183,11 +188,15 @@ function M.parse_buffer(bufnr)
|
||||||
file_old_count = nil
|
file_old_count = nil
|
||||||
file_new_start = nil
|
file_new_start = nil
|
||||||
file_new_count = nil
|
file_new_count = nil
|
||||||
|
old_remaining = nil
|
||||||
|
new_remaining = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
for i, line in ipairs(lines) do
|
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
|
if filename then
|
||||||
|
is_unified_diff = diff_git_file ~= nil
|
||||||
flush_hunk()
|
flush_hunk()
|
||||||
current_filename = filename
|
current_filename = filename
|
||||||
local cache_key = (repo_root or '') .. '\0' .. 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_old_count = tonumber(hc) or 1
|
||||||
file_new_start = tonumber(hs2)
|
file_new_start = tonumber(hs2)
|
||||||
file_new_count = tonumber(hc2) or 1
|
file_new_count = tonumber(hc2) or 1
|
||||||
|
old_remaining = file_old_count
|
||||||
|
new_remaining = file_new_count
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local hs2, hc2 = line:match('%+(%d+),?(%d*) @@')
|
local hs2, hc2 = line:match('%+(%d+),?(%d*) @@')
|
||||||
|
|
@ -243,6 +254,16 @@ function M.parse_buffer(bufnr)
|
||||||
local prefix = line:sub(1, 1)
|
local prefix = line:sub(1, 1)
|
||||||
if prefix == ' ' or prefix == '+' or prefix == '-' then
|
if prefix == ' ' or prefix == '+' or prefix == '-' then
|
||||||
table.insert(hunk_lines, line)
|
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
|
elseif
|
||||||
line == ''
|
line == ''
|
||||||
or line:match('^[MADRC%?!]%s+')
|
or line:match('^[MADRC%?!]%s+')
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ vim.g.loaded_diffs = 1
|
||||||
require('diffs.commands').setup()
|
require('diffs.commands').setup()
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd('FileType', {
|
vim.api.nvim_create_autocmd('FileType', {
|
||||||
pattern = { 'fugitive', 'git', 'gitcommit' },
|
pattern = (vim.g.diffs or {}).filetypes or { 'fugitive', 'git', 'gitcommit' },
|
||||||
callback = function(args)
|
callback = function(args)
|
||||||
local diffs = require('diffs')
|
local diffs = require('diffs')
|
||||||
if args.match == 'git' and not diffs.is_fugitive_buffer(args.buf) then
|
if args.match == 'git' and not diffs.is_fugitive_buffer(args.buf) then
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue