Skip to content

Commit d4f6d33

Browse files
authored
fix(#2240): disable watchers following EMFILE (#2268)
* fix(#2240): disable watchers following EMFILE * fix(#2240): disable watchers following EMFILE
1 parent f873625 commit d4f6d33

File tree

10 files changed

+101
-65
lines changed

10 files changed

+101
-65
lines changed

lua/nvim-tree.lua

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,14 @@ local function setup_autocommands(opts)
202202
end,
203203
})
204204

205-
local has_watchers = opts.filesystem_watchers.enable
206-
207-
if opts.auto_reload_on_write and not has_watchers then
208-
create_nvim_tree_autocmd("BufWritePost", { callback = reloaders.reload_explorer })
209-
end
205+
create_nvim_tree_autocmd("BufWritePost", {
206+
callback = function()
207+
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
208+
log.line("dev", "BufWritePost reloading")
209+
reloaders.reload_explorer()
210+
end
211+
end,
212+
})
210213

211214
create_nvim_tree_autocmd("BufReadPost", {
212215
callback = function(data)
@@ -236,12 +239,14 @@ local function setup_autocommands(opts)
236239
end,
237240
})
238241

239-
if not has_watchers and opts.git.enable then
240-
create_nvim_tree_autocmd("User", {
241-
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
242-
callback = reloaders.reload_git,
243-
})
244-
end
242+
create_nvim_tree_autocmd("User", {
243+
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
244+
callback = function()
245+
if not opts.filesystem_watchers.enable and opts.git.enable then
246+
reloaders.reload_git()
247+
end
248+
end,
249+
})
245250

246251
if opts.tab.sync.open then
247252
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
@@ -277,16 +282,16 @@ local function setup_autocommands(opts)
277282
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
278283
end
279284

280-
if opts.reload_on_bufenter and not has_watchers then
281-
create_nvim_tree_autocmd("BufEnter", {
282-
pattern = "NvimTree_*",
283-
callback = function()
285+
create_nvim_tree_autocmd("BufEnter", {
286+
pattern = "NvimTree_*",
287+
callback = function()
288+
if opts.reload_on_bufenter and not opts.filesystem_watchers.enable then
284289
if utils.is_nvim_tree_buf(0) then
285290
reloaders.reload_explorer()
286291
end
287-
end,
288-
})
289-
end
292+
end
293+
end,
294+
})
290295

291296
if opts.view.centralize_selection then
292297
create_nvim_tree_autocmd("BufEnter", {
@@ -655,6 +660,16 @@ local function validate_options(conf)
655660
end
656661
end
657662

663+
function M.purge_all_state()
664+
require("nvim-tree.watcher").purge_watchers()
665+
view.close_all_tabs()
666+
view.abandon_all_windows()
667+
if core.get_explorer() ~= nil then
668+
git.purge_state()
669+
TreeExplorer = nil
670+
end
671+
end
672+
658673
function M.setup(conf)
659674
if vim.fn.has "nvim-0.8" == 0 then
660675
vim.notify_once("nvim-tree.lua requires Neovim 0.8 or higher", vim.log.levels.WARN)
@@ -703,6 +718,7 @@ function M.setup(conf)
703718
require("nvim-tree.marks").setup(opts)
704719
require("nvim-tree.modified").setup(opts)
705720
require("nvim-tree.help").setup(opts)
721+
require("nvim-tree.watcher").setup(opts)
706722
if M.config.renderer.icons.show.file and pcall(require, "nvim-web-devicons") then
707723
require("nvim-web-devicons").setup()
708724
end
@@ -714,13 +730,7 @@ function M.setup(conf)
714730
commands.setup()
715731
else
716732
-- subsequent calls to setup
717-
require("nvim-tree.watcher").purge_watchers()
718-
view.close_all_tabs()
719-
view.abandon_all_windows()
720-
if core.get_explorer() ~= nil then
721-
git.purge_state()
722-
TreeExplorer = nil
723-
end
733+
M.purge_all_state()
724734
end
725735

726736
vim.g.NvimTreeSetup = 1

lua/nvim-tree/actions/fs/copy-paste.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ local notify = require "nvim-tree.notify"
77

88
local find_file = require("nvim-tree.actions.finders.find-file").fn
99

10-
local M = {}
10+
local M = {
11+
config = {},
12+
}
1113

1214
local clipboard = {
1315
move = {},
@@ -175,7 +177,7 @@ local function do_paste(node, action_type, action_fn)
175177
end
176178

177179
clipboard[action_type] = {}
178-
if M.enable_reload then
180+
if not M.config.filesystem_watchers.enable then
179181
return require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
180182
end
181183
end
@@ -226,7 +228,7 @@ function M.print_clipboard()
226228
end
227229

228230
local function copy_to_clipboard(content)
229-
if M.use_system_clipboard == true then
231+
if M.config.actions.use_system_clipboard == true then
230232
vim.fn.setreg("+", content)
231233
vim.fn.setreg('"', content)
232234
return notify.info(string.format("Copied %s to system clipboard!", content))
@@ -255,8 +257,8 @@ function M.copy_absolute_path(node)
255257
end
256258

257259
function M.setup(opts)
258-
M.use_system_clipboard = opts.actions.use_system_clipboard
259-
M.enable_reload = not opts.filesystem_watchers.enable
260+
M.config.filesystem_watchers = opts.filesystem_watchers
261+
M.config.actions = opts.actions
260262
end
261263

262264
return M

lua/nvim-tree/actions/fs/create-file.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,4 @@ function M.fn(node)
9696
end)
9797
end
9898

99-
function M.setup(opts)
100-
M.enable_reload = not opts.filesystem_watchers.enable
101-
end
102-
10399
return M

lua/nvim-tree/actions/fs/remove-file.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ local view = require "nvim-tree.view"
44
local lib = require "nvim-tree.lib"
55
local notify = require "nvim-tree.notify"
66

7-
local M = {}
7+
local M = {
8+
config = {},
9+
}
810

911
local function close_windows(windows)
1012
if view.View.float.enable and #vim.api.nvim_list_wins() == 1 then
@@ -31,7 +33,7 @@ local function clear_buffer(absolute_path)
3133
end
3234
end
3335
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
34-
if M.close_window then
36+
if M.config.actions.remove_file.close_window then
3537
close_windows(buf.windows)
3638
end
3739
return
@@ -90,7 +92,7 @@ function M.fn(node)
9092
clear_buffer(node.absolute_path)
9193
end
9294
notify.info(node.absolute_path .. " was properly removed.")
93-
if M.enable_reload then
95+
if not M.config.filesystem_watchers.enable then
9496
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
9597
end
9698
end
@@ -110,10 +112,9 @@ function M.fn(node)
110112
end
111113

112114
function M.setup(opts)
113-
M.config = {}
114-
M.config.ui = opts.ui or {}
115-
M.enable_reload = not opts.filesystem_watchers.enable
116-
M.close_window = opts.actions.remove_file.close_window
115+
M.config.ui = opts.ui
116+
M.config.actions = opts.actions
117+
M.config.filesystem_watchers = opts.filesystem_watchers
117118
end
118119

119120
return M

lua/nvim-tree/actions/fs/rename-file.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ local notify = require "nvim-tree.notify"
55

66
local find_file = require("nvim-tree.actions.finders.find-file").fn
77

8-
local M = {}
8+
local M = {
9+
config = {},
10+
}
911

1012
local ALLOWED_MODIFIERS = {
1113
[":p:h"] = true,
@@ -83,7 +85,7 @@ function M.fn(default_modifier)
8385
end
8486

8587
M.rename(node, prepend .. new_file_path .. append)
86-
if M.enable_reload then
88+
if not M.config.filesystem_watchers.enable then
8789
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
8890
end
8991

@@ -93,7 +95,7 @@ function M.fn(default_modifier)
9395
end
9496

9597
function M.setup(opts)
96-
M.enable_reload = not opts.filesystem_watchers.enable
98+
M.config.filesystem_watchers = opts.filesystem_watchers
9799
end
98100

99101
return M

lua/nvim-tree/actions/fs/trash.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local lib = require "nvim-tree.lib"
22
local notify = require "nvim-tree.notify"
33

4-
local M = {}
4+
local M = {
5+
config = {},
6+
}
57

68
local utils = require "nvim-tree.utils"
79
local events = require "nvim-tree.events"
@@ -68,7 +70,7 @@ function M.fn(node)
6870
return
6971
end
7072
events._dispatch_folder_removed(node.absolute_path)
71-
if M.enable_reload then
73+
if not M.config.filesystem_watchers.enable then
7274
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
7375
end
7476
end)
@@ -80,7 +82,7 @@ function M.fn(node)
8082
end
8183
events._dispatch_file_removed(node.absolute_path)
8284
clear_buffer(node.absolute_path)
83-
if M.enable_reload then
85+
if not M.config.filesystem_watchers.enable then
8486
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
8587
end
8688
end)
@@ -102,10 +104,9 @@ function M.fn(node)
102104
end
103105

104106
function M.setup(opts)
105-
M.config = {}
106-
M.config.ui = opts.ui or {}
107-
M.config.trash = opts.trash or {}
108-
M.enable_reload = not opts.filesystem_watchers.enable
107+
M.config.ui = opts.ui
108+
M.config.trash = opts.trash
109+
M.config.filesystem_watchers = opts.filesystem_watchers
109110
end
110111

111112
return M

lua/nvim-tree/actions/init.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ function M.setup(opts)
66
require("nvim-tree.actions.node.file-popup").setup(opts)
77
require("nvim-tree.actions.node.open-file").setup(opts)
88
require("nvim-tree.actions.root.change-dir").setup(opts)
9-
require("nvim-tree.actions.fs.create-file").setup(opts)
109
require("nvim-tree.actions.fs.rename-file").setup(opts)
1110
require("nvim-tree.actions.fs.remove-file").setup(opts)
1211
require("nvim-tree.actions.fs.copy-paste").setup(opts)

lua/nvim-tree/explorer/watch.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ local log = require "nvim-tree.log"
22
local utils = require "nvim-tree.utils"
33
local Watcher = require("nvim-tree.watcher").Watcher
44

5-
local M = {}
5+
local M = {
6+
config = {},
7+
uid = 0,
8+
}
69

710
local function is_git(path)
811
return vim.fn.fnamemodify(path, ":t") == ".git"
@@ -23,7 +26,7 @@ local function is_folder_ignored(path)
2326
end
2427
end
2528

26-
for _, ignore_dir in ipairs(M.ignore_dirs) do
29+
for _, ignore_dir in ipairs(M.config.filesystem_watchers.ignore_dirs) do
2730
if vim.fn.match(path, ignore_dir) ~= -1 then
2831
return true
2932
end
@@ -33,7 +36,7 @@ local function is_folder_ignored(path)
3336
end
3437

3538
function M.create_watcher(node)
36-
if not M.enabled or type(node) ~= "table" then
39+
if not M.config.filesystem_watchers.enable or type(node) ~= "table" then
3740
return nil
3841
end
3942

@@ -50,7 +53,7 @@ function M.create_watcher(node)
5053

5154
local function callback(watcher)
5255
log.line("watcher", "node event scheduled refresh %s", watcher.context)
53-
utils.debounce(watcher.context, M.debounce_delay, function()
56+
utils.debounce(watcher.context, M.config.filesystem_watchers.debounce_delay, function()
5457
if watcher.destroyed then
5558
return
5659
end
@@ -72,9 +75,7 @@ function M.create_watcher(node)
7275
end
7376

7477
function M.setup(opts)
75-
M.enabled = opts.filesystem_watchers.enable
76-
M.debounce_delay = opts.filesystem_watchers.debounce_delay
77-
M.ignore_dirs = opts.filesystem_watchers.ignore_dirs
78+
M.config.filesystem_watchers = opts.filesystem_watchers
7879
M.uid = 0
7980
end
8081

lua/nvim-tree/marks/bulk-move.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ local utils = require "nvim-tree.utils"
44
local FsRename = require "nvim-tree.actions.fs.rename-file"
55
local notify = require "nvim-tree.notify"
66

7-
local M = {}
7+
local M = {
8+
config = {},
9+
}
810

911
function M.bulk_move()
1012
if #Marks.get_marks() == 0 then
@@ -29,14 +31,14 @@ function M.bulk_move()
2931
FsRename.rename(node, to)
3032
end
3133

32-
if M.enable_reload then
34+
if not M.config.filesystem_watchers.enable then
3335
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
3436
end
3537
end)
3638
end
3739

3840
function M.setup(opts)
39-
M.enable_reload = not opts.filesystem_watchers.enable
41+
M.config.filesystem_watchers = opts.filesystem_watchers
4042
end
4143

4244
return M

0 commit comments

Comments
 (0)