Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions doc/diffs.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,27 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
background color.

{fugitive} (boolean|table, default: false)
Enable vim-fugitive integration. Accepts
`true`, `false`, or a table with sub-options
(see |diffs.FugitiveConfig|). When enabled,
the `fugitive` filetype is active and status
buffer keymaps are registered. >lua
Enable vim-fugitive integration. Pass `true`
for defaults, `false` to disable, or a table
with sub-options (see |diffs.FugitiveConfig|).
Passing a table implicitly enables the
integration — no `enabled` field needed.
When active, the `fugitive` filetype is
registered and status buffer keymaps are set. >lua
vim.g.diffs = { fugitive = true }
vim.g.diffs = {
fugitive = { horizontal = 'dd' },
}
<

{neogit} (boolean|table, default: false)
Enable Neogit integration. Accepts `true`,
`false`, or `{ enabled = false }`. When
enabled, `NeogitStatus`, `NeogitCommitView`,
and `NeogitDiffView` filetypes are active and
Neogit highlight overrides are applied. See
|diffs-neogit|. >lua
vim.g.diffs = { neogit = false }
Enable Neogit integration. Pass `true` or
`{}` to enable, `false` to disable. When
active, `NeogitStatus`, `NeogitCommitView`,
and `NeogitDiffView` filetypes are registered
and Neogit highlight overrides are applied.
See |diffs-neogit|. >lua
vim.g.diffs = { neogit = true }
<

{extra_filetypes} (table, default: {})
Expand Down Expand Up @@ -438,20 +440,15 @@ Configuration: ~
>lua
vim.g.diffs = {
fugitive = {
enabled = true, -- false to disable fugitive integration entirely
horizontal = 'du', -- keymap for horizontal split, false to disable
vertical = 'dU', -- keymap for vertical split, false to disable
},
}
<
Fields: ~
{enabled} (boolean, default: false)
Enable fugitive integration. When false, the
`fugitive` filetype is excluded and no status
buffer keymaps are registered. Shorthand:
`fugitive = false` is equivalent to
`fugitive = { enabled = false }`.
Passing a table enables fugitive integration. Use `fugitive = false`
to disable. There is no `enabled` field; table presence is sufficient.

Fields: ~
{horizontal} (string|false, default: 'du')
Keymap for unified diff in horizontal split.
Set to `false` to disable.
Expand Down
83 changes: 43 additions & 40 deletions lua/diffs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@
---@field priorities diffs.PrioritiesConfig

---@class diffs.FugitiveConfig
---@field enabled boolean
---@field horizontal string|false
---@field vertical string|false

---@class diffs.NeogitConfig
---@field enabled boolean

---@class diffs.ConflictKeymaps
---@field ours string|false
Expand All @@ -63,8 +61,8 @@
---@field filetypes? string[] @deprecated use fugitive, neogit, extra_filetypes
---@field extra_filetypes string[]
---@field highlights diffs.Highlights
---@field fugitive diffs.FugitiveConfig
---@field neogit diffs.NeogitConfig
---@field fugitive diffs.FugitiveConfig|false
---@field neogit diffs.NeogitConfig|false
---@field conflict diffs.ConflictConfig

---@class diffs
Expand Down Expand Up @@ -142,14 +140,8 @@ local default_config = {
char_bg = 201,
},
},
fugitive = {
enabled = false,
horizontal = 'du',
vertical = 'dU',
},
neogit = {
enabled = false,
},
fugitive = false,
neogit = false,
conflict = {
enabled = true,
disable_diagnostics = true,
Expand Down Expand Up @@ -492,24 +484,28 @@ local function init()
end
end

local fugitive_defaults = { horizontal = 'du', vertical = 'dU' }
if opts.fugitive == true then
opts.fugitive = { enabled = true }
elseif opts.fugitive == false then
opts.fugitive = { enabled = false }
elseif opts.fugitive == nil then
opts.fugitive = nil
elseif type(opts.fugitive) == 'table' and opts.fugitive.enabled == nil then
opts.fugitive.enabled = true
opts.fugitive = vim.deepcopy(fugitive_defaults)
elseif type(opts.fugitive) == 'table' then
if opts.fugitive.enabled == false then
opts.fugitive = false
else
---@diagnostic disable-next-line: inject-field
opts.fugitive.enabled = nil
opts.fugitive = vim.tbl_extend('keep', opts.fugitive, fugitive_defaults)
end
end

if opts.neogit == true then
opts.neogit = { enabled = true }
elseif opts.neogit == false then
opts.neogit = { enabled = false }
elseif opts.neogit == nil then
opts.neogit = nil
elseif type(opts.neogit) == 'table' and opts.neogit.enabled == nil then
opts.neogit.enabled = true
opts.neogit = {}
elseif type(opts.neogit) == 'table' then
if opts.neogit.enabled == false then
opts.neogit = false
else
---@diagnostic disable-next-line: inject-field
opts.neogit.enabled = nil
end
end

vim.validate({
Expand All @@ -521,8 +517,20 @@ local function init()
'boolean or string (file path)',
},
hide_prefix = { opts.hide_prefix, 'boolean', true },
fugitive = { opts.fugitive, 'table', true },
neogit = { opts.neogit, 'table', true },
fugitive = {
opts.fugitive,
function(v)
return v == nil or v == false or type(v) == 'table'
end,
'table or false',
},
neogit = {
opts.neogit,
function(v)
return v == nil or v == false or type(v) == 'table'
end,
'table or false',
},
extra_filetypes = { opts.extra_filetypes, 'table', true },
highlights = { opts.highlights, 'table', true },
})
Expand Down Expand Up @@ -589,18 +597,19 @@ local function init()
end
end

if opts.fugitive then
if type(opts.fugitive) == 'table' then
---@type diffs.FugitiveConfig
local fug = opts.fugitive
vim.validate({
['fugitive.enabled'] = { opts.fugitive.enabled, 'boolean', true },
['fugitive.horizontal'] = {
opts.fugitive.horizontal,
fug.horizontal,
function(v)
return v == nil or v == false or type(v) == 'string'
end,
'string or false',
},
['fugitive.vertical'] = {
opts.fugitive.vertical,
fug.vertical,
function(v)
return v == nil or v == false or type(v) == 'string'
end,
Expand All @@ -609,12 +618,6 @@ local function init()
})
end

if opts.neogit then
vim.validate({
['neogit.enabled'] = { opts.neogit.enabled, 'boolean', true },
})
end

if opts.conflict then
vim.validate({
['conflict.enabled'] = { opts.conflict.enabled, 'boolean', true },
Expand Down Expand Up @@ -830,7 +833,7 @@ function M.attach(bufnr)
end
attached_buffers[bufnr] = true

if not neogit_attached and config.neogit.enabled and vim.bo[bufnr].filetype:match('^Neogit') then
if not neogit_attached and config.neogit and vim.bo[bufnr].filetype:match('^Neogit') then
neogit_attached = true
vim.schedule(override_neogit_highlights)
end
Expand Down Expand Up @@ -894,7 +897,7 @@ function M.detach_diff()
end
end

---@return diffs.FugitiveConfig
---@return diffs.FugitiveConfig|false
function M.get_fugitive_config()
init()
return config.fugitive
Expand Down
89 changes: 89 additions & 0 deletions spec/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,95 @@ describe('diffs', function()
end)
end)

describe('compute_filetypes', function()
local compute = diffs.compute_filetypes

it('returns core filetypes with empty config', function()
local fts = compute({})
assert.are.same({ 'git', 'gitcommit' }, fts)
end)

it('includes fugitive when fugitive = true', function()
local fts = compute({ fugitive = true })
assert.is_true(vim.tbl_contains(fts, 'fugitive'))
end)

it('includes fugitive when fugitive is a table', function()
local fts = compute({ fugitive = { horizontal = 'dd' } })
assert.is_true(vim.tbl_contains(fts, 'fugitive'))
end)

it('excludes fugitive when fugitive = false', function()
local fts = compute({ fugitive = false })
assert.is_false(vim.tbl_contains(fts, 'fugitive'))
end)

it('excludes fugitive when fugitive is nil', function()
local fts = compute({})
assert.is_false(vim.tbl_contains(fts, 'fugitive'))
end)

it('includes neogit filetypes when neogit = true', function()
local fts = compute({ neogit = true })
assert.is_true(vim.tbl_contains(fts, 'NeogitStatus'))
assert.is_true(vim.tbl_contains(fts, 'NeogitCommitView'))
assert.is_true(vim.tbl_contains(fts, 'NeogitDiffView'))
end)

it('includes neogit filetypes when neogit is a table', function()
local fts = compute({ neogit = {} })
assert.is_true(vim.tbl_contains(fts, 'NeogitStatus'))
end)

it('excludes neogit when neogit = false', function()
local fts = compute({ neogit = false })
assert.is_false(vim.tbl_contains(fts, 'NeogitStatus'))
end)

it('excludes neogit when neogit is nil', function()
local fts = compute({})
assert.is_false(vim.tbl_contains(fts, 'NeogitStatus'))
end)

it('includes extra_filetypes', function()
local fts = compute({ extra_filetypes = { 'diff' } })
assert.is_true(vim.tbl_contains(fts, 'diff'))
end)

it('combines fugitive, neogit, and extra_filetypes', function()
local fts = compute({ fugitive = true, neogit = true, extra_filetypes = { 'diff' } })
assert.is_true(vim.tbl_contains(fts, 'git'))
assert.is_true(vim.tbl_contains(fts, 'fugitive'))
assert.is_true(vim.tbl_contains(fts, 'NeogitStatus'))
assert.is_true(vim.tbl_contains(fts, 'diff'))
end)

it('returns custom filetypes when filetypes key is set', function()
local fts = compute({ filetypes = { 'custom' } })
assert.are.same({ 'custom' }, fts)
end)

it('backward compat: includes fugitive when enabled = true in table', function()
local fts = compute({ fugitive = { enabled = true } })
assert.is_true(vim.tbl_contains(fts, 'fugitive'))
end)

it('backward compat: excludes fugitive when enabled = false in table', function()
local fts = compute({ fugitive = { enabled = false } })
assert.is_false(vim.tbl_contains(fts, 'fugitive'))
end)

it('backward compat: includes neogit when enabled = true in table', function()
local fts = compute({ neogit = { enabled = true } })
assert.is_true(vim.tbl_contains(fts, 'NeogitStatus'))
end)

it('backward compat: excludes neogit when enabled = false in table', function()
local fts = compute({ neogit = { enabled = false } })
assert.is_false(vim.tbl_contains(fts, 'NeogitStatus'))
end)
end)

describe('diff mode', function()
local function create_diff_window()
vim.cmd('new')
Expand Down
Loading