I recently realized that my neovim automatically spawns the same language server (in this case, tsserver
and tailwindcss
) everytime I open a file.
Everything works fine when opening the first file
However, once I opened another file, it starts spawning another lsp client
Here's my nvim config for the lsp.
-- Mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
local opts = { noremap = true, silent = true }
vim.api.nvim_set_keymap("n", "<space>e", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
vim.api.nvim_set_keymap("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts)
vim.api.nvim_set_keymap("n", "]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts)
vim.api.nvim_set_keymap("n", "<space>q", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
-- Mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
end
local capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities())
local rawCapabilitiesWithoutFormatting = vim.lsp.protocol.make_client_capabilities()
rawCapabilitiesWithoutFormatting.textDocument.formatting = false
rawCapabilitiesWithoutFormatting.textDocument.rangeFormatting = false
local capabilitiesWithoutFormatting = require("cmp_nvim_lsp").default_capabilities(rawCapabilitiesWithoutFormatting)
-- Use a loop to conveniently call 'setup' on multiple servers and
-- map buffer local keybindings when the language server attaches, for
-- servers that don't need any special treatment
local servers = {
"bashls",
"clangd",
"cssls",
"eslint",
"gopls",
"html",
"jsonls",
"rust_analyzer",
"svelte",
"tailwindcss",
"vimls",
"volar",
"prismals",
"marksman",
}
for _, lsp in pairs(servers) do
require("lspconfig")[lsp].setup({
on_attach = on_attach,
flags = {
debounce_text_changes = 300,
},
capabilities = capabilities,
})
end
-- setup tsserver manually like a pro
require("lspconfig").tsserver.setup({
on_attach = function(client, bufnr)
client.server_capabilities.document_formatting = false
client.server_capabilities.document_range_formatting = false
on_attach(client, bufnr)
end,
flags = {
debounce_text_changes = 300,
},
capabilities = capabilitiesWithoutFormatting,
settings = {
documentFormatting = false,
},
root_dir = require("lspconfig.util").find_git_ancestor,
})
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
underline = true,
-- This sets the spacing and the prefix, obviously.
virtual_text = {
spacing = 4,
},
signs = true,
update_in_insert = true,
})
It was an issue on
nvim-lspconfig
's end, however it was fixed on a recent patch. Just pull in the latest version and it should work as expected.