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