fix lsp autoformatting on save

tested python and rust
This commit is contained in:
Vladan Popovic 2024-03-20 12:49:19 +01:00
parent b521d84e7f
commit 0fabc551bd

View file

@ -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", "<Cmd>lua vim.lsp.buf.hover()<CR>")
@ -12,14 +13,16 @@ local on_attach = function(client, bufnr)
utils.map("n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>")
utils.map("n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>")
-- Set formatting keybinds conditional on server capabilities
if client.server_capabilities.document_formatting then
utils.map('n', '<leader>lf', '<cmd>lua vim.lsp.buf.formatting()<CR>')
elseif client.server_capabilities.document_range_formatting then
utils.map('n', '<leader>lf', '<cmd>lua vim.lsp.buf.range_formatting()<CR>')
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')