go.nvim/lua/go/gopls.lua

236 lines
6.3 KiB
Lua
Raw Normal View History

local utils = require("go.utils")
2021-11-11 02:01:14 +00:00
local log = utils.log
local M = {}
2022-04-18 12:31:17 +00:00
local cmds = {}
2021-11-11 02:01:14 +00:00
-- https://go.googlesource.com/tools/+/refs/heads/master/gopls/doc/commands.md
-- "executeCommandProvider":{"commands":["gopls.add_dependency","gopls.add_import","gopls.apply_fix","gopls.check_upgrades","gopls.gc_details","gopls.generate","gopls.generate_gopls_mod","gopls.go_get_package","gopls.list_known_packages","gopls.regenerate_cgo","gopls.remove_dependency","gopls.run_tests","gopls.start_debugging","gopls.test","gopls.tidy","gopls.toggle_gc_details","gopls.update_go_sum","gopls.upgrade_dependency","gopls.vendor","gopls.workspace_metadata"]}
local gopls_cmds = {
"gopls.add_dependency",
"gopls.add_import",
"gopls.apply_fix",
"gopls.check_upgrades",
"gopls.gc_details",
"gopls.generate",
"gopls.generate_gopls_mod",
"gopls.go_get_package",
"gopls.list_known_packages",
2022-04-18 12:31:17 +00:00
"gopls.list_imports",
"gopls.regenerate_cgo",
"gopls.remove_dependency",
"gopls.run_tests",
"gopls.start_debugging",
"gopls.test",
"gopls.tidy",
"gopls.toggle_gc_details",
"gopls.update_go_sum",
"gopls.upgrade_dependency",
"gopls.vendor",
"gopls.workspace_metadata",
2021-11-11 02:01:14 +00:00
}
2022-04-18 12:31:17 +00:00
local gopls_with_result = {
"gopls.gc_details",
"gopls.list_known_packages",
"gopls.list_imports",
}
2021-11-11 02:01:14 +00:00
local function check_for_error(msg)
if msg ~= nil and type(msg[1]) == "table" then
2021-11-11 02:01:14 +00:00
for k, v in pairs(msg[1]) do
if k == "error" then
2022-04-18 12:31:17 +00:00
log("LSP", v.message)
2021-11-11 02:01:14 +00:00
break
end
end
end
end
for _, value in ipairs(gopls_cmds) do
local fname = string.sub(value, #"gopls." + 1)
2022-04-18 12:31:17 +00:00
cmds[fname] = function(arg)
2021-11-11 02:01:14 +00:00
log(fname)
local b = vim.api.nvim_get_current_buf()
local uri = vim.uri_from_bufnr(b)
2022-04-18 12:31:17 +00:00
local arguments = { { URI = uri } }
local ft = vim.bo.filetype
if ft == "gomod" or ft == "gosum" then
arguments = { { URIs = { uri } } }
end
arguments = { vim.tbl_extend("keep", arguments[1], arg or {}) }
2021-11-11 02:01:14 +00:00
2022-04-18 12:31:17 +00:00
if vim.tbl_contains(gopls_with_result, value) then
local resp = vim.lsp.buf_request_sync(b, "workspace/executeCommand", {
command = value,
arguments = arguments,
}, 2000)
check_for_error(resp)
log(resp)
return resp
end
vim.schedule(function()
local resp = vim.lsp.buf.execute_command({
command = value,
arguments = arguments,
})
check_for_error(resp)
log(resp)
end)
2021-11-11 02:01:14 +00:00
end
end
2022-04-18 12:31:17 +00:00
M.import = function(path)
cmds.add_import({
ImportPath = path,
})
end
2021-11-13 03:29:42 +00:00
M.list_pkgs = function()
2022-04-18 12:31:17 +00:00
local resp = cmds.list_known_packages() or {}
2021-11-13 03:29:42 +00:00
local pkgs = {}
for _, response in pairs(resp) do
if response.result ~= nil then
pkgs = response.result.Packages
break
end
end
return pkgs
end
2021-11-11 02:01:14 +00:00
-- check_for_upgrades({Modules = {'package'}})
function M.version()
local cache_dir = vim.fn.stdpath("cache")
local path = string.format("%s%sversion.txt", cache_dir, utils.sep())
2022-05-13 10:54:47 +00:00
local cfg = _GO_NVIM_CFG or {}
local gopls = cfg.gopls_cmd or { "gopls" }
2022-04-30 01:57:51 +00:00
2022-04-30 09:22:19 +00:00
if vim.fn.executable(gopls[1]) == 0 then
2022-04-30 01:57:51 +00:00
vim.notify("gopls not found", vim.log.levels.WARN)
return
end
2022-01-31 10:40:40 +00:00
vim.fn.jobstart({ gopls[1], "version" }, {
on_stdout = function(c, data, name)
local msg = ""
if type(data) == "table" and #data > 0 then
data = table.concat(data, " ")
end
if #data > 1 then
msg = msg .. data
end
log(msg)
local version = string.match(msg, "%s+v([%d%.]+)%s+")
if version == nil then
log(version, msg)
return
end
local f = io.open(path, "w+")
if f == nil then
return
end
f:write(version)
f:close()
log(version)
end,
})
local f = io.open(path, "r")
if f == nil then
2022-01-31 10:40:40 +00:00
local version_cmd = gopls[1] .. " version"
2022-01-31 06:28:53 +00:00
return vim.fn.system(version_cmd):match("%s+v([%d%.]+)%s+")
end
local version = f:read("*l")
f:close()
log(version)
return version
end
local setups = {
2022-02-17 02:32:37 +00:00
capabilities = {
textDocument = {
completion = {
completionItem = {
commitCharactersSupport = true,
deprecatedSupport = true,
documentationFormat = { "markdown", "plaintext" },
preselectSupport = true,
insertReplaceSupport = true,
labelDetailsSupport = true,
snippetSupport = true,
resolveSupport = {
properties = {
"documentation",
"details",
"additionalTextEdits",
},
},
},
contextSupport = true,
dynamicRegistration = true,
},
},
},
2022-04-20 14:43:35 +00:00
filetypes = { "go", "gomod", "gosum", "gotmpl", "gohtmltmpl", "gotexttmpl" },
message_level = vim.lsp.protocol.MessageType.Error,
cmd = {
"gopls", -- share the gopls instance if there is one already
"-remote.debug=:0",
},
root_dir = function(fname)
local has_lsp, lspconfig = pcall(require, "lspconfig")
if has_lsp then
local util = lspconfig.util
return util.root_pattern("go.mod", ".git")(fname) or util.path.dirname(fname)
end
end,
flags = { allow_incremental_sync = true, debounce_text_changes = 500 },
settings = {
gopls = {
-- more settings: https://github.com/golang/tools/blob/master/gopls/doc/settings.md
-- flags = {allow_incremental_sync = true, debounce_text_changes = 500},
-- not supported
analyses = { unusedparams = true, unreachable = false },
codelenses = {
generate = true, -- show the `go generate` lens.
gc_details = true, -- // Show a code lens toggling the display of gc's choices.
test = true,
tidy = true,
},
usePlaceholders = true,
completeUnimported = true,
staticcheck = true,
matcher = "Fuzzy",
diagnosticsDelay = "500ms",
experimentalWatchedFileDelay = "100ms",
symbolMatcher = "fuzzy",
["local"] = "",
gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils
buildFlags = { "-tags", "integration" },
-- buildFlags = {"-tags", "functional"}
},
},
}
M.setups = function()
local v = M.version()
2022-04-30 01:57:51 +00:00
if v == nil then
return
end
if v > "0.7" then
setups.settings = vim.tbl_deep_extend("force", setups.settings, {
experimentalPostfixCompletions = true,
experimentalUseInvalidMetadata = true,
hoverKind = "Structured",
})
end
return setups
end
2021-11-11 02:01:14 +00:00
return M