Skip to content

Instantly share code, notes, and snippets.

@cocoastorm
Created February 6, 2024 06:36
Show Gist options
  • Save cocoastorm/c0376a576544033baf6a180617207728 to your computer and use it in GitHub Desktop.
Save cocoastorm/c0376a576544033baf6a180617207728 to your computer and use it in GitHub Desktop.
NeoVim Format & Goimports
-- formatting
vim.api.nvim_create_autocmd('BufWritePre', {
pattern = { "*.go" },
callback = vim.lsp.buf.format,
})
-- goimports
vim.api.nvim_create_autocmd('BufWritePre', {
pattern = '*.go',
callback = function ()
local params = vim.lsp.util.make_range_params()
params.context = {only = {"source.organizeImports"}}
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 5000)
for cid, res in pairs(result or {}) do
for _, r in pairs(res.result or {}) do
if r.edit then
local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16"
vim.lsp.util.apply_workspace_edit(r.edit, enc)
else
vim.lsp.buf.execute_command(r.command)
end
end
end
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment