diff --git a/lua/neogit/lib/mappings_manager.lua b/lua/neogit/lib/mappings_manager.lua index 4c0b0cf9f..6f30155f7 100644 --- a/lua/neogit/lib/mappings_manager.lua +++ b/lua/neogit/lib/mappings_manager.lua @@ -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 diff --git a/lua/neogit/popups/help/actions.lua b/lua/neogit/popups/help/actions.lua index 3a50b5a4c..4f9c3370b 100644 --- a/lua/neogit/popups/help/actions.lua +++ b/lua/neogit/popups/help/actions.lua @@ -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) @@ -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() diff --git a/lua/neogit/popups/init.lua b/lua/neogit/popups/init.lua index 41b4bc4b6..bd5f13b08 100644 --- a/lua/neogit/popups/init.lua +++ b/lua/neogit/popups/init.lua @@ -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) @@ -20,7 +21,7 @@ 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 @@ -28,57 +29,80 @@ end 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 diff --git a/lua/neogit/status.lua b/lua/neogit/status.lua index a096363a9..f5e03145b 100644 --- a/lua/neogit/status.lua +++ b/lua/neogit/status.lua @@ -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