## TODO 1. docs (vimdoc + readme) - this is a non-trivial feature 2. push luarocks version ## Problem diffs.nvim only activates on `fugitive`, `git`, and `gitcommit` filetypes. Neogit uses its own custom filetypes (`NeogitStatus`, `NeogitCommitView`, `NeogitDiffView`) and doesn't set `b:git_dir`, so the plugin never attaches and repo root resolution fails for filetype detection within diff hunks. ## Solution Two changes: 1. **`lua/diffs/init.lua`** — Add the three Neogit filetypes to the default `filetypes` list. The `FileType` autocmd in `plugin/diffs.lua` already handles them correctly since the `is_fugitive_buffer` guard only applies to the `git` filetype. 2. **`lua/diffs/parser.lua`** — Add a CWD-based fallback in `get_repo_root()`. After the existing `b:diffs_repo_root` and `b:git_dir` checks, fall back to `vim.fn.getcwd()` via `git.get_repo_root()` (already cached). Without this, the parser can't resolve filetypes for files in Neogit buffers. Neogit's expanded diffs use standard unified diff format, so the parser handles them without modification. Closes #110.
109 lines
3.8 KiB
Lua
109 lines
3.8 KiB
Lua
if vim.g.loaded_diffs then
|
|
return
|
|
end
|
|
vim.g.loaded_diffs = 1
|
|
|
|
require('diffs.commands').setup()
|
|
|
|
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' and not diffs.is_fugitive_buffer(args.buf) then
|
|
return
|
|
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' })
|