Actually run compdb asynchronously

Turns out vim.schedule() only defers calling the function until main
event loop has some free cycles but can still block the UI, just a bit
later. This patch replaces the external command invoked in the callback
passed into vim.schedule() with vim.fn.jobstart() and moves the other
code into job callbacks.
This commit is contained in:
Kenneth Benzie 2024-03-14 21:13:28 +00:00
parent fb2d661a0e
commit f93360d854

View File

@ -19,22 +19,25 @@ function build.set_dir(dirname)
echo('directory does not exist: ' .. build_dir, 'Error')
return
end
if vim.fn.filereadable(build_dir .. '/compile_commands.json') == 0 then
echo('compile_commands.json not found in: ' .. build_dir, 'Error')
return
end
build.dir = build_dir
-- Invoke async function to avoid blocking the UI
vim.schedule(function()
echo('Processing compile_commands.json with compdb ...', 'DiagnosticInfo')
-- Post-process compile_commands.json with compdb
local compile_commands = current_dir .. '/compile_commands.json'
local output = vim.fn.systemlist(
'compdb -p ' .. build.dir .. ' list > ' .. compile_commands)
-- Post-process compile_commands.json with compdb as an async job to avoid
-- blocking the user interface
vim.fn.jobstart(
'compdb -p ' .. build.dir .. ' list > ' .. compile_commands, {
-- Restart clangd language server
-- TODO: Configure cmake language server?
on_exit = function()
vim.cmd [[ LspRestart clangd ]]
echo('Build directory selected: ' .. dirname, 'DiagnosticInfo')
end,
-- Display any error messages to the user
on_stderr = function(_, output, _)
-- Remove any lines containing a compdb warning
local error = {}
local warning = 'WARNING:compdb'
@ -49,14 +52,11 @@ function build.set_dir(dirname)
echo(vim.fn.join(error, '\n'), 'Error')
return
end
end,
-- TODO: Configure cmake language server?
-- Restart clangd language server
vim.cmd [[ LspRestart clangd ]]
echo('Build directory selected: ' .. dirname, 'DiagnosticInfo')
end)
stderr_buffered = true,
}
)
end
function build.select_dir(callback)