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

@ -10,4 +10,5 @@ ignore = {
-- Global objects defined by the C code
globals = {
"vim",
"TreeExplorer"
}

View file

@ -131,7 +131,7 @@ local function update_base_dir_with_filepath(filepath, bufnr)
end
end
if not vim.startswith(filepath, lib.Tree.cwd or vim.loop.cwd()) then
if not vim.startswith(filepath, TreeExplorer.cwd or vim.loop.cwd()) then
ChangeDir.fn(vim.fn.fnamemodify(filepath, ':p:h'))
end
end
@ -190,7 +190,7 @@ function M.open_on_directory()
end
view.close()
if bufname ~= lib.Tree.cwd then
if bufname ~= TreeExplorer.cwd then
ChangeDir.fn(bufname)
end
M.hijack_current_window()

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

View file

@ -111,7 +111,6 @@ function M.update()
buffer_severity = from_nvim_lsp()
end
local nodes = require'nvim-tree.lib'.Tree.nodes
if #signs then
vim.fn.sign_unplacelist(vim.tbl_map(function(sign)
return {
@ -124,7 +123,7 @@ function M.update()
end
for bufname, severity in pairs(buffer_severity) do
if 0 < severity and severity < 5 then
local node, line = utils.find_node(nodes, function(node)
local node, line = utils.find_node(TreeExplorer.nodes, function(node)
if M.show_on_dirs and not node.open then
return vim.startswith(bufname, node.absolute_path)
else

View file

@ -1,6 +1,7 @@
local uv = vim.loop
local git = require"nvim-tree.git"
local renderer = require"nvim-tree.renderer"
local M = {}
@ -29,7 +30,7 @@ function Explorer:_load(cwd, node)
end
function Explorer:expand(node)
self.init_cb = require"nvim-tree.lib".redraw
self.init_cb = renderer.draw
self:_load(node.link_to or node.absolute_path, node)
end

View file

@ -12,10 +12,12 @@ local M = {
target_winid = nil,
}
TreeExplorer = nil
function M.init(with_open, foldername)
M.Tree = explorer.Explorer.new(foldername)
M.Tree:init(function()
M.redraw()
TreeExplorer = explorer.Explorer.new(foldername)
TreeExplorer:init(function()
renderer.draw()
if with_open then
M.open()
end
@ -27,10 +29,6 @@ function M.init(with_open, foldername)
end)
end
function M.redraw()
renderer.draw(M.Tree, true)
end
local function get_node_at_line(line)
local index = view.View.hide_root_folder and 1 or 2
local function iter(nodes)
@ -61,14 +59,14 @@ function M.get_node_at_cursor()
local help_text = get_node_at_line(line+1)(help_lines)
return {name = help_text}
else
if line == 1 and M.Tree.cwd ~= "/" and not hide_root_folder then
if line == 1 and TreeExplorer.cwd ~= "/" and not hide_root_folder then
return { name = ".." }
end
if M.Tree.cwd == "/" then
if TreeExplorer.cwd == "/" then
line = line + 1
end
return get_node_at_line(line)(M.Tree.nodes)
return get_node_at_line(line)(TreeExplorer.nodes)
end
end
@ -85,9 +83,9 @@ 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
M.Tree:expand(node)
TreeExplorer:expand(node)
else
M.redraw()
renderer.draw()
end
diagnostics.update()
@ -111,11 +109,11 @@ function M.open()
local should_redraw = view.open()
local respect_buf_cwd = vim.g.nvim_tree_respect_buf_cwd or 0
if respect_buf_cwd == 1 and cwd ~= M.Tree.cwd then
if respect_buf_cwd == 1 and cwd ~= TreeExplorer.cwd then
require'nvim-tree.actions.change-dir'.fn(cwd)
end
if should_redraw then
M.redraw()
renderer.draw()
end
end

View file

@ -370,25 +370,23 @@ end
local M = {}
function M.draw(tree, reload)
function M.draw()
if not api.nvim_buf_is_loaded(view.View.bufnr) then return end
local cursor
if view.win_open() then
cursor = api.nvim_win_get_cursor(view.get_winnr())
end
if reload then
index = 0
lines = {}
hl = {}
index = 0
lines = {}
hl = {}
local show_arrows =
vim.g.nvim_tree_indent_markers ~= 1
and icon_state.show_folder_icon
and icon_state.show_folder_arrows
_padding.reload_padding_function()
icon_state = config.get_icon_state()
update_draw_data(tree, show_arrows and 2 or 0, {})
end
local show_arrows =
vim.g.nvim_tree_indent_markers ~= 1
and icon_state.show_folder_icon
and icon_state.show_folder_arrows
_padding.reload_padding_function()
icon_state = config.get_icon_state()
update_draw_data(TreeExplorer, show_arrows and 2 or 0, {})
if view.is_help_ui() then
lines, hl = _help.compute_lines()