From f93360d85490020bd30a0085e727a06b32e31c86 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Thu, 14 Mar 2024 21:13:28 +0000 Subject: [PATCH] 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. --- lua/build.lua | 62 +++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/lua/build.lua b/lua/build.lua index 134c8a4..5565395 100644 --- a/lua/build.lua +++ b/lua/build.lua @@ -19,44 +19,44 @@ 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') + echo('Processing compile_commands.json with compdb ...', 'DiagnosticInfo') + local compile_commands = current_dir .. '/compile_commands.json' + -- 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 - local compile_commands = current_dir .. '/compile_commands.json' - local output = vim.fn.systemlist( - '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, - -- Remove any lines containing a compdb warning - local error = {} - local warning = 'WARNING:compdb' - for _, line in ipairs(output) do - if string.sub(line, 1, #warning) ~= warning then - table.insert(error, line) - end - 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' + for _, line in ipairs(output) do + if string.sub(line, 1, #warning) ~= warning then + table.insert(error, line) + end + end - -- Display the error message if there was one - if table.maxn(error) > 0 then - echo(vim.fn.join(error, '\n'), 'Error') - return - end + -- Display the error message if there was one + if table.maxn(error) > 0 then + 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)