diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index c4cdbcf74f0..449ac2b7eea 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -1,5 +1,4 @@ local log = require("nvim-tree.log") -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local actions = require("nvim-tree.actions") local core = require("nvim-tree.core") @@ -74,7 +73,8 @@ function M.change_root(path, bufnr) end function M.tab_enter() - if 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 ft @@ -89,17 +89,15 @@ function M.tab_enter() return end end - view.open({ focus_tree = false }) + explorer.view:open({ focus_tree = false }) - local explorer = core.get_explorer() - if explorer then - explorer.renderer:draw() - end + explorer.renderer:draw() end end function M.open_on_directory() - local should_proceed = _config.hijack_directories.auto_open or 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 return end @@ -150,21 +148,6 @@ local function setup_autocommands(opts) vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) end - -- prevent new opened file from opening in the same window as nvim-tree - create_nvim_tree_autocmd("BufWipeout", { - pattern = "NvimTree_*", - callback = function() - if not utils.is_nvim_tree_buf(0) then - return - end - if opts.actions.open_file.eject then - view._prevent_buffer_override() - else - view.abandon_current_window() - end - end, - }) - if opts.tab.sync.open then create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) }) end @@ -226,17 +209,6 @@ local function setup_autocommands(opts) }) end - if opts.view.float.enable and opts.view.float.quit_on_focus_loss then - create_nvim_tree_autocmd("WinLeave", { - pattern = "NvimTree_*", - callback = function() - if utils.is_nvim_tree_buf(0) then - view.close() - end - end, - }) - end - -- Handles event dispatch when tree is closed by `:q` create_nvim_tree_autocmd("WinClosed", { pattern = "*", @@ -524,6 +496,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS dev = false, diagnostics = false, git = false, + lifecycle = true, profile = false, watcher = false, }, @@ -692,10 +665,10 @@ local function localise_default_opts() end function M.purge_all_state() - view.close_all_tabs() - view.abandon_all_windows() local explorer = core.get_explorer() if explorer then + explorer.view:close_all_tabs() + explorer.view:abandon_all_windows() require("nvim-tree.git").purge_state() explorer:destroy() core.reset_explorer() @@ -748,7 +721,6 @@ function M.setup(conf) require("nvim-tree.explorer.watch").setup(opts) require("nvim-tree.git").setup(opts) require("nvim-tree.git.utils").setup(opts) - require("nvim-tree.view").setup(opts) require("nvim-tree.lib").setup(opts) require("nvim-tree.renderer.components").setup(opts) require("nvim-tree.buffers").setup(opts) diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 55e2f23d337..2ea494fd85c 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -1,5 +1,4 @@ local log = require("nvim-tree.log") -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local core = require("nvim-tree.core") @@ -14,7 +13,7 @@ local running = {} ---@param path string relative or absolute function M.fn(path) local explorer = core.get_explorer() - if not explorer or not view.is_visible() then + if not explorer or not explorer.view:is_visible() then return end @@ -84,9 +83,9 @@ function M.fn(path) end) :iterate() - if found and view.is_visible() then + if found and explorer.view:is_visible() then explorer.renderer:draw() - view.set_cursor({ line, 0 }) + explorer.view:set_cursor({ line, 0 }) end running[path_real] = false diff --git a/lua/nvim-tree/actions/fs/clipboard.lua b/lua/nvim-tree/actions/fs/clipboard.lua index 3f3ed37c94d..a8c6e15e958 100644 --- a/lua/nvim-tree/actions/fs/clipboard.lua +++ b/lua/nvim-tree/actions/fs/clipboard.lua @@ -31,6 +31,8 @@ local Clipboard = Class:extend() ---@protected ---@param args ClipboardArgs function Clipboard:new(args) + args.explorer:log_lifecycle("Clipboard:new") + self.explorer = args.explorer self.data = { diff --git a/lua/nvim-tree/actions/fs/remove-file.lua b/lua/nvim-tree/actions/fs/remove-file.lua index 8a0f67cda3a..fd71ae6a376 100644 --- a/lua/nvim-tree/actions/fs/remove-file.lua +++ b/lua/nvim-tree/actions/fs/remove-file.lua @@ -1,7 +1,6 @@ local core = require("nvim-tree.core") local utils = require("nvim-tree.utils") local events = require("nvim-tree.events") -local view = require("nvim-tree.view") local lib = require("nvim-tree.lib") local notify = require("nvim-tree.notify") @@ -14,10 +13,12 @@ local M = { ---@param windows integer[] local function close_windows(windows) + local explorer = core.get_explorer() + -- Prevent from closing when the win count equals 1 or 2, -- where the win to remove could be the last opened. -- For details see #2503. - if view.View.float.enable and #vim.api.nvim_list_wins() < 3 then + if explorer and explorer.view.float.enable and #vim.api.nvim_list_wins() < 3 then return end @@ -30,16 +31,17 @@ end ---@param absolute_path string local function clear_buffer(absolute_path) + local explorer = core.get_explorer() local bufs = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 }) for _, buf in pairs(bufs) do if buf.name == absolute_path then local tree_winnr = vim.api.nvim_get_current_win() - if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then + if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.view.float.enable) then vim.api.nvim_set_current_win(buf.windows[1]) vim.cmd(":bn") end vim.api.nvim_buf_delete(buf.bufnr, { force = true }) - if not view.View.float.quit_on_focus_loss then + if explorer and not explorer.view.float.quit_on_focus_loss then vim.api.nvim_set_current_win(tree_winnr) end if M.config.actions.remove_file.close_window then diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 8aacaae3b56..599a8053027 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -1,5 +1,4 @@ local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local core = require("nvim-tree.core") local diagnostics = require("nvim-tree.diagnostics") @@ -67,9 +66,9 @@ local function move(explorer, where, what, skip_gitignored) end if nex then - view.set_cursor({ nex, 0 }) + explorer.view:set_cursor({ nex, 0 }) elseif vim.o.wrapscan and first then - view.set_cursor({ first, 0 }) + explorer.view:set_cursor({ first, 0 }) end end @@ -189,13 +188,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored) -- 4.3) if node_init.name == ".." then -- root node - view.set_cursor({ 1, 0 }) -- move to root node (position 1) + explorer.view:set_cursor({ 1, 0 }) -- move to root node (position 1) else local node_init_line = utils.find_node_line(node_init) if node_init_line < 0 then return end - view.set_cursor({ node_init_line, 0 }) + explorer.view:set_cursor({ node_init_line, 0 }) end -- 4.4) diff --git a/lua/nvim-tree/actions/moves/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index 32ca8398116..fab1a440a2f 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -1,4 +1,3 @@ -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") 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 if not parent or not parent.parent then - view.set_cursor({ 1, 0 }) + node.explorer.view:set_cursor({ 1, 0 }) return end @@ -33,7 +32,7 @@ function M.fn(should_close) return n.absolute_path == parent.absolute_path end) - view.set_cursor({ line + 1, 0 }) + node.explorer.view:set_cursor({ line + 1, 0 }) if should_close then parent.open = false parent.explorer.renderer:draw() diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 55885e3f7f4..99594520f11 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -2,7 +2,7 @@ local lib = require("nvim-tree.lib") local notify = require("nvim-tree.notify") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") +local core = require("nvim-tree.core") local M = {} @@ -19,9 +19,10 @@ end ---Get all windows in the current tabpage that aren't NvimTree. ---@return table with valid win_ids local function usable_win_ids() + local explorer = core.get_explorer() local tabpage = vim.api.nvim_get_current_tabpage() local win_ids = vim.api.nvim_tabpage_list_wins(tabpage) - local tree_winid = view.get_winnr(tabpage) + local tree_winid = explorer and explorer.view:get_winnr(tabpage) return vim.tbl_filter(function(id) local bufid = vim.api.nvim_win_get_buf(id) @@ -187,7 +188,10 @@ end local function open_file_in_tab(filename) if M.quit_on_open then - view.close() + local explorer = core.get_explorer() + if explorer then + explorer.view:close() + end end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -197,7 +201,10 @@ end local function drop(filename) if M.quit_on_open then - view.close() + local explorer = core.get_explorer() + if explorer then + explorer.view:close() + end end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -207,7 +214,10 @@ end local function tab_drop(filename) if M.quit_on_open then - view.close() + local explorer = core.get_explorer() + if explorer then + explorer.view:close() + end end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -228,7 +238,10 @@ local function on_preview(buf_loaded) once = true, }) end - view.focus() + local explorer = core.get_explorer() + if explorer then + explorer.view:focus() + end end local function get_target_winid(mode) @@ -273,6 +286,8 @@ local function set_current_win_no_autocmd(winid, autocmd) end local function open_in_new_window(filename, mode) + local explorer = core.get_explorer() + if type(mode) ~= "string" then mode = "" end @@ -295,7 +310,11 @@ local function open_in_new_window(filename, mode) end, vim.api.nvim_list_wins()) local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one - local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright" + + local new_window_side = "belowright" + if explorer and (explorer.view.side == "right") then + new_window_side = "aboveleft" + end -- Target is invalid: create new window if not vim.tbl_contains(win_ids, target_winid) then @@ -327,7 +346,7 @@ local function open_in_new_window(filename, mode) end end - if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then + if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.float.enable then -- ignore "WinLeave" autocmd on preview -- because the registered "WinLeave" -- will kill the floating window immediately @@ -367,7 +386,12 @@ local function is_already_loaded(filename) end local function edit_in_current_buf(filename) - require("nvim-tree.view").abandon_current_window() + local explorer = core.get_explorer() + + if explorer then + explorer.view:abandon_current_window() + end + if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end @@ -378,6 +402,8 @@ end ---@param filename string ---@return nil function M.fn(mode, filename) + local explorer = core.get_explorer() + if type(mode) ~= "string" then mode = "" end @@ -412,16 +438,16 @@ function M.fn(mode, filename) vim.bo.bufhidden = "" end - if M.resize_window then - view.resize() + if M.resize_window and explorer then + explorer.view:resize() end if mode == "preview" or mode == "preview_no_picker" then return on_preview(buf_loaded) end - if M.quit_on_open then - view.close() + if M.quit_on_open and explorer then + explorer.view:close() end end diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index 8a05bf6db45..73d1df3c75e 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -1,6 +1,5 @@ local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") -local view = require("nvim-tree.view") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -41,11 +40,12 @@ function M.fn(opts) return end - if view.is_visible() then + local explorer = core.get_explorer() + if explorer and explorer.view:is_visible() then -- focus if opts.focus then lib.set_target_win() - view.focus() + explorer.view:focus() end elseif opts.open then -- open diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index ff2da837b87..17e2c581a7b 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -1,5 +1,5 @@ +local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") -local view = require("nvim-tree.view") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -23,10 +23,12 @@ function M.fn(opts) opts.path = nil end - if view.is_visible() then + local explorer = core.get_explorer() + + if explorer and explorer.view:is_visible() then -- focus lib.set_target_win() - view.focus() + explorer.view:focus() else -- open lib.open({ diff --git a/lua/nvim-tree/actions/tree/resize.lua b/lua/nvim-tree/actions/tree/resize.lua index e8d4e950729..48f090887f7 100644 --- a/lua/nvim-tree/actions/tree/resize.lua +++ b/lua/nvim-tree/actions/tree/resize.lua @@ -1,14 +1,19 @@ -local view = require("nvim-tree.view") +local core = require("nvim-tree.core") local M = {} ---Resize the tree, persisting the new size. ---@param opts ApiTreeResizeOpts|nil function M.fn(opts) + local explorer = core.get_explorer() + if not explorer then + return + end + if opts == nil then -- reset to config values - view.configure_width() - view.resize() + explorer.view:configure_width() + explorer.view:resize() return end @@ -16,19 +21,19 @@ function M.fn(opts) local width_cfg = options.width if width_cfg ~= nil then - view.configure_width(width_cfg) - view.resize() + explorer.view:configure_width(width_cfg) + explorer.view:resize() return end - if not view.is_width_determined() then + if not explorer.view:is_width_determined() then -- {absolute} and {relative} do nothing when {width} is a function. return end local absolute = options.absolute if type(absolute) == "number" then - view.resize(absolute) + explorer.view:resize(absolute) return end @@ -39,7 +44,7 @@ function M.fn(opts) relative_size = "+" .. relative_size end - view.resize(relative_size) + explorer.view:resize(relative_size) return end end diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index 10aa978467e..fb976cfbe71 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -1,5 +1,5 @@ +local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") -local view = require("nvim-tree.view") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -10,6 +10,8 @@ local M = {} ---@param cwd boolean|nil legacy -> opts.path ---@param bang boolean|nil legacy -> opts.update_root function M.fn(opts, no_focus, cwd, bang) + local explorer = core.get_explorer() + -- legacy arguments if type(opts) == "boolean" then opts = { @@ -40,9 +42,9 @@ function M.fn(opts, no_focus, cwd, bang) opts.path = nil end - if view.is_visible() then + if explorer and explorer.view:is_visible() then -- close - view.close() + explorer.view:close() else -- open lib.open({ diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 39fba07d52d..3f70df2c880 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -1,5 +1,4 @@ local core = require("nvim-tree.core") -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local actions = require("nvim-tree.actions") local appearance_hi_test = require("nvim-tree.appearance.hi-test") @@ -141,9 +140,9 @@ Api.tree.focus = Api.tree.open ---@field focus boolean|nil default true Api.tree.toggle = wrap(actions.tree.toggle.fn) -Api.tree.close = wrap(view.close) -Api.tree.close_in_this_tab = wrap(view.close_this_tab_only) -Api.tree.close_in_all_tabs = wrap(view.close_all_tabs) +Api.tree.close = wrap_explorer_member("view", "close") +Api.tree.close_in_this_tab = wrap_explorer_member("view", "close_this_tab_only") +Api.tree.close_in_all_tabs = wrap_explorer_member("view", "close_all_tabs") Api.tree.reload = wrap_explorer("reload_explorer") ---@class ApiTreeResizeOpts @@ -202,12 +201,12 @@ Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf) ---@field tabpage number|nil ---@field any_tabpage boolean|nil default false -Api.tree.is_visible = wrap(view.is_visible) +Api.tree.is_visible = wrap_explorer_member("view", "is_visible") ---@class ApiTreeWinIdOpts ---@field tabpage number|nil default nil -Api.tree.winid = wrap(view.winid) +Api.tree.winid = wrap_explorer_member("view", "winid") Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn) Api.fs.remove = wrap_node(actions.fs.remove_file.fn) @@ -239,13 +238,17 @@ local function edit(mode, node, edit_opts) local path = file_link and file_link.link_to or node.absolute_path local cur_tabpage = vim.api.nvim_get_current_tabpage() + local explorer = core.get_explorer() + actions.node.open_file.fn(mode, path) edit_opts = edit_opts or {} 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) + if explorer then + explorer.view:close(cur_tabpage) + end end local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" @@ -255,7 +258,9 @@ local function edit(mode, node, edit_opts) if mode == "tabnew" then vim.cmd(":tabprev") end - view.focus() + if explorer then + explorer.view:focus() + end end end diff --git a/lua/nvim-tree/commands.lua b/lua/nvim-tree/commands.lua index bff880c14fd..88ac3e1e61e 100644 --- a/lua/nvim-tree/commands.lua +++ b/lua/nvim-tree/commands.lua @@ -1,5 +1,5 @@ local api = require("nvim-tree.api") -local view = require("nvim-tree.view") +local core = require("nvim-tree.core") local M = {} @@ -111,7 +111,10 @@ local CMDS = { bar = true, }, command = function(c) - view.resize(c.args) + local explorer = core.get_explorer() + if explorer then + explorer.view:resize(c.args) + end end, }, { diff --git a/lua/nvim-tree/core.lua b/lua/nvim-tree/core.lua index 60d4a0a6f6a..fe9125e612d 100644 --- a/lua/nvim-tree/core.lua +++ b/lua/nvim-tree/core.lua @@ -1,6 +1,5 @@ local events = require("nvim-tree.events") local notify = require("nvim-tree.notify") -local view = require("nvim-tree.view") local log = require("nvim-tree.log") local M = {} @@ -55,7 +54,7 @@ end ---@return integer function M.get_nodes_starting_line() local offset = 1 - if 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 end if TreeExplorer and TreeExplorer.live_filter.filter then diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index 76810d4a796..e6484caf23e 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -1,6 +1,5 @@ local core = require("nvim-tree.core") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local log = require("nvim-tree.log") local DirectoryNode = require("nvim-tree.node.directory") @@ -182,10 +181,15 @@ function M.update_coc() end log.profile_end(profile) - local bufnr = 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) if should_draw then - local explorer = core.get_explorer() if explorer then explorer.renderer:draw() end diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 62230687e40..c6640fb42ee 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -23,6 +23,8 @@ local Filters = Class:extend() ---@protected ---@param args FiltersArgs function Filters:new(args) + args.explorer:log_lifecycle("Filters:new") + self.explorer = args.explorer self.ignore_list = {} self.exclude_list = self.explorer.opts.filters.exclude diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 58972164afe..71b7b4e1780 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -4,7 +4,6 @@ local core = require("nvim-tree.core") local git = require("nvim-tree.git") local log = require("nvim-tree.log") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local node_factory = require("nvim-tree.node.factory") 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 Clipboard = require("nvim-tree.actions.fs.clipboard") local Renderer = require("nvim-tree.renderer") +local View = require("nvim-tree.explorer.view") local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON @@ -35,6 +35,7 @@ local config ---@field sorters Sorter ---@field marks Marks ---@field clipboard Clipboard +---@field view View local Explorer = RootNode:extend() ---@class Explorer @@ -55,6 +56,8 @@ function Explorer:new(args) self.uid_explorer = vim.loop.hrtime() self.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. self.uid_explorer, {}) + self:log_lifecycle("Explorer:new") + self.open = true self.opts = config @@ -64,6 +67,7 @@ function Explorer:new(args) self.live_filter = LiveFilter({ explorer = self }) self.marks = Marks({ explorer = self }) self.clipboard = Clipboard({ explorer = self }) + self.view = View({ explorer = self }) self:create_autocmds() @@ -71,7 +75,7 @@ function Explorer:new(args) end function Explorer:destroy() - log.line("dev", "Explorer:destroy") + self:log_lifecycle("Explorer:des") vim.api.nvim_del_augroup_by_id(self.augroup_id) @@ -84,11 +88,23 @@ function Explorer:create_autocmds() group = self.augroup_id, callback = function() appearance.setup() - view.reset_winhl() + self.view:reset_winhl() self.renderer:draw() end, }) + if self.opts.view.float.enable and self.opts.view.float.quit_on_focus_loss then + vim.api.nvim_create_autocmd("WinLeave", { + group = self.augroup_id, + pattern = "NvimTree_*", + callback = function() + if utils.is_nvim_tree_buf(0) then + self.view:close() + end + end, + }) + end + vim.api.nvim_create_autocmd("BufWritePost", { group = self.augroup_id, callback = function() @@ -141,6 +157,22 @@ function Explorer:create_autocmds() end, }) + -- prevent new opened file from opening in the same window as nvim-tree + vim.api.nvim_create_autocmd("BufWipeout", { + group = self.augroup_id, + pattern = "NvimTree_*", + callback = function() + if not utils.is_nvim_tree_buf(0) then + return + end + if self.opts.actions.open_file.eject then + self.view:prevent_buffer_override() + else + self.view:abandon_current_window() + end + end, + }) + vim.api.nvim_create_autocmd("BufEnter", { group = self.augroup_id, pattern = "NvimTree_*", @@ -486,7 +518,7 @@ function Explorer:reload_explorer() local projects = git.reload_all_projects() self:refresh_nodes(projects) - if view.is_visible() then + if self.view:is_visible() then self.renderer:draw() end event_running = false @@ -508,7 +540,7 @@ end ---nil on no explorer or invalid view win ---@return integer[]|nil function Explorer:get_cursor_position() - local winnr = view.get_winnr() + local winnr = self.view:get_winnr() if not winnr or not vim.api.nvim_win_is_valid(winnr) then return end @@ -523,7 +555,7 @@ function Explorer:get_node_at_cursor() return end - if cursor[1] == 1 and 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 end @@ -557,6 +589,12 @@ function Explorer:get_nodes() return self:clone() end +---Log a lifecycle message with uid_explorer and absolute_path +---@param msg string? +function Explorer:log_lifecycle(msg) + log.line("lifecycle", "%-15s %d %s", msg, self.uid_explorer, self.absolute_path) +end + function Explorer:setup(opts) config = opts end diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 62a7dd9ef64..704945e1dc3 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -1,4 +1,3 @@ -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local Class = require("nvim-tree.classic") @@ -21,6 +20,8 @@ local LiveFilter = Class:extend() ---@protected ---@param args LiveFilterArgs function LiveFilter:new(args) + args.explorer:log_lifecycle("LiveFilter:new") + self.explorer = args.explorer self.prefix = self.explorer.opts.live_filter.prefix self.always_show_folders = self.explorer.opts.live_filter.always_show_folders @@ -56,14 +57,14 @@ local overlay_bufnr = 0 local overlay_winnr = 0 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 vim.api.nvim_create_autocmd("WinLeave", { pattern = "NvimTree_*", group = vim.api.nvim_create_augroup("NvimTree", { clear = false }), callback = function() if utils.is_nvim_tree_buf(0) then - view.close() + self.explorer.view:close() end end, }) @@ -156,7 +157,7 @@ end ---@return integer local function calculate_overlay_win_width(self) - local wininfo = vim.fn.getwininfo(view.get_winnr())[1] + local wininfo = vim.fn.getwininfo(self.explorer.view:get_winnr())[1] if wininfo then return wininfo.width - wininfo.textoff - #self.prefix @@ -166,7 +167,7 @@ local function calculate_overlay_win_width(self) end 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 vim.api.nvim_clear_autocmds({ event = "WinLeave", @@ -198,13 +199,13 @@ local function create_overlay(self) end 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.explorer.renderer:draw() local row = require("nvim-tree.core").get_nodes_starting_line() - 1 local col = #self.prefix > 0 and #self.prefix - 1 or 1 - view.set_cursor({ row, col }) + self.explorer.view:set_cursor({ row, col }) -- needs scheduling to let the cursor move before initializing the window vim.schedule(function() return create_overlay(self) @@ -213,7 +214,7 @@ end function LiveFilter:clear_filter() 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 reset_filter(self) diff --git a/lua/nvim-tree/explorer/sorter.lua b/lua/nvim-tree/explorer/sorter.lua index 799cfa481b1..3fd475e3f43 100644 --- a/lua/nvim-tree/explorer/sorter.lua +++ b/lua/nvim-tree/explorer/sorter.lua @@ -19,6 +19,8 @@ local Sorter = Class:extend() ---@protected ---@param args SorterArgs function Sorter:new(args) + args.explorer:log_lifecycle("Sorter:new") + self.explorer = args.explorer end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/explorer/view.lua similarity index 61% rename from lua/nvim-tree/view.lua rename to lua/nvim-tree/explorer/view.lua index 4ce95bfb6f4..cbe4ebb84b4 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -3,6 +3,8 @@ local utils = require("nvim-tree.utils") local log = require("nvim-tree.log") local notify = require("nvim-tree.notify") +local Class = require("nvim-tree.classic") + ---@class OpenInWinOpts ---@field hijack_current_buf boolean|nil default true ---@field resize boolean|nil default true @@ -14,28 +16,65 @@ local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 +-- TODO global, rework for multiinstance explorer +-- M.View retained for simpler change history M.View = { - adaptive_size = false, - centralize_selection = false, - tabpages = {}, - cursors = {}, - hide_root_folder = false, - live_filter = { - prev_focused_node = nil, - }, - winopts = { - relativenumber = false, - number = false, + tabpages = {} +} + +---@class (exact) View: Class +---@field live_filter table +---@field side string +---@field float table +---@field private explorer Explorer +---@field private adaptive_size boolean +---@field private centralize_selection boolean +---@field private cursors table as per vim.api.nvim_win_get_cursor +---@field private hide_root_folder boolean +---@field private winopts table +---@field private height integer +---@field private preserve_window_proportions boolean +---@field private initial_width integer +---@field private width (fun():integer)|integer|string +---@field private max_width integer +---@field private padding integer +local View = Class:extend() + +---@class View +---@overload fun(args: ViewArgs): View + +---@class (exact) ViewArgs +---@field explorer Explorer + +---@protected +---@param args ViewArgs +function View:new(args) + args.explorer:log_lifecycle("View:new") + + self.explorer = args.explorer + self.adaptive_size = false + self.centralize_selection = self.explorer.opts.view.centralize_selection + self.cursors = {} + self.float = self.explorer.opts.view.float + self.height = self.explorer.opts.view.height + self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false + self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions + self.side = (self.explorer.opts.view.side == "right") and "right" or "left" + self.live_filter = { prev_focused_node = nil, } + + self.winopts = { + relativenumber = self.explorer.opts.view.relativenumber, + number = self.explorer.opts.view.number, list = false, foldenable = false, winfixwidth = true, winfixheight = true, spell = false, - signcolumn = "yes", + signcolumn = self.explorer.opts.view.signcolumn, foldmethod = "manual", foldcolumn = "0", cursorcolumn = false, - cursorline = true, + cursorline = self.explorer.opts.view.cursorline, cursorlineopt = "both", colorcolumn = "0", wrap = false, @@ -53,8 +92,11 @@ M.View = { "NormalFloat:NvimTreeNormalFloat", "FloatBorder:NvimTreeNormalFloatBorder", }, ","), - }, -} + } + + self:configure_width(self.explorer.opts.view.width) + self.initial_width = self:get_width() +end -- The initial state of a tab local tabinitial = { @@ -64,6 +106,8 @@ local tabinitial = { winnr = nil, } +-- TODO global, rework for multiinstance explorer +---@type table local BUFNR_PER_TAB = {} ---@type { name: string, value: any }[] @@ -76,9 +120,10 @@ local BUFFER_OPTIONS = { { name = "swapfile", value = false }, } +---@private ---@param bufnr integer ---@return boolean -local function matches_bufnr(bufnr) +function View:matches_bufnr(bufnr) for _, b in pairs(BUFNR_PER_TAB) do if b == bufnr then return true @@ -87,39 +132,42 @@ local function matches_bufnr(bufnr) return false end -local function wipe_rogue_buffer() +---@private +function View:wipe_rogue_buffer() for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do - if not 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 }) end end end ----@param bufnr integer|boolean|nil -local function create_buffer(bufnr) - wipe_rogue_buffer() +---@private +---@param bufnr integer|false|nil +function View:create_buffer(bufnr) + self:wipe_rogue_buffer() local tab = vim.api.nvim_get_current_tabpage() BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) - vim.api.nvim_buf_set_name(M.get_bufnr(), "NvimTree_" .. tab) + vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab) - bufnr = M.get_bufnr() + bufnr = self:get_bufnr() for _, option in ipairs(BUFFER_OPTIONS) do vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr }) end - require("nvim-tree.keymap").on_attach(M.get_bufnr()) + require("nvim-tree.keymap").on_attach(self:get_bufnr()) - events._dispatch_tree_attached_post(M.get_bufnr()) + events._dispatch_tree_attached_post(self:get_bufnr()) end +---@private ---@param size (fun():integer)|integer|string ---@return integer -local function get_size(size) +function View:get_size(size) if type(size) == "number" then return size elseif type(size) == "function" then - return get_size(size()) + return self:get_size(size()) end local size_as_number = tonumber(size:sub(0, -2)) local percent_as_decimal = size_as_number / 100 @@ -128,11 +176,11 @@ end ---@param size (fun():integer)|integer|nil ---@return integer -local function get_width(size) +function View:get_width(size) if size then - return get_size(size) + return self:get_size(size) else - return get_size(M.View.width) + return self:get_size(self.width) end end @@ -142,20 +190,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 }) end -local function set_window_options_and_buffer() - pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr()) +---@private +function View:set_window_options_and_buffer() + pcall(vim.api.nvim_command, "buffer " .. self: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 @@ -166,7 +216,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 @@ -174,24 +224,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() + self: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 @@ -225,21 +277,23 @@ 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) + self.cursors[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0) end +---@private ---@param tabpage integer -local function close(tabpage) - if not M.is_visible({ tabpage = tabpage }) then +function View:close_internal(tabpage) + if not self: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 tree_win = self:get_winnr(tabpage) local current_win = vim.api.nvim_get_current_win() for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do if vim.api.nvim_win_get_config(win).relative == "" then @@ -259,39 +313,39 @@ 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() +function View:close_all_tabs() for tabpage, _ in pairs(M.View.tabpages) do - close(tabpage) + self:close_internal(tabpage) end end ---@param tabpage integer|nil -function M.close(tabpage) - if M.View.tab.sync.close then - M.close_all_tabs() +function View:close(tabpage) + if self.explorer.opts.tab.sync.close then + 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) - if M.is_visible() then +function View:open(options) + if self:is_visible() then return end local profile = log.profile_start("view open") events._dispatch_on_tree_pre_open() - create_buffer() - open_window() - M.resize() + self:create_buffer() + self:open_window() + self:resize() local opts = options or { focus_tree = true } if not opts.focus_tree then @@ -302,54 +356,55 @@ function M.open(options) log.profile_end(profile) end -local function grow() - local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 - local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) +---@private +function View:grow() + 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) -- number of columns of right-padding to indicate end of path - local padding = get_size(M.View.padding) + local padding = self:get_size(self.padding) -- account for sign/number columns etc. - local wininfo = vim.fn.getwininfo(M.get_winnr()) + local wininfo = vim.fn.getwininfo(self:get_winnr()) if type(wininfo) == "table" and type(wininfo[1]) == "table" then padding = padding + wininfo[1].textoff end - local resizing_width = M.View.initial_width - padding + local resizing_width = self.initial_width - padding local max_width -- maybe bound max - if M.View.max_width == -1 then + if self.max_width == -1 then max_width = -1 else - max_width = get_width(M.View.max_width) - padding + max_width = self:get_width(self.max_width) - padding end local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"] for line_nr, l in pairs(lines) do local count = vim.fn.strchars(l) -- also add space for right-aligned icons - local extmarks = vim.api.nvim_buf_get_extmarks(M.get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true }) + local extmarks = vim.api.nvim_buf_get_extmarks(self:get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true }) count = count + utils.extmarks_length(extmarks) if resizing_width < count then resizing_width = count end - if M.View.adaptive_size and max_width >= 0 and resizing_width >= max_width then + if self.adaptive_size and max_width >= 0 and resizing_width >= max_width then resizing_width = max_width break end end - M.resize(resizing_width + padding) + self:resize(resizing_width + padding) end -function M.grow_from_content() - if M.View.adaptive_size then - grow() +function View:grow_from_content() + if self.adaptive_size then + self:grow() end end ---@param size string|number|nil -function M.resize(size) - if M.View.float.enable and not M.View.adaptive_size then +function View:resize(size) + if self.float.enable and not self.adaptive_size then -- if the floating windows's adaptive size is not desired, then the -- float size should be defined in view.float.open_win_config return @@ -361,7 +416,7 @@ function M.resize(size) size = tonumber(size) if first_char == "+" or first_char == "-" then - size = M.View.width + size + size = self.width + size end end @@ -370,21 +425,21 @@ function M.resize(size) end if size then - M.View.width = size - M.View.height = size + self.width = size + self.height = size end - if not M.is_visible() then + if not self:is_visible() then return end - local winnr = M.get_winnr() or 0 + local winnr = self:get_winnr() or 0 - local new_size = get_width() + local new_size = self:get_width() if new_size ~= vim.api.nvim_win_get_width(winnr) then vim.api.nvim_win_set_width(winnr, new_size) - if not M.View.preserve_window_proportions then + if not self.preserve_window_proportions then vim.cmd(":wincmd =") end end @@ -392,37 +447,39 @@ function M.resize(size) events._dispatch_on_tree_resize(new_size) end -function M.reposition_window() - local move_to = move_tbl[M.View.side] +---@private +function View:reposition_window() + local move_to = move_tbl[self.side] vim.api.nvim_command("wincmd " .. move_to) - M.resize() + self:resize() end -local function set_current_win() +---@private +function View:set_current_win() local current_tab = vim.api.nvim_get_current_tabpage() M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() end ---Open the tree in the a window ---@param opts OpenInWinOpts|nil -function M.open_in_win(opts) +function View:open_in_win(opts) opts = opts or { hijack_current_buf = true, resize = true } events._dispatch_on_tree_pre_open() 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()) - set_current_win() - set_window_options_and_buffer() + self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) + self:setup_tabpage(vim.api.nvim_get_current_tabpage()) + self:set_current_win() + self:set_window_options_and_buffer() if opts.resize then - M.reposition_window() - M.resize() + self:reposition_window() + self:resize() end events._dispatch_on_tree_open() end -function M.abandon_current_window() +function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() BUFNR_PER_TAB[tab] = nil if M.View.tabpages[tab] then @@ -430,7 +487,7 @@ function M.abandon_current_window() end end -function M.abandon_all_windows() +function View:abandon_all_windows() for tab, _ in pairs(vim.api.nvim_list_tabpages()) do BUFNR_PER_TAB[tab] = nil if M.View.tabpages[tab] then @@ -441,7 +498,7 @@ end ---@param opts table|nil ---@return boolean -function M.is_visible(opts) +function View:is_visible(opts) if opts and opts.tabpage then if M.View.tabpages[opts.tabpage] == nil then return false @@ -459,27 +516,27 @@ function M.is_visible(opts) return false end - return M.get_winnr() ~= nil and vim.api.nvim_win_is_valid(M.get_winnr() or 0) + return self:get_winnr() ~= nil and vim.api.nvim_win_is_valid(self:get_winnr() or 0) end ---@param opts table|nil -function M.set_cursor(opts) - if M.is_visible() then - pcall(vim.api.nvim_win_set_cursor, M.get_winnr(), opts) +function View:set_cursor(opts) + if self:is_visible() then + pcall(vim.api.nvim_win_set_cursor, self:get_winnr(), opts) end end ---@param winnr number|nil ---@param open_if_closed boolean|nil -function M.focus(winnr, open_if_closed) - local wnr = winnr or M.get_winnr() +function View:focus(winnr, open_if_closed) + 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 - M.close() - M.open() - wnr = M.get_winnr() - elseif open_if_closed and not M.is_visible() then - M.open() + self:close() + self:open() + wnr = self:get_winnr() + elseif open_if_closed and not self:is_visible() then + self:open() end if wnr then @@ -490,28 +547,28 @@ end --- Retrieve the winid of the open tree. ---@param opts ApiTreeWinIdOpts|nil ---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible -function M.winid(opts) +function View:winid(opts) local tabpage = opts and opts.tabpage if tabpage == 0 then tabpage = vim.api.nvim_get_current_tabpage() end - if M.is_visible({ tabpage = tabpage }) then - return M.get_winnr(tabpage) + if self:is_visible({ tabpage = tabpage }) then + return self:get_winnr(tabpage) else return nil end end --- Restores the state of a NvimTree window if it was initialized before. -function M.restore_tab_state() +function View:restore_tab_state() local tabpage = vim.api.nvim_get_current_tabpage() - M.set_cursor(M.View.cursors[tabpage]) + self:set_cursor(self.cursors[tabpage]) end --- 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. ---@return number|nil -function M.get_winnr(tabpage) +function View:get_winnr(tabpage) tabpage = tabpage or vim.api.nvim_get_current_tabpage() local tabinfo = M.View.tabpages[tabpage] if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then @@ -521,13 +578,13 @@ end --- Returns the current nvim tree bufnr ---@return number -function M.get_bufnr() +function View:get_bufnr() return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] end -function M._prevent_buffer_override() - local view_winnr = M.get_winnr() - local view_bufnr = M.get_bufnr() +function View:prevent_buffer_override() + local view_winnr = self:get_winnr() + local view_bufnr = self:get_bufnr() -- need to schedule to let the new buffer populate the window -- because this event needs to be run on bufWipeout. @@ -554,7 +611,7 @@ function M._prevent_buffer_override() -- might need a better patch vim.cmd("setlocal nowinfixwidth") vim.cmd("setlocal nowinfixheight") - M.open({ focus_tree = false }) + self:open({ focus_tree = false }) local explorer = require("nvim-tree.core").get_explorer() if explorer then @@ -575,65 +632,44 @@ end ---@param cwd string|nil ---@return boolean -function M.is_root_folder_visible(cwd) - return cwd ~= "/" and not M.View.hide_root_folder +function View:is_root_folder_visible(cwd) + return cwd ~= "/" and not self.hide_root_folder end -- used on ColorScheme event -function M.reset_winhl() - local winnr = M.get_winnr() +function View:reset_winhl() + local winnr = self:get_winnr() if winnr and vim.api.nvim_win_is_valid(winnr) then - vim.wo[M.get_winnr()].winhl = M.View.winopts.winhl + vim.wo[self:get_winnr()].winhl = self.winopts.winhl end end ---Check if width determined or calculated on-fly ---@return boolean -function M.is_width_determined() - return type(M.View.width) ~= "function" +function View:is_width_determined() + return type(self.width) ~= "function" end ---Configure width-related config ---@param width string|function|number|table|nil -function M.configure_width(width) +function View:configure_width(width) if type(width) == "table" then - M.View.adaptive_size = true - M.View.width = width.min or DEFAULT_MIN_WIDTH - M.View.max_width = width.max or DEFAULT_MAX_WIDTH - M.View.padding = width.padding or DEFAULT_PADDING + self.adaptive_size = true + self.width = width.min or DEFAULT_MIN_WIDTH + self.max_width = width.max or DEFAULT_MAX_WIDTH + self.padding = width.padding or DEFAULT_PADDING elseif width == nil then - if M.config.width ~= nil then + if self.explorer.opts.view.width ~= nil then -- if we had input config - fallback to it - M.configure_width(M.config.width) + self:configure_width(self.explorer.opts.view.width) else -- otherwise - restore initial width - M.View.width = M.View.initial_width + self.width = self.initial_width end else - M.View.adaptive_size = false - M.View.width = width + self.adaptive_size = false + self.width = width end end -function M.setup(opts) - local options = opts.view or {} - M.View.centralize_selection = options.centralize_selection - M.View.side = (options.side == "right") and "right" or "left" - M.View.height = options.height - M.View.hide_root_folder = opts.renderer.root_folder_label == false - M.View.tab = opts.tab - M.View.preserve_window_proportions = options.preserve_window_proportions - M.View.winopts.cursorline = options.cursorline - M.View.winopts.number = options.number - M.View.winopts.relativenumber = options.relativenumber - M.View.winopts.signcolumn = options.signcolumn - M.View.float = options.float - M.on_attach = opts.on_attach - - M.config = options - M.configure_width(options.width) - - M.View.initial_width = get_width() -end - -return M +return View diff --git a/lua/nvim-tree/iterators/node-iterator.lua b/lua/nvim-tree/iterators/node-iterator.lua index 8cd4abdeda6..925a0a17955 100644 --- a/lua/nvim-tree/iterators/node-iterator.lua +++ b/lua/nvim-tree/iterators/node-iterator.lua @@ -48,7 +48,7 @@ function NodeIterator:recursor(f) end ---@return Node|nil ----@return number|nil +---@return number function NodeIterator:iterate() local iteration_count = 0 local function iter(nodes) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index a464edf39ba..4793075853b 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -1,4 +1,3 @@ -local view = require("nvim-tree.view") local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") @@ -12,9 +11,11 @@ local M = { } function M.set_target_win() + local explorer = core.get_explorer() + local id = vim.api.nvim_get_current_win() - local tree_id = view.get_winnr() - if tree_id and id == tree_id then + + if explorer and id == explorer.view:get_winnr() then M.target_winid = 0 return end @@ -30,11 +31,16 @@ local function handle_buf_cwd(cwd) end local function open_view_and_draw() + local explorer = core.get_explorer() + local cwd = vim.fn.getcwd() - view.open() + + if explorer then + explorer.view:open() + end + handle_buf_cwd(cwd) - local explorer = core.get_explorer() if explorer then explorer.renderer:draw() end @@ -110,25 +116,28 @@ function M.open(opts) local explorer = core.get_explorer() if should_hijack_current_buf() then - view.close_this_tab_only() - view.open_in_win() if explorer then + explorer.view:close_this_tab_only() + explorer.view:open_in_win() explorer.renderer:draw() end elseif opts.winid then - view.open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid }) if explorer then + explorer.view:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid }) explorer.renderer:draw() end elseif opts.current_window then - view.open_in_win({ hijack_current_buf = false, resize = false }) if explorer then + explorer.view:open_in_win({ hijack_current_buf = false, resize = false }) explorer.renderer:draw() end else open_view_and_draw() end - view.restore_tab_state() + + if explorer then + explorer.view:restore_tab_state() + end end function M.setup(opts) diff --git a/lua/nvim-tree/log.lua b/lua/nvim-tree/log.lua index 9665c1335e6..4aa8e9e1849 100644 --- a/lua/nvim-tree/log.lua +++ b/lua/nvim-tree/log.lua @@ -1,4 +1,4 @@ ----@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher" +---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "lifecycle" | "profile" | "watcher" ---@type table local types = {} diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index c940f999983..3771520c4a8 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -25,6 +25,8 @@ local Marks = Class:extend() ---@protected ---@param args MarksArgs function Marks:new(args) + args.explorer:log_lifecycle("Marks:new") + self.explorer = args.explorer self.marks = {} diff --git a/lua/nvim-tree/renderer/builder.lua b/lua/nvim-tree/renderer/builder.lua index 819660e5306..d2727f3f4ac 100644 --- a/lua/nvim-tree/renderer/builder.lua +++ b/lua/nvim-tree/renderer/builder.lua @@ -1,6 +1,5 @@ local notify = require("nvim-tree.notify") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local Class = require("nvim-tree.classic") @@ -379,7 +378,7 @@ end ---@private function Builder:build_header() - if 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) table.insert(self.lines, root_name) self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name)) diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 30af27e81b9..ff66f041a19 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -1,5 +1,4 @@ local log = require("nvim-tree.log") -local view = require("nvim-tree.view") local events = require("nvim-tree.events") local Class = require("nvim-tree.classic") @@ -26,6 +25,8 @@ local Renderer = Class:extend() ---@protected ---@param args RendererArgs function Renderer:new(args) + args.explorer:log_lifecycle("Renderer:new") + self.explorer = args.explorer end @@ -96,28 +97,28 @@ function Renderer:render_hl(bufnr, hl_range_args) end function Renderer:draw() - local bufnr = view.get_bufnr() + local bufnr = self.explorer.view:get_bufnr() if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then return end local profile = log.profile_start("draw") - local cursor = vim.api.nvim_win_get_cursor(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() self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines) if cursor and #builder.lines >= cursor[1] then - vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor) + vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr() or 0, cursor) end - view.grow_from_content() + self.explorer.view:grow_from_content() log.profile_end(profile) - events._dispatch_on_tree_rendered(bufnr, view.get_winnr()) + events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr()) end return Renderer diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 9396ba9d52f..e33aaaf1091 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -143,10 +143,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) end) :iterate() - i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1 - if node and node.explorer.live_filter.filter then - i = i + 1 + + if node then + 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 + return node, i end @@ -537,7 +543,10 @@ function M.focus_file(path) local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node) return node.absolute_path == path end) - require("nvim-tree.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 ---Focus node passed as parameter if visible, otherwise focus first visible parent. @@ -557,7 +566,7 @@ function M.focus_node_or_parent(node) end) if found_node or node.parent == nil then - require("nvim-tree.view").set_cursor({ i + 1, 1 }) + explorer.view:set_cursor({ i + 1, 1 }) break end