start watcher

This commit is contained in:
kiyan42 2020-11-01 17:11:02 +01:00
parent 241937862e
commit 664e9b95c7
3 changed files with 70 additions and 12 deletions

View File

@ -4,6 +4,7 @@
-- change root cwd
local uv = vim.loop
local a = vim.api
local utils = require'nvim-tree.utils'
local M = {}
@ -14,16 +15,10 @@ M.Explorer = {
file_pool = {}
}
local path_sep = vim.fn.has('win32') == 1 and [[\]] or '/'
local function path_join(root, path)
return root..path_sep..path
end
local node_type_funcs = {
directory = {
create = function(parent, name)
local absolute_path = path_join(parent, name)
local absolute_path = utils.path_join(parent, name)
return {
name = name,
absolute_path = absolute_path,
@ -37,7 +32,7 @@ local node_type_funcs = {
},
file = {
create = function(parent, name)
local absolute_path = path_join(parent, name)
local absolute_path = utils.path_join(parent, name)
local executable = uv.fs_access(absolute_path, 'X')
return {
name = name,
@ -49,7 +44,7 @@ local node_type_funcs = {
},
link = {
create = function(parent, name)
local absolute_path = path_join(parent, name)
local absolute_path = utils.path_join(parent, name)
local link_to = uv.fs_realpath(absolute_path)
return {
name = name,
@ -68,6 +63,7 @@ end
function M.Explorer:explore(root)
local cwd = root or self.cwd
local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then
return nil
@ -97,6 +93,11 @@ function M.Explorer:explore(root)
end
end
require'nvim-tree.watcher'.run(self, cwd)
for _, dir in pairs(entries.directory) do
require'nvim-tree.watcher'.run(self, dir.absolute_path)
end
for _, node in pairs(entries.link) do
table.insert(entries.directory, node)
end
@ -145,13 +146,20 @@ function M.Explorer:switch_open_dir(node)
end
end
function M.Explorer:new()
self.cwd = uv.cwd()
function M.Explorer:refresh(files)
return function()
dump(files)
end
end
function M.Explorer:new(cwd)
self.cwd = cwd or uv.cwd()
local entries = self:explore()
if entries then
self.node_tree = require'nvim-tree.git'.gitify(entries)
end
return self
end
@ -159,7 +167,7 @@ function M.configure(opts)
M.config = {
ignore = {},
show_ignored = opts.show_ignored,
ignore_dotfiles = opts.hide_dotfiles
ignore_dotfiles = opts.hide_dotfiles,
}
for _, ignore_pattern in ipairs(opts.ignore) do

9
lua/nvim-tree/utils.lua Normal file
View File

@ -0,0 +1,9 @@
local M = {}
local path_sep = vim.fn.has('win32') == 1 and [[\]] or '/'
function M.path_join(root, path)
return root..path_sep..path
end
return M

41
lua/nvim-tree/watcher.lua Normal file
View File

@ -0,0 +1,41 @@
local utils = require'nvim-tree.utils'
local uv = vim.loop
local M = {}
local watchers = {}
local batch_files = {}
local batch_timer = nil
local function batch_events(explorer, fname)
if batch_timer ~= nil then
batch_timer:stop()
batch_timer:close()
batch_timer = nil
end
batch_files[fname] = fname
batch_timer = uv.new_timer()
batch_timer:start(150, 0, function()
batch_timer:stop()
batch_timer:close()
batch_timer = nil
vim.schedule(explorer:refresh(vim.tbl_values(batch_files)))
batch_files = {}
end)
end
function M.run(explorer, cwd)
if watchers[cwd] then return end
watchers[cwd] = uv.new_fs_event()
uv.fs_event_start(
watchers[cwd],
cwd,
-- recursive not yet available on linux...
-- that sucks, so we have to bind for every dir we explore
{ recursive = false },
function(_, fname) batch_events(explorer, utils.path_join(cwd, fname)) end
)
end
return M