Skip to content

refactor(#2826): move view to instanced window class #3153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
25d1092
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
ca7979a
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
0a052b7
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
ca2ed19
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
53643f5
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
048e637
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
00d7a30
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
937784f
refactor(#2826): singleton View class
alex-courtis Apr 20, 2025
77d2aa8
refactor(#2826): View is an Explorer member
alex-courtis Apr 21, 2025
89d68db
refactor(#2826): move autocmds to Explorer
alex-courtis Apr 21, 2025
ee72e97
refactor(#2826): API uses Explorer's View
alex-courtis Apr 21, 2025
110fa7e
refactor(#2826): move View into Explorer package
alex-courtis Apr 21, 2025
8438a74
refactor(#2826): retain necessary view globals
alex-courtis May 2, 2025
e0c7118
refactor(#2826): move all winhl to appearance constants
alex-courtis Jun 16, 2025
6bdf512
refactor(#2826): add lifecycle logging to all Explorer members
alex-courtis Jun 16, 2025
11c9d0c
refactor(#2826): fix bad cherry-pick
alex-courtis Jun 18, 2025
ca3a817
refactor(#2826): better enumerate_options function
alex-courtis Jun 17, 2025
5db04af
refactor(#2826): add View.tab_line for debugging
alex-courtis Jun 18, 2025
8e66733
refactor(#2826): default lifecycle log off
alex-courtis Jun 17, 2025
127e060
refactor(#2826): add experimental.multi_instance_debug, split globals…
alex-courtis Jun 18, 2025
cdc4395
refactor(#2826): instrument View:get_winnr
alex-courtis Jun 18, 2025
8385953
refactor(#2826): instrument View:setup_tabpage
alex-courtis Jun 18, 2025
3b792d4
refactor(#2826): instrument View:set_current_win, View:prevent_buffer…
alex-courtis Jun 18, 2025
223693b
refactor(#2826): instrument View:get_bufnr
alex-courtis Jun 18, 2025
c8a7212
refactor(#2826): track member bufnr -> winid with global
alex-courtis Jun 18, 2025
3f13193
refactor(#2826): tidy experiment names and logs
alex-courtis Jun 19, 2025
bca6ed8
vim: nvim-tree: track bufnr via buffer-update channel
alex-courtis Jun 19, 2025
0a437b3
vim: nvim-tree: more logging
alex-courtis Jun 19, 2025
6347a2c
vim: nvim-tree: revert: track bufnr via buffer-update channel
alex-courtis Jun 19, 2025
de3f486
refactor(#2826): notify error on view winid and bufnr mismatches
alex-courtis Jun 19, 2025
d822fdd
refactor(#2826): notify error on view winid and bufnr mismatches
alex-courtis Jun 19, 2025
d1df6d4
refactor(#2826): explorer init logging
alex-courtis Jun 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
},
},
experimental = {
multi_instance = false,
},
log = {
enable = false,
Expand Down Expand Up @@ -1514,11 +1515,6 @@ Configuration options for opening a file from nvim-tree.
Resizes the tree when opening a file.
Type: `boolean`, Default: `true`

*nvim-tree.experimental.actions.open_file.relative_path*
Buffers opened by nvim-tree will use with relative paths instead of
absolute.
Type: `boolean`, Default: `true`

*nvim-tree.actions.open_file.window_picker*
Window picker configuration.

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

==============================================================================
13. NETRW *nvim-tree-netrw*
Expand Down Expand Up @@ -3145,7 +3138,6 @@ highlight group is not, hard linking as follows: >
|nvim-tree.diagnostics.show_on_open_dirs|
|nvim-tree.disable_netrw|
|nvim-tree.experimental|
|nvim-tree.experimental.actions.open_file.relative_path|
|nvim-tree.filesystem_watchers.debounce_delay|
|nvim-tree.filesystem_watchers.enable|
|nvim-tree.filesystem_watchers.ignore_dirs|
Expand Down
47 changes: 10 additions & 37 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local log = require("nvim-tree.log")
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils")
local actions = require("nvim-tree.actions")
local core = require("nvim-tree.core")
Expand Down Expand Up @@ -74,7 +73,8 @@ function M.change_root(path, bufnr)
end

function M.tab_enter()
if view.is_visible({ any_tabpage = true }) then
local explorer = core.get_explorer()
if explorer and explorer.view:is_visible({ any_tabpage = true }) then
local bufname = vim.api.nvim_buf_get_name(0)

local ft
Expand All @@ -89,17 +89,15 @@ function M.tab_enter()
return
end
end
view.open({ focus_tree = false })
explorer.view:open({ focus_tree = false })

local explorer = core.get_explorer()
if explorer then
explorer.renderer:draw()
end
explorer.renderer:draw()
end
end

function M.open_on_directory()
local should_proceed = _config.hijack_directories.auto_open or view.is_visible()
local explorer = core.get_explorer()
local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.view:is_visible()
if not should_proceed then
return
end
Expand Down Expand Up @@ -150,21 +148,6 @@ local function setup_autocommands(opts)
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end

-- prevent new opened file from opening in the same window as nvim-tree
create_nvim_tree_autocmd("BufWipeout", {
pattern = "NvimTree_*",
callback = function()
if not utils.is_nvim_tree_buf(0) then
return
end
if opts.actions.open_file.eject then
view._prevent_buffer_override()
else
view.abandon_current_window()
end
end,
})

if opts.tab.sync.open then
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
end
Expand Down Expand Up @@ -226,17 +209,6 @@ local function setup_autocommands(opts)
})
end

if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
create_nvim_tree_autocmd("WinLeave", {
pattern = "NvimTree_*",
callback = function()
if utils.is_nvim_tree_buf(0) then
view.close()
end
end,
})
end

-- Handles event dispatch when tree is closed by `:q`
create_nvim_tree_autocmd("WinClosed", {
pattern = "*",
Expand Down Expand Up @@ -513,6 +485,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
},
experimental = {
multi_instance = false,
},
log = {
enable = false,
Expand Down Expand Up @@ -692,10 +665,10 @@ local function localise_default_opts()
end

function M.purge_all_state()
view.close_all_tabs()
view.abandon_all_windows()
local explorer = core.get_explorer()
if explorer then
explorer.view:close_all_tabs()
explorer.view:abandon_all_windows("purge_all_state")
require("nvim-tree.git").purge_state()
explorer:destroy()
core.reset_explorer()
Expand Down Expand Up @@ -748,12 +721,12 @@ function M.setup(conf)
require("nvim-tree.explorer.watch").setup(opts)
require("nvim-tree.git").setup(opts)
require("nvim-tree.git.utils").setup(opts)
require("nvim-tree.view").setup(opts)
require("nvim-tree.lib").setup(opts)
require("nvim-tree.renderer.components").setup(opts)
require("nvim-tree.buffers").setup(opts)
require("nvim-tree.help").setup(opts)
require("nvim-tree.watcher").setup(opts)
require("nvim-tree.multi-instance-debug").setup(opts)

setup_autocommands(opts)

Expand Down
7 changes: 3 additions & 4 deletions lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local log = require("nvim-tree.log")
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils")
local core = require("nvim-tree.core")

Expand All @@ -14,7 +13,7 @@ local running = {}
---@param path string relative or absolute
function M.fn(path)
local explorer = core.get_explorer()
if not explorer or not view.is_visible() then
if not explorer or not explorer.view:is_visible() then
return
end

Expand Down Expand Up @@ -84,9 +83,9 @@ function M.fn(path)
end)
:iterate()

if found and view.is_visible() then
if found and explorer.view:is_visible() then
explorer.renderer:draw()
view.set_cursor({ line, 0 })
explorer.view:set_cursor({ line, 0 })
end

running[path_real] = false
Expand Down
6 changes: 6 additions & 0 deletions lua/nvim-tree/actions/fs/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ local Clipboard = Class:extend()
---@protected
---@param args ClipboardArgs
function Clipboard:new(args)
args.explorer:log_new("Clipboard")

self.explorer = args.explorer

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

function Clipboard:destroy()
self.explorer:log_destroy("Clipboard")
end

---@param source string
---@param destination string
---@return boolean
Expand Down
10 changes: 6 additions & 4 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local core = require("nvim-tree.core")
local utils = require("nvim-tree.utils")
local events = require("nvim-tree.events")
local view = require("nvim-tree.view")
local lib = require("nvim-tree.lib")
local notify = require("nvim-tree.notify")

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

---@param windows integer[]
local function close_windows(windows)
local explorer = core.get_explorer()

-- Prevent from closing when the win count equals 1 or 2,
-- where the win to remove could be the last opened.
-- For details see #2503.
if view.View.float.enable and #vim.api.nvim_list_wins() < 3 then
if explorer and explorer.view.float.enable and #vim.api.nvim_list_wins() < 3 then
return
end

Expand All @@ -30,16 +31,17 @@ end

---@param absolute_path string
local function clear_buffer(absolute_path)
local explorer = core.get_explorer()
local bufs = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 })
for _, buf in pairs(bufs) do
if buf.name == absolute_path then
local tree_winnr = vim.api.nvim_get_current_win()
if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then
if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.view.float.enable) then
vim.api.nvim_set_current_win(buf.windows[1])
vim.cmd(":bn")
end
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
if not view.View.float.quit_on_focus_loss then
if explorer and not explorer.view.float.quit_on_focus_loss then
vim.api.nvim_set_current_win(tree_winnr)
end
if M.config.actions.remove_file.close_window then
Expand Down
9 changes: 4 additions & 5 deletions lua/nvim-tree/actions/moves/item.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
local core = require("nvim-tree.core")
local diagnostics = require("nvim-tree.diagnostics")

Expand Down Expand Up @@ -67,9 +66,9 @@ local function move(explorer, where, what, skip_gitignored)
end

if nex then
view.set_cursor({ nex, 0 })
explorer.view:set_cursor({ nex, 0 })
elseif vim.o.wrapscan and first then
view.set_cursor({ first, 0 })
explorer.view:set_cursor({ first, 0 })
end
end

Expand Down Expand Up @@ -189,13 +188,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored)

-- 4.3)
if node_init.name == ".." then -- root node
view.set_cursor({ 1, 0 }) -- move to root node (position 1)
explorer.view:set_cursor({ 1, 0 }) -- move to root node (position 1)
else
local node_init_line = utils.find_node_line(node_init)
if node_init_line < 0 then
return
end
view.set_cursor({ node_init_line, 0 })
explorer.view:set_cursor({ node_init_line, 0 })
end

-- 4.4)
Expand Down
5 changes: 2 additions & 3 deletions lua/nvim-tree/actions/moves/parent.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils")

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

if not parent or not parent.parent then
view.set_cursor({ 1, 0 })
node.explorer.view:set_cursor({ 1, 0 })
return
end

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

view.set_cursor({ line + 1, 0 })
node.explorer.view:set_cursor({ line + 1, 0 })
if should_close then
parent.open = false
parent.explorer.renderer:draw()
Expand Down
Loading
Loading