feat: proper Go setup with gopls, formatters, and debugger
- Add Mason bin to Neovim PATH so formatters/linters are discoverable - Add Mason ensure_installed for gofumpt, goimports, delve + stylua, black, yamlfmt - Add Go filetype settings (tabs, tabwidth=4) in golang.lua - Remove redundant Go keymaps — existing LSP and vim-test keymaps already cover everything Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
ba86ab27c1
commit
8b868ed944
3 changed files with 28 additions and 35 deletions
|
|
@ -1,5 +1,18 @@
|
||||||
-- 1. Setup Mason first
|
-- 1. Setup Mason first (LSPs + tools)
|
||||||
require("mason").setup()
|
require("mason").setup({
|
||||||
|
ensure_installed = {
|
||||||
|
-- Go toolchain
|
||||||
|
"gofumpt",
|
||||||
|
"goimports",
|
||||||
|
"delve",
|
||||||
|
-- Lua
|
||||||
|
"stylua",
|
||||||
|
-- Python
|
||||||
|
"black",
|
||||||
|
-- YAML
|
||||||
|
"yamlfmt",
|
||||||
|
},
|
||||||
|
})
|
||||||
require("mason-lspconfig").setup({
|
require("mason-lspconfig").setup({
|
||||||
-- Remove "jdtls" from here to prevent the 'enable' (nil value) crash
|
-- Remove "jdtls" from here to prevent the 'enable' (nil value) crash
|
||||||
ensure_installed = {
|
ensure_installed = {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,13 @@
|
||||||
|
-- Go filetype settings (Go uses tabs, not spaces)
|
||||||
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
|
pattern = { "go", "gomod", "gowork", "gotmpl" },
|
||||||
|
callback = function()
|
||||||
|
vim.opt_local.expandtab = false
|
||||||
|
vim.opt_local.tabstop = 4
|
||||||
|
vim.opt_local.shiftwidth = 4
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
-- Go LSP configuration
|
-- Go LSP configuration
|
||||||
-- Uses gopls installed via Mason
|
-- Uses gopls installed via Mason
|
||||||
|
|
||||||
|
|
@ -35,37 +45,4 @@ vim.lsp.config("gopls", {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
-- 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")
|
vim.lsp.enable("gopls")
|
||||||
|
|
|
||||||
|
|
@ -70,3 +70,6 @@ opt.scrolloff = 8
|
||||||
-- minimal number of screen columns either side of cursor if wrap is `false`
|
-- minimal number of screen columns either side of cursor if wrap is `false`
|
||||||
opt.sidescrolloff = 8
|
opt.sidescrolloff = 8
|
||||||
|
|
||||||
|
-- Add Mason's bin directory to PATH (LSPs, formatters, linters, debuggers)
|
||||||
|
vim.env.PATH = vim.fn.expand("~/.local/share/nvim/mason/bin") .. ":" .. (vim.env.PATH or "")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue