From 8b868ed944d62cc92b38cb2a19da5cb0bfd37efd Mon Sep 17 00:00:00 2001 From: Prabhat Maurya Date: Sun, 26 Jul 2026 00:06:12 +0530 Subject: [PATCH] feat: proper Go setup with gopls, formatters, and debugger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- lua/user/lsp.lua | 17 +++++++++++-- lua/user/lsp_servers/golang.lua | 43 ++++++++------------------------- lua/user/options.lua | 3 +++ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/lua/user/lsp.lua b/lua/user/lsp.lua index 829cb06..a68b7fe 100644 --- a/lua/user/lsp.lua +++ b/lua/user/lsp.lua @@ -1,5 +1,18 @@ --- 1. Setup Mason first -require("mason").setup() +-- 1. Setup Mason first (LSPs + tools) +require("mason").setup({ + ensure_installed = { + -- Go toolchain + "gofumpt", + "goimports", + "delve", + -- Lua + "stylua", + -- Python + "black", + -- YAML + "yamlfmt", + }, +}) require("mason-lspconfig").setup({ -- Remove "jdtls" from here to prevent the 'enable' (nil value) crash ensure_installed = { diff --git a/lua/user/lsp_servers/golang.lua b/lua/user/lsp_servers/golang.lua index fbdbe73..6fc8d00 100644 --- a/lua/user/lsp_servers/golang.lua +++ b/lua/user/lsp_servers/golang.lua @@ -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 -- 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", "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", "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", "gr", function() - vim.cmd("!go test ./...") - end, { buffer = bufnr, desc = "Go: Run tests" }) - end - end, -}) - vim.lsp.enable("gopls") diff --git a/lua/user/options.lua b/lua/user/options.lua index 9730a90..90cd435 100644 --- a/lua/user/options.lua +++ b/lua/user/options.lua @@ -70,3 +70,6 @@ opt.scrolloff = 8 -- minimal number of screen columns either side of cursor if wrap is `false` 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 "") +