diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index dc64044a..125d3ec6 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -89,7 +89,7 @@ function M.tab_enter() return end end - view.open({ focus_tree = false }) + view.View:open({ focus_tree = false }) local explorer = core.get_explorer() if explorer then @@ -686,7 +686,7 @@ local function localise_default_opts() end function M.purge_all_state() - view.close_all_tabs() + view.View:close_all_tabs() view.abandon_all_windows() local explorer = core.get_explorer() if explorer then diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index d0d86c24..7ea5898f 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -198,7 +198,7 @@ end local function open_file_in_tab(filename) if M.quit_on_open then - view.close() + view.View:close() end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -208,7 +208,7 @@ end local function drop(filename) if M.quit_on_open then - view.close() + view.View:close() end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -218,7 +218,7 @@ end local function tab_drop(filename) if M.quit_on_open then - view.close() + view.View:close() end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -427,7 +427,7 @@ function M.fn(mode, filename) end if M.quit_on_open then - view.close() + view.View:close() end end diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index 10aa9784..37c823c9 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -42,7 +42,7 @@ function M.fn(opts, no_focus, cwd, bang) if view.is_visible() then -- close - view.close() + view.View:close() else -- open lib.open({ diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 822dbf20..48054e31 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -1,3 +1,5 @@ +---TODO #2826 wrap all the view methods in explorer + local core = require("nvim-tree.core") local view = require("nvim-tree.view") 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" if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then - view.close(cur_tabpage) + view.View:close(cur_tabpage) end local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 62a7dd9e..a1f46828 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -63,7 +63,7 @@ local function remove_overlay(self) group = vim.api.nvim_create_augroup("NvimTree", { clear = false }), callback = function() if utils.is_nvim_tree_buf(0) then - view.close() + view.View:close() end end, }) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index a464edf3..a755ab9c 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -31,7 +31,7 @@ end local function open_view_and_draw() local cwd = vim.fn.getcwd() - view.open() + view.View:open() handle_buf_cwd(cwd) local explorer = core.get_explorer() @@ -110,7 +110,7 @@ function M.open(opts) local explorer = core.get_explorer() if should_hijack_current_buf() then - view.close_this_tab_only() + view.View:close_this_tab_only() view.open_in_win() if explorer then explorer.renderer:draw() diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index de49a4e7..34457229 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -145,8 +145,9 @@ local function wipe_rogue_buffer() end end +---@private ---@param bufnr integer|boolean|nil -local function create_buffer(bufnr) +function View:create_buffer(bufnr) wipe_rogue_buffer() 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 +---@private ---@param tabpage integer -local function setup_tabpage(tabpage) +function View:setup_tabpage(tabpage) 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 -local function set_window_options_and_buffer() +---@private +function View:set_window_options_and_buffer() pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr()) if vim.fn.has("nvim-0.10") == 1 then local eventignore = vim.api.nvim_get_option_value("eventignore", {}) 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" }) 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. -- 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 end @@ -225,24 +228,26 @@ local function set_window_options_and_buffer() end end +---@private ---@return table -local function open_win_config() - if type(M.View.float.open_win_config) == "function" then - return M.View.float.open_win_config() +function View:open_win_config() + if type(self.float.open_win_config) == "function" then + return self.float.open_win_config() else - return M.View.float.open_win_config + return self.float.open_win_config end end -local function open_window() - if M.View.float.enable then - vim.api.nvim_open_win(0, true, open_win_config()) +---@private +function View:open_window() + if self.float.enable then + vim.api.nvim_open_win(0, true, self:open_win_config()) else vim.api.nvim_command("vsp") M.reposition_window() end - setup_tabpage(vim.api.nvim_get_current_tabpage()) - set_window_options_and_buffer() + self:setup_tabpage(vim.api.nvim_get_current_tabpage()) + self:set_window_options_and_buffer() end ---@param buf integer @@ -276,19 +281,21 @@ local function switch_buf_if_last_buf() 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 -local function save_tab_state(tabnr) +function View:save_tab_state(tabnr) 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) end +---@private ---@param tabpage integer -local function close(tabpage) +function View:close_internal(tabpage) if not M.is_visible({ tabpage = tabpage }) then return end - save_tab_state(tabpage) + self:save_tab_state(tabpage) switch_buf_if_last_buf() local tree_win = M.get_winnr(tabpage) local current_win = vim.api.nvim_get_current_win() @@ -310,37 +317,37 @@ local function close(tabpage) end end -function M.close_this_tab_only() - close(vim.api.nvim_get_current_tabpage()) +function View:close_this_tab_only() + self:close_internal(vim.api.nvim_get_current_tabpage()) end -function M.close_all_tabs() - for tabpage, _ in pairs(M.View.tabpages) do - close(tabpage) +function View:close_all_tabs() + for tabpage, _ in pairs(self.tabpages) do + self:close_internal(tabpage) end end ---@param tabpage integer|nil -function M.close(tabpage) +function View:close(tabpage) if M.View.tab.sync.close then - M.close_all_tabs() + self:close_all_tabs() elseif tabpage then - close(tabpage) + self:close_internal(tabpage) else - M.close_this_tab_only() + self:close_this_tab_only() end end ---@param options table|nil -function M.open(options) +function View:open(options) if M.is_visible() then return end local profile = log.profile_start("view open") - create_buffer() - open_window() + self:create_buffer() + self:open_window() M.resize() 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 vim.api.nvim_set_current_win(opts.winid) end - create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) - setup_tabpage(vim.api.nvim_get_current_tabpage()) + M.View:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) + M.View:setup_tabpage(vim.api.nvim_get_current_tabpage()) set_current_win() - set_window_options_and_buffer() + M.View:set_window_options_and_buffer() if opts.resize then M.reposition_window() 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 M.close() - M.open() + M.View:open() wnr = M.get_winnr() elseif open_if_closed and not M.is_visible() then - M.open() + M.View:open() end if wnr then @@ -610,7 +617,7 @@ function M._prevent_buffer_override() -- might need a better patch vim.cmd("setlocal nowinfixwidth") vim.cmd("setlocal nowinfixheight") - M.open({ focus_tree = false }) + M.View:open({ focus_tree = false }) local explorer = require("nvim-tree.core").get_explorer() if explorer then