feat(nvim): call lsp formatter with filter callback

main
mandlm 2023-01-09 07:40:32 +01:00
parent 70984e2a9a
commit dab755251c
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
1 changed files with 8 additions and 9 deletions

View File

@ -5,6 +5,12 @@ local on_attach = function(client, bufnr)
vim.keymap.set("n", key, command, { noremap = true, silent = true, buffer = bufnr })
end
local function format_buffer()
vim.lsp.buf.format({ timeout_ms = 3000, async = false, filter = function(formatter)
return formatter.name ~= "tsserver" and formatter.name ~= "volar"
end })
end
local telescope = require("telescope.builtin")
nnoremap("gD", vim.lsp.buf.declaration)
@ -15,25 +21,18 @@ local on_attach = function(client, bufnr)
nnoremap("K", vim.lsp.buf.hover)
nnoremap("<leader>rn", vim.lsp.buf.rename)
nnoremap("<leader>ca", vim.lsp.buf.code_action)
nnoremap("<leader>f", vim.lsp.buf.format)
nnoremap("<leader>f", format_buffer)
nnoremap("<leader>d", telescope.diagnostics)
nnoremap("<leader>D", require("lsp_lines").toggle)
nnoremap("<C-p>", vim.diagnostic.goto_prev)
nnoremap("<C-n>", vim.diagnostic.goto_next)
-- disable tsserver formatting
if client.name == "tsserver" then
client.server_capabilities.document_formatting = false
end
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({ timeout_ms = 3000, async = false })
end,
callback = format_buffer,
})
end