Skip to content

feat: #1684 Ability to add custom popup switches #1705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,7 @@ neogit.setup {
-- Scope persisted settings on a per-project basis
use_per_project_settings = true,
-- Table of settings to never persist. Uses format "Filetype--cli-value"
ignored_settings = {
"NeogitPushPopup--force-with-lease",
"NeogitPushPopup--force",
"NeogitPullPopup--rebase",
"NeogitCommitPopup--allow-empty",
"NeogitRevertPopup--no-edit",
},
ignored_settings = {},
-- Configure highlight group features
highlight = {
italic = true,
Expand Down
24 changes: 17 additions & 7 deletions doc/neogit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ to Neovim users.
-- Scope persisted settings on a per-project basis
use_per_project_settings = true,
-- Table of settings to never persist. Uses format "Filetype--cli-value"
ignored_settings = {
"NeogitPushPopup--force-with-lease",
"NeogitPushPopup--force",
"NeogitPullPopup--rebase",
"NeogitCommitPopup--allow-empty",
"NeogitRevertPopup--no-edit",
},
ignored_settings = {},
-- Configure highlight group features
highlight = {
italic = true,
Expand Down Expand Up @@ -2144,5 +2138,21 @@ calling the setup function:
},
})
<

You can also customize existing popups via the Neogit config.
Below is an example of adding a custom switch, but you can use any function
from the builder API.
>lua
require("neogit").setup({
builders = {
NeogitPushPopup = function(builder)
builder:switch('m', 'merge_request.create', 'Create merge request', { cli_prefix = '-o ', persisted = false })
end,
},
})

Keep in mind that builder hooks are executed at the end of the popup
builder, so any switches or options added will be placed at the end.

------------------------------------------------------------------------------
vim:tw=78:ts=8:ft=help:norl:
9 changes: 2 additions & 7 deletions lua/neogit/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ end
---@field notification_icon? string
---@field use_default_keymaps? boolean
---@field highlight? HighlightOptions
---@field builders? { [string]: fun(builder: PopupBuilder) }

---Returns the default Neogit configuration
---@return NeogitConfig
Expand Down Expand Up @@ -550,13 +551,7 @@ function M.get_default_values()
hidden = false,
},
},
ignored_settings = {
"NeogitPushPopup--force-with-lease",
"NeogitPushPopup--force",
"NeogitPullPopup--rebase",
"NeogitPullPopup--force",
"NeogitCommitPopup--allow-empty",
},
ignored_settings = {},
mappings = {
commit_editor = {
["q"] = "Close",
Expand Down
12 changes: 12 additions & 0 deletions lua/neogit/lib/popup/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local git = require("neogit.lib.git")
local state = require("neogit.lib.state")
local util = require("neogit.lib.util")
local notification = require("neogit.lib.notification")
local config = require("neogit.config")

---@class PopupBuilder
---@field state PopupState
Expand Down Expand Up @@ -54,6 +55,7 @@ local M = {}
---@field type string
---@field user_input boolean
---@field value string?
---@field persisted? boolean

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

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

if opts.persisted == nil then
opts.persisted = true
end

local value
if opts.enabled and opts.value then
value = cli .. opts.value
Expand Down Expand Up @@ -253,6 +260,7 @@ function M:switch(key, cli, description, opts)
cli_prefix = opts.cli_prefix,
user_input = opts.user_input,
cli_suffix = opts.cli_suffix,
persisted = opts.persisted,
options = opts.options,
incompatible = util.build_reverse_lookup(opts.incompatible),
dependent = util.build_reverse_lookup(opts.dependent),
Expand Down Expand Up @@ -467,6 +475,10 @@ function M:build()
error("A popup needs to have a name!")
end

if config.values.builders ~= nil and type(config.values.builders[self.state.name]) == "function" then
config.values.builders[self.state.name](self)
end

return self.builder_fn(self.state)
end

Expand Down
9 changes: 7 additions & 2 deletions lua/neogit/lib/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ function M:toggle_switch(switch)
switch.cli = options[(index + 1)] or options[1]
switch.value = switch.cli
switch.enabled = switch.cli ~= ""
state.set({ self.state.name, switch.cli_suffix }, switch.cli)

if switch.persisted ~= false then
state.set({ self.state.name, switch.cli_suffix }, switch.cli)
end

return
end
Expand All @@ -127,7 +130,9 @@ function M:toggle_switch(switch)
end
end

state.set({ self.state.name, switch.cli }, switch.enabled)
if switch.persisted ~= false then
state.set({ self.state.name, switch.cli }, switch.enabled)
end

-- Ensure that other switches/options that are incompatible with this one are disabled
if switch.enabled and #switch.incompatible > 0 then
Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/popups/commit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function M.create(env)
.builder()
:name("NeogitCommitPopup")
:switch("a", "all", "Stage all modified and deleted files")
:switch("e", "allow-empty", "Allow empty commit")
:switch("e", "allow-empty", "Allow empty commit", { persisted = false })
:switch("v", "verbose", "Show diff of changes to be committed")
:switch("h", "no-verify", "Disable hooks")
:switch("R", "reset-author", "Claim authorship and reset author date")
Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/popups/fetch/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function M.create()
:name("NeogitFetchPopup")
:switch("p", "prune", "Prune deleted branches")
:switch("t", "tags", "Fetch all tags")
:switch("F", "force", "force")
:switch("F", "force", "force", { persisted = false })
:group_heading("Fetch from")
:action("p", git.branch.pushRemote_remote_label(), actions.fetch_pushremote)
:action("u", git.branch.upstream_remote_label(), actions.fetch_upstream)
Expand Down
4 changes: 2 additions & 2 deletions lua/neogit/popups/pull/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function M.create()
},
})
:switch("f", "ff-only", "Fast-forward only")
:switch("r", "rebase", "Rebase local commits")
:switch("r", "rebase", "Rebase local commits", { persisted = false })
:switch("a", "autostash", "Autostash")
:switch("t", "tags", "Fetch tags")
:switch("F", "force", "Force")
:switch("F", "force", "Force", { persisted = false })
:group_heading_if(current ~= nil, "Pull into " .. current .. " from")
:group_heading_if(not current, "Pull from")
:action_if(current ~= nil, "p", git.branch.pushRemote_label(), actions.from_pushremote)
Expand Down
4 changes: 2 additions & 2 deletions lua/neogit/popups/push/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function M.create(env)
local p = popup
.builder()
:name("NeogitPushPopup")
:switch("f", "force-with-lease", "Force with lease")
:switch("F", "force", "Force")
:switch("f", "force-with-lease", "Force with lease", { persisted = false })
:switch("F", "force", "Force", { persisted = false })
:switch("h", "no-verify", "Disable hooks")
:switch("d", "dry-run", "Dry run")
:switch("u", "set-upstream", "Set the upstream before pushing")
Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/popups/tag/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function M.create(env)
.builder()
:name("NeogitTagPopup")
:arg_heading("Arguments")
:switch("f", "force", "Force")
:switch("f", "force", "Force", { persisted = false })
:switch("a", "annotate", "Annotate")
:switch("s", "sign", "Sign")
:option("u", "local-user", "", "Sign as", { key_prefix = "-" })
Expand Down
Loading