Skip to content

Commit

Permalink
feat(mappings): allowing passing desc to mappings (nvim-telescope#2892
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jamestrew authored Jan 27, 2024
1 parent f1fd99e commit 2f3857c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ ordered from the lowest priority to the highest priority.
map({"i", "n"}, "<C-r>", function(_prompt_bufnr)
print "You typed <C-r>"
end)
end, { desc = "desc for which key"})
-- needs to return true if you want to map default_mappings and
-- false if not
Expand Down
21 changes: 14 additions & 7 deletions lua/telescope/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
---
--- map({"i", "n"}, "<C-r>", function(_prompt_bufnr)
--- print "You typed <C-r>"
--- end)
--- end, { desc = "desc for which key"})
---
--- -- needs to return true if you want to map default_mappings and
--- -- false if not
Expand Down Expand Up @@ -210,17 +210,24 @@ 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
---@param key_func table|fun()
---@param opts table
---@return string?
local get_desc_for_keyfunc = function(key_func, opts)
if opts and opts.desc then
return "telescope|" .. opts.desc
end

if type(key_func) == "table" then
local name = ""
for _, action in ipairs(v) do
for _, action in ipairs(key_func) 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)
elseif type(key_func) == "function" then
local info = debug.getinfo(key_func)
return "telescopej|" .. vim.json.encode { source = info.source, linedefined = info.linedefined }
end
end
Expand Down Expand Up @@ -262,7 +269,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, desc = get_desc_for_keyfunc(key_func) }))
end, vim.tbl_extend("force", opts, { buffer = prompt_bufnr, desc = get_desc_for_keyfunc(key_func, opts) }))
end

local extract_keymap_opts = function(key_func)
Expand Down

0 comments on commit 2f3857c

Please sign in to comment.