diff --git a/.luacheckrc b/.luacheckrc index 1602e624e9..02c0b899bf 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -17,7 +17,6 @@ globals = { "TelescopeGlobalState", "_TelescopeConfigurationValues", "_TelescopeConfigurationPickers", - "__TelescopeKeymapStore", } -- Global objects defined by the C code diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 28c54b5d3d..44fc90c725 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -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 "" or fname - table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = fname }) - if fname == "" then + table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = v.desc }) + if v.desc == "" then utils.notify("actions.which_key", { msg = "No name available for anonymous functions.", level = "INFO", diff --git a/lua/telescope/actions/utils.lua b/lua/telescope/actions/utils.lua index 5816baa68a..81bd870bfc 100644 --- a/lua/telescope/actions/utils.lua +++ b/lua/telescope/actions/utils.lua @@ -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 "" 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 `@ยด diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 7e5bac9f06..fc4c4dd460 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -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 diff --git a/lua/telescope/mappings.lua b/lua/telescope/mappings.lua index 6b71429406..6c218ee005 100644 --- a/lua/telescope/mappings.lua +++ b/lua/telescope/mappings.lua @@ -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)