From fa65471dc11c7cbbb45ef6ab11abd7b7695a8f42 Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Tue, 6 Jan 2026 20:44:04 +0100 Subject: [PATCH] feat: add :make for golang --- init.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/init.lua b/init.lua index 81f90fb..ccca9d4 100644 --- a/init.lua +++ b/init.lua @@ -340,6 +340,39 @@ vim.api.nvim_create_autocmd('BufWritePre', { vim.filetype.add({ extension = { templ = "templ" } }) +-- Function to find the nearest directory containing go.mod +local function find_go_mod_root() + local path = vim.fn.expand("%:p:h") -- current file's directory + local root = vim.fn.finddir("go.mod", path .. ";") -- search upwards + if root == "" then + return nil -- fallback if no go.mod found + else + return vim.fn.fnamemodify(root, ":h") -- directory containing go.mod + end +end + +-- Autocmd for Go files +vim.api.nvim_create_autocmd("FileType", { + pattern = "go", + callback = function() + -- Set makeprg to run in the module root + local root = find_go_mod_root() + if root then + -- cd into the module root, then run go + vim.opt_local.makeprg = "cd " .. root .. " && go build ./..." + else + vim.opt_local.makeprg = "go build ./..." + end + + -- Set errorformat for Go errors + vim.opt_local.errorformat = table.concat({ + "%f:%l:%c: %m", + "%f:%l: %m", + }, ",") + end, +}) + + vim.g.mapleader = " " vim.keymap.set('n', 'c', ':copen', { desc = "Open Quickfix" })