Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lua/lspconfig/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@ function M.run_command(cmd)
return stdout and stdout or nil
end

---@param cmd string[]
---@param on_done function(string[]?)
---@param on_error? function(integer, string[]?)
function M.run_job(cmd, on_done, on_error)
local stdout = {}
local stderr = {}

local jobid = vim.fn.jobstart(cmd, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can vim.system be used instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think vim.system might actually be better, the syntax is easier. Maybe I could inline the calls and close this PR.

on_stdout = function(_, data, _)
data = table.concat(data, '\n')
if #data > 0 then
stdout[#stdout + 1] = data
end
end,
on_stderr = function(_, data, _)
stderr[#stderr + 1] = table.concat(data, '\n')
end,
on_exit = function(_, code, _)
if code == 0 then
on_done(stdout)
else
if on_error then
on_error(code, stderr)
else
vim.notify(
('[lspconfig] cmd failed with code %d: %s\n%s'):format(code, cmd, table.concat(stderr, '')),
vim.log.levels.WARN
)
end
end
end,
stdout_buffered = true,
stderr_buffered = true,
})

if jobid <= 0 then
vim.notify(('[lspconfig] unable to run cmd: %s'):format(cmd), vim.log.levels.WARN)
return nil
end
end

function M.reenter()
if vim.in_fast_event() then
local co = assert(coroutine.running())
Expand Down
Loading