Skip to content

Commit

Permalink
feat: make help use same popups as status buffer
Browse files Browse the repository at this point in the history
This ensures that when new popups are added they are also visible in
the help buffer and not forgotten.
  • Loading branch information
ten3roberts committed Jul 15, 2023
1 parent 2973302 commit 35d39ac
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 74 deletions.
2 changes: 1 addition & 1 deletion lua/neogit/lib/mappings_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local managers = {}

---@class MappingTable
---@field [1] string mode
---@field [2] string|function command
---@field [2] string|function func
---@field [3] boolean Escape visual mode

---@class MappingsManager
Expand Down
33 changes: 11 additions & 22 deletions lua/neogit/popups/help/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ local function present(commands)
local presenter = util.map(commands, function(command)
local cmd, name, fn = unpack(command)

--- Handle the longer table mapping form (mode, func, esc)
if type(fn) == "table" then
fn = fn[2]
end

return { name = name, key = status_mappings[cmd], fn = fn }
end)

Expand All @@ -22,35 +27,19 @@ end
M.popups = function(env)
local popups = require("neogit.popups")

return present {
{ "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",
popups.open("stash", env.get_stash),
},
local items = vim.list_extend({
{

"CommandHistory",
"History",
function()
require("neogit.buffers.git_command_history"):new():show()
end,
},
}
{ "InitRepo", "Init", require("neogit.lib.git").init.init_repo },
}, popups.mappings_table())

return present(items)
end

M.actions = function()
Expand Down
124 changes: 74 additions & 50 deletions lua/neogit/popups/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ local M = {}
--- 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 async = require("plenary.async")
return async.void(function()
local ok, value = pcall(require, "neogit.popups." .. name)
if ok then
assert(value)
Expand All @@ -20,65 +21,88 @@ function M.open(name, get_args)
local notification = require("neogit.lib.notification")
notification.create(string.format("No such popup: %q", name), vim.log.levels.ERROR)
end
end
end)
end

--- Returns an array useful for creating mappings for the available popups
---@return table<string, Mapping>
function M.mappings_table()
local config = require("neogit.config")
return {
["HelpPopup"] = M.open("help", function()
local status = require("neogit.status")
local line = status.status_buffer:get_current_line()

return {
get_stash = function()
return {
name = line[1]:match("^(stash@{%d+})"),
}
end,
use_magit_keybindings = config.values.use_magit_keybindings,
}
end),
["DiffPopup"] = M.open("diff"),
["PullPopup"] = M.open("pull"),
["RebasePopup"] = M.open("rebase", function()
local status = require("neogit.status")
local line = status.status_buffer:get_current_line()
return { line[1]:match("^(%x%x%x%x%x%x%x+)") }
end),
["MergePopup"] = M.open("merge"),
["PushPopup"] = M.open("push"),
["CommitPopup"] = M.open("commit"),
["LogPopup"] = M.open("log"),
["CherryPickPopup"] = M.open("cherry_pick", function()
local selection = nil
{
"HelpPopup",
"Help",
M.open("help", function()
local status = require("neogit.status")
local line = status.status_buffer:get_current_line()

if vim.api.nvim_get_mode().mode == "V" then
return {
get_stash = function()
return {
name = line[1]:match("^(stash@{%d+})"),
}
end,
use_magit_keybindings = config.values.use_magit_keybindings,
}
end),
},
{ "DiffPopup", "Diff", M.open("diff") },
{ "PullPopup", "Pull", M.open("pull") },
{
"RebasePopup",
"Rebase",
M.open("rebase", function()
local status = require("neogit.status")
selection = status.get_selected_commits()
end
local line = status.status_buffer:get_current_line()
return { line[1]:match("^(%x%x%x%x%x%x%x+)") }
end),
},
{ "MergePopup", "Merge", M.open("merge") },
{ "PushPopup", "Push", M.open("push") },
{ "CommitPopup", "Commit", M.open("commit") },
{ "LogPopup", "Log", M.open("log") },
{
"CherryPickPopup",
"Cherry Pick",
{
"nv",
M.open("cherry_pick", function()
local selection = nil

return { commits = selection }
end),
-- { "nv", a.void(cherry_pick), true },
["StashPopup"] = M.open("stash", function()
local status = require("neogit.status")
local line = status.status_buffer:get_current_line()
return {
name = line[1]:match("^(stash@{%d+})"),
}
end),
["RevertPopup"] = M.open("revert", function()
local status = require("neogit.status")
local line = status.status_buffer:get_current_line()
return { commits = { line[1]:match("^(%x%x%x%x%x%x%x+)") } }
end),
["BranchPopup"] = M.open("branch"),
["FetchPopup"] = M.open("fetch"),
["RemotePopup"] = M.open("remote"),
["ResetPopup"] = M.open("reset"),
if vim.api.nvim_get_mode().mode == "V" then
local status = require("neogit.status")
selection = status.get_selected_commits()
end

return { commits = selection }
end),
true,
},
},
{
"StashPopup",
"Stash",
M.open("stash", function()
local status = require("neogit.status")
local line = status.status_buffer:get_current_line()
return {
name = line[1]:match("^(stash@{%d+})"),
}
end),
},
{
"RevertPopup",
"Revert",
M.open("revert", function()
local status = require("neogit.status")
local line = status.status_buffer:get_current_line()
return { commits = { line[1]:match("^(%x%x%x%x%x%x%x+)") } }
end),
},
{ "BranchPopup", "Branch", M.open("branch") },
{ "FetchPopup", "Fetch", M.open("fetch") },
{ "RemotePopup", "Remote", M.open("remote") },
{ "ResetPopup", "Reset", M.open("reset") },
}
end

Expand Down
10 changes: 9 additions & 1 deletion lua/neogit/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,15 @@ local cmd_func_map = function()

local popups = require("neogit.popups")
--- Load the popups from the centralized popup file
return vim.tbl_extend("error", mappings, popups.mappings_table())
for _, v in ipairs(popups.mappings_table()) do
--- { name, display_name, mapping }
if mappings[v[1]] then
error("Neogit: Mapping '" .. v[1] .. "' is already in use!")
end

mappings[v[1]] = v[3]
end
return mappings
end

-- Sets decoration provider for buffer
Expand Down

0 comments on commit 35d39ac

Please sign in to comment.