debugging watcher

This commit is contained in:
dlond 2025-05-30 02:29:30 +12:00
parent 4c6d17824c
commit c77e2e0a99
1 changed files with 28 additions and 20 deletions

View File

@ -7,15 +7,12 @@ local watcher
local function find_compile_commands() local function find_compile_commands()
local lines = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' } local lines = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' }
if vim.tbl_isempty(lines) then if vim.tbl_isempty(lines) then
return nil return nil
end end
table.sort(lines, function(a, b) table.sort(lines, function(a, b)
return a:match 'debug' and not b:match 'debug' return a:match 'debug' and not b:match 'debug'
end) end)
return vim.fn.fnamemodify(lines[1], ':h') return vim.fn.fnamemodify(lines[1], ':h')
end end
@ -80,27 +77,38 @@ function M.pick_commands_dir()
end end
function M.watch_compile_commands() function M.watch_compile_commands()
if watcher then
return
end
vim.notify('clangd: Starting watcher for compile_commands.json', vim.log.levels.INFO) vim.notify('clangd: Starting watcher for compile_commands.json', vim.log.levels.INFO)
local uv = vim.uv or vim.loop local uv = vim.uv or vim.loop
watcher = uv.new_fs_event() local check_interval = 1000 -- ms
local cwd = vim.fn.getcwd()
watcher:start( local function try_attach()
cwd, local dir = find_compile_commands()
{ recursive = true }, if dir then
vim.schedule_wrap(function(_, fname, status) vim.notify('[clangd] Found compile_commands at: ' .. dir)
if fname and fname:match 'compile_commands%.json$' and status.change then M.setup_clangd(dir)
vim.notify('[clangd] Detected change: ' .. fname, vim.log.levels.INFO) return true
watcher:stop() end
watcher = nil return false
M.setup_clangd(vim.fn.fnamemodify(fname, ':h')) end
local timer = uv.new_timer()
timer:start(0, check_interval, function()
if try_attach() then
timer:stop()
timer:close()
end
end)
local fs_watcher = uv.new_fs_event()
fs_watcher:start(vim.fn.getcwd(), { recursive = true }, function(_, fname, status)
if fname and fname:match 'compile_commands%.json$' and status.change then
fs_watcher:stop()
vim.schedule(function()
vim.notify '[clangd] Detected compile_commands.json change, reloading ...'
M.setup_clangd(vim.fn.fnamemodify(fname, ':h'))
end)
end end
end) end)
)
end end
return { return {