Skip to content
Merged
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
125 changes: 100 additions & 25 deletions lua/nvim-tree/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,101 @@ local function reload_tree_at(toplevel)
end)
end

---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)
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 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()
if not explorer then
return
end

local project = M._projects_by_toplevel[toplevel]

local cursor_node = explorer:get_node_at_cursor()
Comment thread
alex-courtis marked this conversation as resolved.
Resolved

Iterator.builder({ explorer })
:applier(function(n)
local dir = n:as(DirectoryNode)
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()

if cursor_node then
explorer:focus_node_or_parent(cursor_node)
end
end

---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)
---@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)
if not config.g.git.enable or M._projects_by_toplevel[toplevel] ~= project then
return
end

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 +367,18 @@ 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({
Comment thread
alex-courtis marked this conversation as resolved.
Resolved
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