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') a.nvim_out_write(file..' was properly created\n')
end end
events._dispatch_folder_created(file) events._dispatch_folder_created(file)
require'nvim-tree.actions.reloaders'.reload_explorer(function() require'nvim-tree.actions.reloaders'.reload_explorer()
focus_file(file) focus_file(file)
end)
end end
return M return M

View File

@@ -1,7 +1,5 @@
local view = require'nvim-tree.view' local view = require'nvim-tree.view'
local utils = require'nvim-tree.utils' 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 renderer = require"nvim-tree.renderer"
local M = {} local M = {}
@@ -36,15 +34,7 @@ function M.fn(fname)
if not node.open then if not node.open then
node.open = true node.open = true
tree_altered = true tree_altered = true
end TreeExplorer:expand(node)
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
end end
if iterate_nodes(node.nodes) ~= nil then if iterate_nodes(node.nodes) ~= nil then

View File

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

View File

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

View File

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

View File

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

View File

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