refactor(#2826): singleton View class, WIP

This commit is contained in:
Alexander Courtis 2025-04-20 13:20:41 +10:00
parent 3c024975fd
commit a3fe0c9007
7 changed files with 57 additions and 48 deletions

View File

@ -89,7 +89,7 @@ function M.tab_enter()
return return
end end
end end
view.open({ focus_tree = false }) view.View:open({ focus_tree = false })
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then
@ -686,7 +686,7 @@ local function localise_default_opts()
end end
function M.purge_all_state() function M.purge_all_state()
view.close_all_tabs() view.View:close_all_tabs()
view.abandon_all_windows() view.abandon_all_windows()
local explorer = core.get_explorer() local explorer = core.get_explorer()
if explorer then if explorer then

View File

@ -198,7 +198,7 @@ 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.close() view.View:close()
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 +208,7 @@ end
local function drop(filename) local function drop(filename)
if M.quit_on_open then if M.quit_on_open then
view.close() view.View:close()
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 +218,7 @@ end
local function tab_drop(filename) local function tab_drop(filename)
if M.quit_on_open then if M.quit_on_open then
view.close() view.View:close()
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())
@ -427,7 +427,7 @@ function M.fn(mode, filename)
end end
if M.quit_on_open then if M.quit_on_open then
view.close() view.View:close()
end end
end end

View File

@ -42,7 +42,7 @@ function M.fn(opts, no_focus, cwd, bang)
if view.is_visible() then if view.is_visible() then
-- close -- close
view.close() view.View:close()
else else
-- open -- open
lib.open({ lib.open({

View File

@ -1,3 +1,5 @@
---TODO #2826 wrap all the view methods in explorer
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
local view = require("nvim-tree.view") local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
@ -241,7 +243,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
view.close(cur_tabpage) view.View:close(cur_tabpage)
end end
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"

View File

@ -63,7 +63,7 @@ local function remove_overlay(self)
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.close() view.View:close()
end end
end, end,
}) })

View File

@ -31,7 +31,7 @@ end
local function open_view_and_draw() local function open_view_and_draw()
local cwd = vim.fn.getcwd() local cwd = vim.fn.getcwd()
view.open() view.View:open()
handle_buf_cwd(cwd) handle_buf_cwd(cwd)
local explorer = core.get_explorer() local explorer = core.get_explorer()
@ -110,7 +110,7 @@ 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.close_this_tab_only() view.View:close_this_tab_only()
view.open_in_win() view.open_in_win()
if explorer then if explorer then
explorer.renderer:draw() explorer.renderer:draw()

View File

@ -145,8 +145,9 @@ local function wipe_rogue_buffer()
end end
end end
---@private
---@param bufnr integer|boolean|nil ---@param bufnr integer|boolean|nil
local function create_buffer(bufnr) function View:create_buffer(bufnr)
wipe_rogue_buffer() wipe_rogue_buffer()
local tab = vim.api.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
@ -193,20 +194,22 @@ local move_tbl = {
} }
-- setup_tabpage sets up the initial state of a tab -- setup_tabpage sets up the initial state of a tab
---@private
---@param tabpage integer ---@param tabpage integer
local function setup_tabpage(tabpage) function View: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 }) self.tabpages[tabpage] = vim.tbl_extend("force", self.tabpages[tabpage] or tabinitial, { winnr = winnr })
end end
local function set_window_options_and_buffer() ---@private
function View:set_window_options_and_buffer()
pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr()) pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr())
if vim.fn.has("nvim-0.10") == 1 then if vim.fn.has("nvim-0.10") == 1 then
local eventignore = vim.api.nvim_get_option_value("eventignore", {}) local eventignore = vim.api.nvim_get_option_value("eventignore", {})
vim.api.nvim_set_option_value("eventignore", "all", {}) vim.api.nvim_set_option_value("eventignore", "all", {})
for k, v in pairs(M.View.winopts) do for k, v in pairs(self.winopts) do
vim.api.nvim_set_option_value(k, v, { scope = "local" }) vim.api.nvim_set_option_value(k, v, { scope = "local" })
end end
@ -217,7 +220,7 @@ local function set_window_options_and_buffer()
-- #3009 vim.api.nvim_win_set_option does not set local scope without explicit winid. -- #3009 vim.api.nvim_win_set_option does not set local scope without explicit winid.
-- Revert to opt_local instead of propagating it through for just the 0.10 path. -- Revert to opt_local instead of propagating it through for just the 0.10 path.
for k, v in pairs(M.View.winopts) do for k, v in pairs(self.winopts) do
vim.opt_local[k] = v vim.opt_local[k] = v
end end
@ -225,24 +228,26 @@ local function set_window_options_and_buffer()
end end
end end
---@private
---@return table ---@return table
local function open_win_config() function View:open_win_config()
if type(M.View.float.open_win_config) == "function" then if type(self.float.open_win_config) == "function" then
return M.View.float.open_win_config() return self.float.open_win_config()
else else
return M.View.float.open_win_config return self.float.open_win_config
end end
end end
local function open_window() ---@private
if M.View.float.enable then function View:open_window()
vim.api.nvim_open_win(0, true, open_win_config()) if self.float.enable then
vim.api.nvim_open_win(0, true, self:open_win_config())
else else
vim.api.nvim_command("vsp") vim.api.nvim_command("vsp")
M.reposition_window() M.reposition_window()
end end
setup_tabpage(vim.api.nvim_get_current_tabpage()) self:setup_tabpage(vim.api.nvim_get_current_tabpage())
set_window_options_and_buffer() self:set_window_options_and_buffer()
end end
---@param buf integer ---@param buf integer
@ -276,19 +281,21 @@ local function switch_buf_if_last_buf()
end end
end 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
---@param tabnr integer ---@param tabnr integer
local function save_tab_state(tabnr) function View:save_tab_state(tabnr)
local tabpage = tabnr or vim.api.nvim_get_current_tabpage() local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0) M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0)
end end
---@private
---@param tabpage integer ---@param tabpage integer
local function close(tabpage) function View:close_internal(tabpage)
if not M.is_visible({ tabpage = tabpage }) then if not M.is_visible({ tabpage = tabpage }) then
return return
end end
save_tab_state(tabpage) self:save_tab_state(tabpage)
switch_buf_if_last_buf() switch_buf_if_last_buf()
local tree_win = M.get_winnr(tabpage) local tree_win = M.get_winnr(tabpage)
local current_win = vim.api.nvim_get_current_win() local current_win = vim.api.nvim_get_current_win()
@ -310,37 +317,37 @@ local function close(tabpage)
end end
end end
function M.close_this_tab_only() function View:close_this_tab_only()
close(vim.api.nvim_get_current_tabpage()) self:close_internal(vim.api.nvim_get_current_tabpage())
end end
function M.close_all_tabs() function View:close_all_tabs()
for tabpage, _ in pairs(M.View.tabpages) do for tabpage, _ in pairs(self.tabpages) do
close(tabpage) self:close_internal(tabpage)
end end
end end
---@param tabpage integer|nil ---@param tabpage integer|nil
function M.close(tabpage) function View:close(tabpage)
if M.View.tab.sync.close then if M.View.tab.sync.close then
M.close_all_tabs() self:close_all_tabs()
elseif tabpage then elseif tabpage then
close(tabpage) self:close_internal(tabpage)
else else
M.close_this_tab_only() self:close_this_tab_only()
end end
end end
---@param options table|nil ---@param options table|nil
function M.open(options) function View:open(options)
if M.is_visible() then if M.is_visible() then
return return
end end
local profile = log.profile_start("view open") local profile = log.profile_start("view open")
create_buffer() self:create_buffer()
open_window() self:open_window()
M.resize() M.resize()
local opts = options or { focus_tree = true } local opts = options or { focus_tree = true }
@ -467,10 +474,10 @@ function M.open_in_win(opts)
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
vim.api.nvim_set_current_win(opts.winid) vim.api.nvim_set_current_win(opts.winid)
end end
create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) M.View:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
setup_tabpage(vim.api.nvim_get_current_tabpage()) M.View:setup_tabpage(vim.api.nvim_get_current_tabpage())
set_current_win() set_current_win()
set_window_options_and_buffer() M.View:set_window_options_and_buffer()
if opts.resize then if opts.resize then
M.reposition_window() M.reposition_window()
M.resize() M.resize()
@ -532,10 +539,10 @@ function M.focus(winnr, open_if_closed)
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
M.close() M.close()
M.open() M.View:open()
wnr = M.get_winnr() wnr = M.get_winnr()
elseif open_if_closed and not M.is_visible() then elseif open_if_closed and not M.is_visible() then
M.open() M.View:open()
end end
if wnr then if wnr then
@ -610,7 +617,7 @@ function M._prevent_buffer_override()
-- might need a better patch -- might need a better patch
vim.cmd("setlocal nowinfixwidth") vim.cmd("setlocal nowinfixwidth")
vim.cmd("setlocal nowinfixheight") vim.cmd("setlocal nowinfixheight")
M.open({ focus_tree = false }) M.View:open({ focus_tree = false })
local explorer = require("nvim-tree.core").get_explorer() local explorer = require("nvim-tree.core").get_explorer()
if explorer then if explorer then