Skip to content

Commit 74045cd

Browse files
committed
serialize opts
1 parent 5a6b9e1 commit 74045cd

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

lua/fzf-lua/libuv.lua

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ end
8686
---@field process1? boolean
8787
---@field profiler? boolean
8888

89+
local gen_uuid = function()
90+
-- return uv.random(10)
91+
return uv.hrtime()
92+
end
93+
8994
---@param opts fzf-lua.SpawnOpts
9095
---@param fn_transform function?
9196
---@param fn_done function?
@@ -200,10 +205,11 @@ M.spawn = function(opts, fn_transform, fn_done)
200205
end)
201206
end
202207

203-
local abc = require("fzf-lua.libuv").serialize(opts, false)
208+
local uuid = gen_uuid() -- distinguish two call when use worker pool
209+
local optstr = require("fzf-lua.libuv").serialize({ t = fn_transform, opts = opts.opts }, false)
204210
---@param data string data stream
205211
---@return string, string? line array, partial last line (no EOL)
206-
local function split_lines(data, o)
212+
local function split_lines(data, o, id)
207213
-- io.stderr:write("[DEBUG] worker init")
208214
if not _G.fzf_lua_worker_init then
209215
-- TODO: we can pass serialize opts to first queue...
@@ -214,18 +220,25 @@ M.spawn = function(opts, fn_transform, fn_done)
214220
require("fzf-lua.make_entry")
215221
_G.fzf_lua_worker_init = true
216222
end
217-
local trans = require("fzf-lua.make_entry").file
218-
local opts = _G._opts
223+
if id ~= _G.uuid then -- refresh opts
224+
local d = require("fzf-lua.libuv").deserialize(o, false)
225+
_G.trans = d.t
226+
_G.opts = d.opts
227+
_G.uuid = id
228+
end
229+
-- local trans = require("fzf-lua.make_entry").file
230+
-- local worker_opts = assert(_G.fzf_lua_worker_opts, "no opts")
231+
local trans = _G.trans
232+
local opts = _G.opts
219233
local ret = {}
220234
local start_idx = 1
221235
repeat
222236
local nl_idx = data:find(EOL_data or "\n", start_idx, true)
223237
if nl_idx then
224238
local cr = data:byte(nl_idx - 1, nl_idx - 1) == 13 -- \r
225239
local line = data:sub(start_idx, nl_idx - (cr and 2 or 1))
226-
if trans then
227-
line = trans(line, opts)
228-
end
240+
-- line = trans(line, worker_opts)
241+
if trans then line = trans(line, opts) end
229242
if line then ret[#ret + 1] = line end
230243
start_idx = nl_idx + 1
231244
end
@@ -245,7 +258,7 @@ M.spawn = function(opts, fn_transform, fn_done)
245258
if len == 0 then return end
246259
if ref[len - 1] ~= EOL_byte then strbuf:put(EOL_byte) end -- make split_lines happy
247260
write_cb_count = write_cb_count + 1
248-
return work_ctx:queue(strbuf:get())
261+
return work_ctx:queue(strbuf:get(), optstr, uuid)
249262
end
250263
local eol = len
251264
for i = len - 1, stop, -1 do
@@ -261,7 +274,7 @@ M.spawn = function(opts, fn_transform, fn_done)
261274
local data = strbuf:get(eol + 1)
262275
stop = #strbuf
263276
write_cb_count = write_cb_count + 1
264-
work_ctx:queue(data)
277+
work_ctx:queue(data, optstr, uuid)
265278
end
266279
end
267280
if can_finish() then finish(0, 0, "[EOF]", pid) end
@@ -329,7 +342,7 @@ M.deserialize = function(str, b64)
329342
res = b64 ~= false and base64.decode(res) or res
330343
-- safe=false enable call function
331344
local _, obj = serpent.load(res, { safe = false })
332-
assert(type(obj) == "table")
345+
assert(type(obj) == "table", vim.inspect(obj))
333346
return obj
334347
end
335348

@@ -572,10 +585,12 @@ M.spawn_stdio = function(opts)
572585
process1 = opts.process1,
573586
profiler = opts.profiler,
574587
EOL = EOL,
588+
opts = opts,
575589
},
576-
fn_transform and function(x)
577-
return fn_transform(x, opts)
578-
end)
590+
fn_transform)
591+
-- fn_transform and function(x)
592+
-- return fn_transform(x, opts)
593+
-- end)
579594
end
580595

581596

lua/fzf-lua/make_entry.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ local opts2 = setmetatable({}, {
7676
end
7777
})
7878

79-
if _G._fzf_lua_is_headless then
79+
if _G._fzf_lua_is_headless or vim.is_thread() then
8080
local _config = load_config() or {} ---@module 'fzf-lua.config'
8181
---@diagnostic disable-next-line: missing-fields
8282
_config.globals = { git = {}, files = {}, grep = {} }
@@ -567,6 +567,11 @@ M.file = function(x, opts)
567567
local ret = {}
568568
local icon, hl
569569
local colon_start_idx = 1
570+
571+
local path = FzfLua.path
572+
local utils = FzfLua.utils
573+
local devicons = FzfLua.devicons
574+
-- print(vim.inspect(opts))
570575
if utils.__IS_WINDOWS then
571576
if string.byte(x, #x) == 13 then
572577
-- strip ^M added by the "dir /s/b" command

lua/fzf-lua/shell.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,18 @@ M.stringify = function(contents, opts, fzf_field_index)
410410
cmd = contents,
411411
env = env,
412412
cb_finish = on_finish,
413-
cb_write_lines = on_write,
413+
-- cb_write_lines = on_write,
414+
cb_write = on_write,
414415
cb_pid = function(pid) if opts.PidObject then opts.PidObject:set(pid) end end,
415416
process1 = opts.process1,
416417
profiler = opts.profiler,
417418
use_queue = opts.use_queue,
418419
EOL = EOL,
420+
opts = opts,
419421
-- Must send value, 'coroutinify' adds callback as last argument
420422
-- which will conflict with the 'fn_transform' argument
421423
-- send true to force line processing without transformation
422-
}, true)
424+
}, fn_transform)
423425
else
424426
-- callback with newline
425427
local on_write_nl = function(data, cb)

0 commit comments

Comments
 (0)