Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,28 @@ 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.

{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*

Expand Down Expand Up @@ -2223,6 +2245,7 @@ Following is the default configuration, see |nvim_tree.config| for details. >lua
persist = false,
},
experimental = {
git_async_load = false,
},
log = {
enable = false,
Expand Down
15 changes: 15 additions & 0 deletions lua/nvim-tree/_meta/config/experimental.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ 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.
---
---{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
---
--Example below for future reference:
--
--Buffers opened by nvim-tree will use with relative paths instead of absolute.
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ M.d = { -- config-default-start
persist = false,
},
experimental = {
git_async_load = false,
},
log = {
enable = false,
Expand Down
148 changes: 123 additions & 25 deletions lua/nvim-tree/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,122 @@ 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")

-- 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)
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

-- the git dir is not always <toplevel>/.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,
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()

-- 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 })
Comment thread
tomo-2525 marked this conversation as resolved.
Resolved
: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)
Comment thread
tomo-2525 marked this conversation as resolved.
Resolved
end
end)
:recursor(function(n)
return n.group_next and { n.group_next } or (n.nodes and #n.nodes > 0 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)
Comment thread
tomo-2525 marked this conversation as resolved.
Resolved
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
Expand All @@ -272,38 +388,20 @@ 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),
list_ignored = true,
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] = {
Expand Down
Loading