Skip to content

Commit

Permalink
fix: don't error if lspconfig is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Feb 22, 2024
1 parent 4ef6c6c commit 51c2575
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lua/neoconf/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ function M.check()
warn("**neodev.nvim** is not installed. You won't get any proper completion for your Neovim config.")
end

local _, lspconfig = pcall(require, "lspconfig.util")
local has_lspconfig, lspconfig = pcall(require, "lspconfig.util")

if lspconfig then
if has_lspconfig then
ok("**lspconfig** is installed")
local available = lspconfig.available_servers()
if vim.tbl_contains(available, "jsonls") then
Expand All @@ -60,7 +60,7 @@ function M.check()
warn("**lspconfig lua_ls** is not installed? You won't get any auto completion in your lua settings files")
end
else
error("**lspconfig** not installed?")
warn("**lspconfig** not installed?")
end
end

Expand Down
23 changes: 19 additions & 4 deletions lua/neoconf/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,35 @@ function M.merge(...)
end

function M.root_pattern(...)
return require("lspconfig.util").root_pattern(...)
local has_lspconfig, lsputil = pcall(require, "lspconfig.util")
if has_lspconfig then
return lsputil.root_pattern(...)
end
local patterns = vim.tbl_flatten({ ... })
return function(startpath)
local results = vim.fs.find(patterns, { upward = true, path = startpath })
return not vim.tbl_isempty(results) and vim.fs.dirname(results[1])
end
end

function M.find_git_ancestor(...)
return require("lspconfig.util").find_git_ancestor(...)
local has_lspconfig, lsputil = pcall(require, "lspconfig.util")
if has_lspconfig then
return lsputil.find_git_ancestor(...)
end
end

function M.has_lspconfig(server)
return vim.tbl_contains(require("lspconfig.util").available_servers(), server)
local has_lspconfig, lsputil = pcall(require, "lspconfig.util")
return has_lspconfig and vim.tbl_contains(lsputil.available_servers(), server)
end

---@param opts { on_config: fun(config, root_dir:string, original_config), root_dir: fun(), name: string }
function M.on_config(opts)
local lsputil = require("lspconfig.util")
local has_lspconfig, lsputil = pcall(require, "lspconfig.util")
if not has_lspconfig then
return
end
local hook = lsputil.add_hook_after

lsputil.on_setup = hook(lsputil.on_setup, function(initial_config)
Expand Down

0 comments on commit 51c2575

Please sign in to comment.