Skip to content

Commit fa051cf

Browse files
committed
refactor(#2826): multi instance nvim-tree.view
1 parent 0f2cda6 commit fa051cf

22 files changed

+390
-297
lines changed

lua/nvim-tree.lua

+33-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local lib = require "nvim-tree.lib"
22
local log = require "nvim-tree.log"
33
local appearance = require "nvim-tree.appearance"
44
local renderer = require "nvim-tree.renderer"
5-
local view = require "nvim-tree.view"
65
local commands = require "nvim-tree.commands"
76
local utils = require "nvim-tree.utils"
87
local actions = require "nvim-tree.actions"
@@ -81,7 +80,11 @@ function M.change_root(path, bufnr)
8180
end
8281

8382
function M.tab_enter()
84-
if view.is_visible { any_tabpage = true } then
83+
local explorer = core.get_explorer();
84+
if not explorer then
85+
return
86+
end
87+
if explorer.view:is_visible { any_tabpage = true } then
8588
local bufname = vim.api.nvim_buf_get_name(0)
8689

8790
local ft
@@ -96,13 +99,17 @@ function M.tab_enter()
9699
return
97100
end
98101
end
99-
view.open { focus_tree = false }
102+
explorer.view:open { focus_tree = false }
100103
renderer.draw()
101104
end
102105
end
103106

104107
function M.open_on_directory()
105-
local should_proceed = _config.hijack_directories.auto_open or view.is_visible()
108+
local explorer = core.get_explorer();
109+
if not explorer then
110+
return
111+
end
112+
local should_proceed = _config.hijack_directories.auto_open or explorer.view:is_visible()
106113
if not should_proceed then
107114
return
108115
end
@@ -177,8 +184,12 @@ local function setup_autocommands(opts)
177184
-- reset and draw (highlights) when colorscheme is changed
178185
create_nvim_tree_autocmd("ColorScheme", {
179186
callback = function()
187+
local explorer = core.get_explorer();
188+
if not explorer then
189+
return
190+
end
180191
appearance.setup()
181-
view.reset_winhl()
192+
explorer.view:reset_winhl()
182193
renderer.draw()
183194
end,
184195
})
@@ -190,10 +201,14 @@ local function setup_autocommands(opts)
190201
if not utils.is_nvim_tree_buf(0) then
191202
return
192203
end
204+
local explorer = core.get_explorer();
205+
if not explorer then
206+
return
207+
end
193208
if opts.actions.open_file.eject then
194-
view._prevent_buffer_override()
209+
explorer.view:_prevent_buffer_override()
195210
else
196-
view.abandon_current_window()
211+
explorer.view:abandon_current_window()
197212
end
198213
end,
199214
})
@@ -331,8 +346,12 @@ local function setup_autocommands(opts)
331346
create_nvim_tree_autocmd("WinLeave", {
332347
pattern = "NvimTree_*",
333348
callback = function()
349+
local explorer = core.get_explorer()
350+
if not explorer then
351+
return
352+
end
334353
if utils.is_nvim_tree_buf(0) then
335-
view.close()
354+
explorer.view:close()
336355
end
337356
end,
338357
})
@@ -783,8 +802,12 @@ end
783802

784803
function M.purge_all_state()
785804
require("nvim-tree.watcher").purge_watchers()
786-
view.close_all_tabs()
787-
view.abandon_all_windows()
805+
local explorer = core.get_explorer()
806+
if not explorer then
807+
return
808+
end
809+
explorer.view:close_all_tabs()
810+
explorer.view:abandon_all_windows()
788811
if core.get_explorer() ~= nil then
789812
git.purge_state()
790813
core.reset_explorer()
@@ -834,7 +857,6 @@ function M.setup(conf)
834857
require("nvim-tree.explorer").setup(opts)
835858
require("nvim-tree.git").setup(opts)
836859
require("nvim-tree.git.utils").setup(opts)
837-
require("nvim-tree.view").setup(opts)
838860
require("nvim-tree.lib").setup(opts)
839861
require("nvim-tree.renderer").setup(opts)
840862
require("nvim-tree.marks").setup(opts)

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

+4-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 renderer = require "nvim-tree.renderer"
54
local reload = require "nvim-tree.explorer.reload"
@@ -13,7 +12,8 @@ local running = {}
1312
---Find a path in the tree, expand it and focus it
1413
---@param path string relative or absolute
1514
function M.fn(path)
16-
if not core.get_explorer() or not view.is_visible() then
15+
local explorer = core.get_explorer()
16+
if not explorer or not explorer.view:is_visible() then
1717
return
1818
end
1919

@@ -75,9 +75,9 @@ function M.fn(path)
7575
end)
7676
:iterate()
7777

78-
if found and view.is_visible() then
78+
if found and explorer.view:is_visible() then
7979
renderer.draw()
80-
view.set_cursor { line, 0 }
80+
explorer.view:set_cursor { line, 0 }
8181
end
8282

8383
running[path_real] = false

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local utils = require "nvim-tree.utils"
22
local events = require "nvim-tree.events"
3-
local view = require "nvim-tree.view"
43
local lib = require "nvim-tree.lib"
54
local notify = require "nvim-tree.notify"
65

@@ -10,10 +9,14 @@ local M = {
109

1110
---@param windows integer[]
1211
local function close_windows(windows)
12+
local explorer = require "nvim-tree.core".get_explorer()
13+
if not explorer then
14+
return
15+
end
1316
-- Prevent from closing when the win count equals 1 or 2,
1417
-- where the win to remove could be the last opened.
1518
-- For details see #2503.
16-
if view.View.float.enable and #vim.api.nvim_list_wins() < 3 then
19+
if explorer.view.View.float.enable and #vim.api.nvim_list_wins() < 3 then
1720
return
1821
end
1922

@@ -26,16 +29,20 @@ end
2629

2730
---@param absolute_path string
2831
local function clear_buffer(absolute_path)
32+
local explorer = require "nvim-tree.core".get_explorer()
33+
if not explorer then
34+
return
35+
end
2936
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
3037
for _, buf in pairs(bufs) do
3138
if buf.name == absolute_path then
3239
local tree_winnr = vim.api.nvim_get_current_win()
33-
if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then
40+
if buf.hidden == 0 and (#bufs > 1 or explorer.view.View.float.enable) then
3441
vim.api.nvim_set_current_win(buf.windows[1])
3542
vim.cmd ":bn"
3643
end
3744
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
38-
if not view.View.float.quit_on_focus_loss then
45+
if not explorer.view.View.float.quit_on_focus_loss then
3946
vim.api.nvim_set_current_win(tree_winnr)
4047
end
4148
if M.config.actions.remove_file.close_window then

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

+14-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 lib = require "nvim-tree.lib"
54
local explorer_node = require "nvim-tree.explorer.node"
@@ -60,10 +59,14 @@ local function move(where, what, skip_gitignored)
6059
end
6160
end
6261

62+
local explorer = core.get_explorer()
63+
if not explorer then
64+
return
65+
end
6366
if nex then
64-
view.set_cursor { nex, 0 }
67+
explorer.view:set_cursor { nex, 0 }
6568
elseif vim.o.wrapscan and first then
66-
view.set_cursor { first, 0 }
69+
explorer.view:set_cursor { first, 0 }
6770
end
6871
end
6972

@@ -182,13 +185,19 @@ local function move_prev_recursive(what, skip_gitignored)
182185

183186
-- 4.3)
184187
if node_init.name == ".." then -- root node
185-
view.set_cursor { 1, 0 } -- move to root node (position 1)
188+
local explorer = core.get_explorer()
189+
if explorer then
190+
explorer.view:set_cursor { 1, 0 } -- move to root node (position 1)
191+
end
186192
else
187193
local node_init_line = utils.find_node_line(node_init)
188194
if node_init_line < 0 then
189195
return
190196
end
191-
view.set_cursor { node_init_line, 0 }
197+
local explorer = core.get_explorer()
198+
if explorer then
199+
explorer.view:set_cursor { node_init_line, 0 } -- move to root node (position 1)
200+
end
192201
end
193202

194203
-- 4.4)

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local renderer = require "nvim-tree.renderer"
2-
local view = require "nvim-tree.view"
32
local utils = require "nvim-tree.utils"
43
local core = require "nvim-tree.core"
54
local lib = require "nvim-tree.lib"
@@ -21,14 +20,20 @@ function M.fn(should_close)
2120
local parent = utils.get_parent_of_group(node).parent
2221

2322
if not parent or not parent.parent then
24-
return view.set_cursor { 1, 0 }
23+
local explorer = core.get_explorer()
24+
if explorer then
25+
return explorer.view:set_cursor { 1, 0 }
26+
end
2527
end
2628

2729
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
2830
return n.absolute_path == parent.absolute_path
2931
end)
3032

31-
view.set_cursor { line + 1, 0 }
33+
local explorer = core.get_explorer()
34+
if explorer then
35+
explorer.view:set_cursor { line + 1, 0 }
36+
end
3237
if should_close then
3338
parent.open = false
3439
renderer.draw()

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

+34-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
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"
65

76
local M = {}
87

@@ -19,9 +18,11 @@ end
1918
---Get all windows in the current tabpage that aren't NvimTree.
2019
---@return table with valid win_ids
2120
local function usable_win_ids()
21+
local explorer = require "nvim-tree.core".get_explorer()
22+
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.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)
@@ -188,7 +189,10 @@ end
188189

189190
local function open_file_in_tab(filename)
190191
if M.quit_on_open then
191-
view.close()
192+
local explorer = require "nvim-tree.core".get_explorer()
193+
if explorer then
194+
explorer.view:close()
195+
end
192196
end
193197
if M.relative_path then
194198
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -198,7 +202,10 @@ end
198202

199203
local function drop(filename)
200204
if M.quit_on_open then
201-
view.close()
205+
local explorer = require"nvim-tree.core".get_explorer()
206+
if explorer then
207+
explorer.view:close()
208+
end
202209
end
203210
if M.relative_path then
204211
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -208,7 +215,10 @@ end
208215

209216
local function tab_drop(filename)
210217
if M.quit_on_open then
211-
view.close()
218+
local explorer = require"nvim-tree.core".get_explorer()
219+
if explorer then
220+
explorer.view:close()
221+
end
212222
end
213223
if M.relative_path then
214224
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -229,7 +239,10 @@ local function on_preview(buf_loaded)
229239
once = true,
230240
})
231241
end
232-
view.focus()
242+
local explorer = require"nvim-tree.core".get_explorer()
243+
if explorer then
244+
explorer.view:focus()
245+
end
233246
end
234247

235248
local function get_target_winid(mode)
@@ -287,7 +300,8 @@ local function open_in_new_window(filename, mode)
287300
end, vim.api.nvim_list_wins())
288301

289302
local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one
290-
local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright"
303+
local explorer = require"nvim-tree.core".get_explorer()
304+
local new_window_side = (explorer and view.View.side == "right") and "aboveleft" or "belowright"
291305

292306
-- Target is invalid: create new window
293307
if not vim.tbl_contains(win_ids, target_winid) then
@@ -319,7 +333,7 @@ local function open_in_new_window(filename, mode)
319333
end
320334
end
321335

322-
if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then
336+
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.View.float.enable then
323337
-- ignore "WinLeave" autocmd on preview
324338
-- because the registered "WinLeave"
325339
-- will kill the floating window immediately
@@ -359,7 +373,10 @@ local function is_already_loaded(filename)
359373
end
360374

361375
local function edit_in_current_buf(filename)
362-
require("nvim-tree.view").abandon_current_window()
376+
local explorer = require"nvim-tree.core".get_explorer()
377+
if explorer then
378+
explorer.view:abandon_current_window()
379+
end
363380
if M.relative_path then
364381
filename = utils.path_relative(filename, vim.fn.getcwd())
365382
end
@@ -404,15 +421,21 @@ function M.fn(mode, filename)
404421
end
405422

406423
if M.resize_window then
407-
view.resize()
424+
local explorer = require"nvim-tree.core".get_explorer()
425+
if explorer then
426+
explorer.view:resize()
427+
end
408428
end
409429

410430
if mode == "preview" or mode == "preview_no_picker" then
411431
return on_preview(buf_loaded)
412432
end
413433

414434
if M.quit_on_open then
415-
view.close()
435+
local explorer = require"nvim-tree.core".get_explorer()
436+
if explorer then
437+
explorer.view:close()
438+
end
416439
end
417440
end
418441

0 commit comments

Comments
 (0)