Skip to content

Commit

Permalink
feat(nvim): auto-run go mod tidy on save go.* files
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jan 3, 2024
1 parent 28f3e71 commit 0486218
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion modules/neovim/config/after/ftplugin/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ local function tidy()
stderr:read_stop()
stdout:close()
stderr:close()
handle:close()
if handle then
handle:close()
end
vim.schedule(function()
for _, name in ipairs({ "gopls", "golangci_lint_ls" }) do
restart(name)
Expand All @@ -62,4 +64,11 @@ end

vim.api.nvim_create_user_command("GoModTidy", tidy, vim.tbl_extend("force", { desc = "go mod tidy" }, {}))

local group = vim.api.nvim_create_augroup("Go", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = { "go.mod", "go.sum", "go.work" },
callback = tidy,
group = group,
})

vim.keymap.set("n", "<leader>gmt", ":GoModTidy<CR>", { noremap = true, silent = true, desc = "Run go mod tidy" })

0 comments on commit 0486218

Please sign in to comment.