Browse Source

nixpkgs/neovim: fix format on save

Thomas Dy 1 year ago
parent
commit
25a8b470df
1 changed files with 21 additions and 0 deletions
  1. 21 0
      .config/nixpkgs/neovim/lsp.lua

+ 21 - 0
.config/nixpkgs/neovim/lsp.lua

@@ -97,8 +97,29 @@ if vim.fn.executable("nil") == 1 then
   });
   });
 end
 end
 
 
+-- format on save
 local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
 local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
 
 
+vim.api.nvim_create_autocmd('LspAttach', {
+  callback = function(args)
+    local bufnr = args.buf
+    local client = vim.lsp.get_client_by_id(args.data.client_id)
+
+    if client.server_capabilities.documentFormattingProvider then
+      for key, cmd in pairs(vim.api.nvim_get_autocmds({ group = group, buffer = bufnr })) do
+        vim.api.nvim_del_autocmd(cmd.id)
+      end
+      vim.api.nvim_create_autocmd('BufWritePre', {
+        group = group,
+        buffer = bufnr,
+        callback = function()
+          vim.lsp.buf.format()
+        end,
+      })
+    end
+  end,
+})
+
 -- custom LSP servers
 -- custom LSP servers
 local configs = require('lspconfig.configs')
 local configs = require('lspconfig.configs')