Skip to content

Commit dd361c3

Browse files
authored
Merge pull request #1705 from AlexanderArvidsson/feature/1684-custom-popup-builder-callable
2 parents f56d490 + 0d63794 commit dd361c3

File tree

10 files changed

+46
-30
lines changed

10 files changed

+46
-30
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,7 @@ neogit.setup {
101101
-- Scope persisted settings on a per-project basis
102102
use_per_project_settings = true,
103103
-- Table of settings to never persist. Uses format "Filetype--cli-value"
104-
ignored_settings = {
105-
"NeogitPushPopup--force-with-lease",
106-
"NeogitPushPopup--force",
107-
"NeogitPullPopup--rebase",
108-
"NeogitCommitPopup--allow-empty",
109-
"NeogitRevertPopup--no-edit",
110-
},
104+
ignored_settings = {},
111105
-- Configure highlight group features
112106
highlight = {
113107
italic = true,

doc/neogit.txt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,7 @@ to Neovim users.
128128
-- Scope persisted settings on a per-project basis
129129
use_per_project_settings = true,
130130
-- Table of settings to never persist. Uses format "Filetype--cli-value"
131-
ignored_settings = {
132-
"NeogitPushPopup--force-with-lease",
133-
"NeogitPushPopup--force",
134-
"NeogitPullPopup--rebase",
135-
"NeogitCommitPopup--allow-empty",
136-
"NeogitRevertPopup--no-edit",
137-
},
131+
ignored_settings = {},
138132
-- Configure highlight group features
139133
highlight = {
140134
italic = true,
@@ -2144,5 +2138,21 @@ calling the setup function:
21442138
},
21452139
})
21462140
<
2141+
2142+
You can also customize existing popups via the Neogit config.
2143+
Below is an example of adding a custom switch, but you can use any function
2144+
from the builder API.
2145+
>lua
2146+
require("neogit").setup({
2147+
builders = {
2148+
NeogitPushPopup = function(builder)
2149+
builder:switch('m', 'merge_request.create', 'Create merge request', { cli_prefix = '-o ', persisted = false })
2150+
end,
2151+
},
2152+
})
2153+
2154+
Keep in mind that builder hooks are executed at the end of the popup
2155+
builder, so any switches or options added will be placed at the end.
2156+
21472157
------------------------------------------------------------------------------
21482158
vim:tw=78:ts=8:ft=help:norl:

lua/neogit/config.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ end
369369
---@field notification_icon? string
370370
---@field use_default_keymaps? boolean
371371
---@field highlight? HighlightOptions
372+
---@field builders? { [string]: fun(builder: PopupBuilder) }
372373

373374
---Returns the default Neogit configuration
374375
---@return NeogitConfig
@@ -550,13 +551,7 @@ function M.get_default_values()
550551
hidden = false,
551552
},
552553
},
553-
ignored_settings = {
554-
"NeogitPushPopup--force-with-lease",
555-
"NeogitPushPopup--force",
556-
"NeogitPullPopup--rebase",
557-
"NeogitPullPopup--force",
558-
"NeogitCommitPopup--allow-empty",
559-
},
554+
ignored_settings = {},
560555
mappings = {
561556
commit_editor = {
562557
["q"] = "Close",

lua/neogit/lib/popup/builder.lua

Lines changed: 12 additions & 0 deletions
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

Lines changed: 7 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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)