Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions lua/nvim-tree/actions/root/init.lua
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Unnecessary code gone!

This file was deleted.

10 changes: 6 additions & 4 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change calls the new dir_up method. This isn't correct as the call is to the function Explorer.dir_up rather than the method Explorer:dir_up.
You can use

wrap_node(wrap_explorer("dir_up"))

instead. wrap_explorer will call dir_up as a method and wrap_node will pass the node.

Why is that a problem? When dir_up is called, self is set to the node, not explorer.
You can see this by adding some debug to the start of Explorer:dir_up

  require("nvim-tree.log").line("dev", "self %s", self and self.absolute_path or "no self")
  require("nvim-tree.log").line("dev", "node %s", node and node.absolute_path or "no node")

- on the data directory as per base test cases.

[2025-11-03 11:34:32] [dev] self /home/alex/src/keyd/data
[2025-11-03 11:34:32] [dev] node no node

Expected:

[2025-11-03 11:32:34] [dev] self /home/alex/src/keyd
[2025-11-03 11:32:34] [dev] node /home/alex/src/keyd/data

Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor")
Api.tree.get_nodes = wrap_explorer("get_nodes")

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ end

---@return boolean
local function should_change_dir()
local explorer = core.get_explorer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable explorer is unused and has been identified by the linter: https://github.com/nvim-tree/nvim-tree.lua/actions/runs/18935214550/job/54313031114?pr=3209

This line should be deleted.

return M.options.enable and vim.tbl_isempty(vim.v.event)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ local core = require("nvim-tree.core")

local M = {}

---@param node Node
function M.fn(node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has been moved to Explorer:dir_up and it's working beautifully there.
This is great as this function is now unused, therefore we can delete this whole file.

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
return
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
Expand Down
22 changes: 22 additions & 0 deletions lua/nvim-tree/explorer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ 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
---@field uid_explorer number vim.loop.hrtime() at construction time
---@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
Expand Down Expand Up @@ -665,8 +669,26 @@ function Explorer:get_nodes()
return self:clone()
end

---@param node Node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotations are necessary for linting and LSP integration. Thank you for adding them.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change-dir.lua module is required inline however we can use the change_dir member of Explorer. It could be simplified to:

M.change_dir.fn(newdir)

require("nvim-tree.actions.finders.find-file").fn(node.absolute_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module find-file is required inline instead of at the start of the module. This isn't desirable: all modules should be required only once at startup and shared. Inline requires can result in problems appearing at runtime rather than startup, as they are lazily evaluated.

local find_file = require("nvim-tree.actions.finders.find-file")

Why do we have some inline requires? There are circular dependencies that forced it. One of the goals of this refactoring work is to remove circular dependencies.

end
end

Explorer.change_dir = change_dir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explorer.change_dir is assigned to the Explorer class, instead of the instance. This isn't great as all Explorer instances will share the same change_dir.
It should be set to the instance during the constructor e.g. self.change_dir = ...


function Explorer:setup(opts)
config = opts
change_dir.setup(opts)
end

return Explorer
2 changes: 1 addition & 1 deletion lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change requires the moved change-dir.lua module. It's throwing an exception as the module does not exist.
The require should be require("nvim-tree.explorer.change-dir")

See the exception in section "Fail: respect_buf_cwd = true" in the test document.

end
end

Expand Down
Loading