'Neovim Select a Language Prompt
I am using Neovim with LSP and I am having an issue with saving any of my tsx file. I keep getting prompted to select a language server:
Here is how I am configuring my language servers
lspinstall.setup()
local servers = lspinstall.installed_servers()
for _, lsp in ipairs(servers) do
if lsp == 'tsserver' then
require('lsp.tsserver')
elseif lsp == 'efm' then
require('lsp.efm')
elseif lsp == 'html' then
require('lsp.html')
else
nvim_lsp[lsp].setup {on_attach = on_attach, settings = {Lua = {diagnostics = {globals = {'vim'}}}}}
end
end
If I do :LspInfo I am seeing the 2 servers seen in the screenshot.
EFM config
local lspconfig = require 'lspconfig'
local prettier = {formatCommand = './node_modules/.bin/prettier --config-precedence prefer-file --stdin-filepath ${INPUT}', formatStdin = true}
local luaFormat = {
formatCommand = 'lua-format -i --no-keep-simple-function-one-line --column-limit=120 --indent-width=2 --double-quote-to-single-quote',
formatStdin = true
}
lspconfig.efm.setup {
-- cmd = {'efm-langserver', '-logfile', '/tmp/efm.log', '-loglevel', '5'},
on_attach = on_attach,
init_options = {documentFormatting = true},
filetypes = {'javascriptreact', 'javascript', 'lua', 'typescriptreact', 'typescript'},
settings = {
rootMarkers = {'.git/'},
languages = {lua = {luaFormat}, typescript = {prettier}, typescriptreact = {prettier}}
}
}
typescript config
local lspconfig = require 'lspconfig'
lspconfig.tsserver.setup {
on_attach = function(client, bufnr)
client.resolved_capabilities.document_formatting = false
on_attach(client, bufnr)
end,
settings = {diagnostics = {globals = {'on_attach'}}}
}
Thanks for any help
Solution 1:[1]
There was a recent change to better support multiple LSPs, you can see the details here: https://github.com/neovim/neovim/pull/14462 and the gist of it is: use formatting_seq_sync() instead of formatting_sync(): https://neovim.io/doc/user/lsp.html#vim.lsp.buf.formatting_seq_sync()
Solution 2:[2]
You need to not report the formatting capabilities to any lsp you do not want to format.
For example, if you're using tsserver but only want to use null-ls for formatting:
require("lspconfig").tsserver.setup({
on_attach = function(client)
client.resolved_capabilities.document_formatting = false
client.resolved_capabilities.document_range_formatting = false
end,
})
This will make Neovim default to null-ls for formatting and disable the prompt.
Credit: https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Avoiding-LSP-formatting-conflicts
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alexaandru |
| Solution 2 | PatKilg |

