refacto: make git module interface to wait for job to finish

allows simplify the explore/reload/find/initialization by making the
whole code synchronous. No more callback needed.
This commit is contained in:
kiyan 2022-02-21 19:12:16 +01:00
parent a9fe57c0d8
commit f977e5c05a
7 changed files with 62 additions and 94 deletions

View File

@ -102,9 +102,8 @@ function M.fn(node)
a.nvim_out_write(file..' was properly created\n')
end
events._dispatch_folder_created(file)
require'nvim-tree.actions.reloaders'.reload_explorer(function()
focus_file(file)
end)
require'nvim-tree.actions.reloaders'.reload_explorer()
focus_file(file)
end
return M

View File

@ -1,7 +1,5 @@
local view = require'nvim-tree.view'
local utils = require'nvim-tree.utils'
local explorer_module = require"nvim-tree.explorer"
local git = require"nvim-tree.git"
local renderer = require"nvim-tree.renderer"
local M = {}
@ -36,15 +34,7 @@ function M.fn(fname)
if not node.open then
node.open = true
tree_altered = true
end
if #node.nodes == 0 then
local git_finished = false
git.load_project_status(node.absolute_path, function(status)
explorer_module.explore(node, status)
git_finished = true
end)
while not vim.wait(10, function() return git_finished end, 10) do end
TreeExplorer:expand(node)
end
if iterate_nodes(node.nodes) ~= nil then

View File

@ -33,23 +33,19 @@ function M.reload_node_status(parent_node, projects)
end
local event_running = false
function M.reload_explorer(callback)
function M.reload_explorer()
if event_running or not TreeExplorer or not TreeExplorer.cwd or vim.v.exiting ~= vim.NIL then
return
end
event_running = true
git.reload(function(projects)
refresh_nodes(TreeExplorer, projects)
if view.is_visible() then
renderer.draw()
if callback and type(callback) == 'function' then
callback()
end
end
diagnostics.update()
event_running = false
end)
local projects = git.reload()
refresh_nodes(TreeExplorer, projects)
if view.is_visible() then
renderer.draw()
end
diagnostics.update()
event_running = false
end
function M.reload_git()
@ -58,11 +54,10 @@ function M.reload_git()
end
event_running = true
git.reload(function(projects)
M.reload_node_status(TreeExplorer, projects)
renderer.draw()
event_running = false
end)
local projects = git.reload()
M.reload_node_status(TreeExplorer, projects)
renderer.draw()
event_running = false
end
return M

View File

@ -1,7 +1,6 @@
local uv = vim.loop
local git = require"nvim-tree.git"
local renderer = require"nvim-tree.renderer"
local M = {}
@ -13,33 +12,24 @@ Explorer.__index = Explorer
function Explorer.new(cwd)
cwd = uv.fs_realpath(cwd or uv.cwd())
return setmetatable({
local explorer = setmetatable({
cwd = cwd,
nodes = {}
}, Explorer)
explorer:_load(explorer)
return explorer
end
function Explorer:_load(node)
local cwd = node.cwd or node.link_to or node.absolute_path
git.load_project_status(cwd, function(git_statuses)
M.explore(node, git_statuses)
if type(self.init_cb) == "function" then
self.init_cb(self)
self.init_cb = nil
end
end)
local git_statuses = git.load_project_status(cwd)
M.explore(node, git_statuses)
end
function Explorer:expand(node)
self.init_cb = renderer.draw
self:_load(node)
end
function Explorer:init(f)
self.init_cb = f
self:_load(self)
end
function M.setup(opts)
require"nvim-tree.explorer.utils".setup(opts)
end

View File

@ -7,32 +7,26 @@ local M = {
cwd_to_project_root = {}
}
function M.reload(callback)
local num_projects = vim.tbl_count(M.projects)
if not M.config.enable or num_projects == 0 then
return callback({})
function M.reload()
if not M.config.enable then
return {}
end
local done = 0
for project_root in pairs(M.projects) do
M.projects[project_root] = {}
Runner.run {
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,
on_end = function(git_status)
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
}
done = done + 1
if done == num_projects then
callback(M.projects)
end
end
}
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
}
end
return M.projects
end
function M.get_project_root(cwd)
@ -48,35 +42,33 @@ function M.get_project_root(cwd)
return project_root
end
function M.load_project_status(cwd, callback)
function M.load_project_status(cwd)
if not M.config.enable then
return callback({})
return {}
end
local project_root = M.get_project_root(cwd)
if not project_root then
M.cwd_to_project_root[cwd] = false
return callback({})
return {}
end
local status = M.projects[project_root]
if status then
return callback(status)
return status
end
Runner.run {
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,
on_end = function(git_status)
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
}
callback(M.projects[project_root])
end
timeout = M.config.timeout
}
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
}
return M.projects[project_root]
end
function M.setup(opts)

View File

@ -55,7 +55,8 @@ function Runner:_run_git_job()
local stdout = uv.new_pipe(false)
local timer = uv.new_timer()
local function on_finish(output)
local function on_finish()
self._done = true
if timer:is_closing() or stdout:is_closing() or (handle and handle:is_closing()) then
return
end
@ -68,8 +69,6 @@ function Runner:_run_git_job()
end
pcall(uv.kill, pid)
self.on_end(output or self.output)
end
handle, pid = uv.spawn(
@ -78,7 +77,7 @@ function Runner:_run_git_job()
vim.schedule_wrap(function() on_finish() end)
)
timer:start(self.timeout, 0, vim.schedule_wrap(function() on_finish({}) end))
timer:start(self.timeout, 0, vim.schedule_wrap(function() on_finish() end))
local output_leftover = ''
local function manage_output(err, data)
@ -89,6 +88,10 @@ function Runner:_run_git_job()
uv.read_start(stdout, vim.schedule_wrap(manage_output))
end
function Runner:_wait()
while not vim.wait(30, function() return self._done end, 30) do end
end
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
function Runner.run(opts)
local self = setmetatable({
@ -97,10 +100,12 @@ function Runner.run(opts)
list_ignored = opts.list_ignored,
timeout = opts.timeout or 400,
output = {},
on_end = opts.on_end,
_done = false
}, Runner)
self:_run_git_job()
self:_wait()
return self.output
end
return Runner

View File

@ -15,16 +15,11 @@ local M = {
TreeExplorer = nil
function M.init(foldername)
local init_done = false
TreeExplorer = explorer.Explorer.new(foldername)
TreeExplorer:init(function()
init_done = true
if not first_init_done then
events._dispatch_ready()
first_init_done = true
end
end)
while not vim.wait(10, function() return init_done end, 10) do end
if not first_init_done then
events._dispatch_ready()
first_init_done = true
end
end
local function get_node_at_line(line)
@ -80,13 +75,15 @@ end
function M.expand_or_collapse(node)
node.open = not node.open
if node.has_children then node.has_children = false end
if #node.nodes == 0 then
TreeExplorer:expand(node)
else
renderer.draw()
if node.has_children then
node.has_children = false
end
if #node.nodes == 0 then
TreeExplorer:expand(node)
end
renderer.draw()
diagnostics.update()
end