Skip to content

Commit df8c2ca

Browse files
phanenibhagwan
authored andcommitted
refactor: simplify serialization
1 parent 35e27b8 commit df8c2ca

5 files changed

Lines changed: 64 additions & 98 deletions

File tree

lua/fzf-lua/lib/base64.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
if vim.base64 then
2+
return vim.base64
3+
end
4+
5+
---@format disable
16
--[[
27
38
Source: https://github.com/iskolbin/lbase64

lua/fzf-lua/libuv.lua

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -329,46 +329,43 @@ end
329329
-- Coroutine version of spawn so we can use queue
330330
M.async_spawn = coroutinify(M.spawn)
331331

332-
---@param opts table|string
333-
---@param fn_transform_str string
334-
---@param fn_preprocess_str string
335-
---@param fn_postprocess_str string
336-
---@return uv.uv_process_t, integer
337-
M.spawn_stdio = function(opts, fn_transform_str, fn_preprocess_str, fn_postprocess_str)
338-
-- attempt base64 decoding on all params
339-
---@param str string|table
340-
---@return string|table
341-
local base64_conditional_decode = function(str)
342-
if opts._base64 == false or type(str) ~= "string" then return str end
343-
local ok, decoded = pcall(base64.decode, str)
344-
return ok and decoded or str
345-
end
346-
347-
---@param fn_str string
348-
---@return function?
349-
local function load_fn(fn_str)
350-
if type(fn_str) ~= "string" then return end
351-
local fn_loaded = nil
352-
local fn = loadstring(fn_str)
353-
if fn then fn_loaded = fn() end
354-
if type(fn_loaded) ~= "function" then
355-
fn_loaded = nil
356-
end
357-
return fn_loaded
358-
end
332+
---@param obj table
333+
---@param b64? boolean
334+
---@return string, boolean -- boolean used for ./scripts/headless_fd.sh
335+
M.serialize = function(obj, b64)
336+
local str = serpent.line(obj, { comment = false, sortkeys = false })
337+
str = b64 ~= false and base64.encode(str) or str
338+
return "return [==[" .. str .. "]==]", (b64 ~= false and true or false)
339+
end
359340

360-
-- conditionally base64 decode, if not a base64 string, returns original value
361-
opts = base64_conditional_decode(opts)
362-
fn_transform_str = base64_conditional_decode(fn_transform_str)
363-
fn_preprocess_str = base64_conditional_decode(fn_preprocess_str)
364-
fn_postprocess_str = base64_conditional_decode(fn_postprocess_str)
341+
---@param str string
342+
---@param b64? boolean
343+
---@return table
344+
M.deserialize = function(str, b64)
345+
local res = loadstring(str)()
346+
if type(res) == "table" then return res --[[@as table]] end -- ./scripts/headless_fd.sh
347+
res = b64 ~= false and base64.decode(res) or res
348+
local _, obj = serpent.load(res)
349+
assert(type(obj) == "table")
350+
return obj
351+
end
365352

366-
-- opts must be a table, if opts is a string deserialize
367-
if type(opts) == "string" then
368-
_, opts = serpent.load(opts)
369-
assert(type(opts) == "table")
353+
---@param fn_str string
354+
---@return function?
355+
M.load_fn = function(fn_str)
356+
if type(fn_str) ~= "string" then return end
357+
local fn_loaded = nil
358+
local fn = loadstring(fn_str)
359+
if fn then fn_loaded = fn() end
360+
if type(fn_loaded) ~= "function" then
361+
fn_loaded = nil
370362
end
363+
return fn_loaded
364+
end
371365

366+
---@param opts table
367+
---@return uv.uv_process_t, integer
368+
M.spawn_stdio = function(opts)
372369
local EOL = opts.multiline and "\0" or "\n"
373370

374371
-- stdin/stdout are already buffered, not stderr. This means
@@ -398,10 +395,13 @@ M.spawn_stdio = function(opts, fn_transform_str, fn_preprocess_str, fn_postproce
398395
-- err with "fzf-lua fatal: '_G._fzf_lua_server', '_G._devicons_path' both nil"
399396
pcall(require, "fzf-lua.make_entry")
400397

401-
local fn_transform = load_fn(fn_transform_str)
402-
local fn_preprocess = load_fn(fn_preprocess_str)
403-
local fn_postprocess = load_fn(fn_postprocess_str)
398+
local fn_transform_str = opts.fn_transform
399+
local fn_preprocess_str = opts.fn_preprocess
400+
local fn_postprocess_str = opts.fn_postprocess
404401

402+
local fn_transform = M.load_fn(opts.fn_transform)
403+
local fn_preprocess = M.load_fn(opts.fn_preprocess)
404+
local fn_postprocess = M.load_fn(opts.fn_postprocess)
405405

406406
-- run the preprocessing fn
407407
if fn_preprocess then fn_preprocess(opts) end
@@ -568,6 +568,9 @@ end
568568
--
569569
-- this function is a better fit for utils but we're
570570
-- trying to avoid having any 'require' in this file
571+
---@param s string
572+
---@param win_style integer|string? 1=classic, 2=caret
573+
---@return string
571574
M.shellescape = function(s, win_style)
572575
if _is_win or win_style then
573576
if tonumber(win_style) == 1 then

lua/fzf-lua/shell.lua

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ local uv = vim.uv or vim.loop
22
local utils = require "fzf-lua.utils"
33
local path = require "fzf-lua.path"
44
local libuv = require "fzf-lua.libuv"
5-
local base64 = require "fzf-lua.lib.base64"
6-
local serpent = require "fzf-lua.lib.serpent"
75

86
-- path to current file
97
local __FILE__ = debug.getinfo(1, "S").source:gsub("^@", "")
@@ -197,7 +195,9 @@ M.stringify_mt = function(cmd, opts)
197195
"exec_empty_query",
198196
"file_ignore_patterns",
199197
"rg_glob",
200-
"_base64",
198+
"fn_transform",
199+
"fn_preprocess",
200+
"fn_postprocess",
201201
utils.__IS_WINDOWS and "__FZF_VERSION" or nil,
202202
}
203203
-- caller requested rg with glob support
@@ -225,22 +225,6 @@ M.stringify_mt = function(cmd, opts)
225225
return t
226226
end
227227

228-
---@param obj table|string
229-
---@return string
230-
local serialize = function(obj)
231-
local str = type(obj) == "table"
232-
and serpent.line(obj, { comment = false, sortkeys = false })
233-
or tostring(obj)
234-
if opts._base64 ~= false then
235-
-- by default, base64 encode all arguments
236-
return "[==[" .. base64.encode(str) .. "]==]"
237-
else
238-
-- if not encoding, don't string wrap the table
239-
return type(obj) == "table" and str
240-
or "[==[" .. str .. "]==]"
241-
end
242-
end
243-
244228
-- `multiprocess=1` is "optional" if no opt which requires processing
245229
-- is present we return the command as is to be piped to fzf "natively"
246230
if opts.multiprocess == 1
@@ -287,12 +271,7 @@ M.stringify_mt = function(cmd, opts)
287271
opts.fn_preprocess = [[return require("fzf-lua.make_entry").preprocess]]
288272
end
289273
end
290-
local spawn_cmd = M.wrap_spawn_stdio(
291-
serialize(filter_opts(opts)),
292-
serialize(opts.fn_transform or "nil"),
293-
serialize(opts.fn_preprocess or "nil"),
294-
serialize(opts.fn_postprocess or "nil")
295-
)
274+
local spawn_cmd = M.wrap_spawn_stdio(filter_opts(opts))
296275
if opts.argv_expr then
297276
-- prefix the query with `--` so we can support `--fixed-strings` (#781)
298277
spawn_cmd = string.format("%s -- %s", spawn_cmd, FzfLua.core.fzf_query_placeholder)
@@ -315,27 +294,12 @@ M.stringify = function(contents, opts, fzf_field_index)
315294
assert(not opts.__stringified, "twice stringified")
316295
opts.__stringified = true
317296

318-
---@param fn_str string
319-
---@return function?
320-
local function load_fn(fn_str)
321-
if type(fn_str) ~= "string" then return end
322-
local fn_loaded = nil
323-
local fn = loadstring(fn_str)
324-
if fn then fn_loaded = fn() end
325-
if type(fn_loaded) ~= "function" then
326-
fn_loaded = nil
327-
end
328-
return fn_loaded
329-
end
330-
331297
-- Convert string callbacks to callback functions
332298
for _, k in ipairs({ "fn_transform", "fn_preprocess", "fn_postprocess" }) do
333299
local v = opts[k]
334-
opts[k] = load_fn(opts[k]) or v
300+
opts[k] = libuv.load_fn(opts[k]) or v
335301
end
336302

337-
assert(not opts.fn_reload or type(contents) == "function", "fn_reload must be of type function")
338-
339303
local cmd, id = M.pipe_wrap_fn(function(pipe, ...)
340304
local args = { ... }
341305
-- Contents could be dependent or args, e.g. live_grep which
@@ -548,20 +512,16 @@ M.stringify_data2 = function(fn, opts, field_index)
548512
end, opts, field_index)
549513
end
550514

551-
---@param opts string
552-
---@param fn_transform string
553-
---@param fn_preprocess string
554-
---@param fn_postprocess string
515+
---@param opts table
555516
---@return string
556-
M.wrap_spawn_stdio = function(opts, fn_transform, fn_preprocess, fn_postprocess)
517+
M.wrap_spawn_stdio = function(opts)
557518
local is_win = utils.__IS_WINDOWS
558519
local nvim_bin = os.getenv("FZF_LUA_NVIM_BIN") or vim.v.progpath
559520
local cmd_str = ("%s -u NONE -l %s %s"):format(
560521
libuv.shellescape(is_win and vim.fs.normalize(nvim_bin) or nvim_bin),
561522
libuv.shellescape(vim.fn.fnamemodify(is_win and vim.fs.normalize(__FILE__) or __FILE__, ":h") ..
562523
"/spawn.lua"),
563-
libuv.shellescape(("return %s,%s,%s,%s"):format(opts, fn_transform, fn_preprocess, fn_postprocess))
564-
)
524+
libuv.shellescape((libuv.serialize(opts, true))))
565525
return cmd_str
566526
end
567527

lua/fzf-lua/spawn.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ end
3232

3333
-- global var indicating a headless instance
3434
_G._fzf_lua_is_headless = true
35-
local _, pid = require("fzf-lua.libuv").spawn_stdio(loadstring(_G.arg[1])())
35+
local opts = require("fzf-lua.libuv").deserialize(_G.arg[1])
36+
local _, pid = require("fzf-lua.libuv").spawn_stdio(opts)
3637
-- while vim.uv.run() do end -- os.exit in spawn_stdio
3738
while uv.os_getpriority(pid) do
3839
vim.wait(100, function() return uv.os_getpriority(pid) == nil end)

scripts/headless_fd.sh

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,17 @@ nvim -u NONE -l ${BASEDIR}/../lua/fzf-lua/spawn.lua "return
140140
_devicons_path = [[${ICONS_PATH}]],
141141
_devicons_setup = [[${ICONS_SETUP}]],
142142
},
143-
_base64 = false,
144143
debug = [[$debug]] == [[v]] and [[v]] or $debug,
145144
file_icons = [[${file_icons}]] == true or [[${file_icons}]],
146145
git_icons = ${git_icons},
147146
color_icons = ${color_icons},
148147
cmd = [[${cmd:-fd --color=never}]],
149148
cwd = vim.fn.expand([[${cwd:-$BASEDIR}]]),
150-
},
151-
-- fn_transform
152-
[==[
153-
return require(\"fzf-lua.make_entry\").file
154-
]==],
155-
-- fn_preprocess
156-
[==[
157-
return require(\"fzf-lua.make_entry\").preprocess
158-
]==]
149+
fn_transform = [==[
150+
return require(\"fzf-lua.make_entry\").file
151+
]==],
152+
fn_preprocess = [==[
153+
return require(\"fzf-lua.make_entry\").preprocess
154+
]==]
155+
}
159156
"

0 commit comments

Comments
 (0)