diff options
feat: wrap get root dir in coroutine . (#2565)
Co-authored-by: ii14 <59243201+ii14@users.noreply.github.com>
Diffstat (limited to 'lua/lspconfig/server_configurations/rust_analyzer.lua')
| -rw-r--r-- | lua/lspconfig/server_configurations/rust_analyzer.lua | 68 |
1 files changed, 39 insertions, 29 deletions
diff --git a/lua/lspconfig/server_configurations/rust_analyzer.lua b/lua/lspconfig/server_configurations/rust_analyzer.lua index d5c0d940..38039b91 100644 --- a/lua/lspconfig/server_configurations/rust_analyzer.lua +++ b/lua/lspconfig/server_configurations/rust_analyzer.lua @@ -10,6 +10,38 @@ local function reload_workspace(bufnr) end) end +local function get_workspace_dir(cmd) + local co = assert(coroutine.running()) + + local stdout = {} + local stderr = {} + local jobid = vim.fn.jobstart(cmd, { + on_stdout = function(_, data, _) + stdout[#stdout + 1] = table.concat(data, '\n') + 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 (%q) failed:\n%s'):format(table.concat(cmd, ' '), table.concat(stderr, '')), + vim.log.levels.WARN + ) + return + end + + coroutine.yield() + stdout = vim.json.decode(table.concat(stdout, '')) + return stdout and stdout['workspace_root'] or nil +end + return { default_config = { cmd = { 'rust-analyzer' }, @@ -21,36 +53,14 @@ return { cmd[#cmd + 1] = '--manifest-path' cmd[#cmd + 1] = util.path.join(cargo_crate_dir, 'Cargo.toml') end - local cargo_metadata = '' - local cargo_metadata_err = '' - local cm = vim.fn.jobstart(cmd, { - on_stdout = function(_, d, _) - cargo_metadata = table.concat(d, '\n') - end, - on_stderr = function(_, d, _) - cargo_metadata_err = table.concat(d, '\n') - end, - stdout_buffered = true, - stderr_buffered = true, - }) - if cm > 0 then - cm = vim.fn.jobwait({ cm })[1] - else - cm = -1 - end - local cargo_workspace_dir = nil - if cm == 0 then - cargo_workspace_dir = vim.json.decode(cargo_metadata)['workspace_root'] - if cargo_workspace_dir ~= nil then - cargo_workspace_dir = util.path.sanitize(cargo_workspace_dir) - end - else - vim.notify( - string.format('[lspconfig] cmd (%q) failed:\n%s', table.concat(cmd, ' '), cargo_metadata_err), - vim.log.levels.WARN - ) + + local cargo_workspace_root = get_workspace_dir(cmd) + + if cargo_workspace_root then + cargo_workspace_root = util.path.sanitize(cargo_workspace_root) end - return cargo_workspace_dir + + return cargo_workspace_root or cargo_crate_dir or util.root_pattern 'rust-project.json'(fname) or util.find_git_ancestor(fname) |
