From fe59d63f48725e032ca18cd05304ce790e106680 Mon Sep 17 00:00:00 2001 From: tomo-2525 Date: Sun, 22 Nov 2026 15:01:33 +0900 Subject: [PATCH 1/3] feat(#3394): experimental async first git load --- doc/nvim-tree-lua.txt | 14 ++ lua/nvim-tree/_meta/config/experimental.lua | 9 ++ lua/nvim-tree/config.lua | 1 + lua/nvim-tree/git/init.lua | 143 ++++++++++++++++---- 4 files changed, 142 insertions(+), 25 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 3e740f5a4ce..04f3f5b2976 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1918,6 +1918,19 @@ Config: experimental *nvim-tree-config-experimental* In the event of a problem please disable the experiment and raise an issue. + Fields: ~ + • {git_async_load}? (`boolean`, default: `false`) Load the git status + of a project in the background the first time the + project is shown, instead of blocking until + `git status` completes. The tree is drawn without + git status, which is filled in when `git status` + completes. Until then the git filters of + |nvim_tree.config.filters| see no status: + {git_ignored} files are shown and {git_clean} hides + every file, so + |nvim_tree.config.update_focused_file| cannot find + a file that is only shown once the status is known. + ============================================================================== Config: log *nvim-tree-config-log* @@ -2223,6 +2236,7 @@ Following is the default configuration, see |nvim_tree.config| for details. >lua persist = false, }, experimental = { + git_async_load = false, }, log = { enable = false, diff --git a/lua/nvim-tree/_meta/config/experimental.lua b/lua/nvim-tree/_meta/config/experimental.lua index da8467cf0a3..a0478430e07 100644 --- a/lua/nvim-tree/_meta/config/experimental.lua +++ b/lua/nvim-tree/_meta/config/experimental.lua @@ -9,6 +9,15 @@ error("Cannot require a meta file") --- ---@class nvim_tree.config.experimental --- +---Load the git status of a project in the background the first time the project is shown, +---instead of blocking until `git status` completes. The tree is drawn without git status, +---which is filled in when `git status` completes. Until then the git filters of +---|nvim_tree.config.filters| see no status: {git_ignored} files are shown and {git_clean} +---hides every file, so |nvim_tree.config.update_focused_file| cannot find a file that is +---only shown once the status is known. +---(default: `false`) +---@field git_async_load? boolean +--- --Example below for future reference: -- --Buffers opened by nvim-tree will use with relative paths instead of absolute. diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 24d87c389a2..2946f7f91af 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -297,6 +297,7 @@ M.d = { -- config-default-start persist = false, }, experimental = { + git_async_load = false, }, log = { enable = false, diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 1078c2cd682..f45b54cea22 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -252,6 +252,119 @@ local function reload_tree_at(toplevel) end) end +---Watch the files under the git dir of a project that signal a status change, see WATCHED_FILES. +---Returns nil when filesystem watchers are disabled. +---@param toplevel string +---@return Watcher? +local function create_git_watcher(toplevel) + if not config.g.filesystem_watchers.enable then + return nil + end + + log.line("watcher", "git start") + + ---@param w Watcher + local callback = function(w) + log.line("watcher", "git event scheduled '%s'", w.data.toplevel) + utils.debounce("git:watcher:" .. w.data.toplevel, config.g.filesystem_watchers.debounce_delay, function() + if w.destroyed then + return + end + reload_tree_at(w.data.toplevel) + end) + end + + local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join({ toplevel, ".git" }) + return Watcher:create({ + path = git_dir, + files = WATCHED_FILES, + callback = callback, + data = { + toplevel = toplevel, + } + }) +end + +---Reload every explored directory inside a project, so that the git filters and statuses use its current status. +---Updating the statuses alone, as reload_tree_at does, is not enough after the first load: +---the directories were explored with an empty status, so git_ignored files were not filtered out. +---Nested projects are left alone, they have their own status. +---@param toplevel string +local function reload_project_nodes(toplevel) + local explorer = require("nvim-tree.core").get_explorer() + if not explorer then + return + end + + local project = M._projects_by_toplevel[toplevel] + + -- reloading may remove lines above the cursor, e.g. git_ignored files, so keep the cursor on its node, not its line + local cursor_node = explorer:get_node_at_cursor() + + -- same walk as Explorer:refresh_nodes: open directories and grouped directories + Iterator.builder({ explorer }) + :applier(function(n) + local dir = n:as(DirectoryNode) + -- skip the closed directories that have never been explored: they will be explored with the loaded status + -- when opened, and reloading them now would explore them and change their grouping. + -- The root and open directories are always reloaded: with filters.git_clean every child was filtered out + -- against the empty status, so they may have no children yet. + if dir + and (not dir.parent or dir.open or #dir.nodes > 0 or dir.group_next) + and M.get_toplevel(dir.cwd or dir.link_to or dir.absolute_path) == toplevel then + explorer:reload(dir, project) + end + end) + :recursor(function(n) + return n.group_next and { n.group_next } or (n.open and n.nodes) + end) + :iterate() + + explorer.renderer:draw() + + -- moves to the nearest visible parent when the node itself is now filtered out + if cursor_node then + explorer:focus_node_or_parent(cursor_node) + end +end + +---First load of a project without blocking: experimental.git_async_load +---Registers an empty project straight away and returns it, so the tree can be drawn before git status completes. +---The registered project also marks the load as in flight: later load_project calls return it instead of +---starting another git status, and reload_project can run against it as its files are never nil. +---When git status completes the project is filled in and its explored directories are reloaded. +---@param toplevel string +---@return nvim_tree.git.Project empty until git status completes +local function load_project_async(toplevel) + ---@type nvim_tree.git.Project + local project = { + files = {}, + dirs = git_utils.project_files_to_project_dirs({}, toplevel), + watcher = create_git_watcher(toplevel), + } + M._projects_by_toplevel[toplevel] = project + + GitRunner:run({ + toplevel = toplevel, + list_untracked = git_utils.should_show_untracked(toplevel), + list_ignored = true, + timeout = config.g.git.timeout, + callback = function(path_xys) + -- while git ran, git integration may have been disabled after too many timeouts, + -- or the state purged and the project loaded again + if not config.g.git.enable or M._projects_by_toplevel[toplevel] ~= project then + return + end + + -- a timed out job delivers the statuses read so far, the same as the synchronous load + reload_git_project(toplevel, nil, project, path_xys) + reload_project_nodes(toplevel) + end, + }) + + return project +end + --- Load the project status for a path. Does nothing when no toplevel for path. --- Only fetches project status when unknown, otherwise returns existing. ---@param path string absolute @@ -272,6 +385,10 @@ function M.load_project(path) return project end + if config.g.experimental.git_async_load then + return load_project_async(toplevel) + end + local path_xys = GitRunner:run({ toplevel = toplevel, list_untracked = git_utils.should_show_untracked(toplevel), @@ -279,31 +396,7 @@ function M.load_project(path) timeout = config.g.git.timeout, }) - local watcher = nil - if config.g.filesystem_watchers.enable then - log.line("watcher", "git start") - - ---@param w Watcher - local callback = function(w) - log.line("watcher", "git event scheduled '%s'", w.data.toplevel) - utils.debounce("git:watcher:" .. w.data.toplevel, config.g.filesystem_watchers.debounce_delay, function() - if w.destroyed then - return - end - reload_tree_at(w.data.toplevel) - end) - end - - local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join({ toplevel, ".git" }) - watcher = Watcher:create({ - path = git_dir, - files = WATCHED_FILES, - callback = callback, - data = { - toplevel = toplevel, - } - }) - end + local watcher = create_git_watcher(toplevel) if path_xys then M._projects_by_toplevel[toplevel] = { From 78b2504bb1687e49863ee39283a954a2121e5ac7 Mon Sep 17 00:00:00 2001 From: tomo-2525 Date: Sun, 22 Nov 2026 15:20:00 +0900 Subject: [PATCH 2/3] fix(#3394): reload closed explored directories after the async git load Closed directories explored while git status was in flight kept the children filtered against the empty status, and reopening them does not explore them again. Walk every explored directory instead of the open ones only. Document that git.timeout still applies and that reloads still block, and add line comments to the changed functions. Co-Authored-By: Claude Opus 5.5 --- doc/nvim-tree-lua.txt | 9 +++++++++ lua/nvim-tree/_meta/config/experimental.lua | 6 ++++++ lua/nvim-tree/git/init.lua | 9 +++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 04f3f5b2976..70215eebde7 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1931,6 +1931,15 @@ Config: experimental *nvim-tree-config-experimental* |nvim_tree.config.update_focused_file| cannot find a file that is only shown once the status is known. + {timeout} of |nvim_tree.config.git| still applies: + a large repository may need a larger {timeout} for + `git status` to complete. + + Only the first load is in the background. Reloads + such as |nvim_tree.api.tree.reload()| still block, + including those that run while the first load is in + progress. + ============================================================================== Config: log *nvim-tree-config-log* diff --git a/lua/nvim-tree/_meta/config/experimental.lua b/lua/nvim-tree/_meta/config/experimental.lua index a0478430e07..8247c79c267 100644 --- a/lua/nvim-tree/_meta/config/experimental.lua +++ b/lua/nvim-tree/_meta/config/experimental.lua @@ -15,6 +15,12 @@ error("Cannot require a meta file") ---|nvim_tree.config.filters| see no status: {git_ignored} files are shown and {git_clean} ---hides every file, so |nvim_tree.config.update_focused_file| cannot find a file that is ---only shown once the status is known. +--- +---{timeout} of |nvim_tree.config.git| still applies: a large repository may need a larger +---{timeout} for `git status` to complete. +--- +---Only the first load is in the background. Reloads such as |nvim_tree.api.tree.reload()| +---still block, including those that run while the first load is in progress. ---(default: `false`) ---@field git_async_load? boolean --- diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index f45b54cea22..9325b26c8a3 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -263,6 +263,7 @@ local function create_git_watcher(toplevel) log.line("watcher", "git start") + -- moved unchanged from load_project, so that the synchronous and asynchronous loads share it ---@param w Watcher local callback = function(w) log.line("watcher", "git event scheduled '%s'", w.data.toplevel) @@ -274,6 +275,7 @@ local function create_git_watcher(toplevel) end) end + -- the git dir is not always /.git e.g. worktrees and submodules local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join({ toplevel, ".git" }) return Watcher:create({ path = git_dir, @@ -301,7 +303,8 @@ local function reload_project_nodes(toplevel) -- reloading may remove lines above the cursor, e.g. git_ignored files, so keep the cursor on its node, not its line local cursor_node = explorer:get_node_at_cursor() - -- same walk as Explorer:refresh_nodes: open directories and grouped directories + -- unlike Explorer:refresh_nodes, also walk into closed directories that have been explored: + -- their children were filtered against the empty status too, and reopening them does not explore them again Iterator.builder({ explorer }) :applier(function(n) local dir = n:as(DirectoryNode) @@ -316,7 +319,7 @@ local function reload_project_nodes(toplevel) end end) :recursor(function(n) - return n.group_next and { n.group_next } or (n.open and n.nodes) + return n.group_next and { n.group_next } or (n.nodes and #n.nodes > 0 and n.nodes) end) :iterate() @@ -385,10 +388,12 @@ function M.load_project(path) return project end + -- experimental: return an empty project now and fill it in when git status completes if config.g.experimental.git_async_load then return load_project_async(toplevel) end + -- block until git status completes local path_xys = GitRunner:run({ toplevel = toplevel, list_untracked = git_utils.should_show_untracked(toplevel), From 3e4186e1037b6bcb8a62ba1712c871429e65cdac Mon Sep 17 00:00:00 2001 From: tomo-2525 Date: Thu, 26 Nov 2026 03:51:00 +0900 Subject: [PATCH 3/3] chore(#3394): remove code comments that belong in PR comments Co-Authored-By: Claude Opus 5.5 --- lua/nvim-tree/git/init.lua | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 9325b26c8a3..0dc90900de7 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -252,8 +252,7 @@ local function reload_tree_at(toplevel) end) end ----Watch the files under the git dir of a project that signal a status change, see WATCHED_FILES. ----Returns nil when filesystem watchers are disabled. +---Watch the files under the git dir of a project that signal a status change. ---@param toplevel string ---@return Watcher? local function create_git_watcher(toplevel) @@ -263,7 +262,6 @@ local function create_git_watcher(toplevel) log.line("watcher", "git start") - -- moved unchanged from load_project, so that the synchronous and asynchronous loads share it ---@param w Watcher local callback = function(w) log.line("watcher", "git event scheduled '%s'", w.data.toplevel) @@ -275,7 +273,6 @@ local function create_git_watcher(toplevel) end) end - -- the git dir is not always /.git e.g. worktrees and submodules local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join({ toplevel, ".git" }) return Watcher:create({ path = git_dir, @@ -287,10 +284,7 @@ local function create_git_watcher(toplevel) }) end ----Reload every explored directory inside a project, so that the git filters and statuses use its current status. ----Updating the statuses alone, as reload_tree_at does, is not enough after the first load: ----the directories were explored with an empty status, so git_ignored files were not filtered out. ----Nested projects are left alone, they have their own status. +---Reload the explored directories of a project with its current status. ---@param toplevel string local function reload_project_nodes(toplevel) local explorer = require("nvim-tree.core").get_explorer() @@ -300,18 +294,11 @@ local function reload_project_nodes(toplevel) local project = M._projects_by_toplevel[toplevel] - -- reloading may remove lines above the cursor, e.g. git_ignored files, so keep the cursor on its node, not its line local cursor_node = explorer:get_node_at_cursor() - -- unlike Explorer:refresh_nodes, also walk into closed directories that have been explored: - -- their children were filtered against the empty status too, and reopening them does not explore them again Iterator.builder({ explorer }) :applier(function(n) local dir = n:as(DirectoryNode) - -- skip the closed directories that have never been explored: they will be explored with the loaded status - -- when opened, and reloading them now would explore them and change their grouping. - -- The root and open directories are always reloaded: with filters.git_clean every child was filtered out - -- against the empty status, so they may have no children yet. if dir and (not dir.parent or dir.open or #dir.nodes > 0 or dir.group_next) and M.get_toplevel(dir.cwd or dir.link_to or dir.absolute_path) == toplevel then @@ -325,17 +312,12 @@ local function reload_project_nodes(toplevel) explorer.renderer:draw() - -- moves to the nearest visible parent when the node itself is now filtered out if cursor_node then explorer:focus_node_or_parent(cursor_node) end end ----First load of a project without blocking: experimental.git_async_load ----Registers an empty project straight away and returns it, so the tree can be drawn before git status completes. ----The registered project also marks the load as in flight: later load_project calls return it instead of ----starting another git status, and reload_project can run against it as its files are never nil. ----When git status completes the project is filled in and its explored directories are reloaded. +---Register an empty project and load its git status in the background. ---@param toplevel string ---@return nvim_tree.git.Project empty until git status completes local function load_project_async(toplevel) @@ -353,13 +335,10 @@ local function load_project_async(toplevel) list_ignored = true, timeout = config.g.git.timeout, callback = function(path_xys) - -- while git ran, git integration may have been disabled after too many timeouts, - -- or the state purged and the project loaded again if not config.g.git.enable or M._projects_by_toplevel[toplevel] ~= project then return end - -- a timed out job delivers the statuses read so far, the same as the synchronous load reload_git_project(toplevel, nil, project, path_xys) reload_project_nodes(toplevel) end, @@ -388,12 +367,10 @@ function M.load_project(path) return project end - -- experimental: return an empty project now and fill it in when git status completes if config.g.experimental.git_async_load then return load_project_async(toplevel) end - -- block until git status completes local path_xys = GitRunner:run({ toplevel = toplevel, list_untracked = git_utils.should_show_untracked(toplevel),