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 -- Global objects defined by the C code
globals = { globals = {
"vim", "vim",
"TreeExplorer"
} }

View file

@ -131,7 +131,7 @@ local function update_base_dir_with_filepath(filepath, bufnr)
end end
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')) ChangeDir.fn(vim.fn.fnamemodify(filepath, ':p:h'))
end end
end end
@ -190,7 +190,7 @@ function M.open_on_directory()
end end
view.close() view.close()
if bufname ~= lib.Tree.cwd then if bufname ~= TreeExplorer.cwd then
ChangeDir.fn(bufname) ChangeDir.fn(bufname)
end end
M.hijack_current_window() M.hijack_current_window()

View file

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

View file

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

View file

@ -158,7 +158,7 @@ end
function M.copy_path(node) function M.copy_path(node)
local absolute_path = node.absolute_path 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 local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
return copy_to_clipboard(content) return copy_to_clipboard(content)
end end

View file

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

View file

@ -4,7 +4,7 @@ function M.fn(node)
if not node or node.name == ".." then if not node or node.name == ".." then
return require'nvim-tree.actions.change-dir'.fn('..') return require'nvim-tree.actions.change-dir'.fn('..')
else 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) require'nvim-tree.actions.change-dir'.fn(newdir)
return require"nvim-tree.actions.find-file".fn(node.absolute_path) return require"nvim-tree.actions.find-file".fn(node.absolute_path)
end end

View file

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

View file

@ -66,9 +66,9 @@ local keypress_funcs = {
remove = require'nvim-tree.actions.remove-file'.fn, remove = require'nvim-tree.actions.remove-file'.fn,
rename = require'nvim-tree.actions.rename-file'.fn(false), rename = require'nvim-tree.actions.rename-file'.fn(false),
system_open = require'nvim-tree.actions.system-open'.fn, system_open = require'nvim-tree.actions.system-open'.fn,
toggle_dotfiles = require"nvim-tree.actions.toggle-ignore".dotfiles, toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles,
toggle_help = require"nvim-tree.actions.toggle-help".fn, toggle_help = require"nvim-tree.actions.toggles".help,
toggle_ignored = require"nvim-tree.actions.toggle-ignore".ignored, toggle_ignored = require"nvim-tree.actions.toggles".ignored,
trash = require'nvim-tree.actions.trash'.fn, 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 view = require'nvim-tree.view'
local diagnostics = require'nvim-tree.diagnostics' local diagnostics = require'nvim-tree.diagnostics'
local config = require"nvim-tree.config" local config = require"nvim-tree.config"
local renderer = require"nvim-tree.renderer"
local lib = function() return require'nvim-tree.lib' end local lib = function() return require'nvim-tree.lib' end
local M = {} local M = {}
@ -44,7 +45,7 @@ function M.parent_node(should_close)
node.open = false node.open = false
altered_tree = true altered_tree = true
else else
local line, parent = iter(lib().Tree.nodes, true) local line, parent = iter(TreeExplorer.nodes, true)
if parent == nil then if parent == nil then
line = 1 line = 1
elseif should_close then elseif should_close then
@ -57,7 +58,7 @@ function M.parent_node(should_close)
if altered_tree then if altered_tree then
diagnostics.update() diagnostics.update()
lib().redraw() renderer.draw()
end end
end end
end end
@ -73,16 +74,16 @@ function M.sibling(direction)
local parent, _ local parent, _
-- Check if current node is already at root nodes -- 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 if node_path == _node.absolute_path then
line = index line = index
end end
end end
if line > 0 then if line > 0 then
parent = lib().Tree parent = TreeExplorer
else else
_, parent = iter(lib().Tree.nodes, true) _, parent = iter(TreeExplorer.nodes, true)
if parent ~= nil and #parent.nodes > 1 then if parent ~= nil and #parent.nodes > 1 then
line, _ = get_line_from_node(node)(parent.nodes) line, _ = get_line_from_node(node)(parent.nodes)
end end
@ -99,7 +100,7 @@ function M.sibling(direction)
end end
local target_node = parent.nodes[index] 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}) view.set_cursor({line, 0})
end end
end end

View file

@ -1,14 +1,11 @@
local git = require "nvim-tree.git" local git = require "nvim-tree.git"
local diagnostics = require "nvim-tree.diagnostics" local diagnostics = require "nvim-tree.diagnostics"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local renderer = require "nvim-tree.renderer"
local explorer_module = require'nvim-tree.explorer' local explorer_module = require'nvim-tree.explorer'
local M = {} local M = {}
local function get_explorer()
return require "nvim-tree.lib".Tree
end
local function refresh_nodes(node, projects) local function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd) 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 {}) explorer_module.reload(node, node.absolute_path or node.cwd, projects[project_root] or {})
@ -36,16 +33,15 @@ end
local event_running = false local event_running = false
function M.reload_explorer(callback) function M.reload_explorer(callback)
local Explorer = get_explorer() if event_running or not TreeExplorer.cwd or vim.v.exiting ~= vim.NIL then
if event_running or not Explorer.cwd or vim.v.exiting ~= vim.NIL then
return return
end end
event_running = true event_running = true
git.reload(function(projects) git.reload(function(projects)
refresh_nodes(Explorer, projects) refresh_nodes(TreeExplorer, projects)
if view.win_open() then if view.win_open() then
require"nvim-tree.lib".redraw() renderer.draw()
if callback and type(callback) == 'function' then if callback and type(callback) == 'function' then
callback() callback()
end end
@ -62,8 +58,8 @@ function M.reload_git()
event_running = true event_running = true
git.reload(function(projects) git.reload(function(projects)
M.reload_node_status(get_explorer(), projects) M.reload_node_status(TreeExplorer, projects)
require"nvim-tree.lib".redraw() renderer.draw()
event_running = false event_running = false
end) end)
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() buffer_severity = from_nvim_lsp()
end end
local nodes = require'nvim-tree.lib'.Tree.nodes
if #signs then if #signs then
vim.fn.sign_unplacelist(vim.tbl_map(function(sign) vim.fn.sign_unplacelist(vim.tbl_map(function(sign)
return { return {
@ -124,7 +123,7 @@ function M.update()
end end
for bufname, severity in pairs(buffer_severity) do for bufname, severity in pairs(buffer_severity) do
if 0 < severity and severity < 5 then 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 if M.show_on_dirs and not node.open then
return vim.startswith(bufname, node.absolute_path) return vim.startswith(bufname, node.absolute_path)
else else

View file

@ -1,6 +1,7 @@
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 = {}
@ -29,7 +30,7 @@ function Explorer:_load(cwd, node)
end end
function Explorer:expand(node) 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) self:_load(node.link_to or node.absolute_path, node)
end end

View file

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

View file

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