diff options
| author | Lewis Russell <lewis6991@gmail.com> | 2023-08-22 12:23:55 +0100 |
|---|---|---|
| committer | Lewis Russell <lewis6991@gmail.com> | 2023-08-22 13:22:08 +0100 |
| commit | 875d7e84d01f834f336a33a54673151819bea415 (patch) | |
| tree | 4cba843a743afba50fc7d1548c8860dbb2661be0 /lua/lspconfig/async.lua | |
| parent | refactor: add sanitize_cmd() (diff) | |
| download | nvim-lspconfig-875d7e84d01f834f336a33a54673151819bea415.tar nvim-lspconfig-875d7e84d01f834f336a33a54673151819bea415.tar.gz nvim-lspconfig-875d7e84d01f834f336a33a54673151819bea415.tar.bz2 nvim-lspconfig-875d7e84d01f834f336a33a54673151819bea415.tar.lz nvim-lspconfig-875d7e84d01f834f336a33a54673151819bea415.tar.xz nvim-lspconfig-875d7e84d01f834f336a33a54673151819bea415.tar.zst nvim-lspconfig-875d7e84d01f834f336a33a54673151819bea415.zip | |
refactor: move async_run_command()
Diffstat (limited to 'lua/lspconfig/async.lua')
| -rw-r--r-- | lua/lspconfig/async.lua | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lua/lspconfig/async.lua b/lua/lspconfig/async.lua index 8a160c82..3421ccc5 100644 --- a/lua/lspconfig/async.lua +++ b/lua/lspconfig/async.lua @@ -9,6 +9,42 @@ function M.run(func) end)) end +--- @param cmd string|string[] +--- @return string[]? +function M.run_command(cmd) + local co = assert(coroutine.running()) + + local stdout = {} + local stderr = {} + local jobid = vim.fn.jobstart(cmd, { + 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() + coroutine.resume(co) + end, + stdout_buffered = true, + stderr_buffered = true, + }) + + if jobid <= 0 then + vim.notify(('[lspconfig] cmd go failed:\n%s'):format(table.concat(stderr, '')), vim.log.levels.WARN) + return + end + + coroutine.yield() + if next(stdout) == nil then + return nil + end + return stdout and stdout or nil +end + function M.reenter() if vim.in_fast_event() then local co = assert(coroutine.running()) |
