diff options
| author | Raphael <glepnir@neovim.pro> | 2023-06-18 16:53:49 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-18 16:53:49 +0800 |
| commit | 9a2cc569c88662fa41d414bdb65b13ea72349f86 (patch) | |
| tree | fac7323b154c261ca8254f919a3192ae46d2dbba /lua | |
| parent | docs: update server_configurations.md (diff) | |
| download | nvim-lspconfig-9a2cc569c88662fa41d414bdb65b13ea72349f86.tar nvim-lspconfig-9a2cc569c88662fa41d414bdb65b13ea72349f86.tar.gz nvim-lspconfig-9a2cc569c88662fa41d414bdb65b13ea72349f86.tar.bz2 nvim-lspconfig-9a2cc569c88662fa41d414bdb65b13ea72349f86.tar.lz nvim-lspconfig-9a2cc569c88662fa41d414bdb65b13ea72349f86.tar.xz nvim-lspconfig-9a2cc569c88662fa41d414bdb65b13ea72349f86.tar.zst nvim-lspconfig-9a2cc569c88662fa41d414bdb65b13ea72349f86.zip | |
perf(gopls): make get go mod cache path async (#2673)
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/lspconfig/server_configurations/gopls.lua | 9 | ||||
| -rw-r--r-- | lua/lspconfig/server_configurations/rust_analyzer.lua | 48 | ||||
| -rw-r--r-- | lua/lspconfig/util.lua | 34 |
3 files changed, 48 insertions, 43 deletions
diff --git a/lua/lspconfig/server_configurations/gopls.lua b/lua/lspconfig/server_configurations/gopls.lua index fd470beb..139f63d8 100644 --- a/lua/lspconfig/server_configurations/gopls.lua +++ b/lua/lspconfig/server_configurations/gopls.lua @@ -1,6 +1,5 @@ local util = require 'lspconfig.util' - -local mod_cache = vim.trim(vim.fn.system 'go env GOMODCACHE') +local mod_cache = nil return { default_config = { @@ -8,6 +7,12 @@ return { filetypes = { 'go', 'gomod', 'gowork', 'gotmpl' }, 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' + if result and result[1] then + mod_cache = vim.trim(result[1]) + end + end if fname:sub(1, #mod_cache) == mod_cache then local clients = vim.lsp.get_active_clients { name = 'gopls' } if #clients > 0 then diff --git a/lua/lspconfig/server_configurations/rust_analyzer.lua b/lua/lspconfig/server_configurations/rust_analyzer.lua index 0c5eb0c1..8ef2f10e 100644 --- a/lua/lspconfig/server_configurations/rust_analyzer.lua +++ b/lua/lspconfig/server_configurations/rust_analyzer.lua @@ -10,44 +10,6 @@ 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, _) - 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 (%q) failed:\n%s'):format(table.concat(cmd, ' '), table.concat(stderr, '')), - vim.log.levels.WARN - ) - return - end - - coroutine.yield() - if next(stdout) == nil then - return nil - end - stdout = vim.json.decode(table.concat(stdout, '')) - return stdout and stdout['workspace_root'] or nil -end - local function is_library(fname) local cargo_home = os.getenv 'CARGO_HOME' or util.path.join(vim.env.HOME, '.cargo') local registry = util.path.join(cargo_home, 'registry', 'src') @@ -88,10 +50,14 @@ return { cmd[#cmd + 1] = util.path.join(cargo_crate_dir, 'Cargo.toml') end - local cargo_workspace_root = get_workspace_dir(cmd) + local result = util.async_run_command(cmd) + local cargo_workspace_root - if cargo_workspace_root then - cargo_workspace_root = util.path.sanitize(cargo_workspace_root) + if result and result[1] then + result = vim.json.decode(table.concat(result, '')) + if result['workspace_root'] then + cargo_workspace_root = util.path.sanitize(result['workspace_root']) + end end return cargo_workspace_root diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 6a231308..f4e2ab78 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -580,4 +580,38 @@ 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 |
