Skip to content

Commit 7430309

Browse files
committed
fix(resume): do not over "protect"
1 parent c8b5361 commit 7430309

6 files changed

Lines changed: 51 additions & 31 deletions

File tree

lua/fzf-lua/core.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ M.fzf_exec = function(contents, opts)
155155
-- the API accepts both tables and functions which we "stringify"
156156
-- We also send string commands as stringify is also responsible
157157
-- for multiprocess wrapping of shell commands with processing
158-
contents = contents and shell.stringify(contents, opts) or nil
158+
shell.clear_protected()
159+
contents = contents and shell.stringify(contents, opts, nil, true) or nil
159160
assert(contents == nil or type(contents) == "string", "contents must be of type string")
160161
return M.fzf_wrap(contents, opts)
161162
end
@@ -172,7 +173,10 @@ M.fzf_live = function(contents, opts)
172173
-- AKA "live": fzf acts as a selector only (fuzzy matching is disabled)
173174
-- each keypress reloads fzf's input usually based on the typed query
174175
-- utilizes fzf's 'change:reload' event or skim's "interactive" mode
175-
opts.fn_reload = shell.stringify(contents, opts)
176+
-- convert "reload" actions to fzf's `reload` binds
177+
-- convert "exec_silent" actions to fzf's `execute-silent` binds
178+
shell.clear_protected()
179+
opts.fn_reload = shell.stringify(contents, opts, nil, true)
176180
local fzf_field_index = M.fzf_field_index(opts)
177181
local cmd = M.expand_query(opts.fn_reload, fzf_field_index)
178182
contents, opts = M.setup_fzf_interactive_flags(cmd, fzf_field_index, opts)
@@ -190,7 +194,6 @@ M.fzf_resume = function(opts)
190194
assert(opts == config.__resume_data.opts)
191195
opts = M.set_header(opts, opts.headers or {})
192196
opts.cwd = opts.cwd and libuv.expand(opts.cwd) or nil
193-
opts.__resuming = true
194197
M.fzf_wrap(config.__resume_data.contents, config.__resume_data.opts)
195198
end
196199

@@ -199,6 +202,9 @@ end
199202
---@return thread, string, table
200203
M.fzf_wrap = function(contents, opts)
201204
opts = opts or {}
205+
-- Does nothing if already converted
206+
opts = M.convert_reload_actions(contents, opts)
207+
opts = M.convert_exec_silent_actions(opts)
202208
local _co
203209
local wrapped = coroutine.wrap(function()
204210
_co = coroutine.running()
@@ -315,11 +321,6 @@ M.fzf = function(contents, opts)
315321
opts.actions[k] = actions.dummy_abort
316322
end
317323
end
318-
if not opts.__resuming then
319-
-- `opts.__resuming` is only set from `fzf_resume`, since we
320-
-- not resuming clear the shell protected functions registry
321-
shell.clear_protected()
322-
end
323324
-- store last call opts for resume
324325
config.resume_set(nil, opts.__call_opts, opts)
325326
-- caller specified not to resume this call (used by "builtin" provider)
@@ -392,10 +393,6 @@ M.fzf = function(contents, opts)
392393

393394
fzf_win:attach_previewer(previewer)
394395
local fzf_bufnr = fzf_win:create()
395-
-- convert "reload" actions to fzf's `reload` binds
396-
-- convert "exec_silent" actions to fzf's `execute-silent` binds
397-
opts = M.convert_reload_actions(contents, opts)
398-
opts = M.convert_exec_silent_actions(opts)
399396
local selected, exit_code = fzf.raw_fzf(contents, M.build_fzf_cli(opts, fzf_win),
400397
{
401398
fzf_bin = opts.fzf_bin,
@@ -997,7 +994,7 @@ local patch_shell_action = function(v, opts)
997994
items = (zero_matched and zero_selected) and {} or items
998995
end
999996
v.fn(items, opts)
1000-
end, opts, field_index)
997+
end, opts, field_index, true)
1001998
end
1002999

10031000
-- converts actions defined with "reload=true" to use fzf's `reload` bind
@@ -1006,6 +1003,8 @@ end
10061003
---@param opts table
10071004
---@return table
10081005
M.convert_reload_actions = function(reload_cmd, opts)
1006+
if opts.__converted_reload then return opts end
1007+
opts.__converted_reload = true
10091008
local fallback ---@type boolean?
10101009
-- Does not work with fzf version < 0.36, fzf fails with
10111010
-- "error 2: bind action not specified:" (#735)
@@ -1083,7 +1082,8 @@ M.convert_reload_actions = function(reload_cmd, opts)
10831082
-- NOTE: this fixes existence of both load as function and rebind, e.g. git_status with:
10841083
-- setup({ keymap = { fzf = { true, load = function() _G._fzf_load_called = true end } } }
10851084
if type(opts.keymap.fzf.load) == "function" then
1086-
opts.keymap.fzf.load = "execute-silent:" .. shell.stringify_data(opts.keymap.fzf.load, opts)
1085+
opts.keymap.fzf.load = "execute-silent:"
1086+
.. shell.stringify_data(opts.keymap.fzf.load, opts, nil, true)
10871087
end
10881088
if rebind and type(opts.keymap.fzf.load) == "string" then
10891089
return string.format("%s+%s", rebind, opts.keymap.fzf.load)
@@ -1099,6 +1099,8 @@ end
10991099
---@param opts table
11001100
---@return table
11011101
M.convert_exec_silent_actions = function(opts)
1102+
if opts.__converted_exec_silent then return opts end
1103+
opts.__converted_exec_silent = true
11021104
-- `execute-silent` actions are bugged with skim (can't use quotes)
11031105
if utils.has(opts, "sk") then
11041106
return opts

lua/fzf-lua/previewer/builtin.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ function Previewer.base:cmdline(_)
454454
-- save last entry even if we don't display
455455
self.last_entry = entry
456456
return ""
457-
end, self.opts, "{} {q} {n}")
457+
end, self.opts, "{} {q} {n}", false)
458458
return act
459459
end
460460

@@ -481,7 +481,7 @@ function Previewer.base:zero(_)
481481
self.last_entry = nil
482482
vim.fn.delete(self._zero_lock, "d")
483483
end, self.delay)
484-
end, self.opts, ""))
484+
end, self.opts, "", false))
485485
return act
486486
end
487487

lua/fzf-lua/previewer/fzf.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function Previewer.cmd:action(o)
8787
local act = shell.stringify_data(function(items, _, _)
8888
local entry = path.entry_to_file(items[1], self.opts)
8989
return entry.bufname or entry.path
90-
end, self.opts, self.opts.field_index_expr or "{}")
90+
end, self.opts, self.opts.field_index_expr or "{}", false)
9191
return act
9292
end
9393

@@ -214,7 +214,7 @@ function Previewer.cmd_async:cmdline(o)
214214
local cmd = errcmd or ("%s %s %s"):format(
215215
self.cmd, self.args, libuv.shellescape(filepath))
216216
return cmd
217-
end, self.opts, "{} {q}")
217+
end, self.opts, "{} {q}", false)
218218
return act
219219
end
220220

@@ -278,7 +278,7 @@ function Previewer.bat_async:cmdline(o)
278278
line_range,
279279
libuv.shellescape(filepath))
280280
return cmd
281-
end, self.opts, "{} {q}")
281+
end, self.opts, "{} {q}", false)
282282
return act
283283
end
284284

@@ -379,7 +379,7 @@ function Previewer.git_diff:cmdline(o)
379379
-- cmd = string.format("%s %s %s", table.concat(setenv, " "), cmd, pager)
380380
cmd = string.format("%s %s", cmd, pager)
381381
return { cmd = cmd, env = env }
382-
end, self.opts, "{}")
382+
end, self.opts, "{}", false)
383383
return act
384384
end
385385

@@ -398,7 +398,7 @@ function Previewer.man_pages:cmdline(o)
398398
local manpage = require("fzf-lua.providers.manpages").manpage_sh_arg(items[1])
399399
local cmd = self.cmd:format(manpage)
400400
return cmd
401-
end, self.opts, "{}")
401+
end, self.opts, "{}", false)
402402
return act
403403
end
404404

@@ -435,7 +435,7 @@ function Previewer.help_tags:cmdline(o)
435435
end
436436
end
437437
return cmd
438-
end, self.opts, "{}")
438+
end, self.opts, "{}", false)
439439
return act
440440
end
441441

lua/fzf-lua/profiles/telescope.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ end
88
return {
99
{ "default-title" }, -- base profile
1010
desc = "match telescope default highlights|keybinds",
11-
fzf_opts = { ["--layout"] = "default", ["--marker"] = "+" , ["--cycle"] = true},
11+
fzf_opts = { ["--layout"] = "default", ["--marker"] = "+", ["--cycle"] = true },
1212
winopts = {
1313
width = 0.8,
1414
height = 0.9,

lua/fzf-lua/rpc.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ local rpc_nvim_exec_lua = function(opts)
8282
vim.fn.chanclose(chan_id)
8383
end)
8484

85-
if not success or opts.debug then
85+
if not success or opts.debug == "v" or opts.debug == 2 then
8686
io.stderr:write(("[DEBUG] debug = %s\n"):format(opts.debug))
8787
io.stderr:write(("[DEBUG] function ID = %d\n"):format(opts.fnc_id))
8888
io.stderr:write(("[DEBUG] fzf_lua_server = %s\n"):format(opts.fzf_lua_server))
@@ -107,7 +107,18 @@ local args = vim.deepcopy(_G.arg)
107107
args[0] = nil -- remove filename
108108
local opts = {
109109
fnc_id = tonumber(table.remove(args, 1)),
110-
debug = table.remove(args, 1) == "true",
110+
debug = (function()
111+
local ret = table.remove(args, 1)
112+
if ret == "nil" then
113+
return nil
114+
elseif ret == "true" then
115+
return true
116+
elseif ret == "false" then
117+
return false
118+
else
119+
return tonumber(ret) or tostring(ret)
120+
end
121+
end)(),
111122
fzf_selection = args,
112123
fzf_lua_server = vim.env.FZF_LUA_SERVER or vim.env.SKIM_FZF_LUA_SERVER or vim.env.NVIM,
113124
}

lua/fzf-lua/shell.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ end
235235
---Fzf field index expression, e.g. "{+}" (selected), "{q}" (query)
236236
---@param fzf_field_index string?
237237
---@return string, integer?
238-
M.stringify = function(contents, opts, fzf_field_index)
238+
M.stringify = function(contents, opts, fzf_field_index, protect)
239239
assert(contents, "must supply contents")
240240

241241
-- TODO: should we let this assert?
@@ -411,20 +411,27 @@ M.stringify = function(contents, opts, fzf_field_index)
411411
end
412412
end, fzf_field_index or "", opts.debug)
413413

414-
M.set_protected(id)
414+
-- "protect" a function ptr, cleared when opening a new picker in `core.fzf()`
415+
-- protected functions include init (content) commands and acrions (reload
416+
-- and execute-silent), previewer trigger commands are excluded (zero event
417+
-- and preview callback)
418+
if protect then
419+
M.set_protected(id)
420+
end
421+
415422
return cmd, id
416423
end
417424

418-
M.stringify_cmd = function(fn, opts, fzf_field_index)
425+
M.stringify_cmd = function(fn, opts, fzf_field_index, protect)
419426
assert(type(fn) == "function", "fn must be of type function")
420427
return M.stringify(fn, {
421428
__stringify_cmd = true,
422429
PidObject = utils.pid_object("__stringify_cmd_pid", opts),
423430
debug = opts.debug,
424-
}, fzf_field_index)
431+
}, fzf_field_index, protect)
425432
end
426433

427-
M.stringify_data = function(fn, opts, fzf_field_index)
434+
M.stringify_data = function(fn, opts, fzf_field_index, protect)
428435
assert(type(fn) == "function", "fn must be of type function")
429436
return M.stringify(function(cb, _, ...)
430437
local ret = fn(...)
@@ -436,7 +443,7 @@ M.stringify_data = function(fn, opts, fzf_field_index)
436443
cb(tostring(ret))
437444
end
438445
cb(nil)
439-
end, { debug = opts.debug }, fzf_field_index)
446+
end, { debug = opts.debug }, fzf_field_index, protect)
440447
end
441448

442449
return M

0 commit comments

Comments
 (0)