From bd7a44a7f32fc6cb0ca10f7c736f1f1185fd55f2 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 15 Feb 2026 19:37:14 -0500 Subject: [PATCH] refactor: remove enabled field from fugitive/neogit config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: `{ enabled = false }` collapses to `false`, and `enabled = true` or nil strips the field silently. Default config changed from `{ enabled = false, ... }` tables to just `false`. Added 20 compute_filetypes tests covering all config shapes. Updated docs. --- doc/diffs.nvim.txt | 37 +++++++++---------- lua/diffs/init.lua | 83 +++++++++++++++++++++--------------------- spec/init_spec.lua | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 60 deletions(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 116ca3e..1b816f1 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -115,11 +115,13 @@ 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' }, @@ -127,13 +129,13 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: < {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: {}) @@ -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. diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 4140366..a293cf9 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -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 @@ -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 @@ -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, @@ -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({ @@ -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 }, }) @@ -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, @@ -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 }, @@ -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 @@ -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 diff --git a/spec/init_spec.lua b/spec/init_spec.lua index 139fb0d..33b93fc 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -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')