diff --git a/neovim/lua/plugins/lsp.lua b/neovim/lua/plugins/lsp.lua index 13592ad..7af9cf0 100644 --- a/neovim/lua/plugins/lsp.lua +++ b/neovim/lua/plugins/lsp.lua @@ -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", "t", "lua vim.lsp.buf.document_symbol()", opts) - -- buf_set_keymap("n", "ft", "lua vim.lsp.buf.workspace_symbol()", opts) - elseif packer_plugins["telescope.nvim"] then + if utils.try_require("telescope.nvim") ~= nil then buf_set_keymap("n", "t", "Telescope lsp_document_symbols", opts) buf_set_keymap("n", "ft", "Telescope lsp_dynamic_workspace_symbols", opts) end @@ -153,56 +150,56 @@ 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") - saga.init_lsp_saga({ - error_sign = "🔥", - warn_sign = "⚠️", - hint_sign = "🤔", - dianostic_header_icon = " 💬 ", - code_action_icon = "💡", - code_action_prompt = { - enable = false, - sign = false, - }, - }) + utils.try_require("lspsaga", function(saga) + saga.init_lsp_saga({ + error_sign = "🔥", + warn_sign = "⚠️", + hint_sign = "🤔", + dianostic_header_icon = " 💬 ", + code_action_icon = "💡", + code_action_prompt = { + enable = false, + sign = false, + }, + }) + end) end function M.config_null_ls() - local null_ls = require("null-ls") - null_ls.setup({ - sources = { - -- Generic - -- null_ls.builtins.formatting.preittier, - -- null_ls.builtins.formatting.trim_whitespace, - -- null_ls.builtins.formatting.trim_newlines, - -- Fish - -- null_ls.builtins.formatting.fish_indent, - -- Python - null_ls.builtins.formatting.reorder_python_imports, - null_ls.builtins.formatting.black, - null_ls.builtins.diagnostics.mypy, - -- Go - null_ls.builtins.diagnostics.golangci_lint, - -- Text - -- null_ls.builtins.code_actions.proselint, - -- Ansible - -- null_ls.builtins.diagnostics.ansiblelint, - -- Shell - null_ls.builtins.diagnostics.shellcheck, - -- Rust - -- null_ls.builtins.formatting.rustfmt, - -- Lua - null_ls.builtins.diagnostics.luacheck, - null_ls.builtins.formatting.stylua, - }, - }) -end - + utils.try_require("null-ls", function(null_ls) + null_ls.setup({ + sources = { + -- Generic + -- null_ls.builtins.formatting.preittier, + -- null_ls.builtins.formatting.trim_whitespace, + -- null_ls.builtins.formatting.trim_newlines, + -- Fish + -- null_ls.builtins.formatting.fish_indent, + -- Python + null_ls.builtins.formatting.reorder_python_imports, + null_ls.builtins.formatting.black, + null_ls.builtins.diagnostics.mypy, + -- Go + null_ls.builtins.diagnostics.golangci_lint, + -- Text + -- null_ls.builtins.code_actions.proselint, + -- Ansible + -- null_ls.builtins.diagnostics.ansiblelint, + -- Shell + null_ls.builtins.diagnostics.shellcheck, + -- Rust + -- null_ls.builtins.formatting.rustfmt, + -- Lua + null_ls.builtins.diagnostics.luacheck, + null_ls.builtins.formatting.stylua, + }, + }) + end) + end return M diff --git a/neovim/lua/utils.lua b/neovim/lua/utils.lua index e4e6692..5c91d22 100644 --- a/neovim/lua/utils.lua +++ b/neovim/lua/utils.lua @@ -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