Skip to content

refactor(#2826): multi instance view #3109

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

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fa64574
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
3c02497
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
a3fe0c9
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
9d3d0d2
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
b95b873
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
0a04e43
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
f098195
refactor(#2826): singleton View class, WIP
alex-courtis Apr 20, 2025
f309ca2
refactor(#2826): singleton View class
alex-courtis Apr 20, 2025
0eb21f6
Merge remote-tracking branch 'origin/master' into 2826-multi-instance…
alex-courtis Apr 20, 2025
44cb3d2
refactor(#2826): View is an Explorer member
alex-courtis Apr 21, 2025
3a82885
refactor(#2826): move autocmds to Explorer
alex-courtis Apr 21, 2025
9594528
refactor(#2826): API uses Explorer's View
alex-courtis Apr 21, 2025
b5e7406
Merge branch 'master' into 2826-multi-instance-view-amc
alex-courtis Apr 21, 2025
21d532f
refactor(#2826): move View into Explorer package
alex-courtis Apr 21, 2025
6218f9c
Merge branch 'master' into 2826-multi-instance-view-amc
alex-courtis May 2, 2025
e075988
refactor(#2826): retain necessary view globals
alex-courtis May 2, 2025
76186fa
Merge remote-tracking branch 'origin/master' into 2826-multi-instance…
alex-courtis Jun 16, 2025
5377a3f
refactor(#2826): move all winhl to appearance constants
alex-courtis Jun 16, 2025
fc81249
refactor(#2826): rename View to Window
alex-courtis Jun 16, 2025
5443944
refactor(#2826): add lifecycle logging to all Explorer members
alex-courtis Jun 16, 2025
8b9c9c1
refactor(#2826): split global View and instance Window
alex-courtis Jun 16, 2025
d24f959
refactor(#2826): remove unnecessary view setup and members
alex-courtis Jun 16, 2025
c7779d9
refactor(#2826): better enumerate_options function
alex-courtis Jun 17, 2025
0d975b4
refactor(#2826): add View.tab_line for debugging
alex-courtis Jun 17, 2025
1fe6ed8
refactor(#2826): add lib.target_win_id to tabline
alex-courtis Jun 17, 2025
b1a5cb5
Merge remote-tracking branch 'origin/master' into 2826-multi-instance…
alex-courtis Jun 17, 2025
9962193
refactor(#2826): default lifecycle log off
alex-courtis Jun 17, 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
1 change: 1 addition & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
dev = false,
diagnostics = false,
git = false,
lifecycle = false,
profile = false,
watcher = false,
},
Expand Down
36 changes: 6 additions & 30 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ function M.tab_enter()
return
end
end
view.open({ focus_tree = false })

local explorer = core.get_explorer()
if explorer then
explorer.window:open({ focus_tree = false })
explorer.renderer:draw()
end
end
Expand Down Expand Up @@ -150,21 +150,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 +211,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 @@ -524,6 +498,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
dev = false,
diagnostics = false,
git = false,
lifecycle = false,
profile = false,
watcher = false,
},
Expand Down Expand Up @@ -692,9 +667,11 @@ 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.window:close_all_tabs()
end
view.abandon_all_windows()
if explorer then
require("nvim-tree.git").purge_state()
explorer:destroy()
Expand Down Expand Up @@ -748,7 +725,6 @@ 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)
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.window.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.window.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.window.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
41 changes: 31 additions & 10 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
local lib = require("nvim-tree.lib")
local notify = require("nvim-tree.notify")
local utils = require("nvim-tree.utils")
local core = require("nvim-tree.core")
local view = require("nvim-tree.view")

local M = {}
Expand Down Expand Up @@ -187,7 +188,10 @@ end

local function open_file_in_tab(filename)
if M.quit_on_open then
view.close()
local explorer = core.get_explorer()
if explorer then
explorer.window:close()
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
Expand All @@ -197,7 +201,10 @@ end

local function drop(filename)
if M.quit_on_open then
view.close()
local explorer = core.get_explorer()
if explorer then
explorer.window:close()
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
Expand All @@ -207,7 +214,10 @@ end

local function tab_drop(filename)
if M.quit_on_open then
view.close()
local explorer = core.get_explorer()
if explorer then
explorer.window:close()
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
Expand All @@ -228,7 +238,10 @@ local function on_preview(buf_loaded)
once = true,
})
end
view.focus()
local explorer = core.get_explorer()
if explorer then
explorer.window:focus()
end
end

local function get_target_winid(mode)
Expand Down Expand Up @@ -273,6 +286,8 @@ local function set_current_win_no_autocmd(winid, autocmd)
end

local function open_in_new_window(filename, mode)
local explorer = core.get_explorer()

if type(mode) ~= "string" then
mode = ""
end
Expand All @@ -295,7 +310,11 @@ local function open_in_new_window(filename, mode)
end, vim.api.nvim_list_wins())

local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one
local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright"

local new_window_side = "belowright"
if explorer and (explorer.window.side == "right") then
new_window_side = "aboveleft"
end

-- Target is invalid: create new window
if not vim.tbl_contains(win_ids, target_winid) then
Expand Down Expand Up @@ -327,7 +346,7 @@ local function open_in_new_window(filename, mode)
end
end

if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.window.float.enable then
-- ignore "WinLeave" autocmd on preview
-- because the registered "WinLeave"
-- will kill the floating window immediately
Expand Down Expand Up @@ -378,6 +397,8 @@ end
---@param filename string
---@return nil
function M.fn(mode, filename)
local explorer = core.get_explorer()

if type(mode) ~= "string" then
mode = ""
end
Expand Down Expand Up @@ -412,16 +433,16 @@ function M.fn(mode, filename)
vim.bo.bufhidden = ""
end

if M.resize_window then
view.resize()
if M.resize_window and explorer then
explorer.window:resize()
end

if mode == "preview" or mode == "preview_no_picker" then
return on_preview(buf_loaded)
end

if M.quit_on_open then
view.close()
if M.quit_on_open and explorer then
explorer.window:close()
end
end

Expand Down
5 changes: 3 additions & 2 deletions lua/nvim-tree/actions/tree/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ function M.fn(opts)
return
end

if view.is_visible() then
local explorer = core.get_explorer()
if explorer and view.is_visible() then
-- focus
if opts.focus then
lib.set_target_win()
view.focus()
explorer.window:focus()
end
elseif opts.open then
-- open
Expand Down
7 changes: 5 additions & 2 deletions lua/nvim-tree/actions/tree/open.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib")
local view = require("nvim-tree.view")
local finders_find_file = require("nvim-tree.actions.finders.find-file")
Expand All @@ -23,10 +24,12 @@ function M.fn(opts)
opts.path = nil
end

if view.is_visible() then
local explorer = core.get_explorer()

if explorer and view.is_visible() then
-- focus
lib.set_target_win()
view.focus()
explorer.window:focus()
else
-- open
lib.open({
Expand Down
21 changes: 13 additions & 8 deletions lua/nvim-tree/actions/tree/resize.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
local view = require("nvim-tree.view")
local core = require("nvim-tree.core")

local M = {}

---Resize the tree, persisting the new size.
---@param opts ApiTreeResizeOpts|nil
function M.fn(opts)
local explorer = core.get_explorer()
if not explorer then
return
end

if opts == nil then
-- reset to config values
view.configure_width()
view.resize()
explorer.window:configure_width()
explorer.window:resize()
return
end

local options = opts or {}
local width_cfg = options.width

if width_cfg ~= nil then
view.configure_width(width_cfg)
view.resize()
explorer.window:configure_width(width_cfg)
explorer.window:resize()
return
end

if not view.is_width_determined() then
if not explorer.window:is_width_determined() then
-- {absolute} and {relative} do nothing when {width} is a function.
return
end

local absolute = options.absolute
if type(absolute) == "number" then
view.resize(absolute)
explorer.window:resize(absolute)
return
end

Expand All @@ -39,7 +44,7 @@ function M.fn(opts)
relative_size = "+" .. relative_size
end

view.resize(relative_size)
explorer.window:resize(relative_size)
return
end
end
Expand Down
7 changes: 5 additions & 2 deletions lua/nvim-tree/actions/tree/toggle.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib")
local view = require("nvim-tree.view")
local finders_find_file = require("nvim-tree.actions.finders.find-file")
Expand All @@ -10,6 +11,8 @@ local M = {}
---@param cwd boolean|nil legacy -> opts.path
---@param bang boolean|nil legacy -> opts.update_root
function M.fn(opts, no_focus, cwd, bang)
local explorer = core.get_explorer()

-- legacy arguments
if type(opts) == "boolean" then
opts = {
Expand Down Expand Up @@ -40,9 +43,9 @@ function M.fn(opts, no_focus, cwd, bang)
opts.path = nil
end

if view.is_visible() then
if explorer and view.is_visible() then
-- close
view.close()
explorer.window:close()
else
-- open
lib.open({
Expand Down
Loading
Loading