diffs.nvim/plugin/diffs.lua
Barrett Ruth 43ae49ee46
fix: gate ft=git attachment on fugitive config toggle
Problem: the `is_fugitive_buffer` guard in the `FileType` callback
checked the buffer name for `fugitive://` without checking whether
the `fugitive` integration was enabled. This meant `ft=git` fugitive
buffers got highlighted even with `fugitive = false`.

Solution: check `get_fugitive_config()` before `is_fugitive_buffer()`.
When `fugitive = false` (default), no `ft=git` buffer gets through.
2026-03-06 11:12:53 -05:00

124 lines
4.2 KiB
Lua

if vim.g.loaded_diffs then
return
end
vim.g.loaded_diffs = 1
require('diffs.commands').setup()
local gs_cfg = (vim.g.diffs or {}).gitsigns
if gs_cfg == true or type(gs_cfg) == 'table' then
if not require('diffs.gitsigns').setup() then
vim.api.nvim_create_autocmd('User', {
pattern = 'GitAttach',
once = true,
callback = function()
require('diffs.gitsigns').setup()
end,
})
end
end
vim.api.nvim_create_autocmd('FileType', {
pattern = require('diffs').compute_filetypes(vim.g.diffs or {}),
callback = function(args)
local diffs = require('diffs')
if args.match == 'git' then
if not diffs.get_fugitive_config() or not diffs.is_fugitive_buffer(args.buf) then
return
end
end
diffs.attach(args.buf)
if args.match == 'fugitive' then
local fugitive_config = diffs.get_fugitive_config()
if fugitive_config.horizontal or fugitive_config.vertical then
require('diffs.fugitive').setup_keymaps(args.buf, fugitive_config)
end
end
end,
})
vim.api.nvim_create_autocmd('BufReadCmd', {
pattern = 'diffs://*',
callback = function(args)
require('diffs.commands').read_buffer(args.buf)
end,
})
vim.api.nvim_create_autocmd('BufReadPost', {
callback = function(args)
local conflict_config = require('diffs').get_conflict_config()
if conflict_config.enabled then
require('diffs.conflict').attach(args.buf, conflict_config)
end
end,
})
vim.api.nvim_create_autocmd('OptionSet', {
pattern = 'diff',
callback = function()
if vim.wo.diff then
require('diffs').attach_diff()
else
require('diffs').detach_diff()
end
end,
})
local cmds = require('diffs.commands')
vim.keymap.set('n', '<Plug>(diffs-gdiff)', function()
cmds.gdiff(nil, false)
end, { desc = 'Unified diff (horizontal)' })
vim.keymap.set('n', '<Plug>(diffs-gvdiff)', function()
cmds.gdiff(nil, true)
end, { desc = 'Unified diff (vertical)' })
local function conflict_action(fn)
local bufnr = vim.api.nvim_get_current_buf()
local config = require('diffs').get_conflict_config()
fn(bufnr, config)
end
vim.keymap.set('n', '<Plug>(diffs-conflict-ours)', function()
conflict_action(require('diffs.conflict').resolve_ours)
end, { desc = 'Accept current (ours) change' })
vim.keymap.set('n', '<Plug>(diffs-conflict-theirs)', function()
conflict_action(require('diffs.conflict').resolve_theirs)
end, { desc = 'Accept incoming (theirs) change' })
vim.keymap.set('n', '<Plug>(diffs-conflict-both)', function()
conflict_action(require('diffs.conflict').resolve_both)
end, { desc = 'Accept both changes' })
vim.keymap.set('n', '<Plug>(diffs-conflict-none)', function()
conflict_action(require('diffs.conflict').resolve_none)
end, { desc = 'Reject both changes' })
vim.keymap.set('n', '<Plug>(diffs-conflict-next)', function()
require('diffs.conflict').goto_next(vim.api.nvim_get_current_buf())
end, { desc = 'Jump to next conflict' })
vim.keymap.set('n', '<Plug>(diffs-conflict-prev)', function()
require('diffs.conflict').goto_prev(vim.api.nvim_get_current_buf())
end, { desc = 'Jump to previous conflict' })
local function merge_action(fn)
local bufnr = vim.api.nvim_get_current_buf()
local config = require('diffs').get_conflict_config()
fn(bufnr, config)
end
vim.keymap.set('n', '<Plug>(diffs-merge-ours)', function()
merge_action(require('diffs.merge').resolve_ours)
end, { desc = 'Accept ours in merge diff' })
vim.keymap.set('n', '<Plug>(diffs-merge-theirs)', function()
merge_action(require('diffs.merge').resolve_theirs)
end, { desc = 'Accept theirs in merge diff' })
vim.keymap.set('n', '<Plug>(diffs-merge-both)', function()
merge_action(require('diffs.merge').resolve_both)
end, { desc = 'Accept both in merge diff' })
vim.keymap.set('n', '<Plug>(diffs-merge-none)', function()
merge_action(require('diffs.merge').resolve_none)
end, { desc = 'Reject both in merge diff' })
vim.keymap.set('n', '<Plug>(diffs-merge-next)', function()
require('diffs.merge').goto_next(vim.api.nvim_get_current_buf())
end, { desc = 'Jump to next conflict hunk' })
vim.keymap.set('n', '<Plug>(diffs-merge-prev)', function()
require('diffs.merge').goto_prev(vim.api.nvim_get_current_buf())
end, { desc = 'Jump to previous conflict hunk' })