Add a try_require utility rather than checking packer

This commit is contained in:
ViViDboarder 2021-12-16 14:39:34 -08:00
parent e11ba17d15
commit 1c9990750b
2 changed files with 68 additions and 52 deletions

View File

@ -30,8 +30,8 @@ function M.config_lsp_ui()
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
if utils.is_plugin_loaded("trouble") then
require("trouble").setup({
utils.try_require("trouble", function(trouble)
trouble.setup({
fold_open = "",
fold_closed = "",
signs = {
@ -42,7 +42,7 @@ function M.config_lsp_ui()
other = "",
},
})
end
end)
end
local function default_attach(client, bufnr)
@ -108,10 +108,7 @@ local function default_attach(client, bufnr)
end
-- Some override some fuzzy finder bindings to use lsp sources
if packer_plugins["nvim-lspfuzzy"] then
buf_set_keymap("n", "<leader>t", "<cmd>lua vim.lsp.buf.document_symbol()<CR>", opts)
-- buf_set_keymap("n", "<leader>ft", "<cmd>lua vim.lsp.buf.workspace_symbol()<CR>", opts)
elseif packer_plugins["telescope.nvim"] then
if utils.try_require("telescope.nvim") ~= nil then
buf_set_keymap("n", "<leader>t", "<cmd>Telescope lsp_document_symbols<CR>", opts)
buf_set_keymap("n", "<leader>ft", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", opts)
end
@ -153,14 +150,13 @@ function M.config_lsp()
},
},
})
if utils.is_plugin_loaded("null-ls.nvim") then
M.config_null_ls()
utils.try_require("null-ls", function()
lsp_config["null-ls"].setup({ capabilities = capabilities, on_attach = default_attach })
end
end)
end
function M.config_lsp_saga()
local saga = require("lspsaga")
utils.try_require("lspsaga", function(saga)
saga.init_lsp_saga({
error_sign = "🔥",
warn_sign = "⚠️",
@ -172,10 +168,11 @@ function M.config_lsp_saga()
sign = false,
},
})
end)
end
function M.config_null_ls()
local null_ls = require("null-ls")
utils.try_require("null-ls", function(null_ls)
null_ls.setup({
sources = {
-- Generic
@ -203,6 +200,6 @@ function M.config_null_ls()
null_ls.builtins.formatting.stylua,
},
})
end)
end
return M

View File

@ -98,4 +98,23 @@ function M.is_plugin_loaded(name)
return _G["packer_plugins"] and packer_plugins[name] and packer_plugins[name].loaded
end
-- Try to require something and perform some action if it was found
function M.try_require(name, on_found, on_notfound)
local status, module = pcall(require, name)
if status then
if on_found ~= nil then
on_found(module)
end
return module
else
if on_notfound ~= nil then
on_notfound(name)
end
return nil
end
end
return M