From ae5236999f409503c604942658ed6a68564b4e60 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Mar 2026 15:12:25 +1100 Subject: [PATCH 1/7] perf(#3253): extract change-root --- lua/nvim-tree.lua | 68 +------------------- lua/nvim-tree/actions/tree/change-root.lua | 74 ++++++++++++++++++++++ lua/nvim-tree/actions/tree/find-file.lua | 3 +- lua/nvim-tree/actions/tree/open.lua | 3 +- lua/nvim-tree/actions/tree/toggle.lua | 3 +- lua/nvim-tree/config.lua | 9 ++- 6 files changed, 89 insertions(+), 71 deletions(-) create mode 100644 lua/nvim-tree/actions/tree/change-root.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 4d05899bf2a..ff13b9fbdac 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -9,9 +9,7 @@ local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") local config = require("nvim-tree.config") -local M = { - init_root = "", -} +local M = {} --- Helper function to execute some explorer method safely ---@param fn string # key of explorer @@ -24,68 +22,6 @@ local function explorer_fn(fn, ...) end end ---- Update the tree root to a directory or the directory containing ----@param path string relative or absolute ----@param bufnr number|nil -function M.change_root(path, bufnr) - -- skip if current file is in ignore_list - if type(bufnr) == "number" then - local ft - - if vim.fn.has("nvim-0.10") == 1 then - ft = vim.api.nvim_get_option_value("filetype", { buf = bufnr }) or "" - else - ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or "" ---@diagnostic disable-line: deprecated - end - - for _, value in pairs(config.g.update_focused_file.update_root.ignore_list) do - if utils.str_find(path, value) or utils.str_find(ft, value) then - return - end - end - end - - -- don't find inexistent - if vim.fn.filereadable(path) == 0 then - return - end - - local cwd = core.get_cwd() - if cwd == nil then - return - end - - local vim_cwd = vim.fn.getcwd() - - -- test if in vim_cwd - if utils.path_relative(path, vim_cwd) ~= path then - if vim_cwd ~= cwd then - explorer_fn("change_dir", vim_cwd) - end - return - end - -- test if in cwd - if utils.path_relative(path, cwd) ~= path then - return - end - - -- otherwise test M.init_root - if config.g.prefer_startup_root and utils.path_relative(path, M.init_root) ~= path then - explorer_fn("change_dir", M.init_root) - return - end - -- otherwise root_dirs - for _, dir in pairs(config.g.root_dirs) do - dir = vim.fn.fnamemodify(dir, ":p") - if utils.path_relative(path, dir) ~= path then - explorer_fn("change_dir", dir) - return - end - end - -- finally fall back to the folder containing the file - explorer_fn("change_dir", vim.fn.fnamemodify(path, ":p:h")) -end - function M.tab_enter() if view.is_visible({ any_tabpage = true }) then local bufname = vim.api.nvim_buf_get_name(0) @@ -275,8 +211,6 @@ function M.setup(config_user) return end - M.init_root = vim.fn.getcwd() - config.setup(config_user) manage_netrw() diff --git a/lua/nvim-tree/actions/tree/change-root.lua b/lua/nvim-tree/actions/tree/change-root.lua new file mode 100644 index 00000000000..81bdc39f353 --- /dev/null +++ b/lua/nvim-tree/actions/tree/change-root.lua @@ -0,0 +1,74 @@ +local utils = require("nvim-tree.utils") +local core = require("nvim-tree.core") +local config = require("nvim-tree.config") + +local M = {} + +--- Update the tree root to a directory or the directory containing +---@param path string relative or absolute +---@param bufnr number|nil +function M.fn(path, bufnr) + -- skip if current file is in ignore_list + if type(bufnr) == "number" then + local ft + + if vim.fn.has("nvim-0.10") == 1 then + ft = vim.api.nvim_get_option_value("filetype", { buf = bufnr }) or "" + else + ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or "" ---@diagnostic disable-line: deprecated + end + + for _, value in pairs(config.g.update_focused_file.update_root.ignore_list) do + if utils.str_find(path, value) or utils.str_find(ft, value) then + return + end + end + end + + -- don't find inexistent + if vim.fn.filereadable(path) == 0 then + return + end + + local cwd = core.get_cwd() + if cwd == nil then + return + end + + local vim_cwd = vim.fn.getcwd() + + local explorer = core.get_explorer() + if not explorer then + return + end + + -- test if in vim_cwd + if utils.path_relative(path, vim_cwd) ~= path then + if vim_cwd ~= cwd then + explorer:change_dir(vim_cwd) + end + return + end + -- test if in cwd + if utils.path_relative(path, cwd) ~= path then + return + end + + -- otherwise test init_root + if config.g.prefer_startup_root and utils.path_relative(path, config.init_root) ~= path then + explorer:change_dir(config.init_root) + return + end + -- otherwise root_dirs + for _, dir in pairs(config.g.root_dirs) do + dir = vim.fn.fnamemodify(dir, ":p") + if utils.path_relative(path, dir) ~= path then + explorer:change_dir(dir) + return + end + end + -- finally fall back to the folder containing the file + explorer:change_dir(vim.fn.fnamemodify(path, ":p:h")) +end + +return M diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index d58c9cbfb51..deab9d1cdee 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -3,6 +3,7 @@ local lib = require("nvim-tree.lib") local view = require("nvim-tree.view") local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") +local change_root = require("nvim-tree.actions.tree.change-root") local M = {} @@ -58,7 +59,7 @@ function M.fn(opts) -- update root if opts.update_root or config.g.update_focused_file.update_root.enable then - require("nvim-tree").change_root(path, bufnr) + change_root.fn(path, bufnr) end -- find diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index 5244706a033..6e410521a6f 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -2,6 +2,7 @@ local lib = require("nvim-tree.lib") local view = require("nvim-tree.view") local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") +local change_root = require("nvim-tree.actions.tree.change-root") local M = {} @@ -41,7 +42,7 @@ function M.fn(opts) if config.g.update_focused_file.enable or opts.find_file then -- update root if opts.update_root then - require("nvim-tree").change_root(previous_path, previous_buf) + change_root.fn(previous_path, previous_buf) end -- find diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index f38b0999d90..8be5e7f3e4d 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -2,6 +2,7 @@ local lib = require("nvim-tree.lib") local view = require("nvim-tree.view") local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") +local change_root = require("nvim-tree.actions.tree.change-root") local M = {} @@ -56,7 +57,7 @@ function M.fn(opts, no_focus, cwd, bang) if config.g.update_focused_file.enable or opts.find_file then -- update root if opts.update_root then - require("nvim-tree").change_root(previous_path, previous_buf) + change_root.fn(previous_path, previous_buf) end -- find diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 5c3403d36d7..63182fc8a62 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -17,7 +17,11 @@ local M = { ---Immutable OS, detected once on first require ---@type table<"unix"|"macos"|"wsl"|"windows", boolean> - os = nil + os = nil, + + ---Nvim cwd at setup time + ---@type string + init_root = "", } M.os = { @@ -554,6 +558,9 @@ function M.setup(u) -- process merged config process_config(M.g) + + -- store cwd + M.init_root = vim.fn.getcwd() end ---Deep clone defaults From 679af09a2d7010c38d72f639357c759006170fff Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Mar 2026 15:21:39 +1100 Subject: [PATCH 2/7] perf(#3253): extract open_on_directory --- lua/nvim-tree.lua | 39 +++++------------------------ lua/nvim-tree/actions/tree/open.lua | 24 ++++++++++++++++++ lua/nvim-tree/explorer/init.lua | 1 - 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index ff13b9fbdac..a01569c406b 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -11,17 +11,6 @@ local config = require("nvim-tree.config") local M = {} ---- Helper function to execute some explorer method safely ----@param fn string # key of explorer ----@param ... any|nil ----@return function|nil -local function explorer_fn(fn, ...) - local explorer = core.get_explorer() - if explorer then - return explorer[fn](explorer, ...) - end -end - function M.tab_enter() if view.is_visible({ any_tabpage = true }) then local bufname = vim.api.nvim_buf_get_name(0) @@ -47,27 +36,6 @@ function M.tab_enter() end end -function M.open_on_directory() - local should_proceed = config.g.hijack_directories.auto_open or view.is_visible() - if not should_proceed then - return - end - - local buf = vim.api.nvim_get_current_buf() - local bufname = vim.api.nvim_buf_get_name(buf) - if vim.fn.isdirectory(bufname) ~= 1 then - return - end - - - local explorer = core.get_explorer() - if not explorer then - core.init(bufname) - end - - explorer_fn("force_dirchange", bufname, true, false) -end - local function manage_netrw() if config.g.hijack_netrw then vim.cmd("silent! autocmd! FileExplorer *") @@ -126,7 +94,12 @@ local function setup_autocommands() end if config.g.hijack_directories.enable and (config.g.disable_netrw or config.g.hijack_netrw) then - create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory, nested = true }) + create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { + callback = function() + require("nvim-tree.actions.tree.open").open_on_directory() + end, + nested = true + }) end if config.g.view.centralize_selection then diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index 6e410521a6f..aa3cfe23e4a 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -1,5 +1,6 @@ local lib = require("nvim-tree.lib") local view = require("nvim-tree.view") +local core = require("nvim-tree.core") local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") local change_root = require("nvim-tree.actions.tree.change-root") @@ -50,4 +51,27 @@ function M.fn(opts) end end +function M.open_on_directory() + local should_proceed = config.g.hijack_directories.auto_open or view.is_visible() + if not should_proceed then + return + end + + local buf = vim.api.nvim_get_current_buf() + local bufname = vim.api.nvim_buf_get_name(buf) + if vim.fn.isdirectory(bufname) ~= 1 then + return + end + + -- instantiate an explorer if there is not one + if not core.get_explorer() then + core.init(bufname) + end + + local explorer = core.get_explorer() + if explorer then + explorer:force_dirchange(bufname, true, false) + end +end + return M diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index d33be5f10bf..03e028e13a8 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -771,7 +771,6 @@ function Explorer:cd(global, path) vim.cmd((global and "cd " or "lcd ") .. vim.fn.fnameescape(path)) end ----@private ---@param foldername string ---@param should_open_view boolean|nil ---@param should_init boolean|nil From 6759b4b3ba6474702c32e69076337505f055ee8e Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Mar 2026 15:25:37 +1100 Subject: [PATCH 3/7] perf(#3253): extract tab_enter --- lua/nvim-tree.lua | 29 +++-------------------------- lua/nvim-tree/actions/tree/open.lua | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a01569c406b..80b67160b4c 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -11,31 +11,6 @@ local config = require("nvim-tree.config") local M = {} -function M.tab_enter() - if view.is_visible({ any_tabpage = true }) then - local bufname = vim.api.nvim_buf_get_name(0) - - local ft - if vim.fn.has("nvim-0.10") == 1 then - ft = vim.api.nvim_get_option_value("filetype", { buf = 0 }) or "" - else - ft = vim.api.nvim_buf_get_option(0, "ft") ---@diagnostic disable-line: deprecated - end - - for _, filter in ipairs(config.g.tab.sync.ignore) do - if bufname:match(filter) ~= nil or ft:match(filter) ~= nil then - return - end - end - view.open({ focus_tree = false }) - - local explorer = core.get_explorer() - if explorer then - explorer.renderer:draw() - end - end -end - local function manage_netrw() if config.g.hijack_netrw then vim.cmd("silent! autocmd! FileExplorer *") @@ -70,7 +45,9 @@ local function setup_autocommands() }) if config.g.tab.sync.open then - create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) }) + create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(function() + require("nvim-tree.actions.tree.open").tab_enter() + end) }) end if config.g.sync_root_with_cwd then create_nvim_tree_autocmd("DirChanged", { diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index aa3cfe23e4a..7926edee382 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -74,4 +74,29 @@ function M.open_on_directory() end end +function M.tab_enter() + if view.is_visible({ any_tabpage = true }) then + local bufname = vim.api.nvim_buf_get_name(0) + + local ft + if vim.fn.has("nvim-0.10") == 1 then + ft = vim.api.nvim_get_option_value("filetype", { buf = 0 }) or "" + else + ft = vim.api.nvim_buf_get_option(0, "ft") ---@diagnostic disable-line: deprecated + end + + for _, filter in ipairs(config.g.tab.sync.ignore) do + if bufname:match(filter) ~= nil or ft:match(filter) ~= nil then + return + end + end + view.open({ focus_tree = false }) + + local explorer = core.get_explorer() + if explorer then + explorer.renderer:draw() + end + end +end + return M From f75fbfd31f5c822e1d438b1610be50cb6f2c2af7 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Mar 2026 15:38:15 +1100 Subject: [PATCH 4/7] perf(#3253): move requires inline --- lua/nvim-tree.lua | 81 ++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 80b67160b4c..77f42516ed7 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -1,11 +1,5 @@ local api = require("nvim-tree.api") local log = require("nvim-tree.log") -local view = require("nvim-tree.view") -local utils = require("nvim-tree.utils") -local find_file = require("nvim-tree.actions.tree.find-file") -local change_dir = require("nvim-tree.actions.tree.change-dir") -local full_name = require("nvim-tree.renderer.components.full-name") -local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") local config = require("nvim-tree.config") @@ -24,18 +18,16 @@ end local function setup_autocommands() local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true }) - local function create_nvim_tree_autocmd(name, custom_opts) - local default_opts = { group = augroup_id } - vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) - end -- prevent new opened file from opening in the same window as nvim-tree - create_nvim_tree_autocmd("BufWipeout", { + vim.api.nvim_create_autocmd("BufWipeout", { + group = augroup_id, pattern = "NvimTree_*", callback = function() - if not utils.is_nvim_tree_buf(0) then + if not require("nvim-tree.utils").is_nvim_tree_buf(0) then return end + local view = require("nvim-tree.view") if config.g.actions.open_file.eject then view._prevent_buffer_override() else @@ -45,33 +37,39 @@ local function setup_autocommands() }) if config.g.tab.sync.open then - create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(function() - require("nvim-tree.actions.tree.open").tab_enter() - end) }) + vim.api.nvim_create_autocmd("TabEnter", { + group = augroup_id, + callback = vim.schedule_wrap(function() + require("nvim-tree.actions.tree.open").tab_enter() + end) + }) end if config.g.sync_root_with_cwd then - create_nvim_tree_autocmd("DirChanged", { + vim.api.nvim_create_autocmd("DirChanged", { + group = augroup_id, callback = function() - change_dir.fn(vim.loop.cwd()) + require("nvim-tree.actions.tree.change-dir").fn(vim.loop.cwd()) end, }) end if config.g.update_focused_file.enable then - create_nvim_tree_autocmd("BufEnter", { + vim.api.nvim_create_autocmd("BufEnter", { + group = augroup_id, callback = function(event) local exclude = config.g.update_focused_file.exclude if type(exclude) == "function" and exclude(event) then return end - utils.debounce("BufEnter:find_file", config.g.view.debounce_delay, function() - find_file.fn() + require("nvim-tree.utils").debounce("BufEnter:find_file", config.g.view.debounce_delay, function() + require("nvim-tree.actions.tree.find-file").fn() end) end, }) end if config.g.hijack_directories.enable and (config.g.disable_netrw or config.g.hijack_netrw) then - create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { + vim.api.nvim_create_autocmd({ "BufEnter", "BufNewFile" }, { + group = augroup_id, callback = function() require("nvim-tree.actions.tree.open").open_on_directory() end, @@ -80,30 +78,31 @@ local function setup_autocommands() end if config.g.view.centralize_selection then - create_nvim_tree_autocmd("BufEnter", { + vim.api.nvim_create_autocmd("BufEnter", { + group = augroup_id, pattern = "NvimTree_*", - callback = function() - vim.schedule(function() - vim.api.nvim_buf_call(0, function() - local is_term_mode = vim.api.nvim_get_mode().mode == "t" - if is_term_mode then - return - end - vim.cmd([[norm! zz]]) - end) + callback = vim.schedule_wrap(function() + vim.api.nvim_buf_call(0, function() + local is_term_mode = vim.api.nvim_get_mode().mode == "t" + if is_term_mode then + return + end + vim.cmd([[norm! zz]]) end) - end, + end) }) end if config.g.diagnostics.enable then - create_nvim_tree_autocmd("DiagnosticChanged", { + vim.api.nvim_create_autocmd("DiagnosticChanged", { + group = augroup_id, callback = function(ev) log.line("diagnostics", "DiagnosticChanged") require("nvim-tree.diagnostics").update_lsp(ev) end, }) - create_nvim_tree_autocmd("User", { + vim.api.nvim_create_autocmd("User", { + group = augroup_id, pattern = "CocDiagnosticChange", callback = function() log.line("diagnostics", "CocDiagnosticChange") @@ -113,18 +112,20 @@ local function setup_autocommands() end if config.g.view.float.enable and config.g.view.float.quit_on_focus_loss then - create_nvim_tree_autocmd("WinLeave", { + vim.api.nvim_create_autocmd("WinLeave", { + group = augroup_id, pattern = "NvimTree_*", callback = function() - if utils.is_nvim_tree_buf(0) then - view.close() + if require("nvim-tree.utils").is_nvim_tree_buf(0) then + require("nvim-tree.view").close() end end, }) end -- Handles event dispatch when tree is closed by `:q` - create_nvim_tree_autocmd("WinClosed", { + vim.api.nvim_create_autocmd("WinClosed", { + group = augroup_id, pattern = "*", ---@param ev vim.api.keyset.create_autocmd.callback_args callback = function(ev) @@ -138,10 +139,12 @@ local function setup_autocommands() }) -- renderer.full name - full_name.setup_autocommands() + require("nvim-tree.renderer.components.full-name").setup_autocommands() end function M.purge_all_state() + local view = require("nvim-tree.view") + local core = require("nvim-tree.core") view.close_all_tabs() view.abandon_all_windows() local explorer = core.get_explorer() From 7d5ed742b33dcb9de4c3842cb977fa2405251240 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Mar 2026 15:50:59 +1100 Subject: [PATCH 5/7] perf(#3253): extract au find-file, view --- lua/nvim-tree.lua | 34 +++++++----------------- lua/nvim-tree/actions/tree/find-file.lua | 12 +++++++++ lua/nvim-tree/diagnostics.lua | 2 ++ lua/nvim-tree/view.lua | 13 +++++++++ 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 77f42516ed7..caab17a023b 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -1,6 +1,3 @@ -local api = require("nvim-tree.api") -local log = require("nvim-tree.log") -local notify = require("nvim-tree.notify") local config = require("nvim-tree.config") local M = {} @@ -19,20 +16,11 @@ end local function setup_autocommands() local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true }) - -- prevent new opened file from opening in the same window as nvim-tree vim.api.nvim_create_autocmd("BufWipeout", { group = augroup_id, pattern = "NvimTree_*", callback = function() - if not require("nvim-tree.utils").is_nvim_tree_buf(0) then - return - end - local view = require("nvim-tree.view") - if config.g.actions.open_file.eject then - view._prevent_buffer_override() - else - view.abandon_current_window() - end + require("nvim-tree.view").wipeout() end, }) @@ -44,6 +32,7 @@ local function setup_autocommands() end) }) end + if config.g.sync_root_with_cwd then vim.api.nvim_create_autocmd("DirChanged", { group = augroup_id, @@ -52,17 +41,12 @@ local function setup_autocommands() end, }) end + if config.g.update_focused_file.enable then vim.api.nvim_create_autocmd("BufEnter", { group = augroup_id, callback = function(event) - local exclude = config.g.update_focused_file.exclude - if type(exclude) == "function" and exclude(event) then - return - end - require("nvim-tree.utils").debounce("BufEnter:find_file", config.g.view.debounce_delay, function() - require("nvim-tree.actions.tree.find-file").fn() - end) + require("nvim-tree.actions.tree.find-file").buf_enter(event) end, }) end @@ -97,15 +81,14 @@ local function setup_autocommands() vim.api.nvim_create_autocmd("DiagnosticChanged", { group = augroup_id, callback = function(ev) - log.line("diagnostics", "DiagnosticChanged") require("nvim-tree.diagnostics").update_lsp(ev) end, }) + vim.api.nvim_create_autocmd("User", { group = augroup_id, pattern = "CocDiagnosticChange", callback = function() - log.line("diagnostics", "CocDiagnosticChange") require("nvim-tree.diagnostics").update_coc() end, }) @@ -145,6 +128,7 @@ end function M.purge_all_state() local view = require("nvim-tree.view") local core = require("nvim-tree.core") + view.close_all_tabs() view.abandon_all_windows() local explorer = core.get_explorer() @@ -159,8 +143,10 @@ end ---@param config_user? nvim_tree.config user supplied subset of config function M.setup(config_user) + local log = require("nvim-tree.log") + if vim.fn.has("nvim-0.9") == 0 then - notify.warn("nvim-tree.lua requires Neovim 0.9 or higher") + require("nvim-tree.notify").warn("nvim-tree.lua requires Neovim 0.9 or higher") return end @@ -187,7 +173,7 @@ function M.setup(config_user) vim.g.NvimTreeSetup = 1 vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" }) - require("nvim-tree.api.impl").hydrate_post_setup(api) + require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api")) end vim.g.NvimTreeRequired = 1 diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index deab9d1cdee..99d771bdfaa 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -1,5 +1,6 @@ local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") +local utils = require("nvim-tree.utils") local view = require("nvim-tree.view") local config = require("nvim-tree.config") local finders_find_file = require("nvim-tree.actions.finders.find-file") @@ -66,4 +67,15 @@ function M.fn(opts) finders_find_file.fn(path) end +---@param event vim.api.keyset.create_autocmd.callback_args +function M.buf_enter(event) + local exclude = config.g.update_focused_file.exclude + if type(exclude) == "function" and exclude(event) then + return + end + utils.debounce("BufEnter:find_file", config.g.view.debounce_delay, function() + M.fn() + end) +end + return M diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index 3c8c527e2b7..c4a6707026c 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -128,6 +128,7 @@ end ---On disabling LSP, a reset event will be sent for all buffers. ---@param ev table standard event with data.diagnostics populated function M.update_lsp(ev) + log.line("diagnostics", "DiagnosticChanged") if not config.g.diagnostics.enable or not ev or not ev.data or not ev.data.diagnostics then return end @@ -174,6 +175,7 @@ end ---Fired on CocDiagnosticChanged events: ---debounced retrieval, cache update, version increment and draw function M.update_coc() + log.line("diagnostics", "CocDiagnosticChange") if not config.g.diagnostics.enable then return end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index f0fc8a21faa..0b2546b8d36 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -639,4 +639,17 @@ function M.is_width_determined() return type(M.View.width) ~= "function" end +---Called on BufWipeout +---Prevent new opened file from opening in the same window as nvim-tree +function M.wipeout() + if not utils.is_nvim_tree_buf(0) then + return + end + if config.g.actions.open_file.eject then + M._prevent_buffer_override() + else + M.abandon_current_window() + end +end + return M From 5c63c350a6948e4f6d0050e8fd68818e18d71dbf Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Mar 2026 16:01:58 +1100 Subject: [PATCH 6/7] perf(#3253): extract manage_netrw --- lua/nvim-tree.lua | 13 ------------- lua/nvim-tree/config.lua | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index caab17a023b..7ccd723ddad 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -2,17 +2,6 @@ local config = require("nvim-tree.config") local M = {} -local function manage_netrw() - if config.g.hijack_netrw then - vim.cmd("silent! autocmd! FileExplorer *") - vim.cmd("autocmd VimEnter * ++once silent! autocmd! FileExplorer *") - end - if config.g.disable_netrw then - vim.g.loaded_netrw = 1 - vim.g.loaded_netrwPlugin = 1 - end -end - local function setup_autocommands() local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true }) @@ -152,8 +141,6 @@ function M.setup(config_user) config.setup(config_user) - manage_netrw() - require("nvim-tree.log").start() if log.enabled("config") then diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 63182fc8a62..56c02e9caef 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -532,6 +532,19 @@ local function process_config(g) end end +---Hijack and disable netrw if (globally) configured +---@param g nvim_tree.config +local function manage_netrw(g) + if g.hijack_netrw then + vim.cmd("silent! autocmd! FileExplorer *") + vim.cmd("autocmd VimEnter * ++once silent! autocmd! FileExplorer *") + end + if g.disable_netrw then + vim.g.loaded_netrw = 1 + vim.g.loaded_netrwPlugin = 1 + end +end + ---Validate user config and migrate legacy. ---Merge with M.d and persist as M.g ---When no user config M.g is set to M.d and M.u is set to nil @@ -559,6 +572,9 @@ function M.setup(u) -- process merged config process_config(M.g) + -- deal with netrw + manage_netrw(M.g) + -- store cwd M.init_root = vim.fn.getcwd() end From b0abdf2f5d52f475494b7580b44379a2c9991169 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 28 Mar 2026 16:27:46 +1100 Subject: [PATCH 7/7] perf(#3253): extract purge_all_state and document --- lua/nvim-tree.lua | 35 ++++++++++++++--------------------- lua/nvim-tree/core.lua | 14 ++++++++++++++ lua/nvim-tree/watcher.lua | 2 +- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 7ccd723ddad..2965547b5b9 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -114,53 +114,46 @@ local function setup_autocommands() require("nvim-tree.renderer.components.full-name").setup_autocommands() end -function M.purge_all_state() - local view = require("nvim-tree.view") - local core = require("nvim-tree.core") - - view.close_all_tabs() - view.abandon_all_windows() - local explorer = core.get_explorer() - if explorer then - require("nvim-tree.git").purge_state() - explorer:destroy() - core.reset_explorer() - end - -- purge orphaned that were not destroyed by their nodes - require("nvim-tree.watcher").purge_watchers() -end - ----@param config_user? nvim_tree.config user supplied subset of config +---`require("nvim-tree").setup` must be called once to initialise nvim-tree. +---Call again to apply a change in configuration without restarting Nvim. +---@param config_user? nvim_tree.config subset, uses defaults when nil function M.setup(config_user) local log = require("nvim-tree.log") + -- Nvim version check if vim.fn.has("nvim-0.9") == 0 then require("nvim-tree.notify").warn("nvim-tree.lua requires Neovim 0.9 or higher") return end + -- validate and merge with defaults as config.g config.setup(config_user) + -- optionally create the log file require("nvim-tree.log").start() + -- optionally log the configuration if log.enabled("config") then log.line("config", "default config + user") log.raw("config", "%s\n", vim.inspect(config.g)) end + -- idempotent highlight definition require("nvim-tree.appearance").set_hl() + -- idempotent au (re)definition setup_autocommands() + -- subsequent calls to setup clear all state if vim.g.NvimTreeSetup == 1 then - -- subsequent calls to setup - M.purge_all_state() + require("nvim-tree.core").purge_all_state() end + -- hydrate post setup API + require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api")) + vim.g.NvimTreeSetup = 1 vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" }) - - require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api")) end vim.g.NvimTreeRequired = 1 diff --git a/lua/nvim-tree/core.lua b/lua/nvim-tree/core.lua index 60d4a0a6f6a..6b4770ac215 100644 --- a/lua/nvim-tree/core.lua +++ b/lua/nvim-tree/core.lua @@ -2,6 +2,8 @@ local events = require("nvim-tree.events") local notify = require("nvim-tree.notify") local view = require("nvim-tree.view") local log = require("nvim-tree.log") +local git = require("nvim-tree.git") +local watcher = require("nvim-tree.watcher") local M = {} @@ -64,4 +66,16 @@ function M.get_nodes_starting_line() return offset end +function M.purge_all_state() + view.close_all_tabs() + view.abandon_all_windows() + if TreeExplorer then + git.purge_state() + TreeExplorer:destroy() + M.reset_explorer() + end + -- purge orphaned that were not destroyed by their nodes + watcher.purge_watchers() +end + return M diff --git a/lua/nvim-tree/watcher.lua b/lua/nvim-tree/watcher.lua index b29988538ab..d881af318cc 100644 --- a/lua/nvim-tree/watcher.lua +++ b/lua/nvim-tree/watcher.lua @@ -238,7 +238,7 @@ M.Watcher = Watcher function M.disable_watchers(msg) notify.warn(string.format("Disabling watchers: %s", msg)) config.g.filesystem_watchers.enable = false - require("nvim-tree").purge_all_state() + require("nvim-tree.core").purge_all_state() end function M.purge_watchers()