Skip to content

Commit 87409bb

Browse files
authored
fix(#1815): don't schedule find_file calls, debounce update_focused_file with 15ms default (#1828)
* Revert "Revert "fix(#1815): don't schedule find_file calls, debounce update_focused_file with 15ms default (#1820)"" This reverts commit a8d26bb. * fix(#1723): find_file for externally created new file results in folder unable to be opened * fix(#1723): find_file for externally created new file results in folder unable to be opened
1 parent d85b671 commit 87409bb

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

doc/nvim-tree-lua.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ Subsequent calls to setup will replace the previous configuration.
280280
},
281281
update_focused_file = {
282282
enable = false,
283+
debounce_delay = 15,
283284
update_root = false,
284285
ignore_list = {},
285286
},
@@ -508,6 +509,11 @@ until it finds the file.
508509
Enable this feature.
509510
Type: `boolean`, Default: `false`
510511

512+
*nvim-tree.update_focused_file.debounce_delay*
513+
Idle milliseconds between BufEnter and update.
514+
The last BufEnter will be focused, others are discarded.
515+
Type: `number`, Default: `15` (ms)
516+
511517
*nvim-tree.update_focused_file.update_root* (previously `update_focused_file.update_cwd`)
512518
Update the root directory of the tree if the file is not under current
513519
root directory. It prefers vim's cwd and `root_dirs`.

lua/nvim-tree.lua

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,6 @@ end
6969
---@deprecated
7070
M.on_keypress = require("nvim-tree.actions.dispatch").dispatch
7171

72-
function M.toggle(find_file, no_focus, cwd, bang)
73-
if view.is_visible() then
74-
view.close()
75-
else
76-
local previous_buf = vim.api.nvim_get_current_buf()
77-
M.open(cwd)
78-
if _config.update_focused_file.enable or find_file then
79-
M.find_file(false, previous_buf, bang)
80-
end
81-
if no_focus then
82-
vim.cmd "noautocmd wincmd p"
83-
end
84-
end
85-
end
86-
8772
function M.open(cwd)
8873
cwd = cwd ~= "" and cwd or nil
8974
if view.is_visible() then
@@ -143,7 +128,7 @@ local function is_file_readable(fname)
143128
return stat and stat.type == "file" and vim.loop.fs_access(fname, "R")
144129
end
145130

146-
function M.find_file(with_open, bufnr, bang)
131+
local function find_file(with_open, bufnr, bang)
147132
if not with_open and not core.get_explorer() then
148133
return
149134
end
@@ -162,13 +147,35 @@ function M.find_file(with_open, bufnr, bang)
162147
M.open()
163148
end
164149

165-
-- if we don't schedule, it will search for NvimTree
166-
vim.schedule(function()
167-
if bang or _config.update_focused_file.update_root then
168-
M.change_root(filepath, bufnr)
150+
if bang or _config.update_focused_file.update_root then
151+
M.change_root(filepath, bufnr)
152+
end
153+
154+
require("nvim-tree.actions.finders.find-file").fn(filepath)
155+
end
156+
157+
---@deprecated 2022/12/16
158+
function M.find_file(with_open, bufnr, bang)
159+
vim.notify_once(
160+
"require('nvim-tree').find_file is not API and will soon be unavailable. Please use api.tree.find_file as per :help nvim-tree-api",
161+
vim.log.levels.WARN
162+
)
163+
find_file(with_open, bufnr, bang)
164+
end
165+
166+
function M.toggle(with_find_file, no_focus, cwd, bang)
167+
if view.is_visible() then
168+
view.close()
169+
else
170+
local previous_buf = vim.api.nvim_get_current_buf()
171+
M.open(cwd)
172+
if _config.update_focused_file.enable or with_find_file then
173+
find_file(false, previous_buf, bang)
169174
end
170-
require("nvim-tree.actions.finders.find-file").fn(filepath)
171-
end)
175+
if no_focus then
176+
vim.cmd "noautocmd wincmd p"
177+
end
178+
end
172179
end
173180

174181
M.resize = view.resize
@@ -272,7 +279,7 @@ function M.on_enter(netrw_disabled)
272279
if should_focus_other_window then
273280
vim.cmd "noautocmd wincmd p"
274281
if should_find then
275-
M.find_file(false)
282+
find_file(false)
276283
end
277284
end
278285
end
@@ -306,7 +313,7 @@ local function setup_vim_commands()
306313
vim.api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, { bar = true })
307314
vim.api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, { bar = true })
308315
vim.api.nvim_create_user_command("NvimTreeFindFile", function(res)
309-
M.find_file(true, nil, res.bang)
316+
find_file(true, nil, res.bang)
310317
end, { bang = true, bar = true })
311318
vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function(res)
312319
M.toggle(true, false, res.args, res.bang)
@@ -324,7 +331,7 @@ function M.change_dir(name)
324331
change_dir.fn(name)
325332

326333
if _config.update_focused_file.enable then
327-
M.find_file(false)
334+
find_file(false)
328335
end
329336
end
330337

@@ -400,7 +407,9 @@ local function setup_autocommands(opts)
400407
if opts.update_focused_file.enable then
401408
create_nvim_tree_autocmd("BufEnter", {
402409
callback = function()
403-
M.find_file(false)
410+
utils.debounce("BufEnter:find_file", opts.update_focused_file.debounce_delay, function()
411+
find_file(false)
412+
end)
404413
end,
405414
})
406415
end
@@ -572,6 +581,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
572581
},
573582
update_focused_file = {
574583
enable = false,
584+
debounce_delay = 15,
575585
update_root = false,
576586
ignore_list = {},
577587
},

0 commit comments

Comments
 (0)