From 8cb19bc1ab51937c8bbe85dbebc67c8e21fb0252 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 16 Dec 2022 15:26:42 +1100 Subject: [PATCH 1/3] Revert "Revert "fix(#1815): don't schedule find_file calls, debounce update_focused_file with 15ms default (#1820)"" This reverts commit a8d26bb0885cb3bbe09c3f6c9066f1fe028c79d4. --- doc/nvim-tree-lua.txt | 6 ++++++ lua/nvim-tree.lua | 36 +++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 955c06e40e1..11e672fee6f 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -280,6 +280,7 @@ Subsequent calls to setup will replace the previous configuration. }, update_focused_file = { enable = false, + debounce_delay = 15, update_root = false, ignore_list = {}, }, @@ -508,6 +509,11 @@ until it finds the file. Enable this feature. Type: `boolean`, Default: `false` + *nvim-tree.update_focused_file.debounce_delay* + Idle milliseconds between BufEnter and update. + The last BufEnter will be focused, others are discarded. + Type: `number`, Default: `15` (ms) + *nvim-tree.update_focused_file.update_root* (previously `update_focused_file.update_cwd`) Update the root directory of the tree if the file is not under current root directory. It prefers vim's cwd and `root_dirs`. diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 81cb51ca6fd..44b91b2d491 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -76,7 +76,7 @@ function M.toggle(find_file, no_focus, cwd, bang) local previous_buf = vim.api.nvim_get_current_buf() M.open(cwd) if _config.update_focused_file.enable or find_file then - M.find_file(false, previous_buf, bang) + find_file(false, previous_buf, bang) end if no_focus then vim.cmd "noautocmd wincmd p" @@ -143,7 +143,7 @@ local function is_file_readable(fname) return stat and stat.type == "file" and vim.loop.fs_access(fname, "R") end -function M.find_file(with_open, bufnr, bang) +local function find_file(with_open, bufnr, bang) if not with_open and not core.get_explorer() then return end @@ -162,13 +162,20 @@ function M.find_file(with_open, bufnr, bang) M.open() end - -- if we don't schedule, it will search for NvimTree - vim.schedule(function() - if bang or _config.update_focused_file.update_root then - M.change_root(filepath, bufnr) - end - require("nvim-tree.actions.finders.find-file").fn(filepath) - end) + if bang or _config.update_focused_file.update_root then + M.change_root(filepath, bufnr) + end + + require("nvim-tree.actions.finders.find-file").fn(filepath) +end + +---@deprecated 2022/12/16 +function M.find_file(with_open, bufnr, bang) + vim.notify_once( + "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", + vim.log.levels.WARN + ) + find_file(with_open, bufnr, bang) end M.resize = view.resize @@ -272,7 +279,7 @@ function M.on_enter(netrw_disabled) if should_focus_other_window then vim.cmd "noautocmd wincmd p" if should_find then - M.find_file(false) + find_file(false) end end end @@ -306,7 +313,7 @@ local function setup_vim_commands() vim.api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, { bar = true }) vim.api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, { bar = true }) vim.api.nvim_create_user_command("NvimTreeFindFile", function(res) - M.find_file(true, nil, res.bang) + find_file(true, nil, res.bang) end, { bang = true, bar = true }) vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function(res) M.toggle(true, false, res.args, res.bang) @@ -324,7 +331,7 @@ function M.change_dir(name) change_dir.fn(name) if _config.update_focused_file.enable then - M.find_file(false) + find_file(false) end end @@ -400,7 +407,9 @@ local function setup_autocommands(opts) if opts.update_focused_file.enable then create_nvim_tree_autocmd("BufEnter", { callback = function() - M.find_file(false) + utils.debounce("BufEnter:find_file", opts.update_focused_file.debounce_delay, function() + find_file(false) + end) end, }) end @@ -572,6 +581,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS }, update_focused_file = { enable = false, + debounce_delay = 15, update_root = false, ignore_list = {}, }, From 74dde56e0c4be3bb7e1ecdec2deb712124797d7a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 16 Dec 2022 15:29:34 +1100 Subject: [PATCH 2/3] fix(#1723): find_file for externally created new file results in folder unable to be opened --- lua/nvim-tree.lua | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 44b91b2d491..3d294226436 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -69,21 +69,6 @@ end ---@deprecated M.on_keypress = require("nvim-tree.actions.dispatch").dispatch -function M.toggle(find_file, no_focus, cwd, bang) - if view.is_visible() then - view.close() - else - local previous_buf = vim.api.nvim_get_current_buf() - M.open(cwd) - if _config.update_focused_file.enable or find_file then - find_file(false, previous_buf, bang) - end - if no_focus then - vim.cmd "noautocmd wincmd p" - end - end -end - function M.open(cwd) cwd = cwd ~= "" and cwd or nil if view.is_visible() then @@ -178,6 +163,21 @@ function M.find_file(with_open, bufnr, bang) find_file(with_open, bufnr, bang) end +function M.toggle(do_find_file, no_focus, cwd, bang) + if view.is_visible() then + view.close() + else + local previous_buf = vim.api.nvim_get_current_buf() + M.open(cwd) + if _config.update_focused_file.enable or do_find_file then + find_file(false, previous_buf, bang) + end + if no_focus then + vim.cmd "noautocmd wincmd p" + end + end +end + M.resize = view.resize function M.open_on_directory() From 6d72e275930a23ca6d65c0875966c733075aaf7d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 16 Dec 2022 15:31:18 +1100 Subject: [PATCH 3/3] fix(#1723): find_file for externally created new file results in folder unable to be opened --- lua/nvim-tree.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 3d294226436..375ecda7624 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -163,13 +163,13 @@ function M.find_file(with_open, bufnr, bang) find_file(with_open, bufnr, bang) end -function M.toggle(do_find_file, no_focus, cwd, bang) +function M.toggle(with_find_file, no_focus, cwd, bang) if view.is_visible() then view.close() else local previous_buf = vim.api.nvim_get_current_buf() M.open(cwd) - if _config.update_focused_file.enable or do_find_file then + if _config.update_focused_file.enable or with_find_file then find_file(false, previous_buf, bang) end if no_focus then