Skip to content

Commit 40548f0

Browse files
committed
feat(debug_tracelog): trace lua execution via debug.sethook
1 parent 4c0a2f4 commit 40548f0

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

lua/fzf-lua/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ function M.normalize_opts(opts, globals, __resume_key)
320320
"fzf_raw_args",
321321
"file_icon_padding",
322322
"dir_icon",
323+
"debug_tracelog",
323324
}) do
324325
if opts[s] == nil then
325326
opts[s] = M.globals[s]

lua/fzf-lua/core.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ M.fzf = function(contents, opts)
421421
silent_fail = opts.silent_fail,
422422
is_fzf_tmux = opts._is_fzf_tmux,
423423
debug = opts.debug_cmd or opts.debug and not (opts.debug_cmd == false),
424+
debug_tracelog = opts.debug_tracelog,
424425
RIPGREP_CONFIG_PATH = opts.RIPGREP_CONFIG_PATH,
425426
})
426427
-- If a hidden process was killed by [re-]starting a new picker do nothing

lua/fzf-lua/fzf.lua

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,36 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
218218
print("[Fzf-lua]: fzf cmd:", table.concat(cmd, " "))
219219
end
220220

221+
local pid, chan_id, ns_id, fh_trace
222+
if opts.debug_tracelog then
223+
fh_trace = io.open(vim.fn.expand(opts.debug_tracelog), "wb")
224+
if fh_trace then
225+
debug.sethook(function(_, _)
226+
local s = string.format("%s %s:%s %s\n",
227+
os.date("%Y-%m-%dT%H:%M:%S"),
228+
debug.getinfo(2, "S").source,
229+
debug.getinfo(2, "l").currentline,
230+
debug.getinfo(2, "n").name)
231+
fh_trace:write(s)
232+
fh_trace:flush()
233+
end, "l")
234+
end
235+
end
236+
237+
local function unset_debughook()
238+
if fh_trace then
239+
debug.sethook(nil, "", 0)
240+
fh_trace:close()
241+
end
242+
end
243+
244+
local function unhook_onkey()
245+
if tonumber(ns_id) and vim.on_key then
246+
---@diagnostic disable-next-line: param-type-mismatch
247+
vim.on_key(nil, ns_id)
248+
end
249+
end
250+
221251
local co = coroutine.running()
222252
local jobstart = opts.is_fzf_tmux and vim.fn.jobstart or vim.fn.termopen
223253
local shell_cmd = utils.__IS_WINDOWS
@@ -233,7 +263,7 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
233263
-- temporarily set to `false`, for more info see `:help shellslash` (#1055)
234264
local nvim_opt_shellslash = utils.__WIN_HAS_SHELLSLASH and vim.o.shellslash
235265
if nvim_opt_shellslash then vim.o.shellslash = false end
236-
jobstart(shell_cmd, {
266+
chan_id = jobstart(shell_cmd, {
237267
cwd = cwd,
238268
pty = true,
239269
env = {
@@ -263,6 +293,9 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
263293
and libuv.expand(opts.RIPGREP_CONFIG_PATH) or "",
264294
},
265295
on_exit = function(_, rc, _)
296+
-- clear hook
297+
unhook_onkey()
298+
unset_debughook()
266299
local output = {}
267300
local f = io.open(outputtmpname)
268301
if f then
@@ -285,6 +318,14 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
285318
end
286319
})
287320

321+
if tonumber(chan_id) > 0 then
322+
pid = vim.fn.jobpid(chan_id)
323+
else
324+
-- job failed or cmd[0] is not executable
325+
unhook_onkey()
326+
unset_debughook()
327+
end
328+
288329
-- fzf-tmux spawns outside neovim, don't set filetype/insert mode
289330
if not opts.is_fzf_tmux then
290331
vim.bo.filetype = "fzf"

0 commit comments

Comments
 (0)