1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
local configs = require 'lspconfig/configs'
local util = require 'lspconfig/util'
configs.gopls = {
default_config = {
cmd = { 'gopls' },
filetypes = { 'go', 'gomod' },
root_dir = function(fname)
-- First, search for go.work
local primary_root = util.root_pattern 'go.work'(fname)
if primary_root then
return primary_root
end
-- Then, search up the filesystem for go.mod
local go_mod_hierarchy = {}
if primary_root then
return primary_root
end
for path in util.path.iterate_parents(fname) do
for _, p in ipairs(vim.fn.glob(util.path.join(path, 'go.mod'), true, true)) do
if util.path.exists(p) then
table.insert(go_mod_hierarchy, p)
end
end
end
-- Take the top level go.mod
if #go_mod_hierarchy > 0 then
return go_mod_hierarchy[#go_mod_hierarchy]
end
-- Fallback to the git root
return util.find_git_ancestor(fname)
end,
},
docs = {
description = [[
https://github.com/golang/tools/tree/master/gopls
Google's lsp server for golang.
]],
default_config = {
root_dir = [[root_pattern("go.mod", ".git")]],
},
},
}
|