|
1 | 1 | -- https://github.com/neovim/nvim-lspconfig/blob/master/lua/lspconfig/configs/gopls.lua |
| 2 | +local mod_cache = nil |
| 3 | + |
| 4 | +---@param fname string |
| 5 | +---@return string? |
| 6 | +local function get_root(fname) |
| 7 | + if mod_cache and fname:sub(1, #mod_cache) == mod_cache then |
| 8 | + local clients = vim.lsp.get_clients({ name = "gopls" }) |
| 9 | + if #clients > 0 then |
| 10 | + return clients[#clients].config.root_dir |
| 11 | + end |
| 12 | + end |
| 13 | + return vim.fs.root(fname, "go.work") or vim.fs.root(fname, "go.mod") or vim.fs.root(fname, ".git") |
| 14 | +end |
| 15 | + |
2 | 16 | return { |
3 | | - cmd = { "gopls", "-remote.debug=:0", "-remote=auto" }, |
4 | | - filetypes = { "go", "gomod", "gosum", "gotmpl", "gohtmltmpl", "gotexttmpl" }, |
5 | | - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, |
6 | | - capabilities = { |
7 | | - textDocument = { |
8 | | - completion = { |
9 | | - contextSupport = true, |
10 | | - dynamicRegistration = true, |
11 | | - completionItem = { |
12 | | - commitCharactersSupport = true, |
13 | | - deprecatedSupport = true, |
14 | | - preselectSupport = true, |
15 | | - insertReplaceSupport = true, |
16 | | - labelDetailsSupport = true, |
17 | | - snippetSupport = true, |
18 | | - documentationFormat = { "markdown", "plaintext" }, |
19 | | - resolveSupport = { |
20 | | - properties = { |
21 | | - "documentation", |
22 | | - "details", |
23 | | - "additionalTextEdits", |
24 | | - }, |
25 | | - }, |
26 | | - }, |
27 | | - }, |
28 | | - }, |
29 | | - }, |
30 | | - settings = { |
31 | | - gopls = { |
32 | | - staticcheck = true, |
33 | | - semanticTokens = true, |
34 | | - usePlaceholders = true, |
35 | | - completeUnimported = true, |
36 | | - symbolMatcher = "Fuzzy", |
37 | | - buildFlags = { "-tags", "integration" }, |
38 | | - semanticTokenTypes = { string = false }, |
39 | | - codelenses = { |
40 | | - generate = true, |
41 | | - gc_details = true, |
42 | | - test = true, |
43 | | - tidy = true, |
44 | | - vendor = true, |
45 | | - regenerate_cgo = true, |
46 | | - upgrade_dependency = true, |
47 | | - }, |
48 | | - }, |
49 | | - }, |
| 17 | + cmd = { "gopls" }, |
| 18 | + filetypes = { "go", "gomod", "gowork", "gotmpl" }, |
| 19 | + root_dir = function(bufnr, on_dir) |
| 20 | + local fname = vim.api.nvim_buf_get_name(bufnr) |
| 21 | + -- see: https://github.com/neovim/nvim-lspconfig/issues/804 |
| 22 | + if mod_cache then |
| 23 | + on_dir(get_root(fname)) |
| 24 | + return |
| 25 | + end |
| 26 | + local cmd = { "go", "env", "GOMODCACHE" } |
| 27 | + vim.system(cmd, { text = true }, function(output) |
| 28 | + if output.code == 0 then |
| 29 | + if output.stdout then |
| 30 | + mod_cache = vim.trim(output.stdout) |
| 31 | + end |
| 32 | + on_dir(get_root(fname)) |
| 33 | + else |
| 34 | + vim.schedule(function() |
| 35 | + vim.notify(("[gopls] cmd failed with code %d: %s\n%s"):format(output.code, cmd, output.stderr)) |
| 36 | + end) |
| 37 | + end |
| 38 | + end) |
| 39 | + end, |
50 | 40 | } |
0 commit comments