Skip to content

Commit

Permalink
fix(actions): which_key after mappings rework (#2556)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4226740)
  • Loading branch information
Conni2461 committed Jun 9, 2023
1 parent c757f8a commit 776b509
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
1 change: 0 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ globals = {
"TelescopeGlobalState",
"_TelescopeConfigurationValues",
"_TelescopeConfigurationPickers",
"__TelescopeKeymapStore",
}

-- Global objects defined by the C code
Expand Down
22 changes: 3 additions & 19 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1138,26 +1138,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",
Expand Down
29 changes: 14 additions & 15 deletions lua/telescope/actions/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,34 @@ 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
return ret
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 `@´
Expand Down
2 changes: 1 addition & 1 deletion lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,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
Expand Down
19 changes: 18 additions & 1 deletion lua/telescope/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 776b509

Please sign in to comment.