From fbe004142f69962d92eb6ede13a6721f7fdb4d50 Mon Sep 17 00:00:00 2001 From: fdschmidt93 <39233597+fdschmidt93@users.noreply.github.com> Date: Wed, 1 Sep 2021 20:11:53 +0200 Subject: feat: show keymaps for builtin actions (#1084) * Add default mappings ``and `?` for insert and normal mode, respectively, to show registered keymappings (`actions.which_key`) attached to prompt buffer --- lua/telescope/actions/utils.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lua/telescope/actions/utils.lua') diff --git a/lua/telescope/actions/utils.lua b/lua/telescope/actions/utils.lua index f2abfe1..7c8a3ac 100644 --- a/lua/telescope/actions/utils.lua +++ b/lua/telescope/actions/utils.lua @@ -78,4 +78,26 @@ function utils.map_selections(prompt_bufnr, f) end end +local findnth = function(str, nth) + local array = {} + for i in string.gmatch(str, "%d+") do + table.insert(array, tonumber(i)) + end + return array[nth] +end + +--- Utility to collect mappings of prompt buffer in array of `{mode, keybind, name}`. +---@param prompt_bufnr number: The prompt bufnr +function utils.get_registered_mappings(prompt_bufnr) + local ret = {} + for _, mode in ipairs { "n", "i" } do + local mode_mappings = vim.api.nvim_buf_get_keymap(prompt_bufnr, mode) + for _, mapping in ipairs(mode_mappings) do + local funcid = findnth(mapping.rhs, 2) + table.insert(ret, { mode = mode, keybind = mapping.lhs, func = __TelescopeKeymapStore[prompt_bufnr][funcid] }) + end + end + return ret +end + return utils -- cgit v1.2.3