refactor(#2826): rename View to Window

This commit is contained in:
Alexander Courtis 2025-06-16 13:42:56 +10:00
parent 5377a3fd69
commit fc81249d4f
21 changed files with 118 additions and 118 deletions

View File

@ -74,7 +74,7 @@ end
function M.tab_enter() function M.tab_enter()
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer and explorer.view:is_visible({ any_tabpage = true }) then if explorer and explorer.window: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,7 +89,7 @@ function M.tab_enter()
return return
end end
end end
explorer.view:open({ focus_tree = false }) explorer.window:open({ focus_tree = false })
explorer.renderer:draw() explorer.renderer:draw()
end end
@ -97,7 +97,7 @@ end
function M.open_on_directory() function M.open_on_directory()
local explorer = core.get_explorer() local explorer = core.get_explorer()
local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.view:is_visible() local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.window:is_visible()
if not should_proceed then if not should_proceed then
return return
end end
@ -667,8 +667,8 @@ end
function M.purge_all_state() function M.purge_all_state()
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:close_all_tabs() explorer.window:close_all_tabs()
explorer.view:abandon_all_windows() explorer.window: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()

View File

@ -13,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 explorer.view:is_visible() then if not explorer or not explorer.window:is_visible() then
return return
end end
@ -83,9 +83,9 @@ function M.fn(path)
end) end)
:iterate() :iterate()
if found and explorer.view:is_visible() then if found and explorer.window:is_visible() then
explorer.renderer:draw() explorer.renderer:draw()
explorer.view:set_cursor({ line, 0 }) explorer.window:set_cursor({ line, 0 })
end end
running[path_real] = false running[path_real] = false

View File

@ -18,7 +18,7 @@ local function close_windows(windows)
-- 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 explorer and explorer.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 return
end end
@ -36,12 +36,12 @@ local function clear_buffer(absolute_path)
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 explorer and explorer.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.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 explorer and not explorer.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) 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

@ -66,9 +66,9 @@ local function move(explorer, where, what, skip_gitignored)
end end
if nex then if nex then
explorer.view:set_cursor({ nex, 0 }) explorer.window:set_cursor({ nex, 0 })
elseif vim.o.wrapscan and first then elseif vim.o.wrapscan and first then
explorer.view:set_cursor({ first, 0 }) explorer.window:set_cursor({ first, 0 })
end end
end end
@ -188,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
explorer.view:set_cursor({ 1, 0 }) -- move to root node (position 1) explorer.window: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
explorer.view:set_cursor({ node_init_line, 0 }) explorer.window:set_cursor({ node_init_line, 0 })
end end
-- 4.4) -- 4.4)

View File

@ -24,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
node.explorer.view:set_cursor({ 1, 0 }) node.explorer.window:set_cursor({ 1, 0 })
return return
end end
@ -32,7 +32,7 @@ function M.fn(should_close)
return n.absolute_path == parent.absolute_path return n.absolute_path == parent.absolute_path
end) end)
node.explorer.view:set_cursor({ line + 1, 0 }) node.explorer.window: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

@ -22,7 +22,7 @@ local function usable_win_ids()
local explorer = core.get_explorer() 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 = explorer and explorer.view:get_winnr(tabpage) local tree_winid = explorer and explorer.window: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)
@ -190,7 +190,7 @@ local function open_file_in_tab(filename)
if M.quit_on_open then if M.quit_on_open then
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:close() explorer.window:close()
end end
end end
if M.relative_path then if M.relative_path then
@ -203,7 +203,7 @@ local function drop(filename)
if M.quit_on_open then if M.quit_on_open then
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:close() explorer.window:close()
end end
end end
if M.relative_path then if M.relative_path then
@ -216,7 +216,7 @@ local function tab_drop(filename)
if M.quit_on_open then if M.quit_on_open then
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:close() explorer.window:close()
end end
end end
if M.relative_path then if M.relative_path then
@ -240,7 +240,7 @@ local function on_preview(buf_loaded)
end end
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:focus() explorer.window:focus()
end end
end end
@ -312,7 +312,7 @@ local function open_in_new_window(filename, mode)
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 = "belowright" local new_window_side = "belowright"
if explorer and (explorer.view.side == "right") then if explorer and (explorer.window.side == "right") then
new_window_side = "aboveleft" new_window_side = "aboveleft"
end end
@ -346,7 +346,7 @@ local function open_in_new_window(filename, mode)
end end
end end
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.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 -- 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
@ -389,7 +389,7 @@ local function edit_in_current_buf(filename)
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:abandon_current_window() explorer.window:abandon_current_window()
end end
if M.relative_path then if M.relative_path then
@ -439,7 +439,7 @@ function M.fn(mode, filename)
end end
if M.resize_window and explorer then if M.resize_window and explorer then
explorer.view:resize() explorer.window:resize()
end end
if mode == "preview" or mode == "preview_no_picker" then if mode == "preview" or mode == "preview_no_picker" then
@ -447,7 +447,7 @@ function M.fn(mode, filename)
end end
if M.quit_on_open and explorer then if M.quit_on_open and explorer then
explorer.view:close() explorer.window:close()
end end
end end

View File

@ -41,11 +41,11 @@ function M.fn(opts)
end end
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer and explorer.view:is_visible() then if explorer and explorer.window:is_visible() then
-- focus -- focus
if opts.focus then if opts.focus then
lib.set_target_win() lib.set_target_win()
explorer.view:focus() explorer.window:focus()
end end
elseif opts.open then elseif opts.open then
-- open -- open

View File

@ -25,10 +25,10 @@ function M.fn(opts)
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer and explorer.view:is_visible() then if explorer and explorer.window:is_visible() then
-- focus -- focus
lib.set_target_win() lib.set_target_win()
explorer.view:focus() explorer.window:focus()
else else
-- open -- open
lib.open({ lib.open({

View File

@ -12,8 +12,8 @@ function M.fn(opts)
if opts == nil then if opts == nil then
-- reset to config values -- reset to config values
explorer.view:configure_width() explorer.window:configure_width()
explorer.view:resize() explorer.window:resize()
return return
end end
@ -21,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
explorer.view:configure_width(width_cfg) explorer.window:configure_width(width_cfg)
explorer.view:resize() explorer.window:resize()
return return
end end
if not explorer.view:is_width_determined() then if not explorer.window: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
explorer.view:resize(absolute) explorer.window:resize(absolute)
return return
end end
@ -44,7 +44,7 @@ function M.fn(opts)
relative_size = "+" .. relative_size relative_size = "+" .. relative_size
end end
explorer.view:resize(relative_size) explorer.window:resize(relative_size)
return return
end end
end end

View File

@ -42,9 +42,9 @@ function M.fn(opts, no_focus, cwd, bang)
opts.path = nil opts.path = nil
end end
if explorer and explorer.view:is_visible() then if explorer and explorer.window:is_visible() then
-- close -- close
explorer.view:close() explorer.window:close()
else else
-- open -- open
lib.open({ lib.open({

View File

@ -247,7 +247,7 @@ local function edit(mode, node, edit_opts)
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
if explorer then if explorer then
explorer.view:close(cur_tabpage) explorer.window:close(cur_tabpage)
end end
end end
@ -259,7 +259,7 @@ local function edit(mode, node, edit_opts)
vim.cmd(":tabprev") vim.cmd(":tabprev")
end end
if explorer then if explorer then
explorer.view:focus() explorer.window:focus()
end end
end end
end end

View File

@ -113,7 +113,7 @@ local CMDS = {
command = function(c) command = function(c)
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
explorer.view:resize(c.args) explorer.window:resize(c.args)
end end
end, end,
}, },

View File

@ -54,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 TreeExplorer and TreeExplorer.view:is_root_folder_visible(M.get_cwd()) then if TreeExplorer and TreeExplorer.window: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

@ -185,7 +185,7 @@ function M.update_coc()
local bufnr local bufnr
if explorer then if explorer then
bufnr = explorer.view:get_bufnr() bufnr = explorer.window:get_bufnr()
end 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)

View File

@ -19,7 +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.explorer.view") local Window = require("nvim-tree.explorer.window")
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
@ -35,7 +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 ---@field window Window
local Explorer = RootNode:extend() local Explorer = RootNode:extend()
---@class Explorer ---@class Explorer
@ -67,7 +67,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.window = Window({ explorer = self })
self:create_autocmds() self:create_autocmds()
@ -88,7 +88,7 @@ function Explorer:create_autocmds()
group = self.augroup_id, group = self.augroup_id,
callback = function() callback = function()
appearance.setup() appearance.setup()
self.view:reset_winhl() self.window:reset_winhl()
self.renderer:draw() self.renderer:draw()
end, end,
}) })
@ -99,7 +99,7 @@ function Explorer:create_autocmds()
pattern = "NvimTree_*", pattern = "NvimTree_*",
callback = function() callback = function()
if utils.is_nvim_tree_buf(0) then if utils.is_nvim_tree_buf(0) then
self.view:close() self.window:close()
end end
end, end,
}) })
@ -166,9 +166,9 @@ function Explorer:create_autocmds()
return return
end end
if self.opts.actions.open_file.eject then if self.opts.actions.open_file.eject then
self.view:prevent_buffer_override() self.window:prevent_buffer_override()
else else
self.view:abandon_current_window() self.window:abandon_current_window()
end end
end, end,
}) })
@ -518,7 +518,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 self.view:is_visible() then if self.window:is_visible() then
self.renderer:draw() self.renderer:draw()
end end
event_running = false event_running = false
@ -540,7 +540,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 = self.view:get_winnr() local winnr = self.window: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
@ -555,7 +555,7 @@ function Explorer:get_node_at_cursor()
return return
end end
if cursor[1] == 1 and self.view:is_root_folder_visible(core.get_cwd()) then if cursor[1] == 1 and self.window:is_root_folder_visible(core.get_cwd()) then
return self return self
end end

View File

@ -199,13 +199,13 @@ local function create_overlay(self)
end end
function LiveFilter:start_filtering() function LiveFilter:start_filtering()
self.explorer.view.live_filter.prev_focused_node = self.explorer:get_node_at_cursor() self.explorer.window.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
self.explorer.view:set_cursor({ row, col }) self.explorer.window: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)
@ -214,7 +214,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 = self.explorer.view.live_filter.prev_focused_node local last_node = self.explorer.window.live_filter.prev_focused_node
self.filter = nil self.filter = nil
reset_filter(self) reset_filter(self)

View File

@ -23,7 +23,7 @@ M.View = {
tabpages = {} tabpages = {}
} }
---@class (exact) View: Class ---@class (exact) Window: Class
---@field live_filter table ---@field live_filter table
---@field side string ---@field side string
---@field float table ---@field float table
@ -39,18 +39,18 @@ M.View = {
---@field private width (fun():integer)|integer|string ---@field private width (fun():integer)|integer|string
---@field private max_width integer ---@field private max_width integer
---@field private padding integer ---@field private padding integer
local View = Class:extend() local Window = Class:extend()
---@class View ---@class Window
---@overload fun(args: ViewArgs): View ---@overload fun(args: WindowArgs): Window
---@class (exact) ViewArgs ---@class (exact) WindowArgs
---@field explorer Explorer ---@field explorer Explorer
---@protected ---@protected
---@param args ViewArgs ---@param args WindowArgs
function View:new(args) function Window:new(args)
args.explorer:log_lifecycle("View:new") args.explorer:log_lifecycle("Window:new")
self.explorer = args.explorer self.explorer = args.explorer
self.adaptive_size = false self.adaptive_size = false
@ -111,7 +111,7 @@ local BUFFER_OPTIONS = {
---@private ---@private
---@param bufnr integer ---@param bufnr integer
---@return boolean ---@return boolean
function View:matches_bufnr(bufnr) function Window:matches_bufnr(bufnr)
for _, b in pairs(BUFNR_PER_TAB) do for _, b in pairs(BUFNR_PER_TAB) do
if b == bufnr then if b == bufnr then
return true return true
@ -121,7 +121,7 @@ function View:matches_bufnr(bufnr)
end end
---@private ---@private
function View:wipe_rogue_buffer() function Window:wipe_rogue_buffer()
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if not self:matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then if not self:matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then
pcall(vim.api.nvim_buf_delete, bufnr, { force = true }) pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
@ -131,7 +131,7 @@ end
---@private ---@private
---@param bufnr integer|false|nil ---@param bufnr integer|false|nil
function View:create_buffer(bufnr) function Window:create_buffer(bufnr)
self:wipe_rogue_buffer() self:wipe_rogue_buffer()
local tab = vim.api.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
@ -151,7 +151,7 @@ end
---@private ---@private
---@param size (fun():integer)|integer|string ---@param size (fun():integer)|integer|string
---@return integer ---@return integer
function View:get_size(size) function Window:get_size(size)
if type(size) == "number" then if type(size) == "number" then
return size return size
elseif type(size) == "function" then elseif type(size) == "function" then
@ -164,7 +164,7 @@ end
---@param size (fun():integer)|integer|nil ---@param size (fun():integer)|integer|nil
---@return integer ---@return integer
function View:get_width(size) function Window:get_width(size)
if size then if size then
return self:get_size(size) return self:get_size(size)
else else
@ -180,13 +180,13 @@ local move_tbl = {
-- setup_tabpage sets up the initial state of a tab -- setup_tabpage sets up the initial state of a tab
---@private ---@private
---@param tabpage integer ---@param tabpage integer
function View:setup_tabpage(tabpage) function Window:setup_tabpage(tabpage)
local winnr = vim.api.nvim_get_current_win() local winnr = vim.api.nvim_get_current_win()
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr }) M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr })
end end
---@private ---@private
function View:set_window_options_and_buffer() function Window:set_window_options_and_buffer()
pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr()) pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr())
if vim.fn.has("nvim-0.10") == 1 then if vim.fn.has("nvim-0.10") == 1 then
@ -214,7 +214,7 @@ end
---@private ---@private
---@return table ---@return table
function View:open_win_config() function Window:open_win_config()
if type(self.float.open_win_config) == "function" then if type(self.float.open_win_config) == "function" then
return self.float.open_win_config() return self.float.open_win_config()
else else
@ -223,7 +223,7 @@ function View:open_win_config()
end end
---@private ---@private
function View:open_window() function Window:open_window()
if self.float.enable then if self.float.enable then
vim.api.nvim_open_win(0, true, self:open_win_config()) vim.api.nvim_open_win(0, true, self:open_win_config())
else else
@ -268,14 +268,14 @@ end
---save_tab_state saves any state that should be preserved across redraws. ---save_tab_state saves any state that should be preserved across redraws.
---@private ---@private
---@param tabnr integer ---@param tabnr integer
function View:save_tab_state(tabnr) function Window:save_tab_state(tabnr)
local tabpage = tabnr or vim.api.nvim_get_current_tabpage() local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
self.cursors[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0) self.cursors[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0)
end end
---@private ---@private
---@param tabpage integer ---@param tabpage integer
function View:close_internal(tabpage) function Window:close_internal(tabpage)
if not self:is_visible({ tabpage = tabpage }) then if not self:is_visible({ tabpage = tabpage }) then
return return
end end
@ -301,18 +301,18 @@ function View:close_internal(tabpage)
end end
end end
function View:close_this_tab_only() function Window:close_this_tab_only()
self:close_internal(vim.api.nvim_get_current_tabpage()) self:close_internal(vim.api.nvim_get_current_tabpage())
end end
function View:close_all_tabs() function Window:close_all_tabs()
for tabpage, _ in pairs(M.View.tabpages) do for tabpage, _ in pairs(M.View.tabpages) do
self:close_internal(tabpage) self:close_internal(tabpage)
end end
end end
---@param tabpage integer|nil ---@param tabpage integer|nil
function View:close(tabpage) function Window:close(tabpage)
if self.explorer.opts.tab.sync.close then if self.explorer.opts.tab.sync.close then
self:close_all_tabs() self:close_all_tabs()
elseif tabpage then elseif tabpage then
@ -323,7 +323,7 @@ function View:close(tabpage)
end end
---@param options table|nil ---@param options table|nil
function View:open(options) function Window:open(options)
if self:is_visible() then if self:is_visible() then
return return
end end
@ -345,7 +345,7 @@ function View:open(options)
end end
---@private ---@private
function View:grow() function Window:grow()
local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
local lines = vim.api.nvim_buf_get_lines(self:get_bufnr(), starts_at, -1, false) local lines = vim.api.nvim_buf_get_lines(self:get_bufnr(), starts_at, -1, false)
-- number of columns of right-padding to indicate end of path -- number of columns of right-padding to indicate end of path
@ -384,14 +384,14 @@ function View:grow()
self:resize(resizing_width + padding) self:resize(resizing_width + padding)
end end
function View:grow_from_content() function Window:grow_from_content()
if self.adaptive_size then if self.adaptive_size then
self:grow() self:grow()
end end
end end
---@param size string|number|nil ---@param size string|number|nil
function View:resize(size) function Window:resize(size)
if self.float.enable and not self.adaptive_size then if self.float.enable and not self.adaptive_size then
-- if the floating windows's adaptive size is not desired, then the -- if the floating windows's adaptive size is not desired, then the
-- float size should be defined in view.float.open_win_config -- float size should be defined in view.float.open_win_config
@ -436,21 +436,21 @@ function View:resize(size)
end end
---@private ---@private
function View:reposition_window() function Window:reposition_window()
local move_to = move_tbl[self.side] local move_to = move_tbl[self.side]
vim.api.nvim_command("wincmd " .. move_to) vim.api.nvim_command("wincmd " .. move_to)
self:resize() self:resize()
end end
---@private ---@private
function View:set_current_win() function Window:set_current_win()
local current_tab = vim.api.nvim_get_current_tabpage() local current_tab = vim.api.nvim_get_current_tabpage()
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
end end
---Open the tree in the a window ---Open the tree in the a window
---@param opts OpenInWinOpts|nil ---@param opts OpenInWinOpts|nil
function View:open_in_win(opts) function Window:open_in_win(opts)
opts = opts or { hijack_current_buf = true, resize = true } opts = opts or { hijack_current_buf = true, resize = true }
events._dispatch_on_tree_pre_open() events._dispatch_on_tree_pre_open()
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
@ -467,7 +467,7 @@ function View:open_in_win(opts)
events._dispatch_on_tree_open() events._dispatch_on_tree_open()
end end
function View:abandon_current_window() function Window:abandon_current_window()
local tab = vim.api.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
BUFNR_PER_TAB[tab] = nil BUFNR_PER_TAB[tab] = nil
if M.View.tabpages[tab] then if M.View.tabpages[tab] then
@ -475,7 +475,7 @@ function View:abandon_current_window()
end end
end end
function View:abandon_all_windows() function Window:abandon_all_windows()
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
BUFNR_PER_TAB[tab] = nil BUFNR_PER_TAB[tab] = nil
if M.View.tabpages[tab] then if M.View.tabpages[tab] then
@ -486,7 +486,7 @@ end
---@param opts table|nil ---@param opts table|nil
---@return boolean ---@return boolean
function View:is_visible(opts) function Window:is_visible(opts)
if opts and opts.tabpage then if opts and opts.tabpage then
if M.View.tabpages[opts.tabpage] == nil then if M.View.tabpages[opts.tabpage] == nil then
return false return false
@ -508,7 +508,7 @@ function View:is_visible(opts)
end end
---@param opts table|nil ---@param opts table|nil
function View:set_cursor(opts) function Window:set_cursor(opts)
if self:is_visible() then if self:is_visible() then
pcall(vim.api.nvim_win_set_cursor, self:get_winnr(), opts) pcall(vim.api.nvim_win_set_cursor, self:get_winnr(), opts)
end end
@ -516,7 +516,7 @@ end
---@param winnr number|nil ---@param winnr number|nil
---@param open_if_closed boolean|nil ---@param open_if_closed boolean|nil
function View:focus(winnr, open_if_closed) function Window:focus(winnr, open_if_closed)
local wnr = winnr or self:get_winnr() local wnr = winnr or self:get_winnr()
if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
@ -535,7 +535,7 @@ end
--- Retrieve the winid of the open tree. --- Retrieve the winid of the open tree.
---@param opts ApiTreeWinIdOpts|nil ---@param opts ApiTreeWinIdOpts|nil
---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible ---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible
function View:winid(opts) function Window:winid(opts)
local tabpage = opts and opts.tabpage local tabpage = opts and opts.tabpage
if tabpage == 0 then if tabpage == 0 then
tabpage = vim.api.nvim_get_current_tabpage() tabpage = vim.api.nvim_get_current_tabpage()
@ -548,7 +548,7 @@ function View:winid(opts)
end end
--- Restores the state of a NvimTree window if it was initialized before. --- Restores the state of a NvimTree window if it was initialized before.
function View:restore_tab_state() function Window:restore_tab_state()
local tabpage = vim.api.nvim_get_current_tabpage() local tabpage = vim.api.nvim_get_current_tabpage()
self:set_cursor(self.cursors[tabpage]) self:set_cursor(self.cursors[tabpage])
end end
@ -556,7 +556,7 @@ end
--- Returns the window number for nvim-tree within the tabpage specified --- Returns the window number for nvim-tree within the tabpage specified
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number|nil ---@return number|nil
function View:get_winnr(tabpage) function Window:get_winnr(tabpage)
tabpage = tabpage or vim.api.nvim_get_current_tabpage() tabpage = tabpage or vim.api.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage] local tabinfo = M.View.tabpages[tabpage]
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
@ -566,11 +566,11 @@ end
--- Returns the current nvim tree bufnr --- Returns the current nvim tree bufnr
---@return number ---@return number
function View:get_bufnr() function Window:get_bufnr()
return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
end end
function View:prevent_buffer_override() function Window:prevent_buffer_override()
local view_winnr = self:get_winnr() local view_winnr = self:get_winnr()
local view_bufnr = self:get_bufnr() local view_bufnr = self:get_bufnr()
@ -620,12 +620,12 @@ end
---@param cwd string|nil ---@param cwd string|nil
---@return boolean ---@return boolean
function View:is_root_folder_visible(cwd) function Window:is_root_folder_visible(cwd)
return cwd ~= "/" and not self.hide_root_folder return cwd ~= "/" and not self.hide_root_folder
end end
-- used on ColorScheme event -- used on ColorScheme event
function View:reset_winhl() function Window:reset_winhl()
local winnr = self:get_winnr() local winnr = self:get_winnr()
if winnr and vim.api.nvim_win_is_valid(winnr) then if winnr and vim.api.nvim_win_is_valid(winnr) then
vim.wo[self:get_winnr()].winhl = appearance.WIN_HL vim.wo[self:get_winnr()].winhl = appearance.WIN_HL
@ -634,13 +634,13 @@ end
---Check if width determined or calculated on-fly ---Check if width determined or calculated on-fly
---@return boolean ---@return boolean
function View:is_width_determined() function Window:is_width_determined()
return type(self.width) ~= "function" return type(self.width) ~= "function"
end end
---Configure width-related config ---Configure width-related config
---@param width string|function|number|table|nil ---@param width string|function|number|table|nil
function View:configure_width(width) function Window:configure_width(width)
if type(width) == "table" then if type(width) == "table" then
self.adaptive_size = true self.adaptive_size = true
self.width = width.min or DEFAULT_MIN_WIDTH self.width = width.min or DEFAULT_MIN_WIDTH
@ -660,4 +660,4 @@ function View:configure_width(width)
end end
end end
return View return Window

View File

@ -15,7 +15,7 @@ function M.set_target_win()
local id = vim.api.nvim_get_current_win() local id = vim.api.nvim_get_current_win()
if explorer and id == explorer.view:get_winnr() then if explorer and id == explorer.window:get_winnr() then
M.target_winid = 0 M.target_winid = 0
return return
end end
@ -36,7 +36,7 @@ local function open_view_and_draw()
local cwd = vim.fn.getcwd() local cwd = vim.fn.getcwd()
if explorer then if explorer then
explorer.view:open() explorer.window:open()
end end
handle_buf_cwd(cwd) handle_buf_cwd(cwd)
@ -117,18 +117,18 @@ function M.open(opts)
if should_hijack_current_buf() then if should_hijack_current_buf() then
if explorer then if explorer then
explorer.view:close_this_tab_only() explorer.window:close_this_tab_only()
explorer.view:open_in_win() explorer.window:open_in_win()
explorer.renderer:draw() explorer.renderer:draw()
end end
elseif opts.winid then elseif opts.winid then
if explorer then if explorer then
explorer.view:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid }) explorer.window: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
if explorer then if explorer then
explorer.view:open_in_win({ hijack_current_buf = false, resize = false }) explorer.window:open_in_win({ hijack_current_buf = false, resize = false })
explorer.renderer:draw() explorer.renderer:draw()
end end
else else
@ -136,7 +136,7 @@ function M.open(opts)
end end
if explorer then if explorer then
explorer.view:restore_tab_state() explorer.window:restore_tab_state()
end end
end end

View File

@ -378,7 +378,7 @@ end
---@private ---@private
function Builder:build_header() function Builder:build_header()
if self.explorer.view:is_root_folder_visible(self.explorer.absolute_path) then if self.explorer.window: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

@ -97,28 +97,28 @@ function Renderer:render_hl(bufnr, hl_range_args)
end end
function Renderer:draw() function Renderer:draw()
local bufnr = self.explorer.view:get_bufnr() local bufnr = self.explorer.window: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(self.explorer.view:get_winnr() or 0) local cursor = vim.api.nvim_win_get_cursor(self.explorer.window: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(self.explorer.view:get_winnr() or 0, cursor) vim.api.nvim_win_set_cursor(self.explorer.window:get_winnr() or 0, cursor)
end end
self.explorer.view:grow_from_content() self.explorer.window:grow_from_content()
log.profile_end(profile) log.profile_end(profile)
events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr()) events._dispatch_on_tree_rendered(bufnr, self.explorer.window:get_winnr())
end end
return Renderer return Renderer

View File

@ -145,7 +145,7 @@ function M.find_node(nodes, fn)
:iterate() :iterate()
if node then if node then
if not node.explorer.view:is_root_folder_visible() then if not node.explorer.window:is_root_folder_visible() then
i = i - 1 i = i - 1
end end
if node.explorer.live_filter.filter then if node.explorer.live_filter.filter then
@ -545,7 +545,7 @@ function M.focus_file(path)
end) end)
local explorer = require("nvim-tree.core").get_explorer() local explorer = require("nvim-tree.core").get_explorer()
if explorer then if explorer then
explorer.view:set_cursor({ i + 1, 1 }) explorer.window:set_cursor({ i + 1, 1 })
end end
end end
@ -566,7 +566,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
explorer.view:set_cursor({ i + 1, 1 }) explorer.window:set_cursor({ i + 1, 1 })
break break
end end