Skip to content

Commit ff5c82e

Browse files
feat: #1684 Ability to add custom popup switches
1 parent 97f83f1 commit ff5c82e

File tree

8 files changed

+28
-16
lines changed

8 files changed

+28
-16
lines changed

lua/neogit/config.lua

+2-7
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ end
359359
---@field notification_icon? string
360360
---@field use_default_keymaps? boolean
361361
---@field highlight? HighlightOptions
362+
---@field builders? { [string]: fun(builder: PopupBuilder) }
362363

363364
---Returns the default Neogit configuration
364365
---@return NeogitConfig
@@ -533,13 +534,7 @@ function M.get_default_values()
533534
hidden = false,
534535
},
535536
},
536-
ignored_settings = {
537-
"NeogitPushPopup--force-with-lease",
538-
"NeogitPushPopup--force",
539-
"NeogitPullPopup--rebase",
540-
"NeogitPullPopup--force",
541-
"NeogitCommitPopup--allow-empty",
542-
},
537+
ignored_settings = {},
543538
mappings = {
544539
commit_editor = {
545540
["q"] = "Close",

lua/neogit/lib/popup/builder.lua

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local git = require("neogit.lib.git")
22
local state = require("neogit.lib.state")
33
local util = require("neogit.lib.util")
44
local notification = require("neogit.lib.notification")
5+
local config = require("neogit.config")
56

67
---@class PopupBuilder
78
---@field state PopupState
@@ -54,6 +55,7 @@ local M = {}
5455
---@field type string
5556
---@field user_input boolean
5657
---@field value string?
58+
---@field persisted? boolean
5759

5860
---@class PopupConfig
5961
---@field id string
@@ -90,6 +92,7 @@ local M = {}
9092
---@field value? string Allows for pre-building cli flags that can be customized by user input
9193
---@field user_input? boolean If true, allows user to customize the value of the cli flag
9294
---@field dependent? string[] other switches/options with a state dependency on this one
95+
---@field persisted? boolean Allows overwriting the default 'true' to decide if this switch should be persisted
9396

9497
---@class PopupOptionOpts
9598
---@field key_prefix? string Allows overwriting the default '=' to set option
@@ -222,6 +225,10 @@ function M:switch(key, cli, description, opts)
222225
opts.cli_suffix = ""
223226
end
224227

228+
if opts.persisted == nil then
229+
opts.persisted = true
230+
end
231+
225232
local value
226233
if opts.enabled and opts.value then
227234
value = cli .. opts.value
@@ -253,6 +260,7 @@ function M:switch(key, cli, description, opts)
253260
cli_prefix = opts.cli_prefix,
254261
user_input = opts.user_input,
255262
cli_suffix = opts.cli_suffix,
263+
persisted = opts.persisted,
256264
options = opts.options,
257265
incompatible = util.build_reverse_lookup(opts.incompatible),
258266
dependent = util.build_reverse_lookup(opts.dependent),
@@ -467,6 +475,10 @@ function M:build()
467475
error("A popup needs to have a name!")
468476
end
469477

478+
if config.values.builders ~= nil and type(config.values.builders[self.state.name]) == "function" then
479+
config.values.builders[self.state.name](self)
480+
end
481+
470482
return self.builder_fn(self.state)
471483
end
472484

lua/neogit/lib/popup/init.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ function M:toggle_switch(switch)
108108
switch.cli = options[(index + 1)] or options[1]
109109
switch.value = switch.cli
110110
switch.enabled = switch.cli ~= ""
111-
state.set({ self.state.name, switch.cli_suffix }, switch.cli)
111+
112+
if switch.persisted ~= false then
113+
state.set({ self.state.name, switch.cli_suffix }, switch.cli)
114+
end
112115

113116
return
114117
end
@@ -127,7 +130,9 @@ function M:toggle_switch(switch)
127130
end
128131
end
129132

130-
state.set({ self.state.name, switch.cli }, switch.enabled)
133+
if switch.persisted ~= false then
134+
state.set({ self.state.name, switch.cli }, switch.enabled)
135+
end
131136

132137
-- Ensure that other switches/options that are incompatible with this one are disabled
133138
if switch.enabled and #switch.incompatible > 0 then

lua/neogit/popups/commit/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function M.create(env)
88
.builder()
99
:name("NeogitCommitPopup")
1010
:switch("a", "all", "Stage all modified and deleted files")
11-
:switch("e", "allow-empty", "Allow empty commit")
11+
:switch("e", "allow-empty", "Allow empty commit", { persisted = false })
1212
:switch("v", "verbose", "Show diff of changes to be committed")
1313
:switch("h", "no-verify", "Disable hooks")
1414
:switch("R", "reset-author", "Claim authorship and reset author date")

lua/neogit/popups/fetch/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function M.create()
1010
:name("NeogitFetchPopup")
1111
:switch("p", "prune", "Prune deleted branches")
1212
:switch("t", "tags", "Fetch all tags")
13-
:switch("F", "force", "force")
13+
:switch("F", "force", "force", { persisted = false })
1414
:group_heading("Fetch from")
1515
:action("p", git.branch.pushRemote_remote_label(), actions.fetch_pushremote)
1616
:action("u", git.branch.upstream_remote_label(), actions.fetch_upstream)

lua/neogit/popups/pull/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ function M.create()
2121
},
2222
})
2323
:switch("f", "ff-only", "Fast-forward only")
24-
:switch("r", "rebase", "Rebase local commits")
24+
:switch("r", "rebase", "Rebase local commits", { persisted = false })
2525
:switch("a", "autostash", "Autostash")
2626
:switch("t", "tags", "Fetch tags")
27-
:switch("F", "force", "Force")
27+
:switch("F", "force", "Force", { persisted = false })
2828
:group_heading_if(current ~= nil, "Pull into " .. current .. " from")
2929
:group_heading_if(not current, "Pull from")
3030
:action_if(current ~= nil, "p", git.branch.pushRemote_label(), actions.from_pushremote)

lua/neogit/popups/push/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function M.create(env)
1010
local p = popup
1111
.builder()
1212
:name("NeogitPushPopup")
13-
:switch("f", "force-with-lease", "Force with lease")
14-
:switch("F", "force", "Force")
13+
:switch("f", "force-with-lease", "Force with lease", { persisted = false })
14+
:switch("F", "force", "Force", { persisted = false })
1515
:switch("h", "no-verify", "Disable hooks")
1616
:switch("d", "dry-run", "Dry run")
1717
:switch("u", "set-upstream", "Set the upstream before pushing")

lua/neogit/popups/tag/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function M.create(env)
88
.builder()
99
:name("NeogitTagPopup")
1010
:arg_heading("Arguments")
11-
:switch("f", "force", "Force")
11+
:switch("f", "force", "Force", { persisted = false })
1212
:switch("a", "annotate", "Annotate")
1313
:switch("s", "sign", "Sign")
1414
:option("u", "local-user", "", "Sign as", { key_prefix = "-" })

0 commit comments

Comments
 (0)