refacto: set tree explorer in the global state

also remove the redraw method and use renderer.draw immediately
This commit is contained in:
kiyan
2022-02-07 22:07:08 +01:00
parent e42a4337d0
commit ea92e7bf7c
18 changed files with 85 additions and 93 deletions

View File

@@ -9,8 +9,8 @@ local M = {
}
function M.fn(name)
local foldername = name == '..' and vim.fn.fnamemodify(lib().Tree.cwd, ':h') or name
local no_cwd_change = vim.fn.expand(foldername) == lib().Tree.cwd
local foldername = name == '..' and vim.fn.fnamemodify(TreeExplorer.cwd, ':h') or name
local no_cwd_change = vim.fn.expand(foldername) == TreeExplorer.cwd
local new_tab = a.nvim_get_current_tabpage()
local is_window = vim.v.event.scope == "window" and new_tab == M.current_tab
if no_cwd_change or is_window then

View File

@@ -1,3 +1,5 @@
local renderer = require"nvim-tree.renderer"
local M = {}
function M.fn()
@@ -12,8 +14,8 @@ function M.fn()
end
end
iter(require'nvim-tree.lib'.Tree.nodes)
require'nvim-tree.lib'.redraw()
iter(TreeExplorer.nodes)
renderer.draw()
end
return M

View File

@@ -158,7 +158,7 @@ end
function M.copy_path(node)
local absolute_path = node.absolute_path
local relative_path = utils.path_relative(absolute_path, lib.Tree.cwd)
local relative_path = utils.path_relative(absolute_path, TreeExplorer.cwd)
local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
return copy_to_clipboard(content)
end

View File

@@ -9,7 +9,7 @@ local M = {}
local function focus_file(file)
local _, i = utils.find_node(
lib.Tree.nodes,
TreeExplorer.nodes,
function(node) return node.absolute_path == file end
)
require'nvim-tree.view'.set_cursor({i+1, 1})
@@ -61,8 +61,8 @@ function M.fn(node)
node = lib.get_last_group_node(node)
if node.name == '..' then
node = {
absolute_path = lib.Tree.cwd,
nodes = lib.Tree.nodes,
absolute_path = TreeExplorer.cwd,
nodes = TreeExplorer.nodes,
open = true,
}
end

View File

@@ -4,7 +4,7 @@ function M.fn(node)
if not node or node.name == ".." then
return require'nvim-tree.actions.change-dir'.fn('..')
else
local newdir = vim.fn.fnamemodify(require'nvim-tree.lib'.Tree.cwd, ':h')
local newdir = vim.fn.fnamemodify(TreeExplorer.cwd, ':h')
require'nvim-tree.actions.change-dir'.fn(newdir)
return require"nvim-tree.actions.find-file".fn(node.absolute_path)
end

View File

@@ -2,21 +2,17 @@ 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 = {}
local function get_explorer()
return require"nvim-tree.lib".Tree
end
function M.fn(fname)
local i
local hide_root_folder = view.View.hide_root_folder
local Explorer = get_explorer()
if not Explorer then
if not TreeExplorer then
return
end
if Explorer.cwd == '/' or hide_root_folder then
if TreeExplorer.cwd == '/' or hide_root_folder then
i = 0
else
i = 1
@@ -40,7 +36,7 @@ function M.fn(fname)
if status.dirs or status.files then
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
end
require"nvim-tree.lib".redraw()
renderer.draw()
end)
end
if node.open == false then
@@ -56,9 +52,9 @@ function M.fn(fname)
end
end
local index = iterate_nodes(Explorer.nodes)
local index = iterate_nodes(TreeExplorer.nodes)
if tree_altered then
require"nvim-tree.lib".redraw()
renderer.draw()
end
if index and view.win_open() then
view.set_cursor({index, 0})

View File

@@ -66,9 +66,9 @@ local keypress_funcs = {
remove = require'nvim-tree.actions.remove-file'.fn,
rename = require'nvim-tree.actions.rename-file'.fn(false),
system_open = require'nvim-tree.actions.system-open'.fn,
toggle_dotfiles = require"nvim-tree.actions.toggle-ignore".dotfiles,
toggle_help = require"nvim-tree.actions.toggle-help".fn,
toggle_ignored = require"nvim-tree.actions.toggle-ignore".ignored,
toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles,
toggle_help = require"nvim-tree.actions.toggles".help,
toggle_ignored = require"nvim-tree.actions.toggles".ignored,
trash = require'nvim-tree.actions.trash'.fn,
}

View File

@@ -2,6 +2,7 @@ local utils = require'nvim-tree.utils'
local view = require'nvim-tree.view'
local diagnostics = require'nvim-tree.diagnostics'
local config = require"nvim-tree.config"
local renderer = require"nvim-tree.renderer"
local lib = function() return require'nvim-tree.lib' end
local M = {}
@@ -44,7 +45,7 @@ function M.parent_node(should_close)
node.open = false
altered_tree = true
else
local line, parent = iter(lib().Tree.nodes, true)
local line, parent = iter(TreeExplorer.nodes, true)
if parent == nil then
line = 1
elseif should_close then
@@ -57,7 +58,7 @@ function M.parent_node(should_close)
if altered_tree then
diagnostics.update()
lib().redraw()
renderer.draw()
end
end
end
@@ -73,16 +74,16 @@ function M.sibling(direction)
local parent, _
-- Check if current node is already at root nodes
for index, _node in ipairs(lib().Tree.nodes) do
for index, _node in ipairs(TreeExplorer.nodes) do
if node_path == _node.absolute_path then
line = index
end
end
if line > 0 then
parent = lib().Tree
parent = TreeExplorer
else
_, parent = iter(lib().Tree.nodes, true)
_, parent = iter(TreeExplorer.nodes, true)
if parent ~= nil and #parent.nodes > 1 then
line, _ = get_line_from_node(node)(parent.nodes)
end
@@ -99,7 +100,7 @@ function M.sibling(direction)
end
local target_node = parent.nodes[index]
line, _ = get_line_from_node(target_node)(lib().Tree.nodes, true)
line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true)
view.set_cursor({line, 0})
end
end

View File

@@ -1,14 +1,11 @@
local git = require "nvim-tree.git"
local diagnostics = require "nvim-tree.diagnostics"
local view = require "nvim-tree.view"
local renderer = require "nvim-tree.renderer"
local explorer_module = require'nvim-tree.explorer'
local M = {}
local function get_explorer()
return require "nvim-tree.lib".Tree
end
local function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd)
explorer_module.reload(node, node.absolute_path or node.cwd, projects[project_root] or {})
@@ -36,16 +33,15 @@ 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
if event_running or not TreeExplorer.cwd or vim.v.exiting ~= vim.NIL then
return
end
event_running = true
git.reload(function(projects)
refresh_nodes(Explorer, projects)
refresh_nodes(TreeExplorer, projects)
if view.win_open() then
require"nvim-tree.lib".redraw()
renderer.draw()
if callback and type(callback) == 'function' then
callback()
end
@@ -62,8 +58,8 @@ function M.reload_git()
event_running = true
git.reload(function(projects)
M.reload_node_status(get_explorer(), projects)
require"nvim-tree.lib".redraw()
M.reload_node_status(TreeExplorer, projects)
renderer.draw()
event_running = false
end)
end

View File

@@ -1,8 +0,0 @@
local M = {}
function M.fn()
require"nvim-tree.view".toggle_help()
return require"nvim-tree.lib".redraw()
end
return M

View File

@@ -1,15 +0,0 @@
local M = {}
function M.ignored()
local config = require"nvim-tree.explorer.utils".config
config.filter_ignored = not config.filter_ignored
return require'nvim-tree.actions.reloaders'.reload_explorer()
end
function M.dotfiles()
local config = require"nvim-tree.explorer.utils".config
config.filter_dotfiles = not config.filter_dotfiles
return require'nvim-tree.actions.reloaders'.reload_explorer()
end
return M

View File

@@ -0,0 +1,23 @@
local view = require"nvim-tree.view"
local eutils = require"nvim-tree.explorer.utils"
local renderer = require"nvim-tree.renderer"
local reloaders = require"nvim-tree.actions.reloaders"
local M = {}
function M.ignored()
eutils.config.filter_ignored = not eutils.config.filter_ignored
return reloaders.reload_explorer()
end
function M.dotfiles()
eutils.config.filter_dotfiles = not eutils.config.filter_dotfiles
return reloaders.reload_explorer()
end
function M.help()
view.toggle_help()
return renderer.draw()
end
return M