Skip to content

Commit 77d2aa8

Browse files
committed
refactor(#2826): View is an Explorer member
1 parent 937784f commit 77d2aa8

File tree

21 files changed

+170
-114
lines changed

21 files changed

+170
-114
lines changed

lua/nvim-tree.lua

Lines changed: 22 additions & 16 deletions
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
})
@@ -692,10 +699,10 @@ local function localise_default_opts()
692699
end
693700

694701
function M.purge_all_state()
695-
view.View:close_all_tabs()
696-
view.View:abandon_all_windows()
697702
local explorer = core.get_explorer()
698703
if explorer then
704+
explorer.view:close_all_tabs()
705+
explorer.view:abandon_all_windows()
699706
require("nvim-tree.git").purge_state()
700707
explorer:destroy()
701708
core.reset_explorer()
@@ -748,7 +755,6 @@ function M.setup(conf)
748755
require("nvim-tree.explorer.watch").setup(opts)
749756
require("nvim-tree.git").setup(opts)
750757
require("nvim-tree.git.utils").setup(opts)
751-
require("nvim-tree.view").setup(opts)
752758
require("nvim-tree.lib").setup(opts)
753759
require("nvim-tree.renderer.components").setup(opts)
754760
require("nvim-tree.buffers").setup(opts)

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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 4 additions & 5 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 39 additions & 13 deletions
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)
@@ -187,7 +188,10 @@ end
187188

188189
local function open_file_in_tab(filename)
189190
if M.quit_on_open then
190-
view.View:close()
191+
local explorer = core.get_explorer()
192+
if explorer then
193+
explorer.view:close()
194+
end
191195
end
192196
if M.relative_path then
193197
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -197,7 +201,10 @@ end
197201

198202
local function drop(filename)
199203
if M.quit_on_open then
200-
view.View:close()
204+
local explorer = core.get_explorer()
205+
if explorer then
206+
explorer.view:close()
207+
end
201208
end
202209
if M.relative_path then
203210
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -207,7 +214,10 @@ end
207214

208215
local function tab_drop(filename)
209216
if M.quit_on_open then
210-
view.View:close()
217+
local explorer = core.get_explorer()
218+
if explorer then
219+
explorer.view:close()
220+
end
211221
end
212222
if M.relative_path then
213223
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -228,7 +238,10 @@ local function on_preview(buf_loaded)
228238
once = true,
229239
})
230240
end
231-
view.View:focus()
241+
local explorer = core.get_explorer()
242+
if explorer then
243+
explorer.view:focus()
244+
end
232245
end
233246

234247
local function get_target_winid(mode)
@@ -273,6 +286,8 @@ local function set_current_win_no_autocmd(winid, autocmd)
273286
end
274287

275288
local function open_in_new_window(filename, mode)
289+
local explorer = core.get_explorer()
290+
276291
if type(mode) ~= "string" then
277292
mode = ""
278293
end
@@ -295,7 +310,11 @@ local function open_in_new_window(filename, mode)
295310
end, vim.api.nvim_list_wins())
296311

297312
local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one
298-
local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright"
313+
314+
local new_window_side = "belowright"
315+
if explorer and (explorer.view.side == "right") then
316+
new_window_side = "aboveleft"
317+
end
299318

300319
-- Target is invalid: create new window
301320
if not vim.tbl_contains(win_ids, target_winid) then
@@ -327,7 +346,7 @@ local function open_in_new_window(filename, mode)
327346
end
328347
end
329348

330-
if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then
349+
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.float.enable then
331350
-- ignore "WinLeave" autocmd on preview
332351
-- because the registered "WinLeave"
333352
-- will kill the floating window immediately
@@ -367,7 +386,12 @@ local function is_already_loaded(filename)
367386
end
368387

369388
local function edit_in_current_buf(filename)
370-
require("nvim-tree.view").View:abandon_current_window()
389+
local explorer = core.get_explorer()
390+
391+
if explorer then
392+
explorer.view:abandon_current_window()
393+
end
394+
371395
if M.relative_path then
372396
filename = utils.path_relative(filename, vim.fn.getcwd())
373397
end
@@ -378,6 +402,8 @@ end
378402
---@param filename string
379403
---@return nil
380404
function M.fn(mode, filename)
405+
local explorer = core.get_explorer()
406+
381407
if type(mode) ~= "string" then
382408
mode = ""
383409
end
@@ -412,16 +438,16 @@ function M.fn(mode, filename)
412438
vim.bo.bufhidden = ""
413439
end
414440

415-
if M.resize_window then
416-
view.View:resize()
441+
if M.resize_window and explorer then
442+
explorer.view:resize()
417443
end
418444

419445
if mode == "preview" or mode == "preview_no_picker" then
420446
return on_preview(buf_loaded)
421447
end
422448

423-
if M.quit_on_open then
424-
view.View:close()
449+
if M.quit_on_open and explorer then
450+
explorer.view:close()
425451
end
426452
end
427453

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

Lines changed: 3 additions & 3 deletions
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)