From 1b4c1a25d6e40a4ac43775d386cd7cafc83468ca Mon Sep 17 00:00:00 2001 From: Tei Roberts Date: Sat, 15 Jul 2023 13:33:59 +0200 Subject: [PATCH] feat: auto load popups This allows less locations to edit when adding a new popup --- lua/neogit/popups.lua | 17 -------- lua/neogit/popups/echo/actions.lua | 7 +++ lua/neogit/popups/echo/init.lua | 22 ++++++++++ lua/neogit/popups/help/actions.lua | 32 +++++++------- lua/neogit/popups/init.lua | 32 ++++++++++++++ lua/neogit/status.lua | 69 ++++++++++++++++-------------- 6 files changed, 114 insertions(+), 65 deletions(-) delete mode 100644 lua/neogit/popups.lua create mode 100644 lua/neogit/popups/echo/actions.lua create mode 100644 lua/neogit/popups/echo/init.lua create mode 100644 lua/neogit/popups/init.lua diff --git a/lua/neogit/popups.lua b/lua/neogit/popups.lua deleted file mode 100644 index a23174eae..000000000 --- a/lua/neogit/popups.lua +++ /dev/null @@ -1,17 +0,0 @@ -return { - branch = require("neogit.popups.branch"), - cherry_pick = require("neogit.popups.cherry_pick"), - commit = require("neogit.popups.commit"), - diff = require("neogit.popups.diff"), - fetch = require("neogit.popups.fetch"), - help = require("neogit.popups.help"), - log = require("neogit.popups.log"), - merge = require("neogit.popups.merge"), - pull = require("neogit.popups.pull"), - push = require("neogit.popups.push"), - rebase = require("neogit.popups.rebase"), - remote = require("neogit.popups.remote"), - reset = require("neogit.popups.reset"), - revert = require("neogit.popups.revert"), - stash = require("neogit.popups.stash"), -} diff --git a/lua/neogit/popups/echo/actions.lua b/lua/neogit/popups/echo/actions.lua new file mode 100644 index 000000000..7ccf98ba9 --- /dev/null +++ b/lua/neogit/popups/echo/actions.lua @@ -0,0 +1,7 @@ +local M = {} +function M.echo(value) + local notification = require("neogit.lib.notification") + notification.create("Echo: " .. value) +end + +return M diff --git a/lua/neogit/popups/echo/init.lua b/lua/neogit/popups/echo/init.lua new file mode 100644 index 000000000..39f29536e --- /dev/null +++ b/lua/neogit/popups/echo/init.lua @@ -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 diff --git a/lua/neogit/popups/help/actions.lua b/lua/neogit/popups/help/actions.lua index 9188a4874..3a50b5a4c 100644 --- a/lua/neogit/popups/help/actions.lua +++ b/lua/neogit/popups/help/actions.lua @@ -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", diff --git a/lua/neogit/popups/init.lua b/lua/neogit/popups/init.lua new file mode 100644 index 000000000..021cafda0 --- /dev/null +++ b/lua/neogit/popups/init.lua @@ -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 diff --git a/lua/neogit/status.lua b/lua/neogit/status.lua index 7181c3154..a9248b86d 100644 --- a/lua/neogit/status.lua +++ b/lua/neogit/status.lua @@ -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() @@ -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 @@ -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