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
5 changes: 1 addition & 4 deletions .luarc.json
Copy link
Member

Choose a reason for hiding this comment

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

Please don't make unnecessary changes.

Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
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

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(wrap_explorer("dir_up"))
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
@@ -1,21 +1,22 @@
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 = {}

---@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.

Copy link
Member

@alex-courtis alex-courtis Nov 29, 2025

Choose a reason for hiding this comment

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

Looks like this file is still present.

You might have to git rm lua/nvim-tree/explorer/dir-up.lua

Edit: I am mistaken, I was thinking of the old file location.

Edit2: this stands: we really don't need this file and it should be removed.

if not node or node.name == ".." then
require("nvim-tree.actions.root.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.actions.root.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

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,18 @@ 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

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

@alex-courtis alex-courtis Nov 29, 2025

Choose a reason for hiding this comment

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

This still isn't quite right: we're assigning the static change_dir module to a member field on the Explorer class. In this class we are directly calling the module change_dir directly, instead of via the member, which is the right way to use a static module.

We're stuck at a half way point in the refactor of change dir. The outcome of this issue is to have all of change dir moved into the Explorer class as methods. You've achieved this nicely for Explorer:dir_up.

Decision time:

Option 1:

  • complete both refactors, moving everything from change-dir.lua into Explorer

Option 2:

  • revert the change-dir changes
  • merge the dir-up changes
  • create a follow up PR for change-dir

This is my fault: I said "It might be easiest to split into two PRs." where I should have said "complete these changes in two PRs"

I strongly recommend option 2.
The first few multiinstance refactor PRs were done in single PRs; we started doing many refactors in one PR only after we were practiced.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your review once again, I quickly follow instructions and update the PR.

---@field filters Filters
---@field live_filter LiveFilter
---@field sorters Sorter
Expand Down Expand Up @@ -66,6 +71,7 @@ function Explorer:new(args)
self.clipboard = Clipboard({ explorer = self })

self:create_autocmds()
self.change_dir = change_dir

self:_load(self)
end
Expand Down Expand Up @@ -665,8 +671,24 @@ function Explorer:get_nodes()
return self:clone()
end

---@param node Node
function Explorer:dir_up(node)
if not node or node.name == ".." then
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")
change_dir.fn(newdir)
find_file.fn(node.absolute_path)
end
end

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

return Explorer
3 changes: 2 additions & 1 deletion lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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("nvim-tree.actions.root.change-dir").fn(cwd)
change_dir.fn(cwd)
end
end

Expand Down
Loading