Skip to content

Commit 9f6c1a0

Browse files
committed
fix: fzf_live|actions append to fzf events
1 parent c7780cb commit 9f6c1a0

25 files changed

Lines changed: 162 additions & 56 deletions

lua/fzf-lua/config.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ function M.normalize_opts(opts, globals, __resume_key)
381381
end
382382
end
383383

384+
-- `fzf_cli_args` is string, `_fzf_cli_args` is a table used internally
385+
opts._fzf_cli_args = {}
386+
384387
-- backward compatibility, rhs overrides lhs
385388
-- (rhs being the "old" option)
386389
local backward_compat = {
@@ -921,8 +924,8 @@ function M.normalize_opts(opts, globals, __resume_key)
921924
utils.warn("'line_query' requires fzf >= 0.59, ignoring.")
922925
elseif opts.line_query then
923926
utils.map_set(opts, "winopts.preview.winopts.cursorline", true)
924-
utils.map_set(opts, "keymap.fzf.change",
925-
"transform:" .. FzfLua.shell.stringify_data(function(q, _, _)
927+
table.insert(opts._fzf_cli_args, "--bind=" .. libuv.shellescape("change:+transform:"
928+
.. FzfLua.shell.stringify_data(function(q, _, _)
926929
local lnum = q[1]:match(":(%d+)$")
927930
local new_q, subs = q[1]:gsub(":%d*$", "")
928931
-- No subs made, no ":" at end of string, do nothing
@@ -936,7 +939,7 @@ function M.normalize_opts(opts, globals, __resume_key)
936939
trans = string.format("%s+change-preview-window(%s:%s)", trans, optstr, offset)
937940
end
938941
return trans
939-
end, opts, "{q}"))
942+
end, opts, "{q}")))
940943
end
941944

942945
if type(opts.enrich) == "function" then

lua/fzf-lua/core.lua

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ M.fzf = function(contents, opts)
315315
opts = config.normalize_opts(opts or {}, {})
316316
if not opts then return end
317317
end
318+
-- Store contents for unhide
319+
opts._contents = contents
318320
-- flag used to print the query on stdout line 1
319321
-- later to be removed from the result by M.fzf()
320322
-- this provides a solution for saving the query
@@ -371,7 +373,8 @@ M.fzf = function(contents, opts)
371373
-- fzf 0.40 added 'zero' event for when there's no match
372374
-- clears the preview when there are no matching entries
373375
if utils.has(opts, "fzf", { 0, 40 }) and previewer.zero then
374-
utils.map_set(opts, "keymap.fzf.zero", previewer:zero())
376+
table.insert(opts._fzf_cli_args, "--bind="
377+
.. libuv.shellescape("zero:+" .. previewer:zero()))
375378
end
376379
if type(previewer.preview_window) == "function" then
377380
-- do we need to override the preview_window args?
@@ -751,8 +754,14 @@ M.build_fzf_cli = function(opts, fzf_win)
751754
end
752755
end
753756
for _, o in ipairs({ "fzf_args", "fzf_raw_args", "fzf_cli_args", "_fzf_cli_args" }) do
754-
if opts[o] then
755-
table.insert(cli_args, type(opts[o]) == "table" and opts[o] or tostring(opts[o]))
757+
local args = opts[o]
758+
if args then
759+
if type(args) ~= "table" then
760+
args = { tostring(args) }
761+
end
762+
for _, arg in ipairs(args) do
763+
table.insert(cli_args, arg)
764+
end
756765
end
757766
end
758767
return cli_args
@@ -1087,19 +1096,11 @@ M.convert_reload_actions = function(reload_cmd, opts)
10871096
}
10881097
end
10891098
end
1090-
opts.keymap.fzf.load = (function()
1091-
-- NOTE: this fixes existence of both load as function and rebind, e.g. git_status with:
1092-
-- setup({ keymap = { fzf = { true, load = function() _G._fzf_load_called = true end } } }
1093-
if type(opts.keymap.fzf.load) == "function" then
1094-
opts.keymap.fzf.load = "execute-silent:"
1095-
.. shell.stringify_data(opts.keymap.fzf.load, opts, nil, true)
1096-
end
1097-
if rebind and type(opts.keymap.fzf.load) == "string" then
1098-
return string.format("%s+%s", rebind, opts.keymap.fzf.load)
1099-
else
1100-
return rebind or opts.keymap.fzf.load
1101-
end
1102-
end)()
1099+
1100+
if rebind then
1101+
table.insert(opts._fzf_cli_args,
1102+
"--bind=" .. libuv.shellescape(string.format("load:+%s", rebind)))
1103+
end
11031104
return opts
11041105
end
11051106

@@ -1218,20 +1219,20 @@ M.setup_fzf_interactive_flags = function(command, fzf_field_index, opts)
12181219
opts.fzf_opts["--query"] = nil
12191220
opts.query = nil
12201221
-- setup as interactive
1221-
opts._fzf_cli_args = string.format("--interactive --cmd %s",
1222-
libuv.shellescape(no_query_condi .. reload_command))
1222+
table.insert(opts._fzf_cli_args, string.format("--interactive --cmd %s",
1223+
libuv.shellescape(no_query_condi .. reload_command)))
12231224
else
12241225
opts.fzf_opts["--disabled"] = true
12251226
opts.fzf_opts["--query"] = opts.query
12261227
-- OR with true to avoid fzf's "Command failed:" message
12271228
if opts.silent_fail ~= false then
12281229
reload_command = reload_command .. " || " .. utils.shell_nop()
12291230
end
1230-
opts._fzf_cli_args = string.format("--bind=%s", libuv.shellescape(
1231-
string.format("change:reload:%s%s", no_query_condi, reload_command)))
1231+
table.insert(opts._fzf_cli_args, "--bind="
1232+
.. libuv.shellescape(string.format("change:+reload:%s%s", no_query_condi, reload_command)))
12321233
if utils.has(opts, "fzf", { 0, 35 }) then
1233-
opts._fzf_cli_args = opts._fzf_cli_args .. string.format(" --bind=%s",
1234-
libuv.shellescape(string.format("start:reload:%s%s", no_query_condi, reload_command)))
1234+
table.insert(opts._fzf_cli_args, "--bind="
1235+
.. libuv.shellescape(string.format("start:+reload:%s%s", no_query_condi, reload_command)))
12351236
end
12361237
end
12371238

lua/fzf-lua/profiles/hide.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local uv = vim.uv or vim.loop
22
local fzf = require("fzf-lua")
33
local shell = require "fzf-lua.shell"
4+
local libuv = require "fzf-lua.libuv"
45
return {
56
desc = "hide interface instead of abort",
67
keymap = {
@@ -84,14 +85,18 @@ return {
8485
return act
8586
end, opts.actions)
8687
-- Hijack the resize event to reload buffer/tab list on unhide
87-
opts.keymap.fzf.resize = "transform:" .. shell.stringify_data(function(_, _, _)
88-
if opts._unhide_called then
89-
opts._unhide_called = nil
90-
if opts.__reload_cmd then
91-
return string.format("reload:%s", opts.__reload_cmd)
88+
table.insert(opts._fzf_cli_args, "--bind=" .. libuv.shellescape("resize:+transform:"
89+
.. shell.stringify_data(function(_, _, _)
90+
if opts._unhide_called then
91+
opts._unhide_called = nil
92+
if type(opts._contents) == "string"
93+
and type(opts.reload_on_unhide) == "function"
94+
and opts.reload_on_unhide(opts)
95+
then
96+
return string.format("reload:%s", opts._contents)
97+
end
9298
end
93-
end
94-
end, opts, "{q}")
99+
end, opts, "{q}")))
95100
return opts
96101
end,
97102
},

lua/fzf-lua/providers/buffers.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local uv = vim.uv or vim.loop
22
local core = require "fzf-lua.core"
33
local path = require "fzf-lua.path"
44
local utils = require "fzf-lua.utils"
5+
local libuv = require "fzf-lua.libuv"
56
local config = require "fzf-lua.config"
67
local base64 = require "fzf-lua.lib.base64"
78
local devicons = require "fzf-lua.devicons"
@@ -383,8 +384,8 @@ M.tabs = function(opts)
383384

384385
if opts.locate and utils.has(opts, "fzf", { 0, 36 }) then
385386
-- Set cursor to current buffer
386-
utils.map_set(opts, "keymap.fzf.load",
387-
"transform:" .. FzfLua.shell.stringify_data(function(_, _, _)
387+
table.insert(opts._fzf_cli_args, "--bind=" .. libuv.shellescape("load:+transform:"
388+
.. FzfLua.shell.stringify_data(function(_, _, _)
388389
local pos = 0
389390
for tabnr, tabh in ipairs(vim.api.nvim_list_tabpages()) do
390391
pos = pos + 1
@@ -398,7 +399,7 @@ M.tabs = function(opts)
398399
end
399400
end
400401
end
401-
end, opts))
402+
end, opts)))
402403
end
403404

404405
local contents = function(cb)

lua/fzf-lua/test/helpers.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ M.new_child_neovim = function()
139139
_G._fzf_load_called = nil
140140
end,
141141
},
142-
keymap = { fzf = {
143-
true,
144-
load = function() _G._fzf_load_called = true end,
145-
} }
142+
fzf_cli_args = "--bind=" .. FzfLua.libuv.shellescape("load:+execute-silent:"
143+
.. FzfLua.shell.stringify_data(function(_, _, _)
144+
_G._fzf_load_called = true
145+
end, {}))
146146
}))
147147
]])
148148
-- using "FZF_DEFAULT_OPTS" hangs the command on the

tests/api_spec.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,42 @@ T["api"]["fzf_live"]["rg"]["no error"] = new_set(
174174
}
175175
)
176176

177+
T["api"]["events"] = new_set(
178+
{ parametrize = { { "fzf_exec" }, { "fzf_live" } } },
179+
{
180+
function(api)
181+
local prompt = "EventsPrompt>"
182+
helpers.FzfLua[api](child,
183+
api == "fzf_exec"
184+
and [[(function() return { "foo", "bar", "baz" } end)()]]
185+
or [[function() return { "foo", "bar", "baz" } end ]],
186+
{
187+
__expect_lines = true,
188+
prompt = prompt,
189+
exec_empty_query = true,
190+
actions = {
191+
start = {
192+
fn = function(s) _G._fzf_prompt = s[1] end,
193+
field_index = "$FZF_PROMPT",
194+
exec_silent = true,
195+
},
196+
load = {
197+
fn = function(s) _G._fzf_total_count = tonumber(s[2]) end,
198+
field_index = "$FZF_PROMPT $FZF_TOTAL_COUNT",
199+
exec_silent = true,
200+
},
201+
},
202+
__after_open = function()
203+
child.wait_until(function()
204+
return child.lua_get([[_G._fzf_prompt]]) == prompt
205+
end)
206+
child.wait_until(function()
207+
return child.lua_get([[_G._fzf_total_count]]) == 3
208+
end)
209+
end,
210+
})
211+
end
212+
}
213+
)
214+
177215
return T
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--|---------|---------|---------|---------|---------|---------|----
2+
01|
3+
02|~
4+
03|~ ╭─────────────────────────────────────────────────╮
5+
04|~ │EventsPrompt> 3/3 │
6+
05|~ │──────────────────────────────────────────────── │
7+
06|~ │▌ foo │
8+
07|~ │ bar │
9+
08|~ │ baz │
10+
09|~ │ │
11+
10|~ │ │
12+
11|~ │ │
13+
12|~ │ │
14+
13|~ │ │
15+
14|~ │ │
16+
15|~ │ │
17+
16|~ │ │
18+
17|~ │ │
19+
18|~ │ │
20+
19|~ │ │
21+
20|~ │ │
22+
21|~ │ │
23+
22|~ ╰─────────────────────────────────────────────────╯
24+
23|~
25+
24|[No Name] 0,0-1 All
26+
25|
27+
26|
28+
27|
29+
28|-- TERMINAL -- 1,0-1 All
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--|---------|---------|---------|---------|---------|---------|----
2+
01|
3+
02|~
4+
03|~ ╭─────────────────────────────────────────────────╮
5+
04|~ │EventsPrompt> 3/3 │
6+
05|~ │──────────────────────────────────────────────── │
7+
06|~ │▌ foo │
8+
07|~ │ bar │
9+
08|~ │ baz │
10+
09|~ │ │
11+
10|~ │ │
12+
11|~ │ │
13+
12|~ │ │
14+
13|~ │ │
15+
14|~ │ │
16+
15|~ │ │
17+
16|~ │ │
18+
17|~ │ │
19+
18|~ │ │
20+
19|~ │ │
21+
20|~ │ │
22+
21|~ │ │
23+
22|~ ╰─────────────────────────────────────────────────╯
24+
23|~
25+
24|[No Name] 0,0-1 All
26+
25|
27+
26|
28+
27|
29+
28|-- TERMINAL -- 1,0-1 All

tests/screenshots/tests-api_spec.lua---api---fzf_exec---rg---1-+-args-{-1-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭─────────────────────────────────────────────────╮
5-
04|~ │> 118/118
5+
04|~ │> 120/120
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ LICENSE ││
88
07|~ │ Makefile ││

tests/screenshots/tests-api_spec.lua---api---fzf_exec---rg---1-+-args-{-false-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭─────────────────────────────────────────────────╮
5-
04|~ │> 119/119
5+
04|~ │> 121/121
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [st] rg --files -g "!.git" --sort=path││
88
07|~ │ LICENSE ││

0 commit comments

Comments
 (0)