Add format-on-write for elixir and go

Sun, 05 Mar 2023 18:47:45 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Sun, 05 Mar 2023 18:47:45 -0500
changeset 1062
77bf3b44f6f9
parent 1061
8141190cc943
child 1063
2763a54b0410

Add format-on-write for elixir and go

.config/nvim/lua/config/lsp.lua file | annotate | diff | comparison | revisions
--- a/.config/nvim/lua/config/lsp.lua
+++ b/.config/nvim/lua/config/lsp.lua
@@ -26,9 +26,8 @@ vim.api.nvim_create_autocmd("LspAttach",
     local client = vim.lsp.get_client_by_id(args.data.client_id)
 
     -- enable auto diags in message area for below threshold
-    vim.api.nvim_create_augroup('lsp_diags', {clear = false})
     vim.api.nvim_create_autocmd("CursorHold", {
-      group = "lsp_diags",
+      group = vim.api.nvim_create_augroup("lsp_buf_diags", {clear = true}),
       buffer = bufnr,
       callback = function(opts, bufnr, line_nr, client_id)
         vim.diagnostic.open_float(nil, {
@@ -42,6 +41,18 @@ vim.api.nvim_create_autocmd("LspAttach",
   end
 })
 
+-- Format on write, but only certain languages
+local autoformat_filetypes = { elixir = true, go = true }
+
+vim.api.nvim_create_autocmd("BufWritePre", {
+  group = vim.api.nvim_create_augroup("lsp_autoformat", {clear = true}),
+  callback = function(opts, bufnr)
+    if autoformat_filetypes[vim.bo.filetype] then
+      vim.lsp.buf.formatting_seq_sync(nil, 100)
+    end
+  end,
+})
+
 -- This can be removed when mason-lspconfig gets support for standardrb
 if vim.fn.executable("standardrb") == 1 then
   vim.api.nvim_create_autocmd("FileType", {

mercurial