1

Over time I'm adding more and more language servers in nvim. This is starting to get somewhat annoying when I use nvim outside of a project context on a filetype that I don't have an LSP globally installed for. Every time, a warning is printed that the LSP failed to start.

Screenshot

I'm not sure if this error comes from nvim itself or nvim-lspconfig, but it's really annoying. If I expect an LSP to work and it doesn't, I'll check :LspInfo.

How can I silence it?

Hubro
  • 56,214
  • 69
  • 228
  • 381

1 Answers1

0

I added this helper for my lsp configuration

function lsp_binary_exists(server_config)
    local valid_config = server_config.document_config and
        server_config.document_config.default_config and
        type(server_config.document_config.default_config.cmd) == "table" and
        #server_config.document_config.default_config.cmd >= 1

    if not valid_config then
        return false
    end

    local binary = server_config.document_config.default_config.cmd[1]

    return vim.fn.executable(binary) == 1
end

I then call it like so:

local ok, lspconfig = pcall(require, 'lspconfig')

local servers = {
    'rust_analyzer',
}

for _, lsp in ipairs(servers) do
    -- need to check for setup because some of
    -- lsp configurations exist but have no setup (i.e. are deprecated)
    if lspconfig[lsp] and lspconfig[lsp].setup and lsp_binary_exists(lspconfig[lsp]) then
        lspconfig[lsp].setup {
            flags = {
                debounce_text_changes = 150,
            },
        }
    end
end

this way if the binary doesn't exist nvim will skip the language server setup.

Konstantin
  • 186
  • 9