feat: show assigned function in actions.which_key (#1871)
This commit is contained in:
@@ -1128,6 +1128,16 @@ actions.which_key = function(prompt_bufnr, opts)
|
|||||||
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = name })
|
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = name })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elseif type(v.func) == "function" then
|
||||||
|
if not opts.only_show_current_mode or mode == v.mode then
|
||||||
|
local fname = action_utils._get_anon_function_name(v.func)
|
||||||
|
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = fname })
|
||||||
|
utils.notify("actions.which_key", {
|
||||||
|
msg = "No name available for anonymous functions.",
|
||||||
|
level = "INFO",
|
||||||
|
once = true,
|
||||||
|
})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -103,4 +103,36 @@ function utils.get_registered_mappings(prompt_bufnr)
|
|||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Best effort to infer function names for actions.which_key
|
||||||
|
function utils._get_anon_function_name(func_ref)
|
||||||
|
local Path = require "plenary.path"
|
||||||
|
local info = debug.getinfo(func_ref)
|
||||||
|
local fname
|
||||||
|
for i, line in ipairs(Path:new(info.short_src):readlines()) do
|
||||||
|
if i == info.linedefined then
|
||||||
|
fname = line
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- test if assignment or named function, otherwise anon
|
||||||
|
if (fname:match "=" == nil) and (fname:match "function %S+%(" == nil) then
|
||||||
|
return "<anonymous>"
|
||||||
|
else
|
||||||
|
-- (1) remove function
|
||||||
|
-- (2) whitespace and equal
|
||||||
|
-- (3) anything in parenthesis incl. parentheses themselves
|
||||||
|
-- (4) remove TABLE. prefix if available
|
||||||
|
local patterns = { { "function", "" }, { "local", "" }, { "[%s=]", "" }, { "%((.+)%)", "" }, { "(.+)%.", "" } }
|
||||||
|
for _, tbl in ipairs(patterns) do
|
||||||
|
fname = (fname:gsub(tbl[1], tbl[2])) -- make sure only string is returned
|
||||||
|
end
|
||||||
|
-- not sure if this can happen, catch all just in case
|
||||||
|
if fname == nil or fname == "" then
|
||||||
|
return "<anonymous>"
|
||||||
|
end
|
||||||
|
return fname
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return utils
|
return utils
|
||||||
|
|||||||
@@ -477,14 +477,15 @@ end)
|
|||||||
|
|
||||||
--- Telescope Wrapper around vim.notify
|
--- Telescope Wrapper around vim.notify
|
||||||
---@param funname string: name of the function that will be
|
---@param funname string: name of the function that will be
|
||||||
---@param opts table: opts.level string, opts.msg string
|
---@param opts table: opts.level string, opts.msg string, opts.once bool
|
||||||
utils.notify = function(funname, opts)
|
utils.notify = function(funname, opts)
|
||||||
|
opts.once = vim.F.if_nil(opts.once, false)
|
||||||
local level = vim.log.levels[opts.level]
|
local level = vim.log.levels[opts.level]
|
||||||
if not level then
|
if not level then
|
||||||
error("Invalid error level", 2)
|
error("Invalid error level", 2)
|
||||||
end
|
end
|
||||||
|
local notify_fn = opts.once and vim.notify_once or vim.notify
|
||||||
vim.notify(string.format("[telescope.%s]: %s", funname, opts.msg), level, {
|
notify_fn(string.format("[telescope.%s]: %s", funname, opts.msg), level, {
|
||||||
title = "telescope.nvim",
|
title = "telescope.nvim",
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user