Skip to content

Commit

Permalink
feat: auto load popups
Browse files Browse the repository at this point in the history
This allows less locations to edit when adding a new popup
  • Loading branch information
ten3roberts committed Jul 15, 2023
1 parent 6353bf0 commit 1b4c1a2
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 65 deletions.
17 changes: 0 additions & 17 deletions lua/neogit/popups.lua

This file was deleted.

7 changes: 7 additions & 0 deletions lua/neogit/popups/echo/actions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local M = {}
function M.echo(value)
local notification = require("neogit.lib.notification")
notification.create("Echo: " .. value)
end

return M
22 changes: 22 additions & 0 deletions lua/neogit/popups/echo/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local popup = require("neogit.lib.popup")
local actions = require("neogit.popups.echo.actions")

local M = {}

function M.create(...)
local args = { ... }
local p = popup.builder():name("NeogitEchoPopup")
for k, v in ipairs(args) do
p:action(tostring(k), tostring(v), function()
actions.echo(v)
end)
end

local p = p:build()

p:show()

return p
end

return M
32 changes: 15 additions & 17 deletions lua/neogit/popups/help/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@ M.popups = function(env)
local popups = require("neogit.popups")

return present {
{ "HelpPopup", "Help", popups.help.create },
{ "DiffPopup", "Diff", popups.diff.create },
{ "PullPopup", "Pull", popups.pull.create },
{ "RebasePopup", "Rebase", popups.rebase.create },
{ "MergePopup", "Merge", popups.merge.create },
{ "PushPopup", "Push", popups.push.create },
{ "CommitPopup", "Commit", popups.commit.create },
{ "LogPopup", "Log", popups.log.create },
{ "CherryPickPopup", "Apply", popups.cherry_pick.create },
{ "BranchPopup", "Branch", popups.branch.create },
{ "FetchPopup", "Fetch", popups.fetch.create },
{ "ResetPopup", "Reset", popups.reset.create },
{ "RevertPopup", "Revert", popups.revert.create },
{ "RemotePopup", "Remote", popups.remote.create },
{ "HelpPopup", "Help", popups.open("help") },
{ "DiffPopup", "Diff", popups.open("diff") },
{ "PullPopup", "Pull", popups.open("pull") },
{ "RebasePopup", "Rebase", popups.open("rebase") },
{ "MergePopup", "Merge", popups.open("merge") },
{ "PushPopup", "Push", popups.open("push") },
{ "CommitPopup", "Commit", popups.open("commit") },
{ "LogPopup", "Log", popups.open("log") },
{ "CherryPickPopup", "Apply", popups.open("cherry_pick") },
{ "BranchPopup", "Branch", popups.open("branch") },
{ "FetchPopup", "Fetch", popups.open("fetch") },
{ "ResetPopup", "Reset", popups.open("reset") },
{ "RevertPopup", "Revert", popups.open("revert") },
{ "RemotePopup", "Remote", popups.open("remote") },
{ "InitRepo", "Init", require("neogit.lib.git").init.init_repo },
{
"StashPopup",
"Stash",
function()
popups.stash.create(env.get_stash())
end,
popups.open("stash", env.get_stash),
},
{
"CommandHistory",
Expand Down
32 changes: 32 additions & 0 deletions lua/neogit/popups/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local M = {}

---@param name string
--@param get_args nil|fun(): any
--- Creates a curried function which will open the popup with the given name when called
--- Extra arguments are supplied to popup.`create()`
function M.open(name, get_args)
return function()
local ok, value = pcall(require, "neogit.popups." .. name)
if ok then
assert(value)
local args = {}

if get_args == "function" then
args = { get_args() }
end

value.create(table.unpack(args))
else
local notification = require("neogit.lib.notification")
notification.create(string.format("No such popup: %q", name), vim.log.levels.ERROR)
end
end
end

function M.test()
M.open("echo", function()
return "a", "b"
end)()
end

return M
69 changes: 38 additions & 31 deletions lua/neogit/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ end
--- These needs to be a function to avoid a circular dependency
--- between this module and the popup modules
local cmd_func_map = function()
local popups = require("neogit.popups")
return {
["Close"] = function()
M.status_buffer:close()
Expand Down Expand Up @@ -1085,21 +1086,13 @@ local cmd_func_map = function()
end
end
end),

["RefreshBuffer"] = function()
dispatch_refresh(true)
end,
["HelpPopup"] = function()
local line = M.status_buffer:get_current_line()

require("neogit.popups.help").create {
get_stash = function()
return {
name = line[1]:match("^(stash@{%d+})"),
}
end,
use_magit_keybindings = config.values.use_magit_keybindings,
}
end,
-- INTEGRATIONS --

["DiffAtFile"] = function()
if not config.ensure_integration("diffview") then
return
Expand All @@ -1111,32 +1104,46 @@ local cmd_func_map = function()
dv.open(section.name, item.name)
end
end,
["DiffPopup"] = require("neogit.popups.diff").create,
["PullPopup"] = require("neogit.popups.pull").create,
["RebasePopup"] = function()

--- POPUPS ---

["HelpPopup"] = popups.open("help", function()
local line = M.status_buffer:get_current_line()
require("neogit.popups.rebase").create { line[1]:match("^(%x%x%x%x%x%x%x+)") }
end,
["MergePopup"] = require("neogit.popups.merge").create,
["PushPopup"] = require("neogit.popups.push").create,
["CommitPopup"] = require("neogit.popups.commit").create,
["LogPopup"] = require("neogit.popups.log").create,

return {
get_stash = function()
return {
name = line[1]:match("^(stash@{%d+})"),
}
end,
use_magit_keybindings = config.values.use_magit_keybindings,
}
end),
["DiffPopup"] = popups.open("diff"),
["PullPopup"] = popups.open("pull"),
["RebasePopup"] = popups.open("rebase", function()
local line = M.status_buffer:get_current_line()
return { line[1]:match("^(%x%x%x%x%x%x%x+)") }
end),
["MergePopup"] = popups.open("merge"),
["PushPopup"] = popups.open("push"),
["CommitPopup"] = popups.open("commit"),
["LogPopup"] = popups.open("log"),
["CherryPickPopup"] = { "nv", a.void(cherry_pick), true },
["StashPopup"] = function()
["StashPopup"] = popups.open("stash", function()
local line = M.status_buffer:get_current_line()

require("neogit.popups.stash").create {
return {
name = line[1]:match("^(stash@{%d+})"),
}
end,
["RevertPopup"] = function()
end),
["RevertPopup"] = popups.open("revert", function()
local line = M.status_buffer:get_current_line()
require("neogit.popups.revert").create { commits = { line[1]:match("^(%x%x%x%x%x%x%x+)") } }
end,
["BranchPopup"] = require("neogit.popups.branch").create,
["FetchPopup"] = require("neogit.popups.fetch").create,
["RemotePopup"] = require("neogit.popups.remote").create,
["ResetPopup"] = require("neogit.popups.reset").create,
return { commits = { line[1]:match("^(%x%x%x%x%x%x%x+)") } }
end),
["BranchPopup"] = popups.open("neogit.popups.branch"),
["FetchPopup"] = popups.open("neogit.popups.fetch"),
["RemotePopup"] = popups.open("neogit.popups.remote"),
["ResetPopup"] = popups.open("neogit.popups.reset"),
}
end

Expand Down

0 comments on commit 1b4c1a2

Please sign in to comment.