Skip to content

Commit b9f947a

Browse files
committed
refactor: get_action_helpstr parse action dict
1 parent 2b4e6de commit b9f947a

5 files changed

Lines changed: 15 additions & 25 deletions

File tree

lua/fzf-lua/config.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,14 @@ M.set_action_helpstr = function(fn, helpstr)
10631063
M._action_to_helpstr[fn] = helpstr
10641064
end
10651065

1066-
M.get_action_helpstr = function(fn)
1067-
return M._action_to_helpstr[fn]
1066+
---@param v fzf-lua.ActionSpec|any
1067+
---@return string
1068+
M.get_action_helpstr = function(v)
1069+
local t = M._action_to_helpstr
1070+
if type(v) ~= "table" or t[v] then return t[v] or tostring(v) end
1071+
local res = v.desc or t[v[1]] or t[v.fn] or v.header
1072+
if res then return res end
1073+
return type(v[1]) == "string" and v[1] or tostring(v)
10681074
end
10691075

10701076
M._action_to_helpstr = {

lua/fzf-lua/core.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ M.convert_reload_actions = function(reload_cmd, opts)
10201020
M.can_transform(opts) and "transform" or "reload", -- contents is not "cmd" but "reload:cmd"
10211021
reload_cmd,
10221022
type(v.postfix) == "string" and v.postfix or ""),
1023-
desc = v.desc or config.get_action_helpstr(v.fn)
1023+
desc = config.get_action_helpstr(v)
10241024
}
10251025
end
10261026
end
@@ -1075,7 +1075,7 @@ M.convert_exec_silent_actions = function(opts)
10751075
or string.format(":%s", cmd),
10761076
-- can't use postfix since we use "execute-silent:..."
10771077
has_fzf036 and type(v.postfix) == "string" and v.postfix or ""),
1078-
desc = v.desc or config.get_action_helpstr(v.fn)
1078+
desc = config.get_action_helpstr(v)
10791079
}
10801080
end
10811081
end

lua/fzf-lua/profiles/hide.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ return {
7373
then
7474
local fn = act.fn
7575
act.exec_silent = true
76-
act.desc = act.desc or fzf.config.get_action_helpstr(fn)
76+
act.desc = act.desc or fzf.config.get_action_helpstr(act)
7777
act.fn = function(...)
7878
fzf.hide()
7979
if fn then fn(...) end

lua/fzf-lua/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ local FzfLua = require("fzf-lua")
5555
---@field reload? boolean
5656
---@field field_index? string
5757
---@field desc? string
58+
---@field header? string|false
5859
---@field prefix? string
5960
---@field postfix? string
6061
---@field reuse? boolean

lua/fzf-lua/win/help.lua

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,43 +47,26 @@ function M.toggle(keymap, actions, hls, zindex, preview_keymaps, preview_mode, h
4747
for _, m in ipairs({ "builtin", "fzf" }) do
4848
for k, v in pairs(keymap[m]) do
4949
if not keymap_ignore[k] then
50-
-- value can be defined as a table with addl properties (help string)
51-
if type(v) == "table" then
52-
v = v.desc or v[1]
53-
end
50+
v = config.get_action_helpstr(v)
5451
-- only add preview keybinds respective of
5552
-- the current preview mode
56-
if v and (not preview_keymaps[v] or m == preview_mode) then
53+
if not preview_keymaps[v] or m == preview_mode then
5754
if m == "builtin" then
5855
k = utils.neovim_bind_to_fzf(k)
5956
end
60-
v = type(v) == "function" and config.get_action_helpstr(v) or tostring(v)
6157
table.insert(keymaps,
6258
format_bind(m, k, v, opts.mode_width, opts.keybind_width, opts.name_width))
6359
end
6460
end
6561
end
6662
end
6763

68-
---TODO: we can always parse the action into table to avoid this duplicated logic
69-
---(e.g. profile/hide.lua, config.lua)
70-
---@param v fzf-lua.ActionSpec
71-
---@return string?
72-
local get_desc = function(v)
73-
if type(v) == "table" then
74-
return v.desc or config.get_action_helpstr(v[1]) or config.get_action_helpstr(v.fn) or
75-
v.header or tostring(v)
76-
elseif v then
77-
return config.get_action_helpstr(v) or tostring(v)
78-
end
79-
end
80-
8164
-- action keymaps
8265
if actions then
8366
for k, v in pairs(actions) do
8467
if v then -- skips 'v == false'
8568
if k == "default" then k = "enter" end
86-
local desc = get_desc(v)
69+
local desc = config.get_action_helpstr(v)
8770
table.insert(keymaps,
8871
format_bind("action", k,
8972
("%s"):format(desc):gsub(" ", ""),

0 commit comments

Comments
 (0)