Skip to content

Commit 1acaf91

Browse files
phanenibhagwan
authored andcommitted
refactor(rpc): pass more ctx ($FZF_* envs)
chore: types
1 parent 780feba commit 1acaf91

5 files changed

Lines changed: 54 additions & 49 deletions

File tree

lua/fzf-lua/previewer/init.lua

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,13 @@ Previewer.normalize_spec = function(preview, opts)
9999
if type(preview) == "function" then
100100
return (FzfLua.shell.stringify_data(preview, opts, "{}"))
101101
elseif type(preview) == "table" then
102-
preview = vim.tbl_extend("keep", preview, {
103-
fn = preview.fn or preview[1],
104-
-- by default we use current item only "{}"
105-
-- using "{+}" will send multiple selected items
106-
field_index = "{}",
107-
})
108-
if preview.type == "cmd" then
109-
return (FzfLua.shell.stringify_cmd(preview.fn, opts, preview.field_index))
110-
end
111-
return (FzfLua.shell.stringify_data(preview.fn, opts, preview.field_index))
102+
local func = assert(preview.fn or preview[1])
103+
-- by default we use current item only "{}"
104+
-- using "{+}" will send multiple selected items
105+
local field_index = preview.field_index or "{}"
106+
local stringify = preview.type == "cmd" and FzfLua.shell.stringify_cmd
107+
or FzfLua.shell.stringify_data
108+
return (stringify(func, opts, field_index))
112109
else
113110
return preview
114111
end

lua/fzf-lua/rpc.lua

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,25 @@ server_listen(server_socket, server_socket_path)
5858
-- local thread = uv.new_thread(server_listen, server_socket, server_socket_path)
5959
-- io.stdout:write(string.format("thread %s\n", tostring(thread)))
6060

61+
---@class fzf-lua.rpc.Ctx
62+
---@field function_id string An identifier for the RPC function to be executed.
63+
---@field pipe_path string The path to the Unix domain socket for RPC communication.
64+
---@field selection? string[] The selected item(s) (expanded from field expression).
65+
---@field env table<string, string> Environment variables inherited by the RPC process.
66+
67+
---@param opts table
6168
local rpc_nvim_exec_lua = function(opts)
69+
---@type fzf-lua.rpc.Ctx
70+
local ctx = {
71+
function_id = opts.fnc_id,
72+
pipe_path = server_socket_path,
73+
selection = opts.selection,
74+
env = vim.uv.os_environ(),
75+
}
6276
local success, errmsg = pcall(function()
63-
-- for skim compatibility
64-
local preview_lines = vim.env.FZF_PREVIEW_LINES or vim.env.LINES
65-
local preview_cols = vim.env.FZF_PREVIEW_COLUMNS or vim.env.COLUMNS
6677
local chan_id = vim.fn.sockconnect("pipe", opts.fzf_lua_server, { rpc = true })
67-
vim.rpcrequest(chan_id, "nvim_exec_lua", [[
68-
local luaargs = {...}
69-
local function_id = luaargs[1]
70-
local server_socket_path = luaargs[2]
71-
local fzf_selection = luaargs[3]
72-
local fzf_lines = luaargs[4]
73-
local fzf_columns = luaargs[5]
74-
local usr_func = require"fzf-lua.shell".get_func(function_id)
75-
return usr_func(server_socket_path, fzf_selection, fzf_lines, fzf_columns)
76-
]], {
77-
opts.fnc_id,
78-
server_socket_path,
79-
opts.fzf_selection,
80-
tonumber(preview_lines),
81-
tonumber(preview_cols),
82-
})
78+
vim.rpcrequest(chan_id, "nvim_exec_lua",
79+
[[return require"fzf-lua.shell".get_func((...).function_id)(...)]], { ctx })
8380
vim.fn.chanclose(chan_id)
8481
end)
8582

@@ -106,7 +103,7 @@ end
106103

107104
local args = vim.deepcopy(_G.arg)
108105
args[0] = nil -- remove filename
109-
local opts = {
106+
rpc_nvim_exec_lua({
110107
fnc_id = tonumber(table.remove(args, 1)),
111108
debug = (function()
112109
local ret = table.remove(args, 1)
@@ -120,7 +117,6 @@ local opts = {
120117
return tonumber(ret) or tostring(ret)
121118
end
122119
end)(),
123-
fzf_selection = args,
120+
selection = args,
124121
fzf_lua_server = vim.env.FZF_LUA_SERVER or vim.env.SKIM_FZF_LUA_SERVER or vim.env.NVIM,
125-
}
126-
rpc_nvim_exec_lua(opts)
122+
})

lua/fzf-lua/shell.lua

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,28 @@ end
127127
function M.pipe_wrap_fn(fn, fzf_field_index, debug)
128128
fzf_field_index = fzf_field_index or "{+}"
129129

130-
local receiving_function = function(pipe_path, ...)
130+
---@param ctx fzf-lua.rpc.Ctx
131+
local receiving_function = function(ctx)
131132
local pipe = assert(uv.new_pipe(false))
132-
local args = { ... }
133+
local pipe_path = ctx.pipe_path
133134
-- unescape double backslashes on windows
134-
if utils.__IS_WINDOWS and type(args[1]) == "table" then
135-
args[1] = vim.tbl_map(function(x)
135+
if utils.__IS_WINDOWS and type(ctx.selection) == "table" then
136+
ctx.selection = vim.tbl_map(function(x)
136137
return libuv.unescape_fzf(x, vim.g.fzf_lua_fzf_version)
137-
end, args[1])
138+
end, ctx.selection)
138139
end
139-
utils.get_info().selected = args[1] and args[1][1] or nil
140+
-- for skim compatibility
141+
local preview_lines = tonumber(ctx.env.FZF_PREVIEW_LINES or ctx.env.LINES)
142+
local preview_cols = tonumber(ctx.env.FZF_PREVIEW_COLUMNS or ctx.env.COLUMNS)
143+
utils.get_info().selected = (ctx.selection or {})[1]
140144
uv.pipe_connect(pipe, pipe_path, function(err)
141145
if err then
142146
---@diagnostic disable-next-line: undefined-field
143147
err = uv.translate_sys_error(uv.errno[err])
144148
utils.warn(string.format("pipe_connect(%s) failed with error: %s", pipe_path, err))
145149
else
146150
vim.schedule(function()
147-
fn(pipe, unpack(args))
151+
fn(pipe, ctx.selection, preview_lines, preview_cols, ctx)
148152
end)
149153
end
150154
end)
@@ -295,15 +299,15 @@ M.stringify = function(contents, opts, fzf_field_index)
295299
end
296300

297301
local cmd, id = M.pipe_wrap_fn(function(pipe, ...)
298-
local args = { ... }
302+
local args, n = { ... }, select("#", ...)
299303
-- Contents could be dependent or args, e.g. live_grep which
300304
-- generates a different command based on the typed query
301305
-- redefine local contents to prevent override on function call
302306
---@type fzf-lua.content, table?
303307
---@diagnostic disable-next-line: redefined-local, assign-type-mismatch
304308
local contents, env = (function()
305-
local ret = opts.is_live and type(contents) == "function" and contents(unpack(args), opts)
306-
or opts.__stringify_cmd and contents(unpack(args))
309+
local ret = (opts.is_live and type(contents) == "function" and contents(unpack(args), opts))
310+
or (opts.__stringify_cmd and type(contents) == "function" and contents(unpack(args, 1, n)))
307311
or contents
308312
if opts.__stringify_cmd and type(ret) == "table" then
309313
return ret.cmd, (ret.env or opts.env)
@@ -428,7 +432,7 @@ M.stringify = function(contents, opts, fzf_field_index)
428432
vim.tbl_map(function(x) on_write_nl(x) end, contents)
429433
on_finish()
430434
elseif type(contents) == "function" then
431-
contents(on_write_nl, on_write, unpack(args))
435+
contents(on_write_nl, on_write, unpack(args, 1, n))
432436
end
433437
end, debug.traceback)
434438
if not ok and err then
@@ -442,9 +446,9 @@ M.stringify = function(contents, opts, fzf_field_index)
442446
end
443447

444448
---@alias fzf-lua.shell.cmdSpec string|{ cmd: string|string[], env: table? }?
445-
---@alias fzf-lua.shell.cmd fun(items: string[], fzf_lines: integer, fzf_columns: integer): fzf-lua.shell.cmdSpec
446-
---@alias fzf-lua.shell.data fun(items: string[], fzf_lines: integer, fzf_columns: integer): fzf-lua.content?
447-
---@alias fzf-lua.shell.data2 fun(items: string[], opts: fzf-lua.config.Resolved|{}): fzf-lua.content?
449+
---@alias fzf-lua.shell.cmd fun(items: string[], fzf_lines: integer, fzf_columns: integer, ctx?: fzf-lua.rpc.Ctx): fzf-lua.shell.cmdSpec
450+
---@alias fzf-lua.shell.data fun(items: string[], fzf_lines: integer, fzf_columns: integer, ctx?: fzf-lua.rpc.Ctx): fzf-lua.content?
451+
---@alias fzf-lua.shell.data2 fun(items: string[], opts: fzf-lua.config.Resolved|{}, ctx?: fzf-lua.rpc.Ctx): fzf-lua.content?
448452

449453
---@param fn fzf-lua.shell.cmd
450454
---@param opts table

lua/fzf-lua/types.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local FzfLua = require("fzf-lua")
2121
---@field extmarks? table
2222

2323
---@class fzf-lua.cmd.Entry
24-
---@field cmd string[] cmd used to generated content
24+
---@field cmd? string[] cmd used to generated content
2525
---@field cmd_stream? boolean stream process cmd content
2626
---@field cmd_opts? vim.SystemOpts vim.system opts for cmd
2727

@@ -112,6 +112,11 @@ local FzfLua = require("fzf-lua")
112112
---@field _EOL? string
113113
---@field _debug? boolean
114114

115+
---@class fzf-lua.PidObject
116+
---@field new fun(self: fzf-lua.PidObject, _, _): fzf-lua.PidObject
117+
---@field get fun(self: fzf-lua.PidObject): integer
118+
---@field set fun(self: fzf-lua.PidObject, pid: integer?)
119+
115120
---a basic config can be used by fzf_exec?
116121
---generated from the result of `:=FzfLua.config.normalize_opts({}, {})`
117122
---@class fzf-lua.config.Base
@@ -213,7 +218,7 @@ local FzfLua = require("fzf-lua")
213218
---@field _headers? boolean
214219

215220
---@class fzf-lua.config.Resolved: fzf-lua.config.Base
216-
---@field PidObject? table
221+
---@field PidObject? fzf-lua.PidObject
217222
---@field _headers? string[]
218223
---@field _fmt? table
219224
---@field pipe_cmd? string

lua/fzf-lua/utils.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,9 @@ function M.lsp_get_clients(opts)
16211621
end, clients)
16221622
end
16231623

1624+
---@param key string
1625+
---@param opts table
1626+
---@return fzf-lua.PidObject
16241627
function M.pid_object(key, opts)
16251628
local Pid = {}
16261629

0 commit comments

Comments
 (0)