From a29deaa9bce57d32df5ed520b46522f008b36196 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 27 Feb 2023 14:53:31 +0000 Subject: [PATCH] home: vim: use 'lsp-formatting.nvim' A few things that are different: * Async by default. * Takes care of the order of formatters, if I ever need to do that. * Allows for easily disabling formatting (unfortunately this is global state, not buffer-local). * Gets rid of the formatting pause when doing `:wq`. --- home/vim/default.nix | 1 + home/vim/lua/ambroisie/lsp.lua | 15 ++++----------- home/vim/plugin/settings/formatting.lua | 3 +++ 3 files changed, 8 insertions(+), 11 deletions(-) create mode 100644 home/vim/plugin/settings/formatting.lua diff --git a/home/vim/default.nix b/home/vim/default.nix index abd185e..e462f93 100644 --- a/home/vim/default.nix +++ b/home/vim/default.nix @@ -67,6 +67,7 @@ in # LSP and linting nvim-lspconfig # Easy LSP configuration + lsp-format-nvim # Simplified formatting configuration lsp_lines-nvim # Show diagnostics *over* regions null-ls-nvim # LSP integration for linters and formatters nvim-treesitter.withAllGrammars # Better highlighting diff --git a/home/vim/lua/ambroisie/lsp.lua b/home/vim/lua/ambroisie/lsp.lua index d4f3fad..260ef8d 100644 --- a/home/vim/lua/ambroisie/lsp.lua +++ b/home/vim/lua/ambroisie/lsp.lua @@ -1,5 +1,8 @@ local M = {} +-- Simplified LSP formatting configuration +local lsp_format = require("lsp-format") + -- shared LSP configuration callback -- @param client native client configuration -- @param bufnr int? buffer number of the attched client @@ -30,17 +33,7 @@ M.on_attach = function(client, bufnr) }) -- Format on save - if client.supports_method("textDocument/formatting") then - local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) - vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) - vim.api.nvim_create_autocmd("BufWritePre", { - group = augroup, - buffer = bufnr, - callback = function() - vim.lsp.buf.format({ bufnr = bufnr }) - end, - }) - end + lsp_format.on_attach(client, bufnr) -- Mappings local wk = require("which-key") diff --git a/home/vim/plugin/settings/formatting.lua b/home/vim/plugin/settings/formatting.lua new file mode 100644 index 0000000..1bb896b --- /dev/null +++ b/home/vim/plugin/settings/formatting.lua @@ -0,0 +1,3 @@ +local lsp_format = require("lsp-format") + +lsp_format.setup({})