refactor: remove enabled field from fugitive/neogit config (#126)
## Problem Users had to pass `enabled = true` or `enabled = false` inside fugitive/neogit config tables, which was redundant — table presence already implied the integration should be active. ## Solution Remove the `enabled` field from the public API. Table presence now implies enabled, `false` disables, `true` expands to sub-defaults. The `enabled` field is still accepted for backward compatibility. Added 20 `compute_filetypes` tests covering all config shapes (true, false, table, nil, backward-compat enabled field). Updated docs and type annotations.
This commit is contained in:
parent
a00993820f
commit
cbc93f9eaa
3 changed files with 149 additions and 60 deletions
|
|
@ -115,11 +115,13 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
|
||||||
background color.
|
background color.
|
||||||
|
|
||||||
{fugitive} (boolean|table, default: false)
|
{fugitive} (boolean|table, default: false)
|
||||||
Enable vim-fugitive integration. Accepts
|
Enable vim-fugitive integration. Pass `true`
|
||||||
`true`, `false`, or a table with sub-options
|
for defaults, `false` to disable, or a table
|
||||||
(see |diffs.FugitiveConfig|). When enabled,
|
with sub-options (see |diffs.FugitiveConfig|).
|
||||||
the `fugitive` filetype is active and status
|
Passing a table implicitly enables the
|
||||||
buffer keymaps are registered. >lua
|
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 = true }
|
||||||
vim.g.diffs = {
|
vim.g.diffs = {
|
||||||
fugitive = { horizontal = 'dd' },
|
fugitive = { horizontal = 'dd' },
|
||||||
|
|
@ -127,13 +129,13 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads:
|
||||||
<
|
<
|
||||||
|
|
||||||
{neogit} (boolean|table, default: false)
|
{neogit} (boolean|table, default: false)
|
||||||
Enable Neogit integration. Accepts `true`,
|
Enable Neogit integration. Pass `true` or
|
||||||
`false`, or `{ enabled = false }`. When
|
`{}` to enable, `false` to disable. When
|
||||||
enabled, `NeogitStatus`, `NeogitCommitView`,
|
active, `NeogitStatus`, `NeogitCommitView`,
|
||||||
and `NeogitDiffView` filetypes are active and
|
and `NeogitDiffView` filetypes are registered
|
||||||
Neogit highlight overrides are applied. See
|
and Neogit highlight overrides are applied.
|
||||||
|diffs-neogit|. >lua
|
See |diffs-neogit|. >lua
|
||||||
vim.g.diffs = { neogit = false }
|
vim.g.diffs = { neogit = true }
|
||||||
<
|
<
|
||||||
|
|
||||||
{extra_filetypes} (table, default: {})
|
{extra_filetypes} (table, default: {})
|
||||||
|
|
@ -438,20 +440,15 @@ Configuration: ~
|
||||||
>lua
|
>lua
|
||||||
vim.g.diffs = {
|
vim.g.diffs = {
|
||||||
fugitive = {
|
fugitive = {
|
||||||
enabled = true, -- false to disable fugitive integration entirely
|
|
||||||
horizontal = 'du', -- keymap for horizontal split, false to disable
|
horizontal = 'du', -- keymap for horizontal split, false to disable
|
||||||
vertical = 'dU', -- keymap for vertical split, false to disable
|
vertical = 'dU', -- keymap for vertical split, false to disable
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
<
|
<
|
||||||
Fields: ~
|
Passing a table enables fugitive integration. Use `fugitive = false`
|
||||||
{enabled} (boolean, default: false)
|
to disable. There is no `enabled` field; table presence is sufficient.
|
||||||
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 }`.
|
|
||||||
|
|
||||||
|
Fields: ~
|
||||||
{horizontal} (string|false, default: 'du')
|
{horizontal} (string|false, default: 'du')
|
||||||
Keymap for unified diff in horizontal split.
|
Keymap for unified diff in horizontal split.
|
||||||
Set to `false` to disable.
|
Set to `false` to disable.
|
||||||
|
|
|
||||||
|
|
@ -33,12 +33,10 @@
|
||||||
---@field priorities diffs.PrioritiesConfig
|
---@field priorities diffs.PrioritiesConfig
|
||||||
|
|
||||||
---@class diffs.FugitiveConfig
|
---@class diffs.FugitiveConfig
|
||||||
---@field enabled boolean
|
|
||||||
---@field horizontal string|false
|
---@field horizontal string|false
|
||||||
---@field vertical string|false
|
---@field vertical string|false
|
||||||
|
|
||||||
---@class diffs.NeogitConfig
|
---@class diffs.NeogitConfig
|
||||||
---@field enabled boolean
|
|
||||||
|
|
||||||
---@class diffs.ConflictKeymaps
|
---@class diffs.ConflictKeymaps
|
||||||
---@field ours string|false
|
---@field ours string|false
|
||||||
|
|
@ -63,8 +61,8 @@
|
||||||
---@field filetypes? string[] @deprecated use fugitive, neogit, extra_filetypes
|
---@field filetypes? string[] @deprecated use fugitive, neogit, extra_filetypes
|
||||||
---@field extra_filetypes string[]
|
---@field extra_filetypes string[]
|
||||||
---@field highlights diffs.Highlights
|
---@field highlights diffs.Highlights
|
||||||
---@field fugitive diffs.FugitiveConfig
|
---@field fugitive diffs.FugitiveConfig|false
|
||||||
---@field neogit diffs.NeogitConfig
|
---@field neogit diffs.NeogitConfig|false
|
||||||
---@field conflict diffs.ConflictConfig
|
---@field conflict diffs.ConflictConfig
|
||||||
|
|
||||||
---@class diffs
|
---@class diffs
|
||||||
|
|
@ -142,14 +140,8 @@ local default_config = {
|
||||||
char_bg = 201,
|
char_bg = 201,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
fugitive = {
|
fugitive = false,
|
||||||
enabled = false,
|
neogit = false,
|
||||||
horizontal = 'du',
|
|
||||||
vertical = 'dU',
|
|
||||||
},
|
|
||||||
neogit = {
|
|
||||||
enabled = false,
|
|
||||||
},
|
|
||||||
conflict = {
|
conflict = {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
disable_diagnostics = true,
|
disable_diagnostics = true,
|
||||||
|
|
@ -492,24 +484,28 @@ local function init()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local fugitive_defaults = { horizontal = 'du', vertical = 'dU' }
|
||||||
if opts.fugitive == true then
|
if opts.fugitive == true then
|
||||||
opts.fugitive = { enabled = true }
|
opts.fugitive = vim.deepcopy(fugitive_defaults)
|
||||||
elseif opts.fugitive == false then
|
elseif type(opts.fugitive) == 'table' then
|
||||||
opts.fugitive = { enabled = false }
|
if opts.fugitive.enabled == false then
|
||||||
elseif opts.fugitive == nil then
|
opts.fugitive = false
|
||||||
opts.fugitive = nil
|
else
|
||||||
elseif type(opts.fugitive) == 'table' and opts.fugitive.enabled == nil then
|
---@diagnostic disable-next-line: inject-field
|
||||||
opts.fugitive.enabled = true
|
opts.fugitive.enabled = nil
|
||||||
|
opts.fugitive = vim.tbl_extend('keep', opts.fugitive, fugitive_defaults)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts.neogit == true then
|
if opts.neogit == true then
|
||||||
opts.neogit = { enabled = true }
|
opts.neogit = {}
|
||||||
elseif opts.neogit == false then
|
elseif type(opts.neogit) == 'table' then
|
||||||
opts.neogit = { enabled = false }
|
if opts.neogit.enabled == false then
|
||||||
elseif opts.neogit == nil then
|
opts.neogit = false
|
||||||
opts.neogit = nil
|
else
|
||||||
elseif type(opts.neogit) == 'table' and opts.neogit.enabled == nil then
|
---@diagnostic disable-next-line: inject-field
|
||||||
opts.neogit.enabled = true
|
opts.neogit.enabled = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.validate({
|
vim.validate({
|
||||||
|
|
@ -521,8 +517,20 @@ 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 },
|
||||||
fugitive = { opts.fugitive, 'table', true },
|
fugitive = {
|
||||||
neogit = { opts.neogit, 'table', true },
|
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 },
|
extra_filetypes = { opts.extra_filetypes, 'table', true },
|
||||||
highlights = { opts.highlights, 'table', true },
|
highlights = { opts.highlights, 'table', true },
|
||||||
})
|
})
|
||||||
|
|
@ -589,18 +597,19 @@ local function init()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts.fugitive then
|
if type(opts.fugitive) == 'table' then
|
||||||
|
---@type diffs.FugitiveConfig
|
||||||
|
local fug = opts.fugitive
|
||||||
vim.validate({
|
vim.validate({
|
||||||
['fugitive.enabled'] = { opts.fugitive.enabled, 'boolean', true },
|
|
||||||
['fugitive.horizontal'] = {
|
['fugitive.horizontal'] = {
|
||||||
opts.fugitive.horizontal,
|
fug.horizontal,
|
||||||
function(v)
|
function(v)
|
||||||
return v == nil or v == false or type(v) == 'string'
|
return v == nil or v == false or type(v) == 'string'
|
||||||
end,
|
end,
|
||||||
'string or false',
|
'string or false',
|
||||||
},
|
},
|
||||||
['fugitive.vertical'] = {
|
['fugitive.vertical'] = {
|
||||||
opts.fugitive.vertical,
|
fug.vertical,
|
||||||
function(v)
|
function(v)
|
||||||
return v == nil or v == false or type(v) == 'string'
|
return v == nil or v == false or type(v) == 'string'
|
||||||
end,
|
end,
|
||||||
|
|
@ -609,12 +618,6 @@ local function init()
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts.neogit then
|
|
||||||
vim.validate({
|
|
||||||
['neogit.enabled'] = { opts.neogit.enabled, 'boolean', true },
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
if opts.conflict then
|
if opts.conflict then
|
||||||
vim.validate({
|
vim.validate({
|
||||||
['conflict.enabled'] = { opts.conflict.enabled, 'boolean', true },
|
['conflict.enabled'] = { opts.conflict.enabled, 'boolean', true },
|
||||||
|
|
@ -830,7 +833,7 @@ function M.attach(bufnr)
|
||||||
end
|
end
|
||||||
attached_buffers[bufnr] = true
|
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
|
neogit_attached = true
|
||||||
vim.schedule(override_neogit_highlights)
|
vim.schedule(override_neogit_highlights)
|
||||||
end
|
end
|
||||||
|
|
@ -894,7 +897,7 @@ function M.detach_diff()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return diffs.FugitiveConfig
|
---@return diffs.FugitiveConfig|false
|
||||||
function M.get_fugitive_config()
|
function M.get_fugitive_config()
|
||||||
init()
|
init()
|
||||||
return config.fugitive
|
return config.fugitive
|
||||||
|
|
|
||||||
|
|
@ -328,6 +328,95 @@ describe('diffs', function()
|
||||||
end)
|
end)
|
||||||
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()
|
describe('diff mode', function()
|
||||||
local function create_diff_window()
|
local function create_diff_window()
|
||||||
vim.cmd('new')
|
vim.cmd('new')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue