summaryrefslogtreecommitdiff
path: root/lua/telescope/actions
diff options
context:
space:
mode:
authorfdschmidt93 <39233597+fdschmidt93@users.noreply.github.com>2022-04-22 15:52:22 +0200
committerGitHub <noreply@github.com>2022-04-22 15:52:22 +0200
commitd88094fbfd84b297178252230f6faf0e7d2f7650 (patch)
tree2fb0d028d6fa42f990263052524cbd335b9a8d60 /lua/telescope/actions
parent92019d5053674676576b021904935d101b059fd5 (diff)
feat: show assigned function in actions.which_key (#1871)
Diffstat (limited to 'lua/telescope/actions')
-rw-r--r--lua/telescope/actions/init.lua10
-rw-r--r--lua/telescope/actions/utils.lua32
2 files changed, 42 insertions, 0 deletions
diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua
index 402c979..4c3b42a 100644
--- a/lua/telescope/actions/init.lua
+++ b/lua/telescope/actions/init.lua
@@ -1128,6 +1128,16 @@ actions.which_key = function(prompt_bufnr, opts)
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = name })
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
diff --git a/lua/telescope/actions/utils.lua b/lua/telescope/actions/utils.lua
index f69112d..202ee73 100644
--- a/lua/telescope/actions/utils.lua
+++ b/lua/telescope/actions/utils.lua
@@ -103,4 +103,36 @@ function utils.get_registered_mappings(prompt_bufnr)
return ret
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