fix(actions): which_key after mappings rework (#2556)
This commit is contained in:
@@ -17,7 +17,6 @@ globals = {
|
|||||||
"TelescopeGlobalState",
|
"TelescopeGlobalState",
|
||||||
"_TelescopeConfigurationValues",
|
"_TelescopeConfigurationValues",
|
||||||
"_TelescopeConfigurationPickers",
|
"_TelescopeConfigurationPickers",
|
||||||
"__TelescopeKeymapStore",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Global objects defined by the C code
|
-- Global objects defined by the C code
|
||||||
|
|||||||
@@ -1178,26 +1178,10 @@ actions.which_key = function(prompt_bufnr, opts)
|
|||||||
local mappings = {}
|
local mappings = {}
|
||||||
local mode = a.nvim_get_mode().mode
|
local mode = a.nvim_get_mode().mode
|
||||||
for _, v in pairs(action_utils.get_registered_mappings(prompt_bufnr)) do
|
for _, v in pairs(action_utils.get_registered_mappings(prompt_bufnr)) do
|
||||||
-- holds true for registered keymaps
|
if v.desc and v.desc ~= "which_key" and v.desc ~= "nop" then
|
||||||
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 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)
|
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = v.desc })
|
||||||
-- telescope.setup mappings might result in function names that reflect the keys
|
if v.desc == "<anonymous>" then
|
||||||
fname = fname:lower() == v.keybind:lower() and "<anonymous>" or fname
|
|
||||||
table.insert(mappings, { mode = v.mode, keybind = v.keybind, name = fname })
|
|
||||||
if fname == "<anonymous>" then
|
|
||||||
utils.notify("actions.which_key", {
|
utils.notify("actions.which_key", {
|
||||||
msg = "No name available for anonymous functions.",
|
msg = "No name available for anonymous functions.",
|
||||||
level = "INFO",
|
level = "INFO",
|
||||||
|
|||||||
@@ -81,25 +81,25 @@ function utils.map_selections(prompt_bufnr, f)
|
|||||||
end
|
end
|
||||||
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}`.
|
--- Utility to collect mappings of prompt buffer in array of `{mode, keybind, name}`.
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
function utils.get_registered_mappings(prompt_bufnr)
|
function utils.get_registered_mappings(prompt_bufnr)
|
||||||
local ret = {}
|
local ret = {}
|
||||||
for _, mode in ipairs { "n", "i" } do
|
for _, mode in ipairs { "n", "i" } do
|
||||||
local mode_mappings = vim.api.nvim_buf_get_keymap(prompt_bufnr, mode)
|
for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(prompt_bufnr, mode)) do
|
||||||
for _, mapping in ipairs(mode_mappings) do
|
|
||||||
-- ensure only telescope mappings
|
-- ensure only telescope mappings
|
||||||
if mapping.rhs and string.find(mapping.rhs, [[require%('telescope.mappings'%).execute_keymap]]) then
|
if mapping.desc then
|
||||||
local funcid = findnth(mapping.rhs, 2)
|
if mapping.desc:sub(1, 10) == "telescope|" then
|
||||||
table.insert(ret, { mode = mode, keybind = mapping.lhs, func = __TelescopeKeymapStore[prompt_bufnr][funcid] })
|
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 "<anonymous>" or fname
|
||||||
|
table.insert(ret, {
|
||||||
|
mode = mode,
|
||||||
|
keybind = mapping.lhs,
|
||||||
|
desc = fname,
|
||||||
|
})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -107,9 +107,8 @@ function utils.get_registered_mappings(prompt_bufnr)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Best effort to infer function names for actions.which_key
|
-- 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 Path = require "plenary.path"
|
||||||
local info = debug.getinfo(func_ref)
|
|
||||||
local fname
|
local fname
|
||||||
-- if fn defined in string (ie loadstring) source is string
|
-- if fn defined in string (ie loadstring) source is string
|
||||||
-- if fn defined in file, source is file name prefixed with a `@´
|
-- if fn defined in file, source is file name prefixed with a `@´
|
||||||
|
|||||||
@@ -834,7 +834,7 @@ end
|
|||||||
function make_entry.gen_from_keymaps(opts)
|
function make_entry.gen_from_keymaps(opts)
|
||||||
local function get_desc(entry)
|
local function get_desc(entry)
|
||||||
if entry.callback and not entry.desc then
|
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
|
end
|
||||||
return vim.F.if_nil(entry.desc, entry.rhs)
|
return vim.F.if_nil(entry.desc, entry.rhs)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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)
|
local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
|
||||||
if not key_func then
|
if not key_func then
|
||||||
return
|
return
|
||||||
@@ -236,7 +253,7 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
|
|||||||
local ret = key_func(prompt_bufnr)
|
local ret = key_func(prompt_bufnr)
|
||||||
vim.api.nvim_exec_autocmds("User", { pattern = "TelescopeKeymap" })
|
vim.api.nvim_exec_autocmds("User", { pattern = "TelescopeKeymap" })
|
||||||
return ret
|
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
|
end
|
||||||
|
|
||||||
local extract_keymap_opts = function(key_func)
|
local extract_keymap_opts = function(key_func)
|
||||||
|
|||||||
Reference in New Issue
Block a user