Fix call some methods more safely
This commit is contained in:
@@ -14,16 +14,39 @@
|
||||
limitations under the License.
|
||||
]]
|
||||
|
||||
local module_name = "core.user_commands"
|
||||
local utils = require("utils")
|
||||
|
||||
vim.api.nvim_create_user_command(
|
||||
"Update",
|
||||
function (_)
|
||||
require("lazy").update()
|
||||
vim.fn.execute(":TSUpdateSync")
|
||||
vim.fn.execute(":Neorg sync-parsers")
|
||||
vim.fn.execute(":MasonUpdateAll")
|
||||
utils.try_require(
|
||||
"lazy",
|
||||
module_name,
|
||||
function (lazy)
|
||||
lazy.update()
|
||||
end
|
||||
)
|
||||
|
||||
utils.try_require(
|
||||
"nvim-treesitter.install",
|
||||
module_name,
|
||||
function (treesitter_install)
|
||||
treesitter_install.update({ with_sync = true, })("all")
|
||||
end
|
||||
)
|
||||
|
||||
utils.try_require(
|
||||
"mason-update-all",
|
||||
module_name,
|
||||
function (mason_update_all)
|
||||
mason_update_all.update_all()
|
||||
end
|
||||
)
|
||||
end,
|
||||
{
|
||||
desc = "Update lazy plugins, treesitter parsers and mason language servers",
|
||||
desc =
|
||||
"Update lazy plugins, treesitter parsers and mason language servers",
|
||||
force = false,
|
||||
}
|
||||
)
|
||||
|
||||
+27
-6
@@ -37,7 +37,11 @@ P.config = {
|
||||
}
|
||||
|
||||
for server, _ in pairs(P.config) do
|
||||
P.config[server] = require("lsp.config." .. server)
|
||||
local ok, resp = pcall(require, "lsp.config." .. server)
|
||||
if not ok then
|
||||
return
|
||||
end
|
||||
P.config[server] = resp
|
||||
end
|
||||
|
||||
local function ca_rename()
|
||||
@@ -280,29 +284,46 @@ function P.setup_server(self, name)
|
||||
return
|
||||
end
|
||||
|
||||
local lspconfig = require("lspconfig")
|
||||
local ok, lspconfig = pcall(require, "lspconfig")
|
||||
if not ok then
|
||||
utils.err("Missing required plugin lspconfig", package_name)
|
||||
return
|
||||
end
|
||||
|
||||
server.lspconfig.root_dir = lspconfig.util.find_git_ancestor
|
||||
server.lspconfig.capabilities = self.capabilities
|
||||
server.lspconfig.on_attach = function (...)
|
||||
local ok, resp = pcall(self.on_attach, ...)
|
||||
local resp
|
||||
ok, resp = pcall(self.on_attach, ...)
|
||||
if not ok then
|
||||
utils.err(
|
||||
("Failed to load on_attach for %s:\n%s"):format(name, resp)
|
||||
)
|
||||
end
|
||||
end
|
||||
lspconfig[name].setup(server.lspconfig)
|
||||
|
||||
if not pcall(lspconfig[name].setup, server.lspconfig) then
|
||||
utils.err("Unknown LSP server for lspconfig: " .. name, package_name)
|
||||
return
|
||||
end
|
||||
|
||||
self:reload_server_buf(name)
|
||||
end
|
||||
|
||||
function P.setup(self)
|
||||
self._setup_diagnostic()
|
||||
P.capabilities = require("cmp_nvim_lsp").default_capabilities()
|
||||
require("mason-lspconfig").setup_handlers({
|
||||
|
||||
utils.try_require("cmp_nvim_lsp", package_name, function (mod)
|
||||
P.capabilities = mod.default_capabilities()
|
||||
end)
|
||||
|
||||
utils.try_require("mason-lspconfig", package_name, function (mod)
|
||||
mod.setup_handlers({
|
||||
function (name)
|
||||
self:setup_server(name)
|
||||
end,
|
||||
})
|
||||
end)
|
||||
end
|
||||
|
||||
return P
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
-- https://github.com/hrsh7th/nvim-cmp
|
||||
|
||||
local module_name = "plugins.config.cmp"
|
||||
|
||||
local has_words_before = function ()
|
||||
unpack = unpack or table.unpack
|
||||
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
@@ -25,9 +27,14 @@ local has_words_before = function ()
|
||||
:match("%s") == nil
|
||||
end
|
||||
|
||||
local utils = require("utils")
|
||||
local cmp = require("cmp")
|
||||
local luasnip = require("luasnip")
|
||||
local lspkind = require("lspkind")
|
||||
|
||||
local lspkind
|
||||
utils.try_require("lspkind", module_name, function (module)
|
||||
lspkind = module
|
||||
end)
|
||||
|
||||
cmp.setup({
|
||||
enabled = function ()
|
||||
@@ -49,13 +56,17 @@ cmp.setup({
|
||||
},
|
||||
formatting = {
|
||||
format = function (entry, vim_item)
|
||||
if lspkind then
|
||||
vim_item = lspkind.cmp_format({
|
||||
mode = "text",
|
||||
mode = "symbol",
|
||||
maxwidth = 50,
|
||||
ellipsis_char = "...",
|
||||
before = function (_, item)
|
||||
item.dup = 0 -- remove duplicates, see nvim-cmp #511
|
||||
return item
|
||||
end,
|
||||
})(entry, vim_item)
|
||||
|
||||
vim_item.dup = 0
|
||||
end
|
||||
|
||||
return vim_item
|
||||
end,
|
||||
@@ -153,8 +164,13 @@ cmp.setup.cmdline(
|
||||
}
|
||||
)
|
||||
|
||||
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
|
||||
utils.try_require(
|
||||
"nvim-autopairs.completion.cmp",
|
||||
module_name,
|
||||
function (cmp_autopairs)
|
||||
cmp.event:on(
|
||||
"confirm_done",
|
||||
cmp_autopairs.on_confirm_done()
|
||||
)
|
||||
end
|
||||
)
|
||||
|
||||
@@ -16,15 +16,19 @@
|
||||
|
||||
-- https://github.com/rcarriga/nvim-notify
|
||||
|
||||
local telescope = require("telescope")
|
||||
local has_telescope, telescope = pcall(require, "telescope")
|
||||
|
||||
local notify = require("notify")
|
||||
notify.setup({
|
||||
render = "default",
|
||||
stages = "fade",
|
||||
})
|
||||
|
||||
vim.notify = notify
|
||||
|
||||
if has_telescope then
|
||||
telescope.load_extension("notify")
|
||||
vim.keymap.set(
|
||||
"n", "<leader>fn", function () telescope.extensions.notify.notify() end
|
||||
)
|
||||
end
|
||||
|
||||
+16
-1
@@ -52,7 +52,8 @@ end
|
||||
--- @param exes table: Array of exes
|
||||
function M.assert_any_installed(exes)
|
||||
if not M.any_installed(exes) then
|
||||
error("At least one of the following is required:\n" .. table.concat(exes, ", "))
|
||||
error("At least one of the following is required:\n" ..
|
||||
table.concat(exes, ", "))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -98,4 +99,18 @@ function M.err(msg, title)
|
||||
M.notify(msg, title, vim.log.levels.ERROR)
|
||||
end
|
||||
|
||||
function M.try_require(module, err_title, on_success)
|
||||
local has_module, resp = pcall(require, module)
|
||||
|
||||
if has_module then
|
||||
if not on_success then
|
||||
return
|
||||
end
|
||||
|
||||
return on_success(resp)
|
||||
end
|
||||
|
||||
M.err(("Failed to load module %s"):format(module), err_title)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user