fix(which_key): get full path & handle table assignment of funcrefs (#1875)
This commit is contained in:
@@ -1129,12 +1129,16 @@ actions.which_key = function(prompt_bufnr, opts)
|
|||||||
elseif type(v.func) == "function" then
|
elseif type(v.func) == "function" then
|
||||||
if not opts.only_show_current_mode or mode == v.mode then
|
if not opts.only_show_current_mode or mode == v.mode then
|
||||||
local fname = action_utils._get_anon_function_name(v.func)
|
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 "<anonymous>" or fname
|
||||||
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = fname })
|
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = fname })
|
||||||
utils.notify("actions.which_key", {
|
if fname == "<anonymous>" then
|
||||||
msg = "No name available for anonymous functions.",
|
utils.notify("actions.which_key", {
|
||||||
level = "INFO",
|
msg = "No name available for anonymous functions.",
|
||||||
once = true,
|
level = "INFO",
|
||||||
})
|
once = true,
|
||||||
|
})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -108,7 +108,13 @@ function utils._get_anon_function_name(func_ref)
|
|||||||
local Path = require "plenary.path"
|
local Path = require "plenary.path"
|
||||||
local info = debug.getinfo(func_ref)
|
local info = debug.getinfo(func_ref)
|
||||||
local fname
|
local fname
|
||||||
for i, line in ipairs(Path:new(info.short_src):readlines()) do
|
-- if fn defined in string (ie loadstring) source is string
|
||||||
|
-- if fn defined in file, source is file name prefixed with a `@´
|
||||||
|
local path = Path:new((info.source:gsub("@", "")))
|
||||||
|
if not path:exists() then
|
||||||
|
return "<anonymous>"
|
||||||
|
end
|
||||||
|
for i, line in ipairs(path:readlines()) do
|
||||||
if i == info.linedefined then
|
if i == info.linedefined then
|
||||||
fname = line
|
fname = line
|
||||||
break
|
break
|
||||||
@@ -119,11 +125,15 @@ function utils._get_anon_function_name(func_ref)
|
|||||||
if (fname:match "=" == nil) and (fname:match "function %S+%(" == nil) then
|
if (fname:match "=" == nil) and (fname:match "function %S+%(" == nil) then
|
||||||
return "<anonymous>"
|
return "<anonymous>"
|
||||||
else
|
else
|
||||||
-- (1) remove function
|
local patterns = {
|
||||||
-- (2) whitespace and equal
|
{ "function", "" }, -- remove function
|
||||||
-- (3) anything in parenthesis incl. parentheses themselves
|
{ "local", "" }, -- remove local
|
||||||
-- (4) remove TABLE. prefix if available
|
{ "[%s=]", "" }, -- remove whitespace and =
|
||||||
local patterns = { { "function", "" }, { "local", "" }, { "[%s=]", "" }, { "%((.+)%)", "" }, { "(.+)%.", "" } }
|
{ [=[%[["']]=], "" }, -- remove left-hand bracket of table assignment
|
||||||
|
{ [=[["']%]]=], "" }, -- remove right-ahnd bracket of table assignment
|
||||||
|
{ "%((.+)%)", "" }, -- remove function arguments
|
||||||
|
{ "(.+)%.", "" }, -- remove TABLE. prefix if available
|
||||||
|
}
|
||||||
for _, tbl in ipairs(patterns) do
|
for _, tbl in ipairs(patterns) do
|
||||||
fname = (fname:gsub(tbl[1], tbl[2])) -- make sure only string is returned
|
fname = (fname:gsub(tbl[1], tbl[2])) -- make sure only string is returned
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user