Skip to content

Commit 7e53844

Browse files
committed
nvim: safe guard nvim-tree
1 parent 7091c3c commit 7e53844

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

.config/nvim/init.lua

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22
vim.g.loaded_netrw = 1
33
vim.g.loaded_netrwPlugin = 1
44

5+
-- @USAGE:
6+
-- local foo = safe_require('foo')
7+
-- if not foo then return end
8+
_G.safe_require = function(module_name)
9+
local package_exists, module = pcall(require, module_name)
10+
if not package_exists then
11+
vim.defer_fn(function()
12+
vim.schedule(function()
13+
vim.notify('Could not load module: ' .. module_name, 'error', { title = 'Module Not Found' })
14+
end)
15+
end, 1000)
16+
return nil
17+
else
18+
return module
19+
end
20+
end
21+
22+
-- @USAGE: :lua safe_reload('foo')
23+
function _G.safe_reload(module)
24+
package.loaded[module] = nil
25+
return safe_require(module)
26+
end
27+
528
require("user/plugins")
629
require("user/options")
730
require("user/reload")

.config/nvim/lua/user/nvim-tree.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
local nvim_tree = require("nvim-tree")
2-
local nvim_tree_config = require("nvim-tree.config")
1+
local nvim_tree = _G.safe_require("nvim-tree")
32

4-
local tree_cb = nvim_tree_config.nvim_tree_callback
3+
if (nvim_tree == nil) then
4+
return
5+
end
56

67
nvim_tree.setup({
78
disable_netrw = true,

0 commit comments

Comments
 (0)