Skip to content

Commit ea92e7b

Browse files
committed
refacto: set tree explorer in the global state
also remove the redraw method and use renderer.draw immediately
1 parent e42a433 commit ea92e7b

18 files changed

+86
-94
lines changed

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ ignore = {
1010
-- Global objects defined by the C code
1111
globals = {
1212
"vim",
13+
"TreeExplorer"
1314
}

lua/nvim-tree.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ local function update_base_dir_with_filepath(filepath, bufnr)
131131
end
132132
end
133133

134-
if not vim.startswith(filepath, lib.Tree.cwd or vim.loop.cwd()) then
134+
if not vim.startswith(filepath, TreeExplorer.cwd or vim.loop.cwd()) then
135135
ChangeDir.fn(vim.fn.fnamemodify(filepath, ':p:h'))
136136
end
137137
end
@@ -190,7 +190,7 @@ function M.open_on_directory()
190190
end
191191

192192
view.close()
193-
if bufname ~= lib.Tree.cwd then
193+
if bufname ~= TreeExplorer.cwd then
194194
ChangeDir.fn(bufname)
195195
end
196196
M.hijack_current_window()

lua/nvim-tree/actions/change-dir.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ local M = {
99
}
1010

1111
function M.fn(name)
12-
local foldername = name == '..' and vim.fn.fnamemodify(lib().Tree.cwd, ':h') or name
13-
local no_cwd_change = vim.fn.expand(foldername) == lib().Tree.cwd
12+
local foldername = name == '..' and vim.fn.fnamemodify(TreeExplorer.cwd, ':h') or name
13+
local no_cwd_change = vim.fn.expand(foldername) == TreeExplorer.cwd
1414
local new_tab = a.nvim_get_current_tabpage()
1515
local is_window = vim.v.event.scope == "window" and new_tab == M.current_tab
1616
if no_cwd_change or is_window then

lua/nvim-tree/actions/collapse-all.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local renderer = require"nvim-tree.renderer"
2+
13
local M = {}
24

35
function M.fn()
@@ -12,8 +14,8 @@ function M.fn()
1214
end
1315
end
1416

15-
iter(require'nvim-tree.lib'.Tree.nodes)
16-
require'nvim-tree.lib'.redraw()
17+
iter(TreeExplorer.nodes)
18+
renderer.draw()
1719
end
1820

1921
return M

lua/nvim-tree/actions/copy-paste.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ end
158158

159159
function M.copy_path(node)
160160
local absolute_path = node.absolute_path
161-
local relative_path = utils.path_relative(absolute_path, lib.Tree.cwd)
161+
local relative_path = utils.path_relative(absolute_path, TreeExplorer.cwd)
162162
local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
163163
return copy_to_clipboard(content)
164164
end

lua/nvim-tree/actions/create-file.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local M = {}
99

1010
local function focus_file(file)
1111
local _, i = utils.find_node(
12-
lib.Tree.nodes,
12+
TreeExplorer.nodes,
1313
function(node) return node.absolute_path == file end
1414
)
1515
require'nvim-tree.view'.set_cursor({i+1, 1})
@@ -61,8 +61,8 @@ function M.fn(node)
6161
node = lib.get_last_group_node(node)
6262
if node.name == '..' then
6363
node = {
64-
absolute_path = lib.Tree.cwd,
65-
nodes = lib.Tree.nodes,
64+
absolute_path = TreeExplorer.cwd,
65+
nodes = TreeExplorer.nodes,
6666
open = true,
6767
}
6868
end

lua/nvim-tree/actions/dir-up.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function M.fn(node)
44
if not node or node.name == ".." then
55
return require'nvim-tree.actions.change-dir'.fn('..')
66
else
7-
local newdir = vim.fn.fnamemodify(require'nvim-tree.lib'.Tree.cwd, ':h')
7+
local newdir = vim.fn.fnamemodify(TreeExplorer.cwd, ':h')
88
require'nvim-tree.actions.change-dir'.fn(newdir)
99
return require"nvim-tree.actions.find-file".fn(node.absolute_path)
1010
end

lua/nvim-tree/actions/find-file.lua

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ local view = require'nvim-tree.view'
22
local utils = require'nvim-tree.utils'
33
local explorer_module = require"nvim-tree.explorer"
44
local git = require"nvim-tree.git"
5+
local renderer = require"nvim-tree.renderer"
56

67
local M = {}
78

8-
local function get_explorer()
9-
return require"nvim-tree.lib".Tree
10-
end
11-
129
function M.fn(fname)
1310
local i
1411
local hide_root_folder = view.View.hide_root_folder
15-
local Explorer = get_explorer()
16-
if not Explorer then
12+
if not TreeExplorer then
1713
return
1814
end
19-
if Explorer.cwd == '/' or hide_root_folder then
15+
if TreeExplorer.cwd == '/' or hide_root_folder then
2016
i = 0
2117
else
2218
i = 1
@@ -40,7 +36,7 @@ function M.fn(fname)
4036
if status.dirs or status.files then
4137
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
4238
end
43-
require"nvim-tree.lib".redraw()
39+
renderer.draw()
4440
end)
4541
end
4642
if node.open == false then
@@ -56,9 +52,9 @@ function M.fn(fname)
5652
end
5753
end
5854

59-
local index = iterate_nodes(Explorer.nodes)
55+
local index = iterate_nodes(TreeExplorer.nodes)
6056
if tree_altered then
61-
require"nvim-tree.lib".redraw()
57+
renderer.draw()
6258
end
6359
if index and view.win_open() then
6460
view.set_cursor({index, 0})

lua/nvim-tree/actions/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ local keypress_funcs = {
6666
remove = require'nvim-tree.actions.remove-file'.fn,
6767
rename = require'nvim-tree.actions.rename-file'.fn(false),
6868
system_open = require'nvim-tree.actions.system-open'.fn,
69-
toggle_dotfiles = require"nvim-tree.actions.toggle-ignore".dotfiles,
70-
toggle_help = require"nvim-tree.actions.toggle-help".fn,
71-
toggle_ignored = require"nvim-tree.actions.toggle-ignore".ignored,
69+
toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles,
70+
toggle_help = require"nvim-tree.actions.toggles".help,
71+
toggle_ignored = require"nvim-tree.actions.toggles".ignored,
7272
trash = require'nvim-tree.actions.trash'.fn,
7373
}
7474

lua/nvim-tree/actions/movements.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local utils = require'nvim-tree.utils'
22
local view = require'nvim-tree.view'
33
local diagnostics = require'nvim-tree.diagnostics'
44
local config = require"nvim-tree.config"
5+
local renderer = require"nvim-tree.renderer"
56
local lib = function() return require'nvim-tree.lib' end
67

78
local M = {}
@@ -44,7 +45,7 @@ function M.parent_node(should_close)
4445
node.open = false
4546
altered_tree = true
4647
else
47-
local line, parent = iter(lib().Tree.nodes, true)
48+
local line, parent = iter(TreeExplorer.nodes, true)
4849
if parent == nil then
4950
line = 1
5051
elseif should_close then
@@ -57,7 +58,7 @@ function M.parent_node(should_close)
5758

5859
if altered_tree then
5960
diagnostics.update()
60-
lib().redraw()
61+
renderer.draw()
6162
end
6263
end
6364
end
@@ -73,16 +74,16 @@ function M.sibling(direction)
7374
local parent, _
7475

7576
-- Check if current node is already at root nodes
76-
for index, _node in ipairs(lib().Tree.nodes) do
77+
for index, _node in ipairs(TreeExplorer.nodes) do
7778
if node_path == _node.absolute_path then
7879
line = index
7980
end
8081
end
8182

8283
if line > 0 then
83-
parent = lib().Tree
84+
parent = TreeExplorer
8485
else
85-
_, parent = iter(lib().Tree.nodes, true)
86+
_, parent = iter(TreeExplorer.nodes, true)
8687
if parent ~= nil and #parent.nodes > 1 then
8788
line, _ = get_line_from_node(node)(parent.nodes)
8889
end
@@ -99,7 +100,7 @@ function M.sibling(direction)
99100
end
100101
local target_node = parent.nodes[index]
101102

102-
line, _ = get_line_from_node(target_node)(lib().Tree.nodes, true)
103+
line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true)
103104
view.set_cursor({line, 0})
104105
end
105106
end

0 commit comments

Comments
 (0)