chore: move refresh/reloaders into actions.reloaders

This commit is contained in:
kiyan
2022-02-06 16:23:51 +01:00
parent 7b64075bdf
commit 23c95a674f
9 changed files with 84 additions and 73 deletions

View File

@@ -108,7 +108,7 @@ local function do_paste(node, action_type, action_fn)
end
clipboard[action_type] = {}
return lib.refresh_tree()
return require'nvim-tree.actions.reloaders'.reload_explorer()
end
local function do_cut(source, destination)

View File

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

View File

@@ -69,7 +69,7 @@ local keypress_funcs = {
toggle_ignored = lib.toggle_ignored,
toggle_dotfiles = lib.toggle_dotfiles,
toggle_help = lib.toggle_help,
refresh = lib.refresh_tree,
refresh = require'nvim-tree.actions.reloaders'.reload_explorer,
first_sibling = require'nvim-tree.actions.movements'.sibling(-math.huge),
last_sibling = require'nvim-tree.actions.movements'.sibling(math.huge),
prev_sibling = require'nvim-tree.actions.movements'.sibling(-1),

View File

@@ -0,0 +1,68 @@
local git = require "nvim-tree.git"
local diagnostics = require "nvim-tree.diagnostics"
local view = require "nvim-tree.view"
local explorer_module = require'nvim-tree.explorer'
local get_explorer = function() return require "nvim-tree.lib".Tree end
local M = {}
local function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd)
explorer_module.refresh(node.nodes, node.absolute_path or node.cwd, node, projects[project_root] or {})
for _, _node in ipairs(node.nodes) do
if _node.nodes and _node.open then
refresh_nodes(_node, projects)
end
end
end
function M.reload_node_status(parent_node, projects)
local project_root = git.get_project_root(parent_node.absolute_path or parent_node.cwd)
local status = projects[project_root] or {}
for _, node in ipairs(parent_node.nodes) do
if node.nodes then
node.git_status = status.dirs and status.dirs[node.absolute_path]
else
node.git_status = status.files and status.files[node.absolute_path]
end
if node.nodes and #node.nodes > 0 then
M.reload_node_status(node, projects)
end
end
end
local event_running = false
function M.reload_explorer(callback)
local Explorer = get_explorer()
if event_running or not Explorer.cwd or vim.v.exiting ~= vim.NIL then
return
end
event_running = true
git.reload(function(projects)
refresh_nodes(Explorer, projects)
if view.win_open() then
require"nvim-tree.lib".redraw()
if callback and type(callback) == 'function' then
callback()
end
end
diagnostics.update()
event_running = false
end)
end
function M.reload_git()
if not git.config.enable or event_running then
return
end
event_running = true
git.reload(function(projects)
M.reload_node_status(get_explorer(), projects)
require"nvim-tree.lib".redraw()
event_running = false
end)
end
return M

View File

@@ -2,7 +2,6 @@ local a = vim.api
local luv = vim.loop
local utils = require'nvim-tree.utils'
local lib = require'nvim-tree.lib'
local events = require'nvim-tree.events'
local M = {}
@@ -72,7 +71,7 @@ function M.fn(node)
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
end
lib.refresh_tree()
require'nvim-tree.actions.reloaders'.reload_explorer()
end
end

View File

@@ -31,7 +31,7 @@ function M.fn(with_sub)
a.nvim_out_write(node.absolute_path..''..new_name..'\n')
utils.rename_loaded_buffers(node.absolute_path, new_name)
events._dispatch_node_renamed(abs_path, new_name)
lib.refresh_tree()
require'nvim-tree.actions.reloaders'.reload_explorer()
end
end

View File

@@ -8,7 +8,6 @@ local M = {
}
}
local lib = require'nvim-tree.lib'
local utils = require'nvim-tree.utils'
local events = require'nvim-tree.events'
@@ -64,13 +63,13 @@ function M.fn(node)
if node.nodes ~= nil and not node.link_to then
trash_path(function()
events._dispatch_folder_removed(node.absolute_path)
lib.refresh_tree()
require'nvim-tree.actions.reloaders'.reload_explorer()
end)
else
trash_path(function()
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
lib.refresh_tree()
require'nvim-tree.actions.reloaders'.reload_explorer()
end)
end

View File

@@ -112,64 +112,6 @@ function M.expand_or_collapse(node)
diagnostics.update()
end
local function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd)
explorer.refresh(node.nodes, node.absolute_path or node.cwd, node, projects[project_root] or {})
for _, _node in ipairs(node.nodes) do
if _node.nodes and _node.open then
refresh_nodes(_node, projects)
end
end
end
local event_running = false
function M.refresh_tree(callback)
if event_running or not M.Tree.cwd or vim.v.exiting ~= vim.NIL then
return
end
event_running = true
git.reload(function(projects)
refresh_nodes(M.Tree, projects)
if view.win_open() then
M.redraw()
if callback and type(callback) == 'function' then
callback()
end
end
diagnostics.update()
event_running = false
end)
end
local function reload_node_status(parent_node, projects)
local project_root = git.get_project_root(parent_node.absolute_path or parent_node.cwd)
local status = projects[project_root] or {}
for _, node in ipairs(parent_node.nodes) do
if node.nodes then
node.git_status = status.dirs and status.dirs[node.absolute_path]
else
node.git_status = status.files and status.files[node.absolute_path]
end
if node.nodes and #node.nodes > 0 then
reload_node_status(node, projects)
end
end
end
function M.reload_git()
if not git.config.enable or event_running then
return
end
event_running = true
git.reload(function(projects)
reload_node_status(M.Tree, projects)
M.redraw()
event_running = false
end)
end
function M.set_index_and_redraw(fname)
local i
local hide_root_folder = view.View.hide_root_folder
@@ -195,7 +137,7 @@ function M.set_index_and_redraw(fname)
explorer.explore(node.nodes, node.absolute_path, node, {})
git.load_project_status(node.absolute_path, function(status)
if status.dirs or status.files then
reload_node_status(node, git.projects)
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
end
M.redraw()
end)
@@ -254,12 +196,12 @@ end
function M.toggle_ignored()
explorer.config.filter_ignored = not explorer.config.filter_ignored
return M.refresh_tree()
return require'nvim-tree.actions.reloaders'.reload_explorer()
end
function M.toggle_dotfiles()
explorer.config.filter_dotfiles = not explorer.config.filter_dotfiles
return M.refresh_tree()
return require'nvim-tree.actions.reloaders'.reload_explorer()
end
function M.toggle_help()
@@ -273,5 +215,8 @@ M.collapse_all = require'nvim-tree.actions.collapse-all'.fn
M.dir_up = require'nvim-tree.actions.dir-up'.fn
-- @deprecated: use nvim-tree.actions.change-dir.fn
M.change_dir = require'nvim-tree.actions.change-dir'.fn
-- @deprecated: use nvim-tree.actions.reloaders.reload_explorer
M.refresh_tree = require'nvim-tree.actions.reloaders'.reload_explorer
return M