feat(nvim): call lsp formatter with filter callback

This commit is contained in:
Michael Mandl 2023-01-09 07:40:32 +01:00
parent 70984e2a9a
commit dab755251c
Signed by: mandlm
GPG key ID: 4AA25D647AA54CC7

View file

@ -5,6 +5,12 @@ local on_attach = function(client, bufnr)
vim.keymap.set("n", key, command, { noremap = true, silent = true, buffer = bufnr }) vim.keymap.set("n", key, command, { noremap = true, silent = true, buffer = bufnr })
end 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") local telescope = require("telescope.builtin")
nnoremap("gD", vim.lsp.buf.declaration) nnoremap("gD", vim.lsp.buf.declaration)
@ -15,25 +21,18 @@ local on_attach = function(client, bufnr)
nnoremap("K", vim.lsp.buf.hover) nnoremap("K", vim.lsp.buf.hover)
nnoremap("<leader>rn", vim.lsp.buf.rename) nnoremap("<leader>rn", vim.lsp.buf.rename)
nnoremap("<leader>ca", vim.lsp.buf.code_action) 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", telescope.diagnostics)
nnoremap("<leader>D", require("lsp_lines").toggle) nnoremap("<leader>D", require("lsp_lines").toggle)
nnoremap("<C-p>", vim.diagnostic.goto_prev) nnoremap("<C-p>", vim.diagnostic.goto_prev)
nnoremap("<C-n>", vim.diagnostic.goto_next) 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 if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", { vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup, group = augroup,
buffer = bufnr, buffer = bufnr,
callback = function() callback = format_buffer,
vim.lsp.buf.format({ timeout_ms = 3000, async = false })
end,
}) })
end end