Skip to content

Commit 8a6c7ba

Browse files
committed
refacto: move code ton explorer and simplify some internal apis
1 parent 8b27fd4 commit 8a6c7ba

File tree

5 files changed

+75
-51
lines changed

5 files changed

+75
-51
lines changed

lua/nvim-tree.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ function M.on_enter(opts)
8787

8888
local stats = luv.fs_stat(bufname)
8989
local is_dir = stats and stats.type == 'directory'
90+
local cwd
9091
if is_dir then
91-
lib.Tree.cwd = vim.fn.expand(bufname)
92+
cwd = vim.fn.expand(bufname)
9293
end
9394

9495
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
@@ -104,7 +105,7 @@ function M.on_enter(opts)
104105
M.hijack_current_window()
105106
end
106107

107-
lib.init(should_open, lib.Tree.cwd)
108+
lib.init(should_open, cwd)
108109
end
109110

110111
local function is_file_readable(fname)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function M.fn(fname)
1313
local i
1414
local hide_root_folder = view.View.hide_root_folder
1515
local Explorer = get_explorer()
16+
if not Explorer then
17+
return
18+
end
1619
if Explorer.cwd == '/' or hide_root_folder then
1720
i = 0
1821
else
@@ -32,7 +35,7 @@ function M.fn(fname)
3235
if path_matches then
3336
if #node.nodes == 0 then
3437
node.open = true
35-
explorer_module.explore(node.nodes, node.absolute_path, node, {})
38+
explorer_module.explore(node, node.absolute_path, {})
3639
git.load_project_status(node.absolute_path, function(status)
3740
if status.dirs or status.files then
3841
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)

lua/nvim-tree/explorer/explore.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local builders = require'nvim-tree.explorer.node-builders'
77

88
local M = {}
99

10-
function M.explore(nodes, cwd, parent_node, status)
10+
function M.explore(node, cwd, status)
1111
local handle = luv.fs_scandir(cwd)
1212
if type(handle) == 'string' then
1313
api.nvim_err_writeln(handle)
@@ -39,42 +39,42 @@ function M.explore(nodes, cwd, parent_node, status)
3939
end
4040
end
4141

42-
local parent_node_ignored = parent_node and parent_node.git_status == '!!'
42+
local node_ignored = node.git_status == '!!'
4343
-- Group empty dirs
44-
if parent_node and vim.g.nvim_tree_group_empty == 1 then
44+
if vim.g.nvim_tree_group_empty == 1 then
4545
if eutils.should_group(cwd, dirs, files, links) then
4646
local child_node
47-
if dirs[1] then child_node = builders.folder(cwd, dirs[1], status, parent_node_ignored) end
48-
if links[1] then child_node = builders.link(cwd, links[1], status, parent_node_ignored) end
47+
if dirs[1] then child_node = builders.folder(cwd, dirs[1], status, node_ignored) end
48+
if links[1] then child_node = builders.link(cwd, links[1], status, node_ignored) end
4949
if luv.fs_access(child_node.absolute_path, 'R') then
50-
parent_node.group_next = child_node
51-
child_node.git_status = parent_node.git_status
52-
M.explore(nodes, child_node.absolute_path, child_node, status)
50+
node.group_next = child_node
51+
child_node.git_status = node.git_status
52+
M.explore(child_node, child_node.absolute_path, status)
5353
return
5454
end
5555
end
5656
end
5757

5858
for _, dirname in ipairs(dirs) do
59-
local dir = builders.folder(cwd, dirname, status, parent_node_ignored)
59+
local dir = builders.folder(cwd, dirname, status, node_ignored)
6060
if luv.fs_access(dir.absolute_path, 'R') then
61-
table.insert(nodes, dir)
61+
table.insert(node.nodes, dir)
6262
end
6363
end
6464

6565
for _, linkname in ipairs(links) do
66-
local link = builders.link(cwd, linkname, status, parent_node_ignored)
66+
local link = builders.link(cwd, linkname, status, node_ignored)
6767
if link.link_to ~= nil then
68-
table.insert(nodes, link)
68+
table.insert(node.nodes, link)
6969
end
7070
end
7171

7272
for _, filename in ipairs(files) do
73-
local file = builders.file(cwd, filename, status, parent_node_ignored)
74-
table.insert(nodes, file)
73+
local file = builders.file(cwd, filename, status, node_ignored)
74+
table.insert(node.nodes, file)
7575
end
7676

77-
utils.merge_sort(nodes, eutils.node_comparator)
77+
utils.merge_sort(node.nodes, eutils.node_comparator)
7878
end
7979

8080
return M

lua/nvim-tree/explorer/init.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
1+
local uv = vim.loop
2+
3+
local git = require"nvim-tree.git"
4+
15
local M = {}
26

37
M.explore = require"nvim-tree.explorer.explore".explore
48
M.reload = require"nvim-tree.explorer.reload".reload
59

10+
local Explorer = {}
11+
Explorer.__index = Explorer
12+
13+
function Explorer.new(cwd)
14+
cwd = cwd or uv.cwd()
15+
return setmetatable({
16+
cwd = cwd,
17+
nodes = {}
18+
}, Explorer)
19+
end
20+
21+
function Explorer:_load(cwd, node)
22+
git.load_project_status(cwd, function(git_statuses)
23+
M.explore(node, cwd, git_statuses)
24+
if type(self.init_cb) == "function" then
25+
self.init_cb(self)
26+
self.init_cb = nil
27+
end
28+
end)
29+
end
30+
31+
function Explorer:expand(node)
32+
self.init_cb = require"nvim-tree.lib".redraw
33+
self:_load(node.link_to or node.absolute_path, node)
34+
end
35+
36+
function Explorer:init(f)
37+
self.init_cb = f
38+
self:_load(self.cwd, self)
39+
end
40+
641
function M.setup(opts)
742
require"nvim-tree.explorer.utils".setup(opts)
843
end
944

45+
M.Explorer = Explorer
46+
1047
return M

lua/nvim-tree/lib.lua

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
local api = vim.api
2-
local luv = vim.loop
32

43
local renderer = require'nvim-tree.renderer'
54
local diagnostics = require'nvim-tree.diagnostics'
65
local explorer = require'nvim-tree.explorer'
76
local view = require'nvim-tree.view'
87
local events = require'nvim-tree.events'
9-
local git = require'nvim-tree.git'
108

119
local first_init_done = false
1210

1311
local M = {
1412
target_winid = nil,
1513
}
1614

17-
M.Tree = {
18-
nodes = {},
19-
cwd = nil,
20-
}
21-
22-
local function load_children(cwd, children, parent)
23-
git.load_project_status(cwd, function(git_statuses)
24-
explorer.explore(children, cwd, parent, git_statuses)
25-
M.redraw()
26-
end)
27-
end
28-
2915
function M.init(with_open, foldername)
30-
M.Tree.nodes = {}
31-
M.Tree.cwd = foldername or luv.cwd()
32-
33-
if with_open then
34-
M.open()
35-
end
36-
37-
load_children(M.Tree.cwd, M.Tree.nodes)
16+
M.Tree = explorer.Explorer.new(foldername)
17+
M.Tree:init(function()
18+
M.redraw()
19+
if with_open then
20+
M.open()
21+
end
3822

39-
if not first_init_done then
40-
events._dispatch_ready()
41-
first_init_done = true
42-
end
23+
if not first_init_done then
24+
events._dispatch_ready()
25+
first_init_done = true
26+
end
27+
end)
4328
end
4429

4530
function M.redraw()
@@ -100,11 +85,7 @@ function M.expand_or_collapse(node)
10085
node.open = not node.open
10186
if node.has_children then node.has_children = false end
10287
if #node.nodes == 0 then
103-
load_children(
104-
node.link_to or node.absolute_path,
105-
node.nodes,
106-
node
107-
)
88+
M.Tree:expand(node)
10889
else
10990
M.redraw()
11091
end
@@ -143,12 +124,14 @@ function M.close_node(node)
143124
end
144125

145126
function M.toggle_ignored()
146-
explorer.config.filter_ignored = not explorer.config.filter_ignored
127+
local config = require"nvim-tree.explorer.utils".config
128+
config.filter_ignored = not config.filter_ignored
147129
return require'nvim-tree.actions.reloaders'.reload_explorer()
148130
end
149131

150132
function M.toggle_dotfiles()
151-
explorer.config.filter_dotfiles = not explorer.config.filter_dotfiles
133+
local config = require"nvim-tree.explorer.utils".config
134+
config.filter_dotfiles = not config.filter_dotfiles
152135
return require'nvim-tree.actions.reloaders'.reload_explorer()
153136
end
154137

0 commit comments

Comments
 (0)