feat: add :make for golang
This commit is contained in:
33
init.lua
33
init.lua
@@ -340,6 +340,39 @@ vim.api.nvim_create_autocmd('BufWritePre', {
|
|||||||
vim.filetype.add({ extension = { templ = "templ" } })
|
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.g.mapleader = " "
|
||||||
|
|
||||||
vim.keymap.set('n', '<leader>c', ':copen<CR>', { desc = "Open Quickfix" })
|
vim.keymap.set('n', '<leader>c', ':copen<CR>', { desc = "Open Quickfix" })
|
||||||
|
|||||||
Reference in New Issue
Block a user