feat(explorer): add filesystem watchers (#1304)

* feat(explorer): add experimental watchers

This commit introduces watchers to update the tree.
This behavior is introduced behind an "filesystem_watchers" option
which should prevent instabilities.
It will become the default at some point.

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Kiyan
2022-06-05 12:39:39 +02:00
committed by GitHub
parent a5793f1edb
commit b0d27c09b6
19 changed files with 379 additions and 38 deletions

View File

@@ -1,5 +1,8 @@
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local git_utils = require "nvim-tree.git.utils"
local Runner = require "nvim-tree.git.runner"
local Watcher = require("nvim-tree.watcher").Watcher
local M = {
config = nil,
@@ -13,22 +16,37 @@ function M.reload()
end
for project_root in pairs(M.projects) do
M.projects[project_root] = {}
local git_status = Runner.run {
project_root = project_root,
list_untracked = git_utils.should_show_untracked(project_root),
list_ignored = true,
timeout = M.config.timeout,
}
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root),
}
M.reload_project(project_root)
end
return M.projects
end
function M.reload_project(project_root)
local project = M.projects[project_root]
if not project or not M.config.enable then
return
end
local watcher = M.projects[project_root].watcher
M.projects[project_root] = {}
local git_status = Runner.run {
project_root = project_root,
list_untracked = git_utils.should_show_untracked(project_root),
list_ignored = true,
timeout = M.config.timeout,
}
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root),
watcher = watcher,
}
end
function M.get_project(project_root)
return M.projects[project_root]
end
function M.get_project_root(cwd)
if M.cwd_to_project_root[cwd] then
return M.cwd_to_project_root[cwd]
@@ -42,6 +60,35 @@ function M.get_project_root(cwd)
return project_root
end
function M.reload_tree_at(project_root)
local root_node = utils.get_node_from_path(project_root)
if not root_node then
return
end
M.reload_project(project_root)
local project = M.get_project(project_root)
local project_files = project.files and project.files or {}
local project_dirs = project.dirs and project.dirs or {}
local function iterate(n)
local parent_ignored = n.git_status == "!!"
for _, node in pairs(n.nodes) do
node.git_status = project_dirs[node.absolute_path] or project_files[node.absolute_path]
if not node.git_status and parent_ignored then
node.git_status = "!!"
end
if node.nodes and #node.nodes > 0 then
iterate(node)
end
end
end
iterate(root_node)
end
function M.load_project_status(cwd)
if not M.config.enable then
return {}
@@ -64,15 +111,32 @@ function M.load_project_status(cwd)
list_ignored = true,
timeout = M.config.timeout,
}
local watcher = nil
if M.config.watcher.enable then
log.line("watcher", "git start")
watcher = Watcher.new {
absolute_path = utils.path_join { project_root, ".git" },
interval = M.config.watcher.interval,
on_event = function()
log.line("watcher", "git event")
M.reload_tree_at(project_root)
require("nvim-tree.renderer").draw()
end,
}
end
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root),
watcher = watcher,
}
return M.projects[project_root]
end
function M.setup(opts)
M.config = opts.git
M.config.watcher = opts.filesystem_watchers
end
return M

View File

@@ -53,7 +53,9 @@ end
function Runner:_log_raw_output(output)
if output and type(output) == "string" then
log.raw("git", "%s", output)
-- TODO put this back after watcher feature completed
-- log.raw("git", "%s", output)
log.line("git", "done")
end
end