Skip to content

Commit 773cba6

Browse files
phanenibhagwan
authored andcommitted
refactor: move mt_cmd_wrapper into stringify
1 parent e0cca3e commit 773cba6

10 files changed

Lines changed: 48 additions & 71 deletions

File tree

lua/fzf-lua/complete.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ M.path = function(opts)
9393
end
9494
end)()
9595
opts = set_cmp_opts_path(opts)
96-
local contents = core.mt_cmd_wrapper(opts)
97-
return core.fzf_exec(contents, opts)
96+
return core.fzf_exec(opts.cmd, opts)
9897
end
9998

10099
M.file = function(opts)
@@ -115,8 +114,7 @@ M.file = function(opts)
115114
end
116115
end)()
117116
opts = set_cmp_opts_path(opts)
118-
local contents = core.mt_cmd_wrapper(opts)
119-
return core.fzf_exec(contents, opts)
117+
return core.fzf_exec(opts.cmd, opts)
120118
end
121119

122120
M.line = function(opts)

lua/fzf-lua/core.lua

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,9 @@ M.fzf_exec = function(contents, opts)
163163
end
164164
-- Contents sent to fzf can only be nil or a shell command (string)
165165
-- the API accepts both tables and functions which we "stringify"
166-
if contents ~= nil and type(contents) ~= "string" and not opts.__stringified then
166+
if not opts.fn_reload then
167167
opts.__contents = contents
168-
local cmd, id = shell.stringify(opts)
169-
-- Protect function ptr from being overwritten in the circular buffer
170-
-- NOTE: will be cleared when a new picker is opened (not a resume)
171-
shell.set_protected(id)
172-
contents = cmd
168+
contents = shell.stringify(opts)
173169
end
174170
assert(contents == nil or type(contents) == "string", "contents must be of type string")
175171
-- setup as "live": disables fuzzy matching and reload the content
@@ -180,6 +176,7 @@ M.fzf_exec = function(contents, opts)
180176
-- TODO: add support for 'fn_transform' using 'mt_cmd_wrapper'
181177
-- functions can be stored using 'config.bytecode' which uses
182178
-- 'string.dump' to convert from function code to bytes
179+
opts.fn_reload = M.mt_cmd_wrapper(opts) or opts.fn_reload
183180
opts = M.setup_fzf_interactive_native(opts.fn_reload, opts)
184181
contents = opts.__fzf_init_cmd
185182
else
@@ -233,7 +230,7 @@ M.fzf_resume = function(opts)
233230
end
234231

235232
---@param opts table
236-
---@param contents content
233+
---@param contents string?
237234
---@param fn_selected function?
238235
---@return thread
239236
M.fzf_wrap = function(opts, contents, fn_selected)
@@ -320,7 +317,7 @@ M.CTX = function(opts)
320317
return M.__CTX
321318
end
322319

323-
---@param contents content
320+
---@param contents string?
324321
---@param opts {}?
325322
---@return string[]?
326323
M.fzf = function(contents, opts)
@@ -789,7 +786,7 @@ M.build_fzf_cli = function(opts, fzf_win)
789786
end
790787

791788
---@param opts table
792-
---@return string|function
789+
---@return string?
793790
M.mt_cmd_wrapper = function(opts)
794791
assert(opts and opts.cmd)
795792
---@param o table<string, unknown>
@@ -910,10 +907,7 @@ M.mt_cmd_wrapper = function(opts)
910907
opts.__fn_preprocess = opts.__fn_preprocess == nil
911908
and function(o) return make_entry.preprocess(o) end
912909
or opts.__fn_preprocess
913-
opts.__contents = opts.cmd
914-
local cmd, id = shell.stringify(opts)
915-
shell.set_protected(id)
916-
return cmd
910+
return nil
917911
end
918912
end
919913

@@ -1169,18 +1163,17 @@ end
11691163

11701164
-- converts actions defined with "reload=true" to use fzf's `reload` bind
11711165
-- provides a better UI experience without a visible interface refresh
1172-
---@param reload_cmd content
1166+
---@param reload_cmd string?
11731167
---@param opts table
11741168
---@return table
11751169
M.convert_reload_actions = function(reload_cmd, opts)
11761170
local fallback ---@type boolean?
1177-
local has_reload ---@type boolean?
11781171
-- Does not work with fzf version < 0.36, fzf fails with
11791172
-- "error 2: bind action not specified:" (#735)
11801173
-- Not yet supported with skim
11811174
if not utils.has(opts, "fzf", { 0, 36 })
11821175
or utils.has(opts, "sk")
1183-
or type(reload_cmd) ~= "string" then
1176+
or not reload_cmd then
11841177
fallback = true
11851178
end
11861179
-- Two types of action as table:
@@ -1191,7 +1184,6 @@ M.convert_reload_actions = function(reload_cmd, opts)
11911184
if type(v) == "function" or type(v) == "table" then
11921185
assert(type(v) == "function" or (v.fn and v[1] == nil) or (v[1] and v.fn == nil))
11931186
if type(v) == "table" and v.reload then
1194-
has_reload = true
11951187
assert(type(v.fn) == "function")
11961188
-- fallback: we cannot use the `reload` event (old fzf or skim)
11971189
-- convert to "old-style" interface reload using `resume`
@@ -1207,10 +1199,6 @@ M.convert_reload_actions = function(reload_cmd, opts)
12071199
end
12081200
end
12091201
end
1210-
if opts.silent ~= true and has_reload and reload_cmd and type(reload_cmd) ~= "string" then
1211-
utils.warn(
1212-
"actions with `reload` are only supported with string commands, using resume fallback")
1213-
end
12141202
if fallback then
12151203
-- for fallback, conversion to "old-style" actions is sufficient
12161204
return opts

lua/fzf-lua/fzf.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ end
3333
-- contents can be either a table with tostring()able items, or a function that
3434
-- can be called repeatedly for values. The latter can use coroutines for async
3535
-- behavior.
36-
---@param contents string[]|table|function?
36+
---@param contents string
3737
---@param fzf_cli_args string[]
3838
---@param opts table
3939
---@return table selected

lua/fzf-lua/libuv.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ M.wrap_spawn_stdio = function(opts, fn_transform, fn_preprocess, fn_postprocess)
694694
local nvim_bin = os.getenv("FZF_LUA_NVIM_BIN") or vim.v.progpath
695695
local cmd_str = ("%s -u NONE -l %s %s"):format(
696696
M.shellescape(_is_win and vim.fs.normalize(nvim_bin) or nvim_bin),
697-
M.shellescape(vim.fn.fnamemodify(_is_win and vim.fs.normalize(__FILE__) or __FILE__, ":h") .. "/spawn.lua"),
697+
M.shellescape(vim.fn.fnamemodify(_is_win and vim.fs.normalize(__FILE__) or __FILE__, ":h") ..
698+
"/spawn.lua"),
698699
M.shellescape(("return %s,%s,%s,%s"):format(opts, fn_transform, fn_preprocess, fn_postprocess))
699700
)
700701
return cmd_str

lua/fzf-lua/make_entry.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ local function load_config_section(s, datatype, optional)
2727
if datatype == "function" then
2828
is_bytecode = true
2929
exec_opts = { s, datatype }
30-
exec_str = ("return require'fzf-lua'.config.bytecode(...)"):format(s)
30+
exec_str = "return require'fzf-lua'.config.bytecode(...)"
3131
else
3232
exec_opts = {}
3333
exec_str = ("return require'fzf-lua'.config.%s"):format(s)

lua/fzf-lua/providers/files.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ M.files = function(opts)
8888
-- set `opts.cwd` for relative path display
8989
opts.cwd = uv.cwd()
9090
end
91-
local contents = core.mt_cmd_wrapper(opts)
9291
opts = core.set_title_flags(opts, { "cmd" })
9392
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
94-
return core.fzf_exec(contents, opts)
93+
return core.fzf_exec(opts.cmd, opts)
9594
end
9695

9796
M.args = function(opts)
@@ -152,7 +151,6 @@ M.zoxide = function(opts)
152151

153152
opts.__fn_transform = function(x) return make_entry.zoxide(x, opts) end
154153
opts.__mt_transform = [[return require("fzf-lua.make_entry").zoxide]]
155-
local contents = core.mt_cmd_wrapper(opts)
156154

157155
if opts.header == nil then
158156
opts.header = string.format("%8s\t%s", "score", "folder")
@@ -167,7 +165,7 @@ M.zoxide = function(opts)
167165
or "ls -la {2}"
168166
end)()
169167

170-
return core.fzf_exec(contents, opts)
168+
return core.fzf_exec(opts.cmd, opts)
171169
end
172170

173171
return M

lua/fzf-lua/providers/git.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ M.files = function(opts)
2727
if not opts then return end
2828
opts = set_git_cwd_args(opts)
2929
if not opts.cwd then return end
30-
local contents = core.mt_cmd_wrapper(opts)
3130
opts = core.set_header(opts, opts.headers or { "cwd" })
32-
return core.fzf_exec(contents, opts)
31+
return core.fzf_exec(opts.cmd, opts)
3332
end
3433

3534
M.status = function(opts)
@@ -56,13 +55,12 @@ M.status = function(opts)
5655
-- will be set by `core.mt_cmd_wrapper` by commenting out the above
5756
opts.__fn_transform = function(x) return make_entry.git_status(x, opts) end
5857
opts.__mt_transform = [[return require("fzf-lua.make_entry").git_status]]
59-
local contents = core.mt_cmd_wrapper(opts)
6058

6159
opts.header_prefix = opts.header_prefix or "+ - "
6260
opts.header_separator = opts.header_separator or "|"
6361
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
6462

65-
return core.fzf_exec(contents, opts)
63+
return core.fzf_exec(opts.cmd, opts)
6664
end
6765

6866
local function git_cmd(opts)
@@ -108,9 +106,8 @@ M.diff = function(opts)
108106
opts = set_git_cwd_args(opts)
109107
if not opts.cwd then return end
110108
opts.preview = git_preview(opts, "{-1}")
111-
local contents = core.mt_cmd_wrapper(opts)
112109
opts = core.set_header(opts, opts.headers or { "cwd" })
113-
return core.fzf_exec(contents, opts)
110+
return core.fzf_exec(opts.cmd, opts)
114111
end
115112

116113
M.commits = function(opts)
@@ -261,13 +258,12 @@ M.hunks = function(opts)
261258

262259
opts.__fn_transform = function(x) return make_entry.git_hunk(x, opts) end
263260
opts.__mt_transform = [[return require("fzf-lua.make_entry").git_hunk]]
264-
local contents = core.mt_cmd_wrapper(opts)
265261

266262
opts.header_prefix = opts.header_prefix or "+ - "
267263
opts.header_separator = opts.header_separator or "|"
268264
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
269265

270-
return core.fzf_exec(contents, opts)
266+
return core.fzf_exec(opts.cmd, opts)
271267
end
272268

273269
return M

lua/fzf-lua/providers/grep.lua

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,15 @@ M.grep = function(opts)
198198
opts.cmd = get_grep_cmd(opts, opts.search, opts.no_esc)
199199
if not opts.cmd then return end
200200

201-
local contents = core.mt_cmd_wrapper(vim.tbl_deep_extend("force", opts,
202-
-- query was already parsed for globs inside 'get_grep_cmd'
203-
-- no need for our external headless instance to parse again
204-
{ rg_glob = false }))
205-
206-
-- by redirecting the error stream to stdout
207-
-- we make sure a clear error message is displayed
208-
-- when the user enters bad regex expressions
209-
if type(contents) == "string" then
210-
contents = contents .. " 2>&1"
211-
end
201+
-- query was already parsed for globs inside 'get_grep_cmd'
202+
-- no need for our external headless instance to parse again
203+
opts.rg_glob = false
212204

213205
-- search query in header line
214206
opts = core.set_title_flags(opts, { "cmd" })
215207
opts = core.set_header(opts, opts.headers or { "actions", "cwd", "search" })
216208
opts = core.set_fzf_field_index(opts)
217-
core.fzf_exec(contents, opts)
209+
core.fzf_exec(opts.cmd, opts)
218210
end
219211

220212
local function normalize_live_grep_opts(opts)
@@ -297,13 +289,6 @@ M.live_grep_st = function(opts)
297289

298290
assert(not opts.multiprocess)
299291

300-
opts.fn_reload = function(query)
301-
-- can be nil when called as fzf initial command
302-
query = query or ""
303-
opts.no_esc = nil
304-
return get_grep_cmd(opts, query, true)
305-
end
306-
307292
if opts.requires_processing or opts.git_icons or opts.file_icons then
308293
opts.fn_transform = opts.fn_transform or
309294
function(x)
@@ -319,7 +304,13 @@ M.live_grep_st = function(opts)
319304
opts = core.set_title_flags(opts, { "cmd", "live" })
320305
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
321306
opts = core.set_fzf_field_index(opts)
322-
core.fzf_exec(nil, opts)
307+
308+
core.fzf_live(function(query)
309+
-- can be nil when called as fzf initial command
310+
query = query or ""
311+
opts.no_esc = nil
312+
return get_grep_cmd(opts, query, true)
313+
end, opts)
323314
end
324315

325316
-- multi threaded (multi-process actually) version
@@ -345,17 +336,11 @@ M.live_grep_mt = function(opts)
345336
opts.cmd = get_grep_cmd(opts, core.fzf_query_placeholder, 2)
346337
if not opts.cmd then return end
347338

348-
local command = core.mt_cmd_wrapper(opts)
349-
350-
-- signal 'fzf_exec' to set 'change:reload' parameters
351-
-- or skim's "interactive" mode (AKA "live query")
352-
opts.fn_reload = command
353-
354339
-- search query in header line
355340
opts = core.set_title_flags(opts, { "cmd", "live" })
356341
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
357342
opts = core.set_fzf_field_index(opts)
358-
core.fzf_exec(nil, opts)
343+
core.fzf_live(opts.cmd, opts)
359344
end
360345

361346
M.live_grep_glob_st = function(opts)

lua/fzf-lua/providers/lsp.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ M.live_workspace_symbols = function(opts)
816816
opts.lsp_params = { query = opts.lsp_query or opts.query or "" }
817817
opts.query = opts.lsp_query or opts.query
818818

819-
opts.fn_reload = function(query)
819+
local contents = function(query)
820820
opts.query = query
821821
opts.lsp_params = { query = query or "" }
822822
opts = gen_lsp_contents(opts)
@@ -829,7 +829,7 @@ M.live_workspace_symbols = function(opts)
829829
opts.fn_pre_fzf = function() gen_sym2style_map(opts) end
830830
opts.fn_post_fzf = function() M._sym2style = nil end
831831
end
832-
return core.fzf_exec(nil, opts)
832+
return core.fzf_live(contents, opts)
833833
end
834834

835835
M.code_actions = function(opts)

lua/fzf-lua/shell.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,25 @@ M.raw_preview_action_cmd = function(fn, fzf_field_index, debug)
169169
end
170170

171171
---@param opts {}
172-
---@return string, integer
172+
---@return string, integer?
173173
M.stringify = function(opts, fzf_field_index)
174174
-- Fzf field index expression, e.g. "{+}" (selected), "{q}" (query)
175175
fzf_field_index = fzf_field_index or opts.__fzf_field_index or ""
176176

177177
assert(type(opts.__fn_reload) == "function" or opts.__contents, "must supply contents")
178178

179179
-- Mark opts as already "stringified"
180+
assert(not opts.__stringified, "twice stringified")
180181
opts.__stringified = true
181182

182-
return M.raw_async_action(function(pipe, args)
183+
-- no register function
184+
if type(opts.__contents) == "string" then
185+
opts.cmd = opts.__contents
186+
local cmd = require("fzf-lua.core").mt_cmd_wrapper(opts)
187+
if cmd then return cmd, nil end
188+
end
189+
190+
local cmd, id = M.raw_async_action(function(pipe, args)
183191
-- Contents could be dependent or args, e.g. live_grep which
184192
-- generates a different command based on the typed query
185193
local contents = type(opts.__fn_reload) == "function"
@@ -318,6 +326,9 @@ M.stringify = function(opts, fzf_field_index)
318326
fn_load()
319327
end
320328
end, fzf_field_index, opts.debug)
329+
330+
M.set_protected(id)
331+
return cmd, id
321332
end
322333

323334
return M

0 commit comments

Comments
 (0)