Neovim Select a Language Prompt

Viewed 1465

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

2 Answers

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

Related