Skip to content

Commit e65b865

Browse files
committed
feat: --expect improvements with upcoming fzf 0.53
Once fzf 0.53 is releases adding fzf prefix for actions will become much easier due to junegunn/fzf#3810 A few examples below. Adding a prefix to an already existing action (`alt-q` send to quickfix): ```lua :FzfLua live_grep keymap.fzf.alt-q=select-all ``` Using a different (or custom) action: ```lua :lua require("fzf-lua").files({ actions ={ ["ctrl-q"] = { fn = require"fzf-lua".actions.file_sel_to_qf, prefix = "select-all+accept" } } }) ```
1 parent a9e5755 commit e65b865

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

lua/fzf-lua/actions.lua

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@ local M = {}
88
local _default_action = "default"
99

1010
-- return fzf '--expect=' string from actions keyval tbl
11-
M.expect = function(actions)
11+
-- on fzf >= 0.53 add the `prefix` key to the bind flags
12+
M.expect = function(actions, _)
1213
if not actions then return nil end
1314
local keys = {}
15+
local binds = {}
1416
for k, v in pairs(actions) do
15-
if k ~= _default_action and v ~= false then
17+
local is_default = k == _default_action
18+
if not is_default and v ~= false then
1619
table.insert(keys, k)
1720
end
21+
-- Although this only works in fzf 0.53 we don't need to limit the use of these
22+
-- actions as `--expect` precets the bind and thus the extra bind has no effect
23+
if type(v) == "table" and type(v.prefix) == "string" then
24+
table.insert(binds, string.format("%s:%s%s",
25+
is_default and "enter" or k,
26+
-- When used with `--expect` "accept" is implied and "+" is erroneous
27+
v.prefix:gsub("accept$", ""):gsub("%+$", ""),
28+
-- Since default accept bind (enter) is excluded from `--expect`
29+
-- we need to add "+accept" to complete the selection and exit fzf
30+
is_default and "+accept" or ""))
31+
end
1832
end
19-
if #keys > 0 then
20-
return table.concat(keys, ",")
21-
end
22-
return nil
33+
return #keys > 0 and keys or nil, #binds > 0 and binds or nil
2334
end
2435

2536
M.normalize_selected = function(actions, selected)

lua/fzf-lua/core.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,22 @@ M.build_fzf_cli = function(opts)
592592
end
593593
opts.fzf_opts["--bind"] = M.create_fzf_binds(opts.keymap.fzf)
594594
opts.fzf_opts["--color"] = M.create_fzf_colors(opts)
595-
opts.fzf_opts["--expect"] = actions.expect(opts.actions)
595+
do
596+
-- `actions.expect` parses the actions table and returns a list of
597+
-- keys that trigger completion (accept) to be added to `--expect`
598+
-- If the action contains the prefix key, e.g. `{ fn = ... , prefix = "select-all" }`
599+
-- additional binds will be set for the specific action key
600+
-- NOTE: requires fzf >= 0.53 (https://github.com/junegunn/fzf/issues/3810)
601+
local expect_keys, expect_binds = actions.expect(opts.actions, opts)
602+
if expect_keys and #expect_keys > 0 then
603+
opts.fzf_opts["--expect"] = table.concat(expect_keys, ",")
604+
end
605+
if expect_binds and #expect_binds > 0 then
606+
local bind = opts.fzf_opts["--bind"]
607+
opts.fzf_opts["--bind"] = string.format("%s%s%s",
608+
bind, bind and "," or "", table.concat(expect_binds, ","))
609+
end
610+
end
596611
if opts.fzf_opts["--preview-window"] == nil then
597612
opts.fzf_opts["--preview-window"] = M.preview_window(opts)
598613
end
@@ -878,7 +893,9 @@ M.set_header = function(opts, hdr_tbl)
878893
if type(opts.regex_filter) == "string" then
879894
return opts.regex_filter
880895
elseif type(opts.regex_filter) == "table" and type(opts.regex_filter[1]) == "string" then
881-
return string.format("%s%s", opts.regex_filter.exclude and "not " or "", opts.regex_filter[1])
896+
return string.format("%s%s",
897+
opts.regex_filter.exclude and "not " or "",
898+
opts.regex_filter[1])
882899
elseif type(opts.regex_filter) == "function" then
883900
return "<function>"
884901
end

0 commit comments

Comments
 (0)