From e899aa34f3fe863d5418bb66d8073273af9681e0 Mon Sep 17 00:00:00 2001 From: jmarkin Date: Fri, 25 Nov 2022 00:20:51 +0300 Subject: [PATCH 1/6] feat(help menu): add menu popup --- lua/diffview/actions.lua | 12 +++ lua/diffview/config.lua | 63 +++++++------ lua/diffview/ui/panels/help_panel.lua | 122 ++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 25 deletions(-) create mode 100644 lua/diffview/ui/panels/help_panel.lua diff --git a/lua/diffview/actions.lua b/lua/diffview/actions.lua index 04c49a33..f3b2056a 100644 --- a/lua/diffview/actions.lua +++ b/lua/diffview/actions.lua @@ -465,6 +465,18 @@ function M.cycle_layout() end end +function M.help(conf_name) + return function() + M.HelpPanel = require("diffview.ui.panels.help_panel") + M.HelpPanel:init(conf_name, {}) + M.HelpPanel:focus() + end +end + +function M.help_close() + M.HelpPanel:destroy() +end + local action_names = { "close", "close_all_folds", diff --git a/lua/diffview/config.lua b/lua/diffview/config.lua index bbc1076e..040954ec 100644 --- a/lua/diffview/config.lua +++ b/lua/diffview/config.lua @@ -94,6 +94,11 @@ M.defaults = { win_opts = {} }, }, + help_panel = { + win_config = { + win_opts = {} + }, + }, default_args = { DiffviewOpen = {}, DiffviewFileHistory = {}, @@ -120,6 +125,7 @@ M.defaults = { { "n", "cb", actions.conflict_choose("base"), { desc = "Choose the BASE version of a conflict" } }, { "n", "ca", actions.conflict_choose("all"), { desc = "Choose all the versions of a conflict" } }, { "n", "dx", actions.conflict_choose("none"), { desc = "Delete the conflict region" } }, + { "n", "g?", actions.help("view"), { desc = "Open the help panel" } }, }, diff1 = { --[[ Mappings in single window diff layouts ]] }, diff2 = { --[[ Mappings in 2-way diff layouts ]] }, @@ -162,35 +168,42 @@ M.defaults = { { "n", "g", actions.cycle_layout, { desc = "Cycle available layouts" } }, { "n", "[x", actions.prev_conflict, { desc = "Go to the previous conflict" } }, { "n", "]x", actions.next_conflict, { desc = "Go to the next conflict" } }, + { "n", "g?", actions.help("file_panel"), { desc = "Open the help panel" } }, }, file_history_panel = { - { "n", "g!", actions.options, { desc = "Open the option panel" } }, - { "n", "", actions.open_in_diffview, { desc = "Open the entry under the cursor in a diffview" } }, - { "n", "y", actions.copy_hash, { desc = "Copy the commit hash of the entry under the cursor" } }, - { "n", "L", actions.open_commit_log, { desc = "Show commit details" } }, - { "n", "zR", actions.open_all_folds, { desc = "Expand all folds" } }, - { "n", "zM", actions.close_all_folds, { desc = "Collapse all folds" } }, - { "n", "j", actions.next_entry, { desc = "Bring the cursor to the next file entry" } }, - { "n", "", actions.next_entry, { desc = "Bring the cursor to the next file entry" } }, - { "n", "k", actions.prev_entry, { desc = "Bring the cursor to the previous file entry." } }, - { "n", "", actions.prev_entry, { desc = "Bring the cursor to the previous file entry." } }, - { "n", "", actions.select_entry, { desc = "Open the diff for the selected entry." } }, - { "n", "o", actions.select_entry, { desc = "Open the diff for the selected entry." } }, - { "n", "<2-LeftMouse>", actions.select_entry, { desc = "Open the diff for the selected entry." } }, - { "n", "", actions.scroll_view(-0.25), { desc = "Scroll the view up" } }, - { "n", "", actions.scroll_view(0.25), { desc = "Scroll the view down" } }, - { "n", "", actions.select_next_entry, { desc = "Open the diff for the next file" } }, - { "n", "", actions.select_prev_entry, { desc = "Open the diff for the previous file" } }, - { "n", "gf", actions.goto_file, { desc = "Open the file in a new split in the previous tabpage" } }, - { "n", "", actions.goto_file_split, { desc = "Open the file in a new split" } }, - { "n", "gf", actions.goto_file_tab, { desc = "Open the file in a new tabpage" } }, - { "n", "e", actions.focus_files, { desc = "Bring focus to the file panel" } }, - { "n", "b", actions.toggle_files, { desc = "Toggle the file panel" } }, - { "n", "g", actions.cycle_layout, { desc = "Cycle available layouts" } }, + { "n", "g!", actions.options, { desc = "Open the option panel" } }, + { "n", "", actions.open_in_diffview, { desc = "Open the entry under the cursor in a diffview" } }, + { "n", "y", actions.copy_hash, { desc = "Copy the commit hash of the entry under the cursor" } }, + { "n", "L", actions.open_commit_log, { desc = "Show commit details" } }, + { "n", "zR", actions.open_all_folds, { desc = "Expand all folds" } }, + { "n", "zM", actions.close_all_folds, { desc = "Collapse all folds" } }, + { "n", "j", actions.next_entry, { desc = "Bring the cursor to the next file entry" } }, + { "n", "", actions.next_entry, { desc = "Bring the cursor to the next file entry" } }, + { "n", "k", actions.prev_entry, { desc = "Bring the cursor to the previous file entry." } }, + { "n", "", actions.prev_entry, { desc = "Bring the cursor to the previous file entry." } }, + { "n", "", actions.select_entry, { desc = "Open the diff for the selected entry." } }, + { "n", "o", actions.select_entry, { desc = "Open the diff for the selected entry." } }, + { "n", "<2-LeftMouse>", actions.select_entry, { desc = "Open the diff for the selected entry." } }, + { "n", "", actions.scroll_view(-0.25), { desc = "Scroll the view up" } }, + { "n", "", actions.scroll_view(0.25), { desc = "Scroll the view down" } }, + { "n", "", actions.select_next_entry, { desc = "Open the diff for the next file" } }, + { "n", "", actions.select_prev_entry, { desc = "Open the diff for the previous file" } }, + { "n", "gf", actions.goto_file, { desc = "Open the file in a new split in the previous tabpage" } }, + { "n", "", actions.goto_file_split, { desc = "Open the file in a new split" } }, + { "n", "gf", actions.goto_file_tab, { desc = "Open the file in a new tabpage" } }, + { "n", "e", actions.focus_files, { desc = "Bring focus to the file panel" } }, + { "n", "b", actions.toggle_files, { desc = "Toggle the file panel" } }, + { "n", "g", actions.cycle_layout, { desc = "Cycle available layouts" } }, + { "n", "g?", actions.help("file_history_panel"), { desc = "Open the help panel" } }, }, option_panel = { - { "n", "", actions.select_entry, { desc = "Change the current option" } }, - { "n", "q", actions.close, { desc = "Close the panel" } }, + { "n", "", actions.select_entry, { desc = "Change the current option" } }, + { "n", "q", actions.close, { desc = "Close the panel" } }, + { "n", "g?", actions.help("option_panel"), { desc = "Open the help panel" } }, + }, + help_panel = { + { "n", "q", actions.help_close, { desc = "Close help menu" } }, + { "n", "", actions.help_close, { desc = "Close help menu" } }, }, }, } diff --git a/lua/diffview/ui/panels/help_panel.lua b/lua/diffview/ui/panels/help_panel.lua new file mode 100644 index 00000000..fa7bb065 --- /dev/null +++ b/lua/diffview/ui/panels/help_panel.lua @@ -0,0 +1,122 @@ +local Panel = require("diffview.ui.panel").Panel +local get_user_config = require("diffview.config").get_config +local oop = require("diffview.oop") + +---@class HelpPanel : Panel +---@field keymap_name string +---@field lines string[] +---@field keys string[] +local HelpPanel = oop.create_class("HelpPanel", Panel) + +HelpPanel.winopts = vim.tbl_extend("force", Panel.winopts, { + wrap = false, + breakindent = true, + cursorbind = true, +}) + +HelpPanel.bufopts = vim.tbl_extend("force", Panel.bufopts, { + buftype = "nowrite", +}) + +HelpPanel.default_type = "float" + +---@class HelpPanelSpec +---@field config PanelConfig +---@field name string + +---@param keymap_name string +---@param opt HelpPanelSpec +function HelpPanel:init(keymap_name, opt) + HelpPanel:super().init(self, { + bufname = opt.name, + config = opt.config or get_user_config().help_panel.win_config, + }) + + self.keymap_name = keymap_name + + local keymaps = get_user_config().keymaps + local maps = keymaps[self.keymap_name] + local keys = {} + for k, _ in pairs(keymaps[self.keymap_name]) do + table.insert(keys, k) + end + table.sort(keys) + self.keys = keys + + local lines = { "" } + local max_width = 0 + + for _, lhs in pairs(keys) do + local mapping = maps[lhs] + if type(lhs) == "number" then + mapping = mapping[3] + lhs = mapping[2] + else + if type(mapping) ~= "function" then + mapping = mapping[2] + end + end + local txt = string.format("%14s -> %s", lhs, mapping) + + if #txt > max_width then + max_width = #txt + end + table.insert(lines, txt) + end + local height = #lines + 1 + local width = max_width + 1 + + local title_line = "Actions for current panel for apply" + title_line = string.rep(" ", math.floor(width * 0.5 - #title_line * 0.5) - 1) .. title_line + table.insert(lines, 1, title_line) + + self.default_config_float.height = height + self.default_config_float.width = width + local viewport_width = vim.o.columns + local viewport_height = vim.o.lines + self.default_config_float.col = math.floor(viewport_width * 0.5 - width * 0.5) + self.default_config_float.row = math.floor(viewport_height * 0.5 - height * 0.5) + self.lines = lines + + self:on_autocmd("BufWinEnter", { + callback = function() + vim.bo[self.bufid].bufhidden = "wipe" + end, + }) +end + +function HelpPanel:apply_cmd() + local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) + local cmd = self.keys[row] + if cmd ~= nil then + self.destroy(self) + vim.cmd.normal({ cmd, bang = true }) + end +end + +function HelpPanel:init_buffer() + HelpPanel:super().init_buffer(self) + local conf = get_user_config().keymaps + local default_opt = { silent = true, nowait = true, buffer = self.bufid } + + for lhs, mapping in pairs(conf.help_panel) do + if type(mapping) ~= "function" then + default_opt.desc = mapping[2] + mapping = mapping[1] + end + vim.keymap.set("n", lhs, mapping, default_opt) + end + vim.keymap.set("n", "", function() + self.apply_cmd(self) + end, default_opt) +end + +function HelpPanel:update_components() end + +function HelpPanel:render() + self.render_data:clear() + + self.render_data.lines = self.lines +end + +return HelpPanel From 42752d59eda6d400ad5fb196810fe1ed90fbb8f3 Mon Sep 17 00:00:00 2001 From: jmarkin Date: Fri, 25 Nov 2022 12:46:37 +0300 Subject: [PATCH 2/6] feat(help menu): executable entity in menu --- lua/diffview/ui/panels/help_panel.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/diffview/ui/panels/help_panel.lua b/lua/diffview/ui/panels/help_panel.lua index fa7bb065..758684d7 100644 --- a/lua/diffview/ui/panels/help_panel.lua +++ b/lua/diffview/ui/panels/help_panel.lua @@ -87,14 +87,20 @@ end function HelpPanel:apply_cmd() local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) - local cmd = self.keys[row] + local cmd = self.keys[row-2] if cmd ~= nil then + local keymap_entry = get_user_config().keymaps[self.keymap_name][cmd] + if type(keymap_entry) == "function" then + keymap_entry() + else + keymap_entry[1]() + end self.destroy(self) - vim.cmd.normal({ cmd, bang = true }) end end function HelpPanel:init_buffer() + self.current_view_bufid = vim.api.nvim_win_get_buf(0) HelpPanel:super().init_buffer(self) local conf = get_user_config().keymaps local default_opt = { silent = true, nowait = true, buffer = self.bufid } From c8930cdad4b736f7190807f35d96140dbd3c33ea Mon Sep 17 00:00:00 2001 From: jmarkin Date: Fri, 25 Nov 2022 12:48:04 +0300 Subject: [PATCH 3/6] feat(help menu): refactor --- lua/diffview/ui/panels/help_panel.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/diffview/ui/panels/help_panel.lua b/lua/diffview/ui/panels/help_panel.lua index 758684d7..3ff8b179 100644 --- a/lua/diffview/ui/panels/help_panel.lua +++ b/lua/diffview/ui/panels/help_panel.lua @@ -100,7 +100,6 @@ function HelpPanel:apply_cmd() end function HelpPanel:init_buffer() - self.current_view_bufid = vim.api.nvim_win_get_buf(0) HelpPanel:super().init_buffer(self) local conf = get_user_config().keymaps local default_opt = { silent = true, nowait = true, buffer = self.bufid } From 76fb7e026f2f662505f74ffc5290d0c7a48f716b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sindre=20T=2E=20Str=C3=B8m?= Date: Sat, 26 Nov 2022 15:58:00 +0100 Subject: [PATCH 4/6] Refactor --- lua/diffview/actions.lua | 10 +- lua/diffview/ui/panels/help_panel.lua | 138 ++++++++++++++------------ 2 files changed, 76 insertions(+), 72 deletions(-) diff --git a/lua/diffview/actions.lua b/lua/diffview/actions.lua index f3b2056a..1766c4a2 100644 --- a/lua/diffview/actions.lua +++ b/lua/diffview/actions.lua @@ -1,6 +1,7 @@ local lazy = require("diffview.lazy") local DiffView = lazy.access("diffview.scene.views.diff.diff_view", "DiffView") ---@type DiffView|LazyModule +local HelpPanel = lazy.access("diffview.ui.panels.help_panel", "HelpPanel") ---@type HelpPanel|LazyModule local FileHistoryView = lazy.access("diffview.scene.views.file_history.file_history_view", "FileHistoryView") ---@type FileHistoryView|LazyModule local StandardView = lazy.access("diffview.scene.views.standard.standard_view", "StandardView") ---@type StandardView|LazyModule local vcs = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils" @@ -467,15 +468,12 @@ end function M.help(conf_name) return function() - M.HelpPanel = require("diffview.ui.panels.help_panel") - M.HelpPanel:init(conf_name, {}) - M.HelpPanel:focus() + local help_panel = HelpPanel(conf_name) --[[@as HelpPanel ]] + help_panel:focus() end end -function M.help_close() - M.HelpPanel:destroy() -end +function M.help_close() end local action_names = { "close", diff --git a/lua/diffview/ui/panels/help_panel.lua b/lua/diffview/ui/panels/help_panel.lua index 3ff8b179..063f4147 100644 --- a/lua/diffview/ui/panels/help_panel.lua +++ b/lua/diffview/ui/panels/help_panel.lua @@ -1,21 +1,26 @@ local Panel = require("diffview.ui.panel").Panel local get_user_config = require("diffview.config").get_config local oop = require("diffview.oop") +local utils = require("diffview.utils") + +local api = vim.api + +local M = {} ---@class HelpPanel : Panel ---@field keymap_name string ---@field lines string[] ----@field keys string[] +---@field maps table[] local HelpPanel = oop.create_class("HelpPanel", Panel) HelpPanel.winopts = vim.tbl_extend("force", Panel.winopts, { wrap = false, breakindent = true, - cursorbind = true, + scl = "no", }) HelpPanel.bufopts = vim.tbl_extend("force", Panel.bufopts, { - buftype = "nowrite", + buftype = "nofile", }) HelpPanel.default_type = "float" @@ -27,56 +32,14 @@ HelpPanel.default_type = "float" ---@param keymap_name string ---@param opt HelpPanelSpec function HelpPanel:init(keymap_name, opt) + opt = opt or {} HelpPanel:super().init(self, { bufname = opt.name, config = opt.config or get_user_config().help_panel.win_config, }) self.keymap_name = keymap_name - - local keymaps = get_user_config().keymaps - local maps = keymaps[self.keymap_name] - local keys = {} - for k, _ in pairs(keymaps[self.keymap_name]) do - table.insert(keys, k) - end - table.sort(keys) - self.keys = keys - - local lines = { "" } - local max_width = 0 - - for _, lhs in pairs(keys) do - local mapping = maps[lhs] - if type(lhs) == "number" then - mapping = mapping[3] - lhs = mapping[2] - else - if type(mapping) ~= "function" then - mapping = mapping[2] - end - end - local txt = string.format("%14s -> %s", lhs, mapping) - - if #txt > max_width then - max_width = #txt - end - table.insert(lines, txt) - end - local height = #lines + 1 - local width = max_width + 1 - - local title_line = "Actions for current panel for apply" - title_line = string.rep(" ", math.floor(width * 0.5 - #title_line * 0.5) - 1) .. title_line - table.insert(lines, 1, title_line) - - self.default_config_float.height = height - self.default_config_float.width = width - local viewport_width = vim.o.columns - local viewport_height = vim.o.lines - self.default_config_float.col = math.floor(viewport_width * 0.5 - width * 0.5) - self.default_config_float.row = math.floor(viewport_height * 0.5 - height * 0.5) - self.lines = lines + self.lines = {} self:on_autocmd("BufWinEnter", { callback = function() @@ -87,15 +50,15 @@ end function HelpPanel:apply_cmd() local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) - local cmd = self.keys[row-2] - if cmd ~= nil then - local keymap_entry = get_user_config().keymaps[self.keymap_name][cmd] - if type(keymap_entry) == "function" then - keymap_entry() - else - keymap_entry[1]() - end - self.destroy(self) + local mapping = self.maps[row-2] + local last_winid = vim.fn.win_getid(vim.fn.winnr("#")) + + if mapping then + api.nvim_win_call(last_winid, function() + api.nvim_feedkeys(utils.t(mapping[2]), "m", false) + end) + + self:destroy() end end @@ -104,24 +67,67 @@ function HelpPanel:init_buffer() local conf = get_user_config().keymaps local default_opt = { silent = true, nowait = true, buffer = self.bufid } - for lhs, mapping in pairs(conf.help_panel) do - if type(mapping) ~= "function" then - default_opt.desc = mapping[2] - mapping = mapping[1] - end - vim.keymap.set("n", lhs, mapping, default_opt) + for _, mapping in ipairs(conf.help_panel) do + local map_opt = vim.tbl_extend("force", default_opt, mapping[4] or {}, { buffer = self.bufid }) + vim.keymap.set(mapping[1], mapping[2], mapping[3], map_opt) end + vim.keymap.set("n", "", function() self.apply_cmd(self) end, default_opt) end -function HelpPanel:update_components() end +function HelpPanel:update_components() + local keymaps = get_user_config().keymaps + local maps = keymaps[self.keymap_name] + + if not maps then + utils.err(("Unknown keymap group '%s'!"):format(self.keymap_name)) + else + maps = utils.vec_slice(maps) + -- Sort mappings by description + table.sort(maps, function(a, b) + return tostring(a[4] and a[4].desc or a[2]) < tostring(b[4] and b[4].desc or b[2]) + end) + + local lines = { "" } + local max_width = 0 + + for _, mapping in ipairs(maps) do + local lhs, rhs, opt = mapping[2], mapping[3], mapping[4] or {} + local txt = string.format("%14s -> %s", lhs, opt.desc or rhs) + + max_width = math.max(max_width, #txt) + table.insert(lines, txt) + end + + local height = #lines + 1 + local width = max_width + 2 + local title_line = ("Keymaps for '%s' | to use"):format(self.keymap_name) + title_line = string.rep(" ", math.floor(width * 0.5 - #title_line * 0.5) - 1) .. title_line + table.insert(lines, 1, title_line) + + self.maps = maps + self.lines = lines + + self.config_producer = function() + local c = vim.deepcopy(Panel.default_config_float) + local viewport_width = vim.o.columns + local viewport_height = vim.o.lines + c.col = math.floor(viewport_width * 0.5 - width * 0.5) + c.row = math.floor(viewport_height * 0.5 - height * 0.5) + c.width = width + c.height = height + + return c + end + end +end function HelpPanel:render() self.render_data:clear() - - self.render_data.lines = self.lines + self.render_data.lines = utils.vec_slice(self.lines or {}) end -return HelpPanel +M.HelpPanel = HelpPanel +return M From d340c4f486907445f14b65f39ccd3315b479038e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sindre=20T=2E=20Str=C3=B8m?= Date: Sun, 27 Nov 2022 00:45:49 +0100 Subject: [PATCH 5/6] Use the `close` event. Update docs. --- README.md | 4 ++++ doc/diffview_defaults.txt | 4 ++++ lua/diffview/actions.lua | 10 ++++++---- lua/diffview/config.lua | 4 ++-- lua/diffview/ui/panels/help_panel.lua | 18 +++++++++++++++++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 400e9d2f..7f892069 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,10 @@ require("diffview").setup({ { "n", "", actions.select_entry, { desc = "Change the current option" } }, { "n", "q", actions.close, { desc = "Close the panel" } }, }, + help_panel = { + { "n", "q", actions.close, { desc = "Close help menu" } }, + { "n", "", actions.close, { desc = "Close help menu" } }, + }, }, }) ``` diff --git a/doc/diffview_defaults.txt b/doc/diffview_defaults.txt index f2cfaac6..4f917107 100644 --- a/doc/diffview_defaults.txt +++ b/doc/diffview_defaults.txt @@ -172,6 +172,10 @@ require("diffview").setup({ { "n", "", actions.select_entry, { desc = "Change the current option" } }, { "n", "q", actions.close, { desc = "Close the panel" } }, }, + help_panel = { + { "n", "q", actions.close, { desc = "Close help menu" } }, + { "n", "", actions.close, { desc = "Close help menu" } }, + }, }, }) diff --git a/lua/diffview/actions.lua b/lua/diffview/actions.lua index 1766c4a2..564d022a 100644 --- a/lua/diffview/actions.lua +++ b/lua/diffview/actions.lua @@ -468,13 +468,15 @@ end function M.help(conf_name) return function() - local help_panel = HelpPanel(conf_name) --[[@as HelpPanel ]] - help_panel:focus() + local view = lib.get_current_view() + + if view then + local help_panel = HelpPanel(view, conf_name) --[[@as HelpPanel ]] + help_panel:focus() + end end end -function M.help_close() end - local action_names = { "close", "close_all_folds", diff --git a/lua/diffview/config.lua b/lua/diffview/config.lua index 040954ec..4ff547dc 100644 --- a/lua/diffview/config.lua +++ b/lua/diffview/config.lua @@ -202,8 +202,8 @@ M.defaults = { { "n", "g?", actions.help("option_panel"), { desc = "Open the help panel" } }, }, help_panel = { - { "n", "q", actions.help_close, { desc = "Close help menu" } }, - { "n", "", actions.help_close, { desc = "Close help menu" } }, + { "n", "q", actions.close, { desc = "Close help menu" } }, + { "n", "", actions.close, { desc = "Close help menu" } }, }, }, } diff --git a/lua/diffview/ui/panels/help_panel.lua b/lua/diffview/ui/panels/help_panel.lua index 063f4147..0297485b 100644 --- a/lua/diffview/ui/panels/help_panel.lua +++ b/lua/diffview/ui/panels/help_panel.lua @@ -26,18 +26,21 @@ HelpPanel.bufopts = vim.tbl_extend("force", Panel.bufopts, { HelpPanel.default_type = "float" ---@class HelpPanelSpec +---@field parent StandardView ---@field config PanelConfig ---@field name string +---@param parent StandardView ---@param keymap_name string ---@param opt HelpPanelSpec -function HelpPanel:init(keymap_name, opt) +function HelpPanel:init(parent, keymap_name, opt) opt = opt or {} HelpPanel:super().init(self, { bufname = opt.name, config = opt.config or get_user_config().help_panel.win_config, }) + self.parent = parent self.keymap_name = keymap_name self.lines = {} @@ -46,6 +49,19 @@ function HelpPanel:init(keymap_name, opt) vim.bo[self.bufid].bufhidden = "wipe" end, }) + + self:on_autocmd("WinLeave", { + callback = function() + pcall(self.close, self) + end, + }) + + parent.emitter:on("close", function(e) + if self:is_focused() then + pcall(self.close, self) + e:stop_propagation() + end + end) end function HelpPanel:apply_cmd() From 1adf96ab537dbcd14ea17e2f27562644ed15e713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sindre=20T=2E=20Str=C3=B8m?= Date: Sun, 27 Nov 2022 01:23:46 +0100 Subject: [PATCH 6/6] Use better descriptions for lua functions. --- lua/diffview/ui/panels/help_panel.lua | 32 ++++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lua/diffview/ui/panels/help_panel.lua b/lua/diffview/ui/panels/help_panel.lua index 0297485b..55822fc4 100644 --- a/lua/diffview/ui/panels/help_panel.lua +++ b/lua/diffview/ui/panels/help_panel.lua @@ -16,7 +16,7 @@ local HelpPanel = oop.create_class("HelpPanel", Panel) HelpPanel.winopts = vim.tbl_extend("force", Panel.winopts, { wrap = false, breakindent = true, - scl = "no", + signcolumn = "no", }) HelpPanel.bufopts = vim.tbl_extend("force", Panel.bufopts, { @@ -74,7 +74,7 @@ function HelpPanel:apply_cmd() api.nvim_feedkeys(utils.t(mapping[2]), "m", false) end) - self:destroy() + pcall(self.close, self) end end @@ -89,7 +89,7 @@ function HelpPanel:init_buffer() end vim.keymap.set("n", "", function() - self.apply_cmd(self) + self:apply_cmd() end, default_opt) end @@ -100,18 +100,34 @@ function HelpPanel:update_components() if not maps then utils.err(("Unknown keymap group '%s'!"):format(self.keymap_name)) else - maps = utils.vec_slice(maps) + maps = vim.tbl_map(function(v) + local desc = v[4] and v[4].desc + + if not desc then + if type(v[3]) == "string" then + desc = v[3] + elseif type(v[3]) == "function" then + local info = debug.getinfo(v[3], "S") + desc = (""):format(info.short_src, info.linedefined) + end + end + + return utils.vec_join(v, desc) + end, maps) -- Sort mappings by description table.sort(maps, function(a, b) - return tostring(a[4] and a[4].desc or a[2]) < tostring(b[4] and b[4].desc or b[2]) + a, b = a[5], b[5] + -- Ensure lua functions are sorted last + if a:match("^ %s", lhs, opt.desc or rhs) + local txt = string.format("%14s -> %s", mapping[2], mapping[5]) max_width = math.max(max_width, #txt) table.insert(lines, txt) @@ -119,7 +135,7 @@ function HelpPanel:update_components() local height = #lines + 1 local width = max_width + 2 - local title_line = ("Keymaps for '%s' | to use"):format(self.keymap_name) + local title_line = ("Keymaps for '%s' — to use"):format(self.keymap_name) title_line = string.rep(" ", math.floor(width * 0.5 - #title_line * 0.5) - 1) .. title_line table.insert(lines, 1, title_line)