diff options
| -rw-r--r-- | lua/lspconfig/async.lua | 36 | ||||
| -rw-r--r-- | lua/lspconfig/server_configurations/gopls.lua | 3 | ||||
| -rw-r--r-- | lua/lspconfig/server_configurations/rust_analyzer.lua | 3 | ||||
| -rw-r--r-- | lua/lspconfig/util.lua | 34 |
4 files changed, 40 insertions, 36 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()) diff --git a/lua/lspconfig/server_configurations/gopls.lua b/lua/lspconfig/server_configurations/gopls.lua index 139f63d8..f27033b7 100644 --- a/lua/lspconfig/server_configurations/gopls.lua +++ b/lua/lspconfig/server_configurations/gopls.lua @@ -1,4 +1,5 @@ local util = require 'lspconfig.util' +local async = require 'lspconfig.async' local mod_cache = nil return { @@ -8,7 +9,7 @@ return { root_dir = function(fname) -- see: https://github.com/neovim/nvim-lspconfig/issues/804 if not mod_cache then - local result = util.async_run_command 'go env GOMODCACHE' + local result = async.run_command 'go env GOMODCACHE' if result and result[1] then mod_cache = vim.trim(result[1]) end diff --git a/lua/lspconfig/server_configurations/rust_analyzer.lua b/lua/lspconfig/server_configurations/rust_analyzer.lua index fc79046d..f7cc2524 100644 --- a/lua/lspconfig/server_configurations/rust_analyzer.lua +++ b/lua/lspconfig/server_configurations/rust_analyzer.lua @@ -1,4 +1,5 @@ local util = require 'lspconfig.util' +local async = require 'lspconfig.async' local function reload_workspace(bufnr) bufnr = util.validate_bufnr(bufnr) @@ -50,7 +51,7 @@ return { cmd[#cmd + 1] = util.path.join(cargo_crate_dir, 'Cargo.toml') end - local result = util.async_run_command(cmd) + local result = async.run_command(cmd) local cargo_workspace_root if result and result[1] then diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 2233f7ac..98f9a323 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -401,38 +401,4 @@ function M.strip_archive_subpath(path) return path end -function M.async_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 - return M |
