nvim-tree.lua/lua/nvim-tree/explorer/watch.lua
Alexander Courtis bcb2a5a80d
fix(#1720): .git watch only FETCH_HEAD, HEAD, HEAD.lock, config, index (#1732)
* fix(#1720): .git watch only HEAD, config and index

* fix(#1720): .git watch only FETCH_HEAD, HEAD, HEAD.lock, config, index
2022-11-12 14:38:33 +11:00

93 lines
2.3 KiB
Lua

local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local git = require "nvim-tree.git"
local Watcher = require("nvim-tree.watcher").Watcher
local M = {}
local function reload_and_get_git_project(path)
local project_root = git.get_project_root(path)
git.reload_project(project_root, path)
return project_root, git.get_project(project_root) or {}
end
local function update_parent_statuses(node, project, root)
while project and node and node.absolute_path ~= root do
require("nvim-tree.explorer.common").update_git_status(node, false, project)
node = node.parent
end
end
local function is_git(path)
return vim.fn.fnamemodify(path, ":t") == ".git"
end
local IGNORED_PATHS = {
-- disable watchers on kernel filesystems
-- which have a lot of unwanted events
"/sys",
"/proc",
"/dev",
}
local function is_folder_ignored(path)
for _, folder in ipairs(IGNORED_PATHS) do
if vim.startswith(path, folder) then
return true
end
end
for _, ignore_dir in ipairs(M.ignore_dirs) do
if vim.fn.match(path, ignore_dir) ~= -1 then
return true
end
end
return false
end
function M.refresh_path(path)
log.line("watcher", "node event executing '%s'", path)
local n = utils.get_node_from_path(path)
if not n then
return
end
local node = utils.get_parent_of_group(n)
local project_root, project = reload_and_get_git_project(path)
require("nvim-tree.explorer.reload").reload(node, project)
update_parent_statuses(node, project, project_root)
require("nvim-tree.renderer").draw()
end
function M.create_watcher(absolute_path)
if not M.enabled then
return nil
end
if is_git(absolute_path) or is_folder_ignored(absolute_path) then
return nil
end
local function callback(watcher)
log.line("watcher", "node event scheduled %s", watcher.context)
utils.debounce(watcher.context, M.debounce_delay, function()
M.refresh_path(watcher._path)
end)
end
M.uid = M.uid + 1
return Watcher:new(absolute_path, nil, callback, {
context = "explorer:watch:" .. absolute_path .. ":" .. M.uid,
})
end
function M.setup(opts)
M.enabled = opts.filesystem_watchers.enable
M.debounce_delay = opts.filesystem_watchers.debounce_delay
M.ignore_dirs = opts.filesystem_watchers.ignore_dirs
M.uid = 0
end
return M