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 on_attach = function(client, bufnr)
local utils = require('utils') local utils = require('utils')
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
-- Mappings. -- Mappings.
utils.map("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>") 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>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>")
utils.map("n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_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.supports_method("textDocument/formatting") then
if client.server_capabilities.document_formatting then vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
utils.map('n', '<leader>lf', '<cmd>lua vim.lsp.buf.formatting()<CR>') vim.api.nvim_create_autocmd("BufWritePre", {
elseif client.server_capabilities.document_range_formatting then group = augroup,
utils.map('n', '<leader>lf', '<cmd>lua vim.lsp.buf.range_formatting()<CR>') buffer = bufnr,
callback = function()
vim.lsp.buf.format()
end,
})
end end
client.server_capabilities.semanticTokensProvider = nil
end end
local nvim_lsp = require('lspconfig') local nvim_lsp = require('lspconfig')