From 0f2b74b7d5ae610f60f47cd1aad713ef2c43b714 Mon Sep 17 00:00:00 2001 From: uanela Date: Thu, 30 Oct 2025 10:50:57 +0200 Subject: [PATCH 1/3] chore: moving root actions to explorer --- lua/nvim-tree.lua | 14 ++++++----- lua/nvim-tree/actions/init.lua | 2 -- lua/nvim-tree/actions/root/init.lua | 10 -------- lua/nvim-tree/api.lua | 10 ++++---- .../{actions/root => explorer}/change-dir.lua | 1 + .../{actions/root => explorer}/dir-up.lua | 5 ++-- lua/nvim-tree/explorer/init.lua | 23 +++++++++++++++++++ lua/nvim-tree/lib.lua | 2 +- 8 files changed, 41 insertions(+), 26 deletions(-) delete mode 100644 lua/nvim-tree/actions/root/init.lua rename lua/nvim-tree/{actions/root => explorer}/change-dir.lua (98%) rename lua/nvim-tree/{actions/root => explorer}/dir-up.lua (74%) 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..1e0cdd9f0c9 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -23,6 +23,10 @@ local Renderer = require("nvim-tree.renderer") local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON +-- local dir_up = require("lua.nvim-tree.explorer.dir-up") +local change_dir = require("nvim-tree.explorer.change-dir") + + local config ---@class (exact) Explorer: RootNode @@ -30,6 +34,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 +670,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 From d9d70f04b7be98d553a2f37e7c1698a07b2e8163 Mon Sep 17 00:00:00 2001 From: uanela Date: Thu, 30 Oct 2025 10:58:49 +0200 Subject: [PATCH 2/3] chore: syncing changes --- lua/nvim-tree/explorer/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 1e0cdd9f0c9..89d512364e9 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -23,7 +23,6 @@ local Renderer = require("nvim-tree.renderer") local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON --- local dir_up = require("lua.nvim-tree.explorer.dir-up") local change_dir = require("nvim-tree.explorer.change-dir") From e104ddd3a560879844a89302864e4e7ce393c60f Mon Sep 17 00:00:00 2001 From: uanela Date: Thu, 27 Nov 2025 08:43:58 +0200 Subject: [PATCH 3/3] refactor: standardize explorer action wrapping and fix module requires - Use wrap_explorer for change_root_to_parent to match other API methods - Fix incorrect require paths in dir-up.lua (remove "lua." prefix) - Remove unused explorer variable in change-dir.lua - Move change_dir assignment in Explorer:new before _load call - Add find_file require to explorer/init.lua for dir_up method --- .luarc.json | 5 +---- lua/nvim-tree/api.lua | 2 +- lua/nvim-tree/explorer/change-dir.lua | 1 - lua/nvim-tree/explorer/dir-up.lua | 8 +++++--- lua/nvim-tree/explorer/init.lua | 10 +++++----- lua/nvim-tree/lib.lua | 3 ++- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/.luarc.json b/.luarc.json index 83ba413ca7c..03fc5c74dcd 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,10 +1,7 @@ { "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json", "workspace": { - "library": [ - "$VIMRUNTIME/lua/vim", - "${3rd}/luv/library" - ] + "library": ["$VIMRUNTIME/lua/vim", "${3rd}/luv/library"] }, "format": { "defaultConfig": { diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 30a583c03f8..e0e41426840 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -170,7 +170,7 @@ Api.tree.change_root_to_node = wrap_node(function(node) end) -Api.tree.change_root_to_parent = wrap_node(Explorer.dir_up) +Api.tree.change_root_to_parent = wrap_node(wrap_explorer("dir_up")) Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor") Api.tree.get_nodes = wrap_explorer("get_nodes") diff --git a/lua/nvim-tree/explorer/change-dir.lua b/lua/nvim-tree/explorer/change-dir.lua index f72eb837678..f23f3c12086 100644 --- a/lua/nvim-tree/explorer/change-dir.lua +++ b/lua/nvim-tree/explorer/change-dir.lua @@ -66,7 +66,6 @@ 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/explorer/dir-up.lua b/lua/nvim-tree/explorer/dir-up.lua index 5997dd1b0fb..4e8326ee548 100644 --- a/lua/nvim-tree/explorer/dir-up.lua +++ b/lua/nvim-tree/explorer/dir-up.lua @@ -1,11 +1,13 @@ local utils = require("nvim-tree.utils") local core = require("nvim-tree.core") +local change_dir = require("nvim-tree.explorer.change-dir") +local find_file = require("nvim-tree.actions.finders.find-file") local M = {} function M.fn(node) if not node or node.name == ".." then - require("lua.nvim-tree.explorer.change-dir").fn("..") + change_dir.fn("..") else local cwd = core.get_cwd() if cwd == nil then @@ -13,8 +15,8 @@ function M.fn(node) end local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h") - require("lua.nvim-tree.explorer.change-dir").fn(newdir) - require("nvim-tree.actions.finders.find-file").fn(node.absolute_path) + change_dir.fn(newdir) + 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 89d512364e9..c9789fe2e39 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -24,6 +24,7 @@ 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 find_file = require("nvim-tree.actions.finders.find-file") local config @@ -70,6 +71,7 @@ function Explorer:new(args) self.clipboard = Clipboard({ explorer = self }) self:create_autocmds() + self.change_dir = change_dir self:_load(self) end @@ -672,20 +674,18 @@ end ---@param node Node function Explorer:dir_up(node) if not node or node.name == ".." then - require("nvim-tree.explorer.change-dir").fn("..") + 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) + change_dir.fn(newdir) + find_file.fn(node.absolute_path) end end -Explorer.change_dir = change_dir - function Explorer:setup(opts) config = opts change_dir.setup(opts) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index e2ae2edb710..a7c85fda21b 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -1,6 +1,7 @@ local view = require("nvim-tree.view") local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") +local change_dir = require("nvim-tree.explorer.change-dir") ---@class LibOpenOpts ---@field path string|nil path @@ -25,7 +26,7 @@ end ---@param cwd string local function handle_buf_cwd(cwd) if M.respect_buf_cwd and cwd ~= core.get_cwd() then - require("lua.nvim-tree.explorer.change-dir").fn(cwd) + change_dir.fn(cwd) end end