go.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. local cmd, b, o = vim.cmd, vim.b, vim.o
  2. o.expandtab = false
  3. --[[
  4. go install golang.org/x/tools/gopls@latest
  5. go install mvdan.cc/gofumpt@latest
  6. go install honnef.co/go/tools/cmd/staticcheck@latest
  7. --]]
  8. b.ale_linters = { "gopls", "govet", "staticcheck" }
  9. b.ale_fixers = {} -- disable all fixers, use autocmd instead
  10. b.ale_go_staticcheck_options = "-checks all"
  11. -- use gopls+gofumpt to fix imports and format on save
  12. -- gofumpt is selected as formatter at lsp setup
  13. vim.api.nvim_create_autocmd("BufWritePre", {
  14. pattern = "*.go",
  15. callback = function()
  16. local params = vim.lsp.util.make_range_params()
  17. params.context = { only = { "source.organizeImports" } }
  18. local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params)
  19. for cid, res in pairs(result or {}) do
  20. for _, r in pairs(res.result or {}) do
  21. if r.edit then
  22. local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16"
  23. vim.lsp.util.apply_workspace_edit(r.edit, enc)
  24. end
  25. end
  26. end
  27. vim.lsp.buf.format({ async = false })
  28. end,
  29. })
  30. for _, x in ipairs({
  31. -- { "stru", [[type struct{<CR>i int<CR>}<Esc>2k_4li]] },
  32. -- { "iface", [[type interface{<CR>A() *A<CR>}<Esc>2k_4li]] },
  33. { "stru", [[<Esc>bitype <Esc>Astruct{<CR>}<Esc>O]] },
  34. { "iface", [[<Esc>bitype <Esc>Ainterface{<CR>}<Esc>O]] },
  35. -- { "func", [[func() {<CR>return<CR>}<Esc>2k_4li]] },
  36. { "func", [[<Esc>bifunc <Esc>A( xw ) {<CR>}<Esc>k$Txciw]] },
  37. { "fn", [[func(){ xw }<Esc>Txciw]] },
  38. { "enil", [[if err != nil {<CR>}<Esc>O]] },
  39. {
  40. "gomain",
  41. [[package main<CR><CR>import (<CR>"fmt"<CR>)<CR><CR>func main() {<CR>fmt.Println("Hello!")<CR>}<Esc>O]],
  42. },
  43. }) do
  44. cmd.inoreabbrev({ args = x })
  45. end