fix(actions): which_key after mappings rework (#2556)

This commit is contained in:
Simon Hauser
2023-06-08 23:10:26 +02:00
committed by GitHub
parent 991d5db624
commit 42267407ae
5 changed files with 36 additions and 37 deletions

View File

@@ -1178,26 +1178,10 @@ actions.which_key = function(prompt_bufnr, opts)
local mappings = {}
local mode = a.nvim_get_mode().mode
for _, v in pairs(action_utils.get_registered_mappings(prompt_bufnr)) do
-- holds true for registered keymaps
if type(v.func) == "table" then
local name = ""
for _, action in ipairs(v.func) do
if type(action) == "string" then
name = name == "" and action or name .. " + " .. action
end
end
if name and name ~= "which_key" and name ~= "nop" then
if not opts.only_show_current_mode or mode == v.mode then
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = name })
end
end
elseif type(v.func) == "function" then
if v.desc and v.desc ~= "which_key" and v.desc ~= "nop" then
if not opts.only_show_current_mode or mode == v.mode then
local fname = action_utils._get_anon_function_name(v.func)
-- telescope.setup mappings might result in function names that reflect the keys
fname = fname:lower() == v.keybind:lower() and "<anonymous>" or fname
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = fname })
if fname == "<anonymous>" then
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = v.desc })
if v.desc == "<anonymous>" then
utils.notify("actions.which_key", {
msg = "No name available for anonymous functions.",
level = "INFO",

View File

@@ -81,25 +81,25 @@ 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
for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(prompt_bufnr, mode)) do
-- ensure only telescope mappings
if mapping.rhs and string.find(mapping.rhs, [[require%('telescope.mappings'%).execute_keymap]]) then
local funcid = findnth(mapping.rhs, 2)
table.insert(ret, { mode = mode, keybind = mapping.lhs, func = __TelescopeKeymapStore[prompt_bufnr][funcid] })
if mapping.desc then
if mapping.desc:sub(1, 10) == "telescope|" then
table.insert(ret, { mode = mode, keybind = mapping.lhs, desc = mapping.desc:sub(11) })
elseif mapping.desc:sub(1, 11) == "telescopej|" then
local fname = utils._get_anon_function_name(vim.json.decode(mapping.desc:sub(12)))
fname = fname:lower() == mapping.lhs:lower() and "<anonymous>" or fname
table.insert(ret, {
mode = mode,
keybind = mapping.lhs,
desc = fname,
})
end
end
end
end
@@ -107,9 +107,8 @@ function utils.get_registered_mappings(prompt_bufnr)
end
-- Best effort to infer function names for actions.which_key
function utils._get_anon_function_name(func_ref)
function utils._get_anon_function_name(info)
local Path = require "plenary.path"
local info = debug.getinfo(func_ref)
local fname
-- if fn defined in string (ie loadstring) source is string
-- if fn defined in file, source is file name prefixed with a `@´

View File

@@ -834,7 +834,7 @@ end
function make_entry.gen_from_keymaps(opts)
local function get_desc(entry)
if entry.callback and not entry.desc then
return require("telescope.actions.utils")._get_anon_function_name(entry.callback)
return require("telescope.actions.utils")._get_anon_function_name(debug.getinfo(entry.callback))
end
return vim.F.if_nil(entry.desc, entry.rhs)
end

View File

@@ -199,6 +199,23 @@ mappings.default_mappings = config.values.default_mappings
},
}
-- normal names are prefixed with telescope|
-- encoded objects are prefixed with telescopej|
local get_desc_for_keyfunc = function(v)
if type(v) == "table" then
local name = ""
for _, action in ipairs(v) do
if type(action) == "string" then
name = name == "" and action or name .. " + " .. action
end
end
return "telescope|" .. name
elseif type(v) == "function" then
local info = debug.getinfo(v)
return "telescopej|" .. vim.json.encode { source = info.source, linedefined = info.linedefined }
end
end
local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
if not key_func then
return
@@ -236,7 +253,7 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
local ret = key_func(prompt_bufnr)
vim.api.nvim_exec_autocmds("User", { pattern = "TelescopeKeymap" })
return ret
end, vim.tbl_extend("force", opts, { buffer = prompt_bufnr }))
end, vim.tbl_extend("force", opts, { buffer = prompt_bufnr, desc = get_desc_for_keyfunc(key_func) }))
end
local extract_keymap_opts = function(key_func)