7

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:

enter image description here

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

jrock2004
  • 3,229
  • 5
  • 40
  • 73

2 Answers2

3

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

PatKilg
  • 240
  • 3
  • 8
2

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()

Credits go to https://www.reddit.com/r/neovim/comments/nlhnio/noevim_is_asking_to_select_a_languageserver_when/gzj6shx/?utm_source=reddit&utm_medium=web2x&context=3

alexaandru
  • 131
  • 1
  • 1
  • 4
  • This seems to run either the wrong formatter, or multiple formatters for me. – PatKilg Mar 30 '22 at 04:39
  • `formatting_seq_sync(nil, 1000, {"null-ls"})` will make sure the null-ls language server does the formatting. However the other language(s) server(s) WILL be invoked before your chosen one, so you'll get poor performance. – Emmanuel Touzery May 24 '22 at 20:55