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.