From 0fabc551bda45d6698b1ffdeb868e771be08d565 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Wed, 20 Mar 2024 12:49:19 +0100 Subject: [PATCH] fix lsp autoformatting on save tested python and rust --- lua/lsp.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lua/lsp.lua b/lua/lsp.lua index a9ea942..4ac70b8 100644 --- a/lua/lsp.lua +++ b/lua/lsp.lua @@ -1,5 +1,6 @@ local on_attach = function(client, bufnr) local utils = require('utils') + local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) -- Mappings. utils.map("n", "K", "lua vim.lsp.buf.hover()") @@ -12,14 +13,16 @@ local on_attach = function(client, bufnr) utils.map("n", "wa", "lua vim.lsp.buf.add_workspace_folder()") utils.map("n", "wr", "lua vim.lsp.buf.remove_workspace_folder()") - -- Set formatting keybinds conditional on server capabilities - if client.server_capabilities.document_formatting then - utils.map('n', 'lf', 'lua vim.lsp.buf.formatting()') - elseif client.server_capabilities.document_range_formatting then - utils.map('n', 'lf', 'lua vim.lsp.buf.range_formatting()') + if client.supports_method("textDocument/formatting") then + 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() + end, + }) end - - client.server_capabilities.semanticTokensProvider = nil end local nvim_lsp = require('lspconfig')