refactor(#2826): View is an Explorer member

This commit is contained in:
Alexander Courtis 2025-04-21 12:02:19 +10:00
parent 0eb21f66f7
commit 44cb3d2f0a
21 changed files with 170 additions and 114 deletions

View File

@ -1,5 +1,4 @@
local log = require("nvim-tree.log") local log = require("nvim-tree.log")
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local actions = require("nvim-tree.actions") local actions = require("nvim-tree.actions")
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
@ -74,7 +73,8 @@ function M.change_root(path, bufnr)
end end
function M.tab_enter() function M.tab_enter()
if view.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 bufname = vim.api.nvim_buf_get_name(0)
local ft local ft
@ -89,17 +89,15 @@ function M.tab_enter()
return return
end end
end end
view.View:open({ focus_tree = false }) explorer.view:open({ focus_tree = false })
local explorer = core.get_explorer() explorer.renderer:draw()
if explorer then
explorer.renderer:draw()
end
end end
end end
function M.open_on_directory() function M.open_on_directory()
local should_proceed = _config.hijack_directories.auto_open or view.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 if not should_proceed then
return return
end end
@ -150,6 +148,7 @@ local function setup_autocommands(opts)
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end end
-- TODO #2826 move this to explorer
-- prevent new opened file from opening in the same window as nvim-tree -- prevent new opened file from opening in the same window as nvim-tree
create_nvim_tree_autocmd("BufWipeout", { create_nvim_tree_autocmd("BufWipeout", {
pattern = "NvimTree_*", pattern = "NvimTree_*",
@ -157,10 +156,14 @@ local function setup_autocommands(opts)
if not utils.is_nvim_tree_buf(0) then if not utils.is_nvim_tree_buf(0) then
return return
end end
if opts.actions.open_file.eject then
view.View:prevent_buffer_override() local explorer = core.get_explorer()
else if explorer then
view.View:abandon_current_window() if opts.actions.open_file.eject then
explorer.view:prevent_buffer_override()
else
explorer.view:abandon_current_window()
end
end end
end, end,
}) })
@ -226,12 +229,16 @@ local function setup_autocommands(opts)
}) })
end end
-- TODO #2826 move this to explorer
if opts.view.float.enable and opts.view.float.quit_on_focus_loss then if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
create_nvim_tree_autocmd("WinLeave", { create_nvim_tree_autocmd("WinLeave", {
pattern = "NvimTree_*", pattern = "NvimTree_*",
callback = function() callback = function()
if utils.is_nvim_tree_buf(0) then if utils.is_nvim_tree_buf(0) then
view.close() local explorer = core.get_explorer()
if explorer then
explorer.view:close()
end
end end
end, end,
}) })
@ -686,10 +693,10 @@ local function localise_default_opts()
end end
function M.purge_all_state() function M.purge_all_state()
view.View:close_all_tabs()
view.View:abandon_all_windows()
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:close_all_tabs()
explorer.view:abandon_all_windows()
require("nvim-tree.git").purge_state() require("nvim-tree.git").purge_state()
explorer:destroy() explorer:destroy()
core.reset_explorer() core.reset_explorer()
@ -742,7 +749,6 @@ function M.setup(conf)
require("nvim-tree.explorer.watch").setup(opts) require("nvim-tree.explorer.watch").setup(opts)
require("nvim-tree.git").setup(opts) require("nvim-tree.git").setup(opts)
require("nvim-tree.git.utils").setup(opts) require("nvim-tree.git.utils").setup(opts)
require("nvim-tree.view").setup(opts)
require("nvim-tree.lib").setup(opts) require("nvim-tree.lib").setup(opts)
require("nvim-tree.renderer.components").setup(opts) require("nvim-tree.renderer.components").setup(opts)
require("nvim-tree.buffers").setup(opts) require("nvim-tree.buffers").setup(opts)

View File

@ -1,5 +1,4 @@
local log = require("nvim-tree.log") local log = require("nvim-tree.log")
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
@ -14,7 +13,7 @@ local running = {}
---@param path string relative or absolute ---@param path string relative or absolute
function M.fn(path) function M.fn(path)
local explorer = core.get_explorer() local explorer = core.get_explorer()
if not explorer or not view.View:is_visible() then if not explorer or not explorer.view:is_visible() then
return return
end end
@ -84,9 +83,9 @@ function M.fn(path)
end) end)
:iterate() :iterate()
if found and view.View:is_visible() then if found and explorer.view:is_visible() then
explorer.renderer:draw() explorer.renderer:draw()
view.View:set_cursor({ line, 0 }) explorer.view:set_cursor({ line, 0 })
end end
running[path_real] = false running[path_real] = false

View File

@ -1,7 +1,6 @@
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local events = require("nvim-tree.events") local events = require("nvim-tree.events")
local view = require("nvim-tree.view")
local lib = require("nvim-tree.lib") local lib = require("nvim-tree.lib")
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
@ -14,10 +13,12 @@ local M = {
---@param windows integer[] ---@param windows integer[]
local function close_windows(windows) local function close_windows(windows)
local explorer = core.get_explorer()
-- Prevent from closing when the win count equals 1 or 2, -- Prevent from closing when the win count equals 1 or 2,
-- where the win to remove could be the last opened. -- where the win to remove could be the last opened.
-- For details see #2503. -- 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 return
end end
@ -30,16 +31,17 @@ end
---@param absolute_path string ---@param absolute_path string
local function clear_buffer(absolute_path) local function clear_buffer(absolute_path)
local explorer = core.get_explorer()
local bufs = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 }) local bufs = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 })
for _, buf in pairs(bufs) do for _, buf in pairs(bufs) do
if buf.name == absolute_path then if buf.name == absolute_path then
local tree_winnr = vim.api.nvim_get_current_win() 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.api.nvim_set_current_win(buf.windows[1])
vim.cmd(":bn") vim.cmd(":bn")
end end
vim.api.nvim_buf_delete(buf.bufnr, { force = true }) 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) vim.api.nvim_set_current_win(tree_winnr)
end end
if M.config.actions.remove_file.close_window then if M.config.actions.remove_file.close_window then

View File

@ -1,5 +1,4 @@
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
local diagnostics = require("nvim-tree.diagnostics") local diagnostics = require("nvim-tree.diagnostics")
@ -67,9 +66,9 @@ local function move(explorer, where, what, skip_gitignored)
end end
if nex then if nex then
view.View:set_cursor({ nex, 0 }) explorer.view:set_cursor({ nex, 0 })
elseif vim.o.wrapscan and first then elseif vim.o.wrapscan and first then
view.View:set_cursor({ first, 0 }) explorer.view:set_cursor({ first, 0 })
end end
end end
@ -189,13 +188,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored)
-- 4.3) -- 4.3)
if node_init.name == ".." then -- root node if node_init.name == ".." then -- root node
view.View:set_cursor({ 1, 0 }) -- move to root node (position 1) explorer.view:set_cursor({ 1, 0 }) -- move to root node (position 1)
else else
local node_init_line = utils.find_node_line(node_init) local node_init_line = utils.find_node_line(node_init)
if node_init_line < 0 then if node_init_line < 0 then
return return
end end
view.View:set_cursor({ node_init_line, 0 }) explorer.view:set_cursor({ node_init_line, 0 })
end end
-- 4.4) -- 4.4)

View File

@ -1,4 +1,3 @@
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local DirectoryNode = require("nvim-tree.node.directory") local DirectoryNode = require("nvim-tree.node.directory")
@ -25,7 +24,7 @@ function M.fn(should_close)
local parent = (node:get_parent_of_group() or node).parent local parent = (node:get_parent_of_group() or node).parent
if not parent or not parent.parent then if not parent or not parent.parent then
view.View:set_cursor({ 1, 0 }) node.explorer.view:set_cursor({ 1, 0 })
return return
end end
@ -33,7 +32,7 @@ function M.fn(should_close)
return n.absolute_path == parent.absolute_path return n.absolute_path == parent.absolute_path
end) end)
view.View:set_cursor({ line + 1, 0 }) node.explorer.view:set_cursor({ line + 1, 0 })
if should_close then if should_close then
parent.open = false parent.open = false
parent.explorer.renderer:draw() parent.explorer.renderer:draw()

View File

@ -2,7 +2,7 @@
local lib = require("nvim-tree.lib") local lib = require("nvim-tree.lib")
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view") local core = require("nvim-tree.core")
local M = {} local M = {}
@ -19,9 +19,10 @@ end
---Get all windows in the current tabpage that aren't NvimTree. ---Get all windows in the current tabpage that aren't NvimTree.
---@return table with valid win_ids ---@return table with valid win_ids
local function usable_win_ids() local function usable_win_ids()
local explorer = core.get_explorer()
local tabpage = vim.api.nvim_get_current_tabpage() local tabpage = vim.api.nvim_get_current_tabpage()
local win_ids = vim.api.nvim_tabpage_list_wins(tabpage) local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
local tree_winid = view.View:get_winnr(tabpage) local tree_winid = explorer and explorer.view:get_winnr(tabpage)
return vim.tbl_filter(function(id) return vim.tbl_filter(function(id)
local bufid = vim.api.nvim_win_get_buf(id) local bufid = vim.api.nvim_win_get_buf(id)
@ -198,7 +199,10 @@ end
local function open_file_in_tab(filename) local function open_file_in_tab(filename)
if M.quit_on_open then if M.quit_on_open then
view.View:close() local explorer = core.get_explorer()
if explorer then
explorer.view:close()
end
end end
if M.relative_path then if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd()) filename = utils.path_relative(filename, vim.fn.getcwd())
@ -208,7 +212,10 @@ end
local function drop(filename) local function drop(filename)
if M.quit_on_open then if M.quit_on_open then
view.View:close() local explorer = core.get_explorer()
if explorer then
explorer.view:close()
end
end end
if M.relative_path then if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd()) filename = utils.path_relative(filename, vim.fn.getcwd())
@ -218,7 +225,10 @@ end
local function tab_drop(filename) local function tab_drop(filename)
if M.quit_on_open then if M.quit_on_open then
view.View:close() local explorer = core.get_explorer()
if explorer then
explorer.view:close()
end
end end
if M.relative_path then if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd()) filename = utils.path_relative(filename, vim.fn.getcwd())
@ -239,7 +249,10 @@ local function on_preview(buf_loaded)
once = true, once = true,
}) })
end end
view.View:focus() local explorer = core.get_explorer()
if explorer then
explorer.view:focus()
end
end end
local function get_target_winid(mode) local function get_target_winid(mode)
@ -279,6 +292,8 @@ local function set_current_win_no_autocmd(winid, autocmd)
end end
local function open_in_new_window(filename, mode) local function open_in_new_window(filename, mode)
local explorer = core.get_explorer()
if type(mode) ~= "string" then if type(mode) ~= "string" then
mode = "" mode = ""
end end
@ -301,7 +316,11 @@ local function open_in_new_window(filename, mode)
end, vim.api.nvim_list_wins()) 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 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.view.side == "right") then
new_window_side = "aboveleft"
end
-- Target is invalid: create new window -- Target is invalid: create new window
if not vim.tbl_contains(win_ids, target_winid) then if not vim.tbl_contains(win_ids, target_winid) then
@ -333,7 +352,7 @@ local function open_in_new_window(filename, mode)
end end
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.view.float.enable then
-- ignore "WinLeave" autocmd on preview -- ignore "WinLeave" autocmd on preview
-- because the registered "WinLeave" -- because the registered "WinLeave"
-- will kill the floating window immediately -- will kill the floating window immediately
@ -373,7 +392,12 @@ local function is_already_loaded(filename)
end end
local function edit_in_current_buf(filename) local function edit_in_current_buf(filename)
require("nvim-tree.view").View:abandon_current_window() local explorer = core.get_explorer()
if explorer then
explorer.view:abandon_current_window()
end
if M.relative_path then if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd()) filename = utils.path_relative(filename, vim.fn.getcwd())
end end
@ -384,6 +408,8 @@ end
---@param filename string ---@param filename string
---@return nil ---@return nil
function M.fn(mode, filename) function M.fn(mode, filename)
local explorer = core.get_explorer()
if type(mode) ~= "string" then if type(mode) ~= "string" then
mode = "" mode = ""
end end
@ -418,16 +444,16 @@ function M.fn(mode, filename)
vim.bo.bufhidden = "" vim.bo.bufhidden = ""
end end
if M.resize_window then if M.resize_window and explorer then
view.View:resize() explorer.view:resize()
end end
if mode == "preview" or mode == "preview_no_picker" then if mode == "preview" or mode == "preview_no_picker" then
return on_preview(buf_loaded) return on_preview(buf_loaded)
end end
if M.quit_on_open then if M.quit_on_open and explorer then
view.View:close() explorer.view:close()
end end
end end

View File

@ -1,6 +1,5 @@
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib") local lib = require("nvim-tree.lib")
local view = require("nvim-tree.view")
local finders_find_file = require("nvim-tree.actions.finders.find-file") local finders_find_file = require("nvim-tree.actions.finders.find-file")
local M = {} local M = {}
@ -41,11 +40,12 @@ function M.fn(opts)
return return
end end
if view.View:is_visible() then local explorer = core.get_explorer()
if explorer and explorer.view:is_visible() then
-- focus -- focus
if opts.focus then if opts.focus then
lib.set_target_win() lib.set_target_win()
view.View:focus() explorer.view:focus()
end end
elseif opts.open then elseif opts.open then
-- open -- open

View File

@ -1,5 +1,5 @@
local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib") local lib = require("nvim-tree.lib")
local view = require("nvim-tree.view")
local finders_find_file = require("nvim-tree.actions.finders.find-file") local finders_find_file = require("nvim-tree.actions.finders.find-file")
local M = {} local M = {}
@ -23,10 +23,12 @@ function M.fn(opts)
opts.path = nil opts.path = nil
end end
if view.View:is_visible() then local explorer = core.get_explorer()
if explorer and explorer.view:is_visible() then
-- focus -- focus
lib.set_target_win() lib.set_target_win()
view.View:focus() explorer.view:focus()
else else
-- open -- open
lib.open({ lib.open({

View File

@ -1,14 +1,19 @@
local view = require("nvim-tree.view") local core = require("nvim-tree.core")
local M = {} local M = {}
---Resize the tree, persisting the new size. ---Resize the tree, persisting the new size.
---@param opts ApiTreeResizeOpts|nil ---@param opts ApiTreeResizeOpts|nil
function M.fn(opts) function M.fn(opts)
local explorer = core.get_explorer()
if not explorer then
return
end
if opts == nil then if opts == nil then
-- reset to config values -- reset to config values
view.View:configure_width() explorer.view:configure_width()
view.View:resize() explorer.view:resize()
return return
end end
@ -16,19 +21,19 @@ function M.fn(opts)
local width_cfg = options.width local width_cfg = options.width
if width_cfg ~= nil then if width_cfg ~= nil then
view.View:configure_width(width_cfg) explorer.view:configure_width(width_cfg)
view.View:resize() explorer.view:resize()
return return
end end
if not view.View:is_width_determined() then if not explorer.view:is_width_determined() then
-- {absolute} and {relative} do nothing when {width} is a function. -- {absolute} and {relative} do nothing when {width} is a function.
return return
end end
local absolute = options.absolute local absolute = options.absolute
if type(absolute) == "number" then if type(absolute) == "number" then
view.View:resize(absolute) explorer.view:resize(absolute)
return return
end end
@ -39,7 +44,7 @@ function M.fn(opts)
relative_size = "+" .. relative_size relative_size = "+" .. relative_size
end end
view.View:resize(relative_size) explorer.view:resize(relative_size)
return return
end end
end end

View File

@ -1,5 +1,5 @@
local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib") local lib = require("nvim-tree.lib")
local view = require("nvim-tree.view")
local finders_find_file = require("nvim-tree.actions.finders.find-file") local finders_find_file = require("nvim-tree.actions.finders.find-file")
local M = {} local M = {}
@ -10,6 +10,8 @@ local M = {}
---@param cwd boolean|nil legacy -> opts.path ---@param cwd boolean|nil legacy -> opts.path
---@param bang boolean|nil legacy -> opts.update_root ---@param bang boolean|nil legacy -> opts.update_root
function M.fn(opts, no_focus, cwd, bang) function M.fn(opts, no_focus, cwd, bang)
local explorer = core.get_explorer()
-- legacy arguments -- legacy arguments
if type(opts) == "boolean" then if type(opts) == "boolean" then
opts = { opts = {
@ -40,9 +42,9 @@ function M.fn(opts, no_focus, cwd, bang)
opts.path = nil opts.path = nil
end end
if view.View:is_visible() then if explorer and explorer.view:is_visible() then
-- close -- close
view.View:close() explorer.view:close()
else else
-- open -- open
lib.open({ lib.open({

View File

@ -1,5 +1,5 @@
local api = require("nvim-tree.api") local api = require("nvim-tree.api")
local view = require("nvim-tree.view") local core = require("nvim-tree.core")
local M = {} local M = {}
@ -111,7 +111,10 @@ local CMDS = {
bar = true, bar = true,
}, },
command = function(c) command = function(c)
view.View:resize(c.args) local explorer = core.get_explorer()
if explorer then
explorer.view:resize(c.args)
end
end, end,
}, },
{ {

View File

@ -1,6 +1,5 @@
local events = require("nvim-tree.events") local events = require("nvim-tree.events")
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
local view = require("nvim-tree.view")
local log = require("nvim-tree.log") local log = require("nvim-tree.log")
local M = {} local M = {}
@ -55,7 +54,7 @@ end
---@return integer ---@return integer
function M.get_nodes_starting_line() function M.get_nodes_starting_line()
local offset = 1 local offset = 1
if view.View:is_root_folder_visible(M.get_cwd()) then if TreeExplorer and TreeExplorer.view:is_root_folder_visible(M.get_cwd()) then
offset = offset + 1 offset = offset + 1
end end
if TreeExplorer and TreeExplorer.live_filter.filter then if TreeExplorer and TreeExplorer.live_filter.filter then

View File

@ -1,6 +1,5 @@
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
local log = require("nvim-tree.log") local log = require("nvim-tree.log")
local DirectoryNode = require("nvim-tree.node.directory") local DirectoryNode = require("nvim-tree.node.directory")
@ -182,10 +181,15 @@ function M.update_coc()
end end
log.profile_end(profile) log.profile_end(profile)
local bufnr = view.View:get_bufnr() local explorer = core.get_explorer()
local bufnr
if explorer then
bufnr = explorer.view:get_bufnr()
end
local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr) local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
if should_draw then if should_draw then
local explorer = core.get_explorer()
if explorer then if explorer then
explorer.renderer:draw() explorer.renderer:draw()
end end

View File

@ -4,7 +4,6 @@ local core = require("nvim-tree.core")
local git = require("nvim-tree.git") local git = require("nvim-tree.git")
local log = require("nvim-tree.log") local log = require("nvim-tree.log")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
local node_factory = require("nvim-tree.node.factory") local node_factory = require("nvim-tree.node.factory")
local DirectoryNode = require("nvim-tree.node.directory") local DirectoryNode = require("nvim-tree.node.directory")
@ -20,6 +19,7 @@ local LiveFilter = require("nvim-tree.explorer.live-filter")
local Sorter = require("nvim-tree.explorer.sorter") local Sorter = require("nvim-tree.explorer.sorter")
local Clipboard = require("nvim-tree.actions.fs.clipboard") local Clipboard = require("nvim-tree.actions.fs.clipboard")
local Renderer = require("nvim-tree.renderer") local Renderer = require("nvim-tree.renderer")
local View = require("nvim-tree.view")
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
@ -35,6 +35,7 @@ local config
---@field sorters Sorter ---@field sorters Sorter
---@field marks Marks ---@field marks Marks
---@field clipboard Clipboard ---@field clipboard Clipboard
---@field view View
local Explorer = RootNode:extend() local Explorer = RootNode:extend()
---@class Explorer ---@class Explorer
@ -64,6 +65,7 @@ function Explorer:new(args)
self.live_filter = LiveFilter({ explorer = self }) self.live_filter = LiveFilter({ explorer = self })
self.marks = Marks({ explorer = self }) self.marks = Marks({ explorer = self })
self.clipboard = Clipboard({ explorer = self }) self.clipboard = Clipboard({ explorer = self })
self.view = View({ explorer = self })
self:create_autocmds() self:create_autocmds()
@ -84,7 +86,7 @@ function Explorer:create_autocmds()
group = self.augroup_id, group = self.augroup_id,
callback = function() callback = function()
appearance.setup() appearance.setup()
view.View:reset_winhl() self.view:reset_winhl()
self.renderer:draw() self.renderer:draw()
end, end,
}) })
@ -486,7 +488,7 @@ function Explorer:reload_explorer()
local projects = git.reload_all_projects() local projects = git.reload_all_projects()
self:refresh_nodes(projects) self:refresh_nodes(projects)
if view.View:is_visible() then if self.view:is_visible() then
self.renderer:draw() self.renderer:draw()
end end
event_running = false event_running = false
@ -508,7 +510,7 @@ end
---nil on no explorer or invalid view win ---nil on no explorer or invalid view win
---@return integer[]|nil ---@return integer[]|nil
function Explorer:get_cursor_position() function Explorer:get_cursor_position()
local winnr = view.View:get_winnr() local winnr = self.view:get_winnr()
if not winnr or not vim.api.nvim_win_is_valid(winnr) then if not winnr or not vim.api.nvim_win_is_valid(winnr) then
return return
end end
@ -523,7 +525,7 @@ function Explorer:get_node_at_cursor()
return return
end end
if cursor[1] == 1 and view.View:is_root_folder_visible(core.get_cwd()) then if cursor[1] == 1 and self.view:is_root_folder_visible(core.get_cwd()) then
return self return self
end end

View File

@ -1,4 +1,3 @@
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local Class = require("nvim-tree.classic") local Class = require("nvim-tree.classic")
@ -56,14 +55,14 @@ local overlay_bufnr = 0
local overlay_winnr = 0 local overlay_winnr = 0
local function remove_overlay(self) local function remove_overlay(self)
if view.View.float.enable and view.View.float.quit_on_focus_loss then if self.explorer.view.float.enable and self.explorer.view.float.quit_on_focus_loss then
-- return to normal nvim-tree float behaviour when filter window is closed -- return to normal nvim-tree float behaviour when filter window is closed
vim.api.nvim_create_autocmd("WinLeave", { vim.api.nvim_create_autocmd("WinLeave", {
pattern = "NvimTree_*", pattern = "NvimTree_*",
group = vim.api.nvim_create_augroup("NvimTree", { clear = false }), group = vim.api.nvim_create_augroup("NvimTree", { clear = false }),
callback = function() callback = function()
if utils.is_nvim_tree_buf(0) then if utils.is_nvim_tree_buf(0) then
view.View:close() self.explorer.view:close()
end end
end, end,
}) })
@ -156,7 +155,7 @@ end
---@return integer ---@return integer
local function calculate_overlay_win_width(self) local function calculate_overlay_win_width(self)
local wininfo = vim.fn.getwininfo(view.View:get_winnr())[1] local wininfo = vim.fn.getwininfo(self.explorer.view:get_winnr())[1]
if wininfo then if wininfo then
return wininfo.width - wininfo.textoff - #self.prefix return wininfo.width - wininfo.textoff - #self.prefix
@ -166,7 +165,7 @@ local function calculate_overlay_win_width(self)
end end
local function create_overlay(self) local function create_overlay(self)
if view.View.float.enable then if self.explorer.view.float.enable then
-- don't close nvim-tree float when focus is changed to filter window -- don't close nvim-tree float when focus is changed to filter window
vim.api.nvim_clear_autocmds({ vim.api.nvim_clear_autocmds({
event = "WinLeave", event = "WinLeave",
@ -198,13 +197,13 @@ local function create_overlay(self)
end end
function LiveFilter:start_filtering() function LiveFilter:start_filtering()
view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor() self.explorer.view.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
self.filter = self.filter or "" self.filter = self.filter or ""
self.explorer.renderer:draw() self.explorer.renderer:draw()
local row = require("nvim-tree.core").get_nodes_starting_line() - 1 local row = require("nvim-tree.core").get_nodes_starting_line() - 1
local col = #self.prefix > 0 and #self.prefix - 1 or 1 local col = #self.prefix > 0 and #self.prefix - 1 or 1
view.View:set_cursor({ row, col }) self.explorer.view:set_cursor({ row, col })
-- needs scheduling to let the cursor move before initializing the window -- needs scheduling to let the cursor move before initializing the window
vim.schedule(function() vim.schedule(function()
return create_overlay(self) return create_overlay(self)
@ -213,7 +212,7 @@ end
function LiveFilter:clear_filter() function LiveFilter:clear_filter()
local node = self.explorer:get_node_at_cursor() local node = self.explorer:get_node_at_cursor()
local last_node = view.View.live_filter.prev_focused_node local last_node = self.explorer.view.live_filter.prev_focused_node
self.filter = nil self.filter = nil
reset_filter(self) reset_filter(self)

View File

@ -48,7 +48,7 @@ function NodeIterator:recursor(f)
end end
---@return Node|nil ---@return Node|nil
---@return number|nil ---@return number
function NodeIterator:iterate() function NodeIterator:iterate()
local iteration_count = 0 local iteration_count = 0
local function iter(nodes) local function iter(nodes)

View File

@ -1,4 +1,3 @@
local view = require("nvim-tree.view")
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
@ -12,9 +11,11 @@ local M = {
} }
function M.set_target_win() function M.set_target_win()
local explorer = core.get_explorer()
local id = vim.api.nvim_get_current_win() local id = vim.api.nvim_get_current_win()
local tree_id = view.View:get_winnr()
if tree_id and id == tree_id then if explorer and id == explorer.view:get_winnr() then
M.target_winid = 0 M.target_winid = 0
return return
end end
@ -30,11 +31,16 @@ local function handle_buf_cwd(cwd)
end end
local function open_view_and_draw() local function open_view_and_draw()
local explorer = core.get_explorer()
local cwd = vim.fn.getcwd() local cwd = vim.fn.getcwd()
view.View:open()
if explorer then
explorer.view:open()
end
handle_buf_cwd(cwd) handle_buf_cwd(cwd)
local explorer = core.get_explorer()
if explorer then if explorer then
explorer.renderer:draw() explorer.renderer:draw()
end end
@ -110,25 +116,28 @@ function M.open(opts)
local explorer = core.get_explorer() local explorer = core.get_explorer()
if should_hijack_current_buf() then if should_hijack_current_buf() then
view.View:close_this_tab_only()
view.View:open_in_win()
if explorer then if explorer then
explorer.view:close_this_tab_only()
explorer.view:open_in_win()
explorer.renderer:draw() explorer.renderer:draw()
end end
elseif opts.winid then elseif opts.winid then
view.View:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid })
if explorer then if explorer then
explorer.view:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid })
explorer.renderer:draw() explorer.renderer:draw()
end end
elseif opts.current_window then elseif opts.current_window then
view.View:open_in_win({ hijack_current_buf = false, resize = false })
if explorer then if explorer then
explorer.view:open_in_win({ hijack_current_buf = false, resize = false })
explorer.renderer:draw() explorer.renderer:draw()
end end
else else
open_view_and_draw() open_view_and_draw()
end end
view.View:restore_tab_state()
if explorer then
explorer.view:restore_tab_state()
end
end end
function M.setup(opts) function M.setup(opts)

View File

@ -1,6 +1,5 @@
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
local Class = require("nvim-tree.classic") local Class = require("nvim-tree.classic")
@ -379,7 +378,7 @@ end
---@private ---@private
function Builder:build_header() function Builder:build_header()
if view.View:is_root_folder_visible(self.explorer.absolute_path) then if self.explorer.view:is_root_folder_visible(self.explorer.absolute_path) then
local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label) local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label)
table.insert(self.lines, root_name) table.insert(self.lines, root_name)
self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name)) self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name))

View File

@ -1,5 +1,4 @@
local log = require("nvim-tree.log") local log = require("nvim-tree.log")
local view = require("nvim-tree.view")
local events = require("nvim-tree.events") local events = require("nvim-tree.events")
local Class = require("nvim-tree.classic") local Class = require("nvim-tree.classic")
@ -96,28 +95,28 @@ function Renderer:render_hl(bufnr, hl_range_args)
end end
function Renderer:draw() function Renderer:draw()
local bufnr = view.View:get_bufnr() local bufnr = self.explorer.view:get_bufnr()
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return return
end end
local profile = log.profile_start("draw") local profile = log.profile_start("draw")
local cursor = vim.api.nvim_win_get_cursor(view.View:get_winnr() or 0) local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr() or 0)
local builder = Builder(self.explorer):build() local builder = Builder(self.explorer):build()
self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines) self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines)
if cursor and #builder.lines >= cursor[1] then if cursor and #builder.lines >= cursor[1] then
vim.api.nvim_win_set_cursor(view.View:get_winnr() or 0, cursor) vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr() or 0, cursor)
end end
view.View:grow_from_content() self.explorer.view:grow_from_content()
log.profile_end(profile) log.profile_end(profile)
events._dispatch_on_tree_rendered(bufnr, view.View:get_winnr()) events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr())
end end
return Renderer return Renderer

View File

@ -144,10 +144,16 @@ function M.find_node(nodes, fn)
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes) return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
end) end)
:iterate() :iterate()
i = require("nvim-tree.view").View:is_root_folder_visible() and i or i - 1
if node and node.explorer.live_filter.filter then if node then
i = i + 1 if not node.explorer.view:is_root_folder_visible() then
i = i - 1
end
if node.explorer.live_filter.filter then
i = i + 1
end
end end
return node, i return node, i
end end
@ -497,7 +503,10 @@ function M.focus_file(path)
local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node) local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node)
return node.absolute_path == path return node.absolute_path == path
end) end)
require("nvim-tree.view").View:set_cursor({ i + 1, 1 }) local explorer = require("nvim-tree.core").get_explorer()
if explorer then
explorer.view:set_cursor({ i + 1, 1 })
end
end end
---Focus node passed as parameter if visible, otherwise focus first visible parent. ---Focus node passed as parameter if visible, otherwise focus first visible parent.
@ -517,7 +526,7 @@ function M.focus_node_or_parent(node)
end) end)
if found_node or node.parent == nil then if found_node or node.parent == nil then
require("nvim-tree.view").View:set_cursor({ i + 1, 1 }) explorer.view:set_cursor({ i + 1, 1 })
break break
end end

View File

@ -10,8 +10,6 @@ local Class = require("nvim-tree.classic")
---@field resize boolean|nil default true ---@field resize boolean|nil default true
---@field winid number|nil 0 or nil for current ---@field winid number|nil 0 or nil for current
local M = {}
local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MIN_WIDTH = 30
local DEFAULT_MAX_WIDTH = -1 local DEFAULT_MAX_WIDTH = -1
local DEFAULT_PADDING = 1 local DEFAULT_PADDING = 1
@ -671,9 +669,4 @@ function View:configure_width(width)
end end
end end
function M.setup(opts) return View
-- TODO #2826 move this to explorer constructor
M.View = View({ explorer = { opts = opts } }) ---@diagnostic disable-line: missing-fields
end
return M