68 lines
2.3 KiB
Lua
68 lines
2.3 KiB
Lua
-- Go LSP configuration
|
|
-- Uses gopls installed via Mason
|
|
|
|
local capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities())
|
|
|
|
vim.lsp.config("gopls", {
|
|
cmd = { "gopls" },
|
|
filetypes = { "go", "gomod", "gowork", "gotmpl" },
|
|
capabilities = capabilities,
|
|
root_markers = { "go.mod", ".git" },
|
|
settings = {
|
|
gopls = {
|
|
hints = {
|
|
assignVariableTypes = true,
|
|
compositeLiteralFields = true,
|
|
compositeLiteralTypes = true,
|
|
constantValues = true,
|
|
functionTypeParameters = true,
|
|
parameterNames = true,
|
|
rangeVariableTypes = true,
|
|
},
|
|
analyses = {
|
|
unusedparams = true,
|
|
shadow = true,
|
|
nilness = true,
|
|
},
|
|
staticcheck = true,
|
|
gofumpt = true, -- prefer gofumpt formatting
|
|
usePlaceholders = true,
|
|
completeUnimported = true,
|
|
},
|
|
},
|
|
})
|
|
|
|
-- Go-specific LSP attach handlers
|
|
vim.api.nvim_create_autocmd("LspAttach", {
|
|
callback = function(args)
|
|
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
|
if client and client.name == "gopls" then
|
|
local bufnr = args.buf
|
|
|
|
-- Go: organize imports on demand
|
|
vim.keymap.set("n", "<leader>gi", function()
|
|
vim.lsp.buf.code_action({
|
|
context = { only = { "source.organizeImports" } },
|
|
apply = true,
|
|
})
|
|
end, { buffer = bufnr, desc = "Go: Organize imports" })
|
|
|
|
-- Go: toggle test file
|
|
vim.keymap.set("n", "<leader>gt", function()
|
|
local fname = vim.fn.expand("%")
|
|
if fname:match("_test%.go$") then
|
|
vim.cmd("edit " .. fname:gsub("_test%.go$", ".go"))
|
|
else
|
|
vim.cmd("edit " .. fname:gsub("%.go$", "_test.go"))
|
|
end
|
|
end, { buffer = bufnr, desc = "Go: Toggle test file" })
|
|
|
|
-- Go: run current file's tests
|
|
vim.keymap.set("n", "<leader>gr", function()
|
|
vim.cmd("!go test ./...")
|
|
end, { buffer = bufnr, desc = "Go: Run tests" })
|
|
end
|
|
end,
|
|
})
|
|
|
|
vim.lsp.enable("gopls")
|