Skip to content

Commit 44cb3d2

Browse files
committed
refactor(#2826): View is an Explorer member
1 parent 0eb21f6 commit 44cb3d2

21 files changed

+170
-114
lines changed

lua/nvim-tree.lua

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local log = require("nvim-tree.log")
2-
local view = require("nvim-tree.view")
32
local utils = require("nvim-tree.utils")
43
local actions = require("nvim-tree.actions")
54
local core = require("nvim-tree.core")
@@ -74,7 +73,8 @@ function M.change_root(path, bufnr)
7473
end
7574

7675
function M.tab_enter()
77-
if view.View:is_visible({ any_tabpage = true }) then
76+
local explorer = core.get_explorer()
77+
if explorer and explorer.view:is_visible({ any_tabpage = true }) then
7878
local bufname = vim.api.nvim_buf_get_name(0)
7979

8080
local ft
@@ -89,17 +89,15 @@ function M.tab_enter()
8989
return
9090
end
9191
end
92-
view.View:open({ focus_tree = false })
92+
explorer.view:open({ focus_tree = false })
9393

94-
local explorer = core.get_explorer()
95-
if explorer then
96-
explorer.renderer:draw()
97-
end
94+
explorer.renderer:draw()
9895
end
9996
end
10097

10198
function M.open_on_directory()
102-
local should_proceed = _config.hijack_directories.auto_open or view.View:is_visible()
99+
local explorer = core.get_explorer()
100+
local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.view:is_visible()
103101
if not should_proceed then
104102
return
105103
end
@@ -150,17 +148,22 @@ local function setup_autocommands(opts)
150148
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
151149
end
152150

151+
-- TODO #2826 move this to explorer
153152
-- prevent new opened file from opening in the same window as nvim-tree
154153
create_nvim_tree_autocmd("BufWipeout", {
155154
pattern = "NvimTree_*",
156155
callback = function()
157156
if not utils.is_nvim_tree_buf(0) then
158157
return
159158
end
160-
if opts.actions.open_file.eject then
161-
view.View:prevent_buffer_override()
162-
else
163-
view.View:abandon_current_window()
159+
160+
local explorer = core.get_explorer()
161+
if explorer then
162+
if opts.actions.open_file.eject then
163+
explorer.view:prevent_buffer_override()
164+
else
165+
explorer.view:abandon_current_window()
166+
end
164167
end
165168
end,
166169
})
@@ -226,12 +229,16 @@ local function setup_autocommands(opts)
226229
})
227230
end
228231

232+
-- TODO #2826 move this to explorer
229233
if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
230234
create_nvim_tree_autocmd("WinLeave", {
231235
pattern = "NvimTree_*",
232236
callback = function()
233237
if utils.is_nvim_tree_buf(0) then
234-
view.close()
238+
local explorer = core.get_explorer()
239+
if explorer then
240+
explorer.view:close()
241+
end
235242
end
236243
end,
237244
})
@@ -686,10 +693,10 @@ local function localise_default_opts()
686693
end
687694

688695
function M.purge_all_state()
689-
view.View:close_all_tabs()
690-
view.View:abandon_all_windows()
691696
local explorer = core.get_explorer()
692697
if explorer then
698+
explorer.view:close_all_tabs()
699+
explorer.view:abandon_all_windows()
693700
require("nvim-tree.git").purge_state()
694701
explorer:destroy()
695702
core.reset_explorer()
@@ -742,7 +749,6 @@ function M.setup(conf)
742749
require("nvim-tree.explorer.watch").setup(opts)
743750
require("nvim-tree.git").setup(opts)
744751
require("nvim-tree.git.utils").setup(opts)
745-
require("nvim-tree.view").setup(opts)
746752
require("nvim-tree.lib").setup(opts)
747753
require("nvim-tree.renderer.components").setup(opts)
748754
require("nvim-tree.buffers").setup(opts)

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local log = require("nvim-tree.log")
2-
local view = require("nvim-tree.view")
32
local utils = require("nvim-tree.utils")
43
local core = require("nvim-tree.core")
54

@@ -14,7 +13,7 @@ local running = {}
1413
---@param path string relative or absolute
1514
function M.fn(path)
1615
local explorer = core.get_explorer()
17-
if not explorer or not view.View:is_visible() then
16+
if not explorer or not explorer.view:is_visible() then
1817
return
1918
end
2019

@@ -84,9 +83,9 @@ function M.fn(path)
8483
end)
8584
:iterate()
8685

87-
if found and view.View:is_visible() then
86+
if found and explorer.view:is_visible() then
8887
explorer.renderer:draw()
89-
view.View:set_cursor({ line, 0 })
88+
explorer.view:set_cursor({ line, 0 })
9089
end
9190

9291
running[path_real] = false

lua/nvim-tree/actions/fs/remove-file.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local core = require("nvim-tree.core")
22
local utils = require("nvim-tree.utils")
33
local events = require("nvim-tree.events")
4-
local view = require("nvim-tree.view")
54
local lib = require("nvim-tree.lib")
65
local notify = require("nvim-tree.notify")
76

@@ -14,10 +13,12 @@ local M = {
1413

1514
---@param windows integer[]
1615
local function close_windows(windows)
16+
local explorer = core.get_explorer()
17+
1718
-- Prevent from closing when the win count equals 1 or 2,
1819
-- where the win to remove could be the last opened.
1920
-- For details see #2503.
20-
if view.View.float.enable and #vim.api.nvim_list_wins() < 3 then
21+
if explorer and explorer.view.float.enable and #vim.api.nvim_list_wins() < 3 then
2122
return
2223
end
2324

@@ -30,16 +31,17 @@ end
3031

3132
---@param absolute_path string
3233
local function clear_buffer(absolute_path)
34+
local explorer = core.get_explorer()
3335
local bufs = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 })
3436
for _, buf in pairs(bufs) do
3537
if buf.name == absolute_path then
3638
local tree_winnr = vim.api.nvim_get_current_win()
37-
if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then
39+
if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.view.float.enable) then
3840
vim.api.nvim_set_current_win(buf.windows[1])
3941
vim.cmd(":bn")
4042
end
4143
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
42-
if not view.View.float.quit_on_focus_loss then
44+
if explorer and not explorer.view.float.quit_on_focus_loss then
4345
vim.api.nvim_set_current_win(tree_winnr)
4446
end
4547
if M.config.actions.remove_file.close_window then

lua/nvim-tree/actions/moves/item.lua

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local utils = require("nvim-tree.utils")
2-
local view = require("nvim-tree.view")
32
local core = require("nvim-tree.core")
43
local diagnostics = require("nvim-tree.diagnostics")
54

@@ -67,9 +66,9 @@ local function move(explorer, where, what, skip_gitignored)
6766
end
6867

6968
if nex then
70-
view.View:set_cursor({ nex, 0 })
69+
explorer.view:set_cursor({ nex, 0 })
7170
elseif vim.o.wrapscan and first then
72-
view.View:set_cursor({ first, 0 })
71+
explorer.view:set_cursor({ first, 0 })
7372
end
7473
end
7574

@@ -189,13 +188,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored)
189188

190189
-- 4.3)
191190
if node_init.name == ".." then -- root node
192-
view.View:set_cursor({ 1, 0 }) -- move to root node (position 1)
191+
explorer.view:set_cursor({ 1, 0 }) -- move to root node (position 1)
193192
else
194193
local node_init_line = utils.find_node_line(node_init)
195194
if node_init_line < 0 then
196195
return
197196
end
198-
view.View:set_cursor({ node_init_line, 0 })
197+
explorer.view:set_cursor({ node_init_line, 0 })
199198
end
200199

201200
-- 4.4)

lua/nvim-tree/actions/moves/parent.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local view = require("nvim-tree.view")
21
local utils = require("nvim-tree.utils")
32

43
local DirectoryNode = require("nvim-tree.node.directory")
@@ -25,15 +24,15 @@ function M.fn(should_close)
2524
local parent = (node:get_parent_of_group() or node).parent
2625

2726
if not parent or not parent.parent then
28-
view.View:set_cursor({ 1, 0 })
27+
node.explorer.view:set_cursor({ 1, 0 })
2928
return
3029
end
3130

3231
local _, line = utils.find_node(parent.explorer.nodes, function(n)
3332
return n.absolute_path == parent.absolute_path
3433
end)
3534

36-
view.View:set_cursor({ line + 1, 0 })
35+
node.explorer.view:set_cursor({ line + 1, 0 })
3736
if should_close then
3837
parent.open = false
3938
parent.explorer.renderer:draw()

lua/nvim-tree/actions/node/open-file.lua

+39-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
local lib = require("nvim-tree.lib")
33
local notify = require("nvim-tree.notify")
44
local utils = require("nvim-tree.utils")
5-
local view = require("nvim-tree.view")
5+
local core = require("nvim-tree.core")
66

77
local M = {}
88

@@ -19,9 +19,10 @@ end
1919
---Get all windows in the current tabpage that aren't NvimTree.
2020
---@return table with valid win_ids
2121
local function usable_win_ids()
22+
local explorer = core.get_explorer()
2223
local tabpage = vim.api.nvim_get_current_tabpage()
2324
local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
24-
local tree_winid = view.View:get_winnr(tabpage)
25+
local tree_winid = explorer and explorer.view:get_winnr(tabpage)
2526

2627
return vim.tbl_filter(function(id)
2728
local bufid = vim.api.nvim_win_get_buf(id)
@@ -198,7 +199,10 @@ end
198199

199200
local function open_file_in_tab(filename)
200201
if M.quit_on_open then
201-
view.View:close()
202+
local explorer = core.get_explorer()
203+
if explorer then
204+
explorer.view:close()
205+
end
202206
end
203207
if M.relative_path then
204208
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -208,7 +212,10 @@ end
208212

209213
local function drop(filename)
210214
if M.quit_on_open then
211-
view.View:close()
215+
local explorer = core.get_explorer()
216+
if explorer then
217+
explorer.view:close()
218+
end
212219
end
213220
if M.relative_path then
214221
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -218,7 +225,10 @@ end
218225

219226
local function tab_drop(filename)
220227
if M.quit_on_open then
221-
view.View:close()
228+
local explorer = core.get_explorer()
229+
if explorer then
230+
explorer.view:close()
231+
end
222232
end
223233
if M.relative_path then
224234
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -239,7 +249,10 @@ local function on_preview(buf_loaded)
239249
once = true,
240250
})
241251
end
242-
view.View:focus()
252+
local explorer = core.get_explorer()
253+
if explorer then
254+
explorer.view:focus()
255+
end
243256
end
244257

245258
local function get_target_winid(mode)
@@ -279,6 +292,8 @@ local function set_current_win_no_autocmd(winid, autocmd)
279292
end
280293

281294
local function open_in_new_window(filename, mode)
295+
local explorer = core.get_explorer()
296+
282297
if type(mode) ~= "string" then
283298
mode = ""
284299
end
@@ -301,7 +316,11 @@ local function open_in_new_window(filename, mode)
301316
end, vim.api.nvim_list_wins())
302317

303318
local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one
304-
local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright"
319+
320+
local new_window_side = "belowright"
321+
if explorer and (explorer.view.side == "right") then
322+
new_window_side = "aboveleft"
323+
end
305324

306325
-- Target is invalid: create new window
307326
if not vim.tbl_contains(win_ids, target_winid) then
@@ -333,7 +352,7 @@ local function open_in_new_window(filename, mode)
333352
end
334353
end
335354

336-
if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then
355+
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.float.enable then
337356
-- ignore "WinLeave" autocmd on preview
338357
-- because the registered "WinLeave"
339358
-- will kill the floating window immediately
@@ -373,7 +392,12 @@ local function is_already_loaded(filename)
373392
end
374393

375394
local function edit_in_current_buf(filename)
376-
require("nvim-tree.view").View:abandon_current_window()
395+
local explorer = core.get_explorer()
396+
397+
if explorer then
398+
explorer.view:abandon_current_window()
399+
end
400+
377401
if M.relative_path then
378402
filename = utils.path_relative(filename, vim.fn.getcwd())
379403
end
@@ -384,6 +408,8 @@ end
384408
---@param filename string
385409
---@return nil
386410
function M.fn(mode, filename)
411+
local explorer = core.get_explorer()
412+
387413
if type(mode) ~= "string" then
388414
mode = ""
389415
end
@@ -418,16 +444,16 @@ function M.fn(mode, filename)
418444
vim.bo.bufhidden = ""
419445
end
420446

421-
if M.resize_window then
422-
view.View:resize()
447+
if M.resize_window and explorer then
448+
explorer.view:resize()
423449
end
424450

425451
if mode == "preview" or mode == "preview_no_picker" then
426452
return on_preview(buf_loaded)
427453
end
428454

429-
if M.quit_on_open then
430-
view.View:close()
455+
if M.quit_on_open and explorer then
456+
explorer.view:close()
431457
end
432458
end
433459

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local core = require("nvim-tree.core")
22
local lib = require("nvim-tree.lib")
3-
local view = require("nvim-tree.view")
43
local finders_find_file = require("nvim-tree.actions.finders.find-file")
54

65
local M = {}
@@ -41,11 +40,12 @@ function M.fn(opts)
4140
return
4241
end
4342

44-
if view.View:is_visible() then
43+
local explorer = core.get_explorer()
44+
if explorer and explorer.view:is_visible() then
4545
-- focus
4646
if opts.focus then
4747
lib.set_target_win()
48-
view.View:focus()
48+
explorer.view:focus()
4949
end
5050
elseif opts.open then
5151
-- open

0 commit comments

Comments
 (0)