From e6dd280c9bed25e3e9e486d6d384078de4470031 Mon Sep 17 00:00:00 2001 From: joshashtondev Date: Tue, 27 Oct 2026 13:48:12 -0600 Subject: [PATCH] =?UTF-8?q?nixos:=20gen=20108=20@=20lilac=20=E2=80=94=2020?= =?UTF-8?q?26-10-27=2013:48=2026.05.20260918.cf9d2fb=20(Yarara)=20-----=20?= =?UTF-8?q?adding=20golang=20support=20to=20neovim?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- home/programs/nvim.nix | 103 ++++++++++++++++++++++++++++++++++++++++- home/programs/tmux.nix | 2 +- 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/home/programs/nvim.nix b/home/programs/nvim.nix index 4c6b657..d1cdc3b 100644 --- a/home/programs/nvim.nix +++ b/home/programs/nvim.nix @@ -151,7 +151,7 @@ in -- started per buffer. Indent is deliberately left alone so treesitter -- doesn't fight the Eclipse-matching tab settings below. vim.api.nvim_create_autocmd("FileType", { - pattern = { "java", "xml", "nix", "lua", "rs" }, + pattern = { "java", "xml", "nix", "lua", "rs", "go", "gomod" }, callback = function(args) pcall(vim.treesitter.start, args.buf) end, @@ -168,6 +168,17 @@ in end, }) + -- Go indentation must match gofmt: real tabs, displayed 4 wide. + vim.api.nvim_create_autocmd("FileType", { + pattern = { "go", "gomod" }, + callback = function() + vim.bo.expandtab = false + vim.bo.tabstop = 4 + vim.bo.shiftwidth = 4 + vim.bo.softtabstop = 4 + end, + }) + local cmp = require("cmp") local luasnip = require("luasnip") cmp.setup({ @@ -206,6 +217,94 @@ in }, } + -- Go: gopls (LSP) + golangci-lint-langserver (lint diagnostics) via + -- Neovim's native LSP config, so no nvim-lspconfig plugin is needed. + -- Like rust-analyzer above, both binaries are resolved from PATH, so a + -- project's flake devShell/direnv pins are used automatically. + vim.lsp.config("gopls", { + cmd = { "gopls" }, + filetypes = { "go", "gomod", "gowork", "gotmpl" }, + root_markers = { "go.work", "go.mod", ".git" }, + capabilities = capabilities, + settings = { + gopls = { + gofumpt = true, + usePlaceholders = true, + completeUnimported = true, + staticcheck = false, -- golangci-lint already covers this + analyses = { + unusedparams = true, + unusedwrite = true, + useany = true, + }, + hints = { + assignVariableTypes = true, + compositeLiteralFields = true, + constantValues = true, + parameterNames = true, + rangeVariableTypes = true, + }, + }, + }, + on_attach = function(_, bufnr) + local map = function(k, f) vim.keymap.set("n", k, f, { buffer = bufnr, silent = true }) end + map("gd", vim.lsp.buf.definition) + map("gr", vim.lsp.buf.references) + map("K", vim.lsp.buf.hover) + map("rn", vim.lsp.buf.rename) + map("ca", vim.lsp.buf.code_action) + map("f", function() vim.lsp.buf.format({ async = true }) end) + map("ci", function() + vim.lsp.buf.code_action({ + context = { only = { "source.organizeImports" }, diagnostics = {} }, + apply = true, + }) + end) + end, + }) + + -- golangci-lint v2 syntax (current in nixpkgs). Needs golangci-lint on + -- PATH; a project .golangci.yml (with `version: "2"`) controls the rules. + vim.lsp.config("golangci_lint_ls", { + cmd = { "golangci-lint-langserver" }, + filetypes = { "go", "gomod" }, + root_markers = { ".golangci.yml", ".golangci.yaml", ".golangci.toml", ".golangci.json", "go.work", "go.mod", ".git" }, + capabilities = capabilities, + init_options = { + command = { + "golangci-lint", "run", + "--output.json.path", "stdout", + "--show-stats=false", + "--issues-exit-code=1", + }, + }, + }) + + vim.lsp.enable({ "gopls", "golangci_lint_ls" }) + + -- On save: organize imports via gopls, then format (gofumpt via gopls). + vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*.go", + callback = function(args) + local clients = vim.lsp.get_clients({ bufnr = args.buf, name = "gopls" }) + local client = clients[1] + if not client then + return + end + local params = vim.lsp.util.make_range_params(0, client.offset_encoding) + params.context = { only = { "source.organizeImports" } } + local result = vim.lsp.buf_request_sync(args.buf, "textDocument/codeAction", params, 1000) + for _, res in pairs(result or {}) do + for _, action in pairs(res.result or {}) do + if action.edit then + vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding) + end + end + end + vim.lsp.buf.format({ bufnr = args.buf, async = false }) + end, + }) + local bundles = vim.fn.glob( "${javaDebug}/share/vscode/extensions/vscjava.vscode-java-debug/server/com.microsoft.java.debug.plugin-*.jar", true, true) @@ -360,7 +459,7 @@ in { plugin = luasnip; } { plugin = cmp_luasnip; } { plugin = rustaceanvim; } - { plugin = (nvim-treesitter.withPlugins (p: [ p.java p.xml p.nix p.lua p.rust ])); } + { plugin = (nvim-treesitter.withPlugins (p: [ p.java p.xml p.nix p.lua p.rust p.go p.gomod ])); } { plugin = vim-obsession; } ]; diff --git a/home/programs/tmux.nix b/home/programs/tmux.nix index 015cfdb..3d79fe7 100644 --- a/home/programs/tmux.nix +++ b/home/programs/tmux.nix @@ -46,7 +46,7 @@ bind -n M-K resize-pane -U 2 bind -n M-L resize-pane -R 2 - bind -n M-z resize-pane -Z + bind -n M-f resize-pane -Z bind -n M-c new-window