debugging watcher

This commit is contained in:
dlond 2025-05-30 05:34:43 +12:00
parent 01eb5e1bcc
commit de6105eb24
1 changed files with 38 additions and 43 deletions

View File

@ -23,8 +23,6 @@ function M.stop_clangd()
end end
function M.start_clangd(dir) function M.start_clangd(dir)
M.stop_clangd()
local cmd = { local cmd = {
'clangd', 'clangd',
'--background-index', '--background-index',
@ -50,11 +48,13 @@ function M.start_clangd(dir)
} }
end end
function M.reload_clangd() function M.reload_clangd(dir)
M.stop_clangd() M.stop_clangd()
dir = dir or find_compile_commands()
vim.defer_fn(function() vim.defer_fn(function()
M.start_clangd(find_compile_commands()) M.start_clangd(dir)
end, 100) end, 100)
end end
@ -82,10 +82,22 @@ function M.pick_commands_dir()
:find() :find()
end end
local watcher, debounce_timer
function M.watch_compile_commands(dir) function M.watch_compile_commands(dir)
local uv = vim.uv or vim.loop local uv = vim.uv or vim.loop
local watcher = uv.new_fs_event()
local debounce_timer if watcher then
watcher:stop()
watcher:close()
watcher = nil
end
if debounce_timer then
debounce_timer:stop()
debounce_timer:close()
debounce_timer = nil
end
local watch_path = dir or vim.fn.getcwd() local watch_path = dir or vim.fn.getcwd()
local watch_file = watch_path .. '/compile_commands.json' local watch_file = watch_path .. '/compile_commands.json'
@ -94,46 +106,29 @@ function M.watch_compile_commands(dir)
return return
end end
watcher:start(watch_path, { recursive = true }, function(err, fname, status) watcher = uv.new_fs_event()
-- vim.schedule(function() watcher:start(
-- print('[clangd] Watcher triggered: ', fname, vim.inspect(status)) watch_path,
-- end) { recursive = true },
vim.schedule_wrap(function(err, fname, status)
if err then if err then
vim.schedule(function()
vim.notify('[clangd] Watcher error: ' .. err, vim.log.levels.ERROR) vim.notify('[clangd] Watcher error: ' .. err, vim.log.levels.ERROR)
end)
return return
end end
-- if fname then if fname and fname:match '.*/compile_commands%.json$' and status.change then
-- vim.schedule(function()
-- vim.notify('[clangd] File triggered: ' .. fname)
-- end)
-- end
if fname and fname:match 'compile_commands%.json$' and status.change then
vim.schedule(function()
vim.notify '[clangd] Matched pattern for compile_commands.json'
end)
end
if fname and fname:match 'compile_commands%.json$' and status.change then
if debounce_timer then
debounce_timer.stop()
debounce_timer.close()
end
debounce_timer = uv.new_timer() debounce_timer = uv.new_timer()
debounce_timer:start(200, 0, function() debounce_timer:start(200, 0, function()
vim.schedule(function() vim.schedule(function()
vim.notify '[clangd] Detected compile_commands.json change. Reloading ...' vim.notify '[clangd] Detected compile_commands.json change. Reloading ...'
watcher:stop() watch_path = vim.fn.fnamemodify(fname, ':h')
M.start_clangd(vim.fn.fnamemodify(fname, ':h')) M.start_clangd(watch_path)
M.watch_compile_commands(watch_path)
end) end)
end) end)
end end
end) end)
)
vim.notify('[clangd] Watching: ' .. watch_file)
end end
return { return {