Skip to content

Commit 776b509

Browse files
committed
fix(actions): which_key after mappings rework (#2556)
(cherry picked from commit 4226740)
1 parent c757f8a commit 776b509

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

.luacheckrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ globals = {
1717
"TelescopeGlobalState",
1818
"_TelescopeConfigurationValues",
1919
"_TelescopeConfigurationPickers",
20-
"__TelescopeKeymapStore",
2120
}
2221

2322
-- Global objects defined by the C code

lua/telescope/actions/init.lua

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,26 +1138,10 @@ actions.which_key = function(prompt_bufnr, opts)
11381138
local mappings = {}
11391139
local mode = a.nvim_get_mode().mode
11401140
for _, v in pairs(action_utils.get_registered_mappings(prompt_bufnr)) do
1141-
-- holds true for registered keymaps
1142-
if type(v.func) == "table" then
1143-
local name = ""
1144-
for _, action in ipairs(v.func) do
1145-
if type(action) == "string" then
1146-
name = name == "" and action or name .. " + " .. action
1147-
end
1148-
end
1149-
if name and name ~= "which_key" and name ~= "nop" then
1150-
if not opts.only_show_current_mode or mode == v.mode then
1151-
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = name })
1152-
end
1153-
end
1154-
elseif type(v.func) == "function" then
1141+
if v.desc and v.desc ~= "which_key" and v.desc ~= "nop" then
11551142
if not opts.only_show_current_mode or mode == v.mode then
1156-
local fname = action_utils._get_anon_function_name(v.func)
1157-
-- telescope.setup mappings might result in function names that reflect the keys
1158-
fname = fname:lower() == v.keybind:lower() and "<anonymous>" or fname
1159-
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = fname })
1160-
if fname == "<anonymous>" then
1143+
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = v.desc })
1144+
if v.desc == "<anonymous>" then
11611145
utils.notify("actions.which_key", {
11621146
msg = "No name available for anonymous functions.",
11631147
level = "INFO",

lua/telescope/actions/utils.lua

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,34 @@ function utils.map_selections(prompt_bufnr, f)
8181
end
8282
end
8383

84-
local findnth = function(str, nth)
85-
local array = {}
86-
for i in string.gmatch(str, "%d+") do
87-
table.insert(array, tonumber(i))
88-
end
89-
return array[nth]
90-
end
91-
9284
--- Utility to collect mappings of prompt buffer in array of `{mode, keybind, name}`.
9385
---@param prompt_bufnr number: The prompt bufnr
9486
function utils.get_registered_mappings(prompt_bufnr)
9587
local ret = {}
9688
for _, mode in ipairs { "n", "i" } do
97-
local mode_mappings = vim.api.nvim_buf_get_keymap(prompt_bufnr, mode)
98-
for _, mapping in ipairs(mode_mappings) do
89+
for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(prompt_bufnr, mode)) do
9990
-- ensure only telescope mappings
100-
if mapping.rhs and string.find(mapping.rhs, [[require%('telescope.mappings'%).execute_keymap]]) then
101-
local funcid = findnth(mapping.rhs, 2)
102-
table.insert(ret, { mode = mode, keybind = mapping.lhs, func = __TelescopeKeymapStore[prompt_bufnr][funcid] })
91+
if mapping.desc then
92+
if mapping.desc:sub(1, 10) == "telescope|" then
93+
table.insert(ret, { mode = mode, keybind = mapping.lhs, desc = mapping.desc:sub(11) })
94+
elseif mapping.desc:sub(1, 11) == "telescopej|" then
95+
local fname = utils._get_anon_function_name(vim.json.decode(mapping.desc:sub(12)))
96+
fname = fname:lower() == mapping.lhs:lower() and "<anonymous>" or fname
97+
table.insert(ret, {
98+
mode = mode,
99+
keybind = mapping.lhs,
100+
desc = fname,
101+
})
102+
end
103103
end
104104
end
105105
end
106106
return ret
107107
end
108108

109109
-- Best effort to infer function names for actions.which_key
110-
function utils._get_anon_function_name(func_ref)
110+
function utils._get_anon_function_name(info)
111111
local Path = require "plenary.path"
112-
local info = debug.getinfo(func_ref)
113112
local fname
114113
-- if fn defined in string (ie loadstring) source is string
115114
-- if fn defined in file, source is file name prefixed with a `@´

lua/telescope/make_entry.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ end
827827
function make_entry.gen_from_keymaps(opts)
828828
local function get_desc(entry)
829829
if entry.callback and not entry.desc then
830-
return require("telescope.actions.utils")._get_anon_function_name(entry.callback)
830+
return require("telescope.actions.utils")._get_anon_function_name(debug.getinfo(entry.callback))
831831
end
832832
return vim.F.if_nil(entry.desc, entry.rhs)
833833
end

lua/telescope/mappings.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ mappings.default_mappings = config.values.default_mappings
199199
},
200200
}
201201

202+
-- normal names are prefixed with telescope|
203+
-- encoded objects are prefixed with telescopej|
204+
local get_desc_for_keyfunc = function(v)
205+
if type(v) == "table" then
206+
local name = ""
207+
for _, action in ipairs(v) do
208+
if type(action) == "string" then
209+
name = name == "" and action or name .. " + " .. action
210+
end
211+
end
212+
return "telescope|" .. name
213+
elseif type(v) == "function" then
214+
local info = debug.getinfo(v)
215+
return "telescopej|" .. vim.json.encode { source = info.source, linedefined = info.linedefined }
216+
end
217+
end
218+
202219
local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
203220
if not key_func then
204221
return
@@ -236,7 +253,7 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
236253
local ret = key_func(prompt_bufnr)
237254
vim.api.nvim_exec_autocmds("User", { pattern = "TelescopeKeymap" })
238255
return ret
239-
end, vim.tbl_extend("force", opts, { buffer = prompt_bufnr }))
256+
end, vim.tbl_extend("force", opts, { buffer = prompt_bufnr, desc = get_desc_for_keyfunc(key_func) }))
240257
end
241258

242259
local extract_keymap_opts = function(key_func)

0 commit comments

Comments
 (0)