Skip to content

Commit 471afc1

Browse files
committed
refacto: abstract TreeExplorer in core.lua
1 parent d2b12d6 commit 471afc1

17 files changed

+94
-65
lines changed

lua/nvim-tree.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local view = require "nvim-tree.view"
99
local utils = require "nvim-tree.utils"
1010
local change_dir = require "nvim-tree.actions.change-dir"
1111
local legacy = require "nvim-tree.legacy"
12+
local core = require "nvim-tree.core"
1213

1314
local _config = {}
1415

@@ -58,8 +59,8 @@ function M.open_replacing_current_buffer()
5859
end
5960

6061
local cwd = vim.fn.fnamemodify(bufname, ":p:h")
61-
if not TreeExplorer or cwd ~= TreeExplorer.cwd then
62-
lib.init(cwd)
62+
if not core.get_explorer() or cwd ~= core.get_cwd() then
63+
core.init(cwd)
6364
end
6465
view.open_in_current_win { hijack_current_buf = false, resize = false }
6566
require("nvim-tree.renderer").draw()
@@ -97,13 +98,13 @@ local function update_base_dir_with_filepath(filepath, bufnr)
9798
end
9899
end
99100

100-
if not vim.startswith(filepath, TreeExplorer.cwd) then
101+
if not vim.startswith(filepath, core.get_explorer().cwd) then
101102
change_dir.fn(vim.fn.fnamemodify(filepath, ":p:h"))
102103
end
103104
end
104105

105106
function M.find_file(with_open, bufnr)
106-
if not with_open and not TreeExplorer then
107+
if not with_open and not core.get_explorer() then
107108
return
108109
end
109110

@@ -248,8 +249,7 @@ function M.on_enter(netrw_disabled)
248249
end
249250

250251
if should_open or should_hijack or existing_tree_wins[1] ~= nil then
251-
lib.init(cwd)
252-
lib.open()
252+
lib.open(cwd)
253253

254254
if should_focus_other_window then
255255
vim.cmd "noautocmd wincmd p"

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local a = vim.api
2+
23
local utils = require "nvim-tree.utils"
4+
local core = require "nvim-tree.core"
35

46
local M = {
57
current_tab = a.nvim_get_current_tabpage(),
@@ -10,12 +12,12 @@ local M = {
1012
}
1113

1214
function M.fn(name, with_open)
13-
if not TreeExplorer then
15+
if not core.get_explorer() then
1416
return
1517
end
1618

17-
local foldername = name == ".." and vim.fn.fnamemodify(utils.path_remove_trailing(TreeExplorer.cwd), ":h") or name
18-
local no_cwd_change = vim.fn.expand(foldername) == TreeExplorer.cwd
19+
local foldername = name == ".." and vim.fn.fnamemodify(utils.path_remove_trailing(core.get_cwd()), ":h") or name
20+
local no_cwd_change = vim.fn.expand(foldername) == core.get_cwd()
1921
local new_tab = a.nvim_get_current_tabpage()
2022
local is_window = (vim.v.event.scope == "window" or vim.v.event.changed_window) and new_tab == M.current_tab
2123
if no_cwd_change or is_window then
@@ -33,7 +35,7 @@ function M.force_dirchange(foldername, with_open)
3335
vim.cmd("lcd " .. vim.fn.fnameescape(foldername))
3436
end
3537
end
36-
require("nvim-tree.lib").init(foldername)
38+
require("nvim-tree.core").init(foldername)
3739
if with_open then
3840
require("nvim-tree.lib").open()
3941
else

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local renderer = require "nvim-tree.renderer"
22
local utils = require "nvim-tree.utils"
3+
local core = require "nvim-tree.core"
34

45
local M = {}
56

@@ -31,7 +32,7 @@ function M.fn(keep_buffers)
3132
end
3233
end
3334

34-
iter(TreeExplorer.nodes)
35+
iter(core.get_explorer().nodes)
3536
renderer.draw()
3637
end
3738

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local uv = vim.loop
33

44
local lib = require "nvim-tree.lib"
55
local utils = require "nvim-tree.utils"
6+
local core = require "nvim-tree.core"
67

78
local M = {}
89

@@ -168,7 +169,7 @@ end
168169

169170
function M.copy_path(node)
170171
local absolute_path = node.absolute_path
171-
local relative_path = utils.path_relative(absolute_path, TreeExplorer.cwd)
172+
local relative_path = utils.path_relative(absolute_path, core.get_cwd())
172173
local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
173174
return copy_to_clipboard(content)
174175
end

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ local uv = vim.loop
44
local utils = require "nvim-tree.utils"
55
local events = require "nvim-tree.events"
66
local lib = require "nvim-tree.lib"
7+
local core = require "nvim-tree.core"
78

89
local M = {}
910

1011
local function focus_file(file)
11-
local _, i = utils.find_node(TreeExplorer.nodes, function(node)
12+
local _, i = utils.find_node(core.get_explorer().nodes, function(node)
1213
return node.absolute_path == file
1314
end)
1415
require("nvim-tree.view").set_cursor { i + 1, 1 }
@@ -62,8 +63,8 @@ function M.fn(node)
6263
node = lib.get_last_group_node(node)
6364
if node.name == ".." then
6465
node = {
65-
absolute_path = TreeExplorer.cwd,
66-
nodes = TreeExplorer.nodes,
66+
absolute_path = core.get_cwd(),
67+
nodes = core.get_explorer().nodes,
6768
open = true,
6869
}
6970
end

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
local utils = require "nvim-tree.utils"
2+
local core = require "nvim-tree.core"
23

34
local M = {}
45

56
function M.fn(node)
67
if not node or node.name == ".." then
78
return require("nvim-tree.actions.change-dir").fn ".."
89
else
9-
local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(TreeExplorer.cwd), ":h")
10+
local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(core.get_cwd()), ":h")
1011
require("nvim-tree.actions.change-dir").fn(newdir)
1112
return require("nvim-tree.actions.find-file").fn(node.absolute_path)
1213
end

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
local view = require "nvim-tree.view"
22
local utils = require "nvim-tree.utils"
33
local renderer = require "nvim-tree.renderer"
4+
local core = require "nvim-tree.core"
45

56
local M = {}
67

78
local running = {}
89

910
function M.fn(fname)
10-
if running[fname] or not TreeExplorer then
11+
if running[fname] or not core.get_explorer() then
1112
return
1213
end
1314
running[fname] = true
@@ -30,7 +31,7 @@ function M.fn(fname)
3031
end
3132

3233
if #node.nodes == 0 then
33-
TreeExplorer:expand(node)
34+
core.get_explorer():expand(node)
3435
end
3536

3637
if iterate_nodes(node.nodes) ~= nil then
@@ -43,7 +44,7 @@ function M.fn(fname)
4344
end
4445
end
4546

46-
local index = iterate_nodes(TreeExplorer.nodes)
47+
local index = iterate_nodes(core.get_explorer().nodes)
4748
if tree_altered then
4849
renderer.draw()
4950
end

lua/nvim-tree/actions/movements.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local utils = require "nvim-tree.utils"
22
local view = require "nvim-tree.view"
33
local diagnostics = require "nvim-tree.diagnostics"
44
local renderer = require "nvim-tree.renderer"
5+
local core = require "nvim-tree.core"
6+
57
local lib = function()
68
return require "nvim-tree.lib"
79
end
@@ -49,7 +51,7 @@ function M.parent_node(should_close)
4951
node.open = false
5052
altered_tree = true
5153
else
52-
local line, parent = iter(TreeExplorer.nodes, true)
54+
local line, parent = iter(core.get_explorer().nodes, true)
5355
if parent == nil then
5456
line = 1
5557
elseif should_close then
@@ -82,16 +84,16 @@ function M.sibling(direction)
8284
local parent, _
8385

8486
-- Check if current node is already at root nodes
85-
for index, _node in ipairs(TreeExplorer.nodes) do
87+
for index, _node in ipairs(core.get_explorer().nodes) do
8688
if node_path == _node.absolute_path then
8789
line = index
8890
end
8991
end
9092

9193
if line > 0 then
92-
parent = TreeExplorer
94+
parent = core.get_explorer()
9395
else
94-
_, parent = iter(TreeExplorer.nodes, true)
96+
_, parent = iter(core.get_explorer().nodes, true)
9597
if parent ~= nil and #parent.nodes > 1 then
9698
line, _ = get_line_from_node(node)(parent.nodes)
9799
end
@@ -108,7 +110,7 @@ function M.sibling(direction)
108110
end
109111
local target_node = parent.nodes[index]
110112

111-
line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true)
113+
line, _ = get_line_from_node(target_node)(core.get_explorer().nodes, true)
112114
if not view.is_root_folder_visible() then
113115
line = line - 1
114116
end
@@ -119,7 +121,7 @@ end
119121
function M.find_git_item(where)
120122
return function()
121123
local node_cur = lib().get_node_at_cursor()
122-
local nodes_by_line = lib().get_nodes_by_line(TreeExplorer.nodes, view.View.hide_root_folder and 1 or 2)
124+
local nodes_by_line = lib().get_nodes_by_line(core.get_explorer().nodes, view.View.hide_root_folder and 1 or 2)
123125

124126
local cur, first, prev, nex = nil, nil, nil, nil
125127
for line, node in pairs(nodes_by_line) do

lua/nvim-tree/actions/reloaders.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local diagnostics = require "nvim-tree.diagnostics"
33
local view = require "nvim-tree.view"
44
local renderer = require "nvim-tree.renderer"
55
local explorer_module = require "nvim-tree.explorer"
6+
local core = require "nvim-tree.core"
67

78
local M = {}
89

@@ -34,13 +35,13 @@ end
3435

3536
local event_running = false
3637
function M.reload_explorer()
37-
if event_running or not TreeExplorer or not TreeExplorer.cwd or vim.v.exiting ~= vim.NIL then
38+
if event_running or not core.get_explorer() or vim.v.exiting ~= vim.NIL then
3839
return
3940
end
4041
event_running = true
4142

4243
local projects = git.reload()
43-
refresh_nodes(TreeExplorer, projects)
44+
refresh_nodes(core.get_explorer(), projects)
4445
if view.is_visible() then
4546
renderer.draw()
4647
end
@@ -49,13 +50,13 @@ function M.reload_explorer()
4950
end
5051

5152
function M.reload_git()
52-
if not TreeExplorer or not git.config.enable or event_running then
53+
if not core.get_explorer() or not git.config.enable or event_running then
5354
return
5455
end
5556
event_running = true
5657

5758
local projects = git.reload()
58-
M.reload_node_status(TreeExplorer, projects)
59+
M.reload_node_status(core.get_explorer(), projects)
5960
renderer.draw()
6061
event_running = false
6162
end

lua/nvim-tree/actions/run-command.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local utils = require "nvim-tree.utils"
2+
local core = require "nvim-tree.core"
23

34
local M = {}
45

@@ -7,7 +8,7 @@ local M = {}
78
---(the topmost node in the nvim-tree window)
89
local function get_node_path(node)
910
if node.name == ".." then
10-
return utils.path_remove_trailing(TreeExplorer.cwd)
11+
return utils.path_remove_trailing(core.get_cwd())
1112
else
1213
return node.absolute_path
1314
end

lua/nvim-tree/actions/search-node.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
local utils = require "nvim-tree.utils"
22
local view = require "nvim-tree.view"
33
local renderer = require "nvim-tree.renderer"
4+
local core = require "nvim-tree.core"
45

56
local M = {}
67

78
function M.fn()
8-
if not TreeExplorer then
9+
if not core.get_explorer() then
910
return
1011
end
1112

1213
local input_path = vim.fn.input("Search node: ", "", "file")
1314
utils.clear_prompt()
1415

1516
local absolute_input_path = utils.path_join {
16-
TreeExplorer.cwd,
17+
core.get_cwd(),
1718
input_path,
1819
}
1920

@@ -54,7 +55,7 @@ function M.fn()
5455
-- if node is not open -> open it
5556
if not node.open then
5657
node.open = true
57-
TreeExplorer:expand(node)
58+
core.get_explorer():expand(node)
5859
tree_altered = true
5960
end
6061

@@ -70,7 +71,7 @@ function M.fn()
7071
return index
7172
end
7273

73-
local index = search_node(TreeExplorer.nodes)
74+
local index = search_node(core.get_explorer().nodes)
7475

7576
if tree_altered then
7677
renderer.draw()

lua/nvim-tree/core.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local events = require "nvim-tree.events"
2+
local explorer = require "nvim-tree.explorer"
3+
4+
local M = {}
5+
6+
local first_init_done = false
7+
8+
TreeExplorer = nil
9+
10+
function M.init(foldername)
11+
TreeExplorer = explorer.Explorer.new(foldername)
12+
if not first_init_done then
13+
events._dispatch_ready()
14+
first_init_done = true
15+
end
16+
end
17+
18+
function M.get_explorer()
19+
return TreeExplorer
20+
end
21+
22+
function M.get_cwd()
23+
return TreeExplorer.cwd
24+
end
25+
26+
return M

lua/nvim-tree/diagnostics.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local a = vim.api
22
local utils = require "nvim-tree.utils"
33
local view = require "nvim-tree.view"
4+
local core = require "nvim-tree.core"
45

56
local M = {}
67

@@ -103,7 +104,7 @@ local function is_using_coc()
103104
end
104105

105106
function M.update()
106-
if not M.enable or not TreeExplorer or not view.is_buf_valid(view.get_bufnr()) then
107+
if not M.enable or not core.get_explorer() or not view.is_buf_valid(view.get_bufnr()) then
107108
return
108109
end
109110
local buffer_severity
@@ -125,7 +126,7 @@ function M.update()
125126
end
126127
for bufname, severity in pairs(buffer_severity) do
127128
if 0 < severity and severity < 5 then
128-
local node, line = utils.find_node(TreeExplorer.nodes, function(node)
129+
local node, line = utils.find_node(core.get_explorer().nodes, function(node)
129130
if M.show_on_dirs and not node.open then
130131
return vim.startswith(bufname, node.absolute_path)
131132
else

0 commit comments

Comments
 (0)