Skip to content

Commit 0a06f65

Browse files
authored
refactor(#2826): move view to instanced window class (#3153)
* refactor(#2826): singleton View class, WIP * refactor(#2826): singleton View class, WIP * refactor(#2826): singleton View class, WIP * refactor(#2826): singleton View class, WIP * refactor(#2826): singleton View class, WIP * refactor(#2826): singleton View class, WIP * refactor(#2826): singleton View class, WIP * refactor(#2826): singleton View class * refactor(#2826): View is an Explorer member * refactor(#2826): move autocmds to Explorer * refactor(#2826): API uses Explorer's View * refactor(#2826): move View into Explorer package * refactor(#2826): retain necessary view globals * refactor(#2826): move all winhl to appearance constants * refactor(#2826): add lifecycle logging to all Explorer members * refactor(#2826): fix bad cherry-pick * refactor(#2826): better enumerate_options function * refactor(#2826): add View.tab_line for debugging * refactor(#2826): default lifecycle log off * refactor(#2826): add experimental.multi_instance_debug, split globals out of view, move diagnostics to its own module * refactor(#2826): instrument View:get_winnr * refactor(#2826): instrument View:setup_tabpage * refactor(#2826): instrument View:set_current_win, View:prevent_buffer_override * refactor(#2826): instrument View:get_bufnr * refactor(#2826): track member bufnr -> winid with global * refactor(#2826): tidy experiment names and logs * vim: nvim-tree: track bufnr via buffer-update channel * vim: nvim-tree: more logging * vim: nvim-tree: revert: track bufnr via buffer-update channel * refactor(#2826): notify error on view winid and bufnr mismatches * refactor(#2826): notify error on view winid and bufnr mismatches * refactor(#2826): explorer init logging
1 parent d54a187 commit 0a06f65

35 files changed

+1249
-830
lines changed

doc/nvim-tree-lua.txt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
638638
},
639639
},
640640
experimental = {
641+
multi_instance = false,
641642
},
642643
log = {
643644
enable = false,
@@ -1514,11 +1515,6 @@ Configuration options for opening a file from nvim-tree.
15141515
Resizes the tree when opening a file.
15151516
Type: `boolean`, Default: `true`
15161517

1517-
*nvim-tree.experimental.actions.open_file.relative_path*
1518-
Buffers opened by nvim-tree will use with relative paths instead of
1519-
absolute.
1520-
Type: `boolean`, Default: `true`
1521-
15221518
*nvim-tree.actions.open_file.window_picker*
15231519
Window picker configuration.
15241520

@@ -3008,9 +3004,6 @@ Windows WSL and PowerShell
30083004
- Executable file detection is disabled as this is non-performant and can
30093005
freeze nvim
30103006
- Some filesystem watcher error related to permissions will not be reported
3011-
- Some users have reported unspecified issues with
3012-
|nvim-tree.experimental.actions.open_file.relative_path|. Please report any
3013-
issues or disable this feature.
30143007

30153008
==============================================================================
30163009
13. NETRW *nvim-tree-netrw*
@@ -3145,7 +3138,6 @@ highlight group is not, hard linking as follows: >
31453138
|nvim-tree.diagnostics.show_on_open_dirs|
31463139
|nvim-tree.disable_netrw|
31473140
|nvim-tree.experimental|
3148-
|nvim-tree.experimental.actions.open_file.relative_path|
31493141
|nvim-tree.filesystem_watchers.debounce_delay|
31503142
|nvim-tree.filesystem_watchers.enable|
31513143
|nvim-tree.filesystem_watchers.ignore_dirs|

lua/nvim-tree.lua

Lines changed: 10 additions & 37 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.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.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.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,21 +148,6 @@ local function setup_autocommands(opts)
150148
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
151149
end
152150

153-
-- prevent new opened file from opening in the same window as nvim-tree
154-
create_nvim_tree_autocmd("BufWipeout", {
155-
pattern = "NvimTree_*",
156-
callback = function()
157-
if not utils.is_nvim_tree_buf(0) then
158-
return
159-
end
160-
if opts.actions.open_file.eject then
161-
view._prevent_buffer_override()
162-
else
163-
view.abandon_current_window()
164-
end
165-
end,
166-
})
167-
168151
if opts.tab.sync.open then
169152
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
170153
end
@@ -226,17 +209,6 @@ local function setup_autocommands(opts)
226209
})
227210
end
228211

229-
if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
230-
create_nvim_tree_autocmd("WinLeave", {
231-
pattern = "NvimTree_*",
232-
callback = function()
233-
if utils.is_nvim_tree_buf(0) then
234-
view.close()
235-
end
236-
end,
237-
})
238-
end
239-
240212
-- Handles event dispatch when tree is closed by `:q`
241213
create_nvim_tree_autocmd("WinClosed", {
242214
pattern = "*",
@@ -513,6 +485,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
513485
},
514486
},
515487
experimental = {
488+
multi_instance = false,
516489
},
517490
log = {
518491
enable = false,
@@ -692,10 +665,10 @@ local function localise_default_opts()
692665
end
693666

694667
function M.purge_all_state()
695-
view.close_all_tabs()
696-
view.abandon_all_windows()
697668
local explorer = core.get_explorer()
698669
if explorer then
670+
explorer.view:close_all_tabs()
671+
explorer.view:abandon_all_windows("purge_all_state")
699672
require("nvim-tree.git").purge_state()
700673
explorer:destroy()
701674
core.reset_explorer()
@@ -748,12 +721,12 @@ function M.setup(conf)
748721
require("nvim-tree.explorer.watch").setup(opts)
749722
require("nvim-tree.git").setup(opts)
750723
require("nvim-tree.git.utils").setup(opts)
751-
require("nvim-tree.view").setup(opts)
752724
require("nvim-tree.lib").setup(opts)
753725
require("nvim-tree.renderer.components").setup(opts)
754726
require("nvim-tree.buffers").setup(opts)
755727
require("nvim-tree.help").setup(opts)
756728
require("nvim-tree.watcher").setup(opts)
729+
require("nvim-tree.multi-instance-debug").setup(opts)
757730

758731
setup_autocommands(opts)
759732

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.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.is_visible() then
86+
if found and explorer.view:is_visible() then
8887
explorer.renderer:draw()
89-
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/clipboard.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ local Clipboard = Class:extend()
3131
---@protected
3232
---@param args ClipboardArgs
3333
function Clipboard:new(args)
34+
args.explorer:log_new("Clipboard")
35+
3436
self.explorer = args.explorer
3537

3638
self.data = {
@@ -42,6 +44,10 @@ function Clipboard:new(args)
4244
self.reg = self.explorer.opts.actions.use_system_clipboard and "+" or "1"
4345
end
4446

47+
function Clipboard:destroy()
48+
self.explorer:log_destroy("Clipboard")
49+
end
50+
4551
---@param source string
4652
---@param destination string
4753
---@return boolean

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.set_cursor({ nex, 0 })
69+
explorer.view:set_cursor({ nex, 0 })
7170
elseif vim.o.wrapscan and first then
72-
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.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.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.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.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()

0 commit comments

Comments
 (0)