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,44 +19,44 @@ function build.set_dir(dirname)
echo('directory does not exist: ' .. build_dir, 'Error') echo('directory does not exist: ' .. build_dir, 'Error')
return return
end 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 build.dir = build_dir
-- Invoke async function to avoid blocking the UI echo('Processing compile_commands.json with compdb ...', 'DiagnosticInfo')
vim.schedule(function() local compile_commands = current_dir .. '/compile_commands.json'
echo('Processing compile_commands.json with compdb ...', 'DiagnosticInfo') -- 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, {
-- Post-process compile_commands.json with compdb -- Restart clangd language server
local compile_commands = current_dir .. '/compile_commands.json' -- TODO: Configure cmake language server?
local output = vim.fn.systemlist( on_exit = function()
'compdb -p ' .. build.dir .. ' list > ' .. compile_commands) vim.cmd [[ LspRestart clangd ]]
echo('Build directory selected: ' .. dirname, 'DiagnosticInfo')
end,
-- Remove any lines containing a compdb warning -- Display any error messages to the user
local error = {} on_stderr = function(_, output, _)
local warning = 'WARNING:compdb' -- Remove any lines containing a compdb warning
for _, line in ipairs(output) do local error = {}
if string.sub(line, 1, #warning) ~= warning then local warning = 'WARNING:compdb'
table.insert(error, line) for _, line in ipairs(output) do
end if string.sub(line, 1, #warning) ~= warning then
end table.insert(error, line)
end
end
-- Display the error message if there was one -- Display the error message if there was one
if table.maxn(error) > 0 then if table.maxn(error) > 0 then
echo(vim.fn.join(error, '\n'), 'Error') echo(vim.fn.join(error, '\n'), 'Error')
return return
end end
end,
-- TODO: Configure cmake language server? stderr_buffered = true,
}
-- Restart clangd language server )
vim.cmd [[ LspRestart clangd ]]
echo('Build directory selected: ' .. dirname, 'DiagnosticInfo')
end)
end end
function build.select_dir(callback) function build.select_dir(callback)