diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 4b8389e93a0..455af762c20 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -4,6 +4,7 @@ local utils = require("nvim-tree.utils") local actions = require("nvim-tree.actions") local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") +local Explorer = require("nvim-tree.explorer") local _config = {} @@ -47,7 +48,7 @@ function M.change_root(path, bufnr) -- test if in vim_cwd if utils.path_relative(path, vim_cwd) ~= path then if vim_cwd ~= cwd then - actions.root.change_dir.fn(vim_cwd) + Explorer.change_dir.fn(vim_cwd) end return end @@ -58,19 +59,20 @@ function M.change_root(path, bufnr) -- otherwise test M.init_root if _config.prefer_startup_root and utils.path_relative(path, M.init_root) ~= path then - actions.root.change_dir.fn(M.init_root) + Explorer.change_dir.fn(M.init_root) return end -- otherwise root_dirs + for _, dir in pairs(_config.root_dirs) do dir = vim.fn.fnamemodify(dir, ":p") if utils.path_relative(path, dir) ~= path then - actions.root.change_dir.fn(dir) + Explorer.change_dir.fn(dir) return end end -- finally fall back to the folder containing the file - actions.root.change_dir.fn(vim.fn.fnamemodify(path, ":p:h")) + Explorer.change_dir.fn(vim.fn.fnamemodify(path, ":p:h")) end function M.tab_enter() @@ -110,7 +112,7 @@ function M.open_on_directory() return end - actions.root.change_dir.force_dirchange(bufname, true) + Explorer.change_dir.force_dirchange(bufname, true) end ---@return table @@ -134,7 +136,7 @@ end ---@param name string|nil function M.change_dir(name) if name then - actions.root.change_dir.fn(name) + Explorer.change_dir.fn(name) end if _config.update_focused_file.update_root.enable then diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index 0a85a26832c..d117a24119a 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -4,13 +4,11 @@ M.finders = require("nvim-tree.actions.finders") M.fs = require("nvim-tree.actions.fs") M.moves = require("nvim-tree.actions.moves") M.node = require("nvim-tree.actions.node") -M.root = require("nvim-tree.actions.root") M.tree = require("nvim-tree.actions.tree") function M.setup(opts) M.fs.setup(opts) M.node.setup(opts) - M.root.setup(opts) M.tree.setup(opts) end diff --git a/lua/nvim-tree/actions/root/init.lua b/lua/nvim-tree/actions/root/init.lua deleted file mode 100644 index 1177e2050e1..00000000000 --- a/lua/nvim-tree/actions/root/init.lua +++ /dev/null @@ -1,10 +0,0 @@ -local M = {} - -M.change_dir = require("nvim-tree.actions.root.change-dir") -M.dir_up = require("nvim-tree.actions.root.dir-up") - -function M.setup(opts) - M.change_dir.setup(opts) -end - -return M diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index a1ee507f8af..30a583c03f8 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -7,6 +7,7 @@ local events = require("nvim-tree.events") local help = require("nvim-tree.help") local keymap = require("nvim-tree.keymap") local notify = require("nvim-tree.notify") +local Explorer = require("nvim-tree.explorer") local DirectoryNode = require("nvim-tree.node.directory") local FileLinkNode = require("nvim-tree.node.file-link") @@ -159,16 +160,17 @@ end) Api.tree.change_root_to_node = wrap_node(function(node) if node.name == ".." or node:is(RootNode) then - actions.root.change_dir.fn("..") + Explorer.change_dir.fn("..") else node = node:as(DirectoryNode) if node then - actions.root.change_dir.fn(node:last_group_node().absolute_path) + Explorer.change_dir.fn(node:last_group_node().absolute_path) end end end) -Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn) + +Api.tree.change_root_to_parent = wrap_node(Explorer.dir_up) Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor") Api.tree.get_nodes = wrap_explorer("get_nodes") @@ -274,7 +276,7 @@ local function open_or_expand_or_dir_up(mode, toggle_group) local dir = node:as(DirectoryNode) if root or node.name == ".." then - actions.root.change_dir.fn("..") + Explorer.change_dir.fn("..") elseif dir then dir:expand_or_collapse(toggle_group) elseif not toggle_group then diff --git a/lua/nvim-tree/actions/root/change-dir.lua b/lua/nvim-tree/explorer/change-dir.lua similarity index 98% rename from lua/nvim-tree/actions/root/change-dir.lua rename to lua/nvim-tree/explorer/change-dir.lua index f23f3c12086..f72eb837678 100644 --- a/lua/nvim-tree/actions/root/change-dir.lua +++ b/lua/nvim-tree/explorer/change-dir.lua @@ -66,6 +66,7 @@ end ---@return boolean local function should_change_dir() + local explorer = core.get_explorer() return M.options.enable and vim.tbl_isempty(vim.v.event) end diff --git a/lua/nvim-tree/actions/root/dir-up.lua b/lua/nvim-tree/explorer/dir-up.lua similarity index 74% rename from lua/nvim-tree/actions/root/dir-up.lua rename to lua/nvim-tree/explorer/dir-up.lua index a8c41c8c6f0..5997dd1b0fb 100644 --- a/lua/nvim-tree/actions/root/dir-up.lua +++ b/lua/nvim-tree/explorer/dir-up.lua @@ -3,10 +3,9 @@ local core = require("nvim-tree.core") local M = {} ----@param node Node function M.fn(node) if not node or node.name == ".." then - require("nvim-tree.actions.root.change-dir").fn("..") + require("lua.nvim-tree.explorer.change-dir").fn("..") else local cwd = core.get_cwd() if cwd == nil then @@ -14,7 +13,7 @@ function M.fn(node) end local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h") - require("nvim-tree.actions.root.change-dir").fn(newdir) + require("lua.nvim-tree.explorer.change-dir").fn(newdir) require("nvim-tree.actions.finders.find-file").fn(node.absolute_path) end end diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index afd1b182cee..89d512364e9 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -23,6 +23,9 @@ local Renderer = require("nvim-tree.renderer") local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON +local change_dir = require("nvim-tree.explorer.change-dir") + + local config ---@class (exact) Explorer: RootNode @@ -30,6 +33,7 @@ local config ---@field opts table user options ---@field augroup_id integer ---@field renderer Renderer +---@field change_dir any ---@field filters Filters ---@field live_filter LiveFilter ---@field sorters Sorter @@ -665,8 +669,26 @@ function Explorer:get_nodes() return self:clone() end +---@param node Node +function Explorer:dir_up(node) + if not node or node.name == ".." then + require("nvim-tree.explorer.change-dir").fn("..") + else + local cwd = core.get_cwd() + if cwd == nil then + return + end + local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h") + require("nvim-tree.explorer.change-dir").fn(newdir) + require("nvim-tree.actions.finders.find-file").fn(node.absolute_path) + end +end + +Explorer.change_dir = change_dir + function Explorer:setup(opts) config = opts + change_dir.setup(opts) end return Explorer diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index a464edf39ba..e2ae2edb710 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -25,7 +25,7 @@ end ---@param cwd string local function handle_buf_cwd(cwd) if M.respect_buf_cwd and cwd ~= core.get_cwd() then - require("nvim-tree.actions.root.change-dir").fn(cwd) + require("lua.nvim-tree.explorer.change-dir").fn(cwd) end end