Skip to content

Commit 0f2b74b

Browse files
committed
chore: moving root actions to explorer
1 parent e179ad2 commit 0f2b74b

File tree

8 files changed

+41
-26
lines changed

8 files changed

+41
-26
lines changed

lua/nvim-tree.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local utils = require("nvim-tree.utils")
44
local actions = require("nvim-tree.actions")
55
local core = require("nvim-tree.core")
66
local notify = require("nvim-tree.notify")
7+
local Explorer = require("nvim-tree.explorer")
78

89
local _config = {}
910

@@ -47,7 +48,7 @@ function M.change_root(path, bufnr)
4748
-- test if in vim_cwd
4849
if utils.path_relative(path, vim_cwd) ~= path then
4950
if vim_cwd ~= cwd then
50-
actions.root.change_dir.fn(vim_cwd)
51+
Explorer.change_dir.fn(vim_cwd)
5152
end
5253
return
5354
end
@@ -58,19 +59,20 @@ function M.change_root(path, bufnr)
5859

5960
-- otherwise test M.init_root
6061
if _config.prefer_startup_root and utils.path_relative(path, M.init_root) ~= path then
61-
actions.root.change_dir.fn(M.init_root)
62+
Explorer.change_dir.fn(M.init_root)
6263
return
6364
end
6465
-- otherwise root_dirs
66+
6567
for _, dir in pairs(_config.root_dirs) do
6668
dir = vim.fn.fnamemodify(dir, ":p")
6769
if utils.path_relative(path, dir) ~= path then
68-
actions.root.change_dir.fn(dir)
70+
Explorer.change_dir.fn(dir)
6971
return
7072
end
7173
end
7274
-- finally fall back to the folder containing the file
73-
actions.root.change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
75+
Explorer.change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
7476
end
7577

7678
function M.tab_enter()
@@ -110,7 +112,7 @@ function M.open_on_directory()
110112
return
111113
end
112114

113-
actions.root.change_dir.force_dirchange(bufname, true)
115+
Explorer.change_dir.force_dirchange(bufname, true)
114116
end
115117

116118
---@return table
@@ -134,7 +136,7 @@ end
134136
---@param name string|nil
135137
function M.change_dir(name)
136138
if name then
137-
actions.root.change_dir.fn(name)
139+
Explorer.change_dir.fn(name)
138140
end
139141

140142
if _config.update_focused_file.update_root.enable then

lua/nvim-tree/actions/init.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ M.finders = require("nvim-tree.actions.finders")
44
M.fs = require("nvim-tree.actions.fs")
55
M.moves = require("nvim-tree.actions.moves")
66
M.node = require("nvim-tree.actions.node")
7-
M.root = require("nvim-tree.actions.root")
87
M.tree = require("nvim-tree.actions.tree")
98

109
function M.setup(opts)
1110
M.fs.setup(opts)
1211
M.node.setup(opts)
13-
M.root.setup(opts)
1412
M.tree.setup(opts)
1513
end
1614

lua/nvim-tree/actions/root/init.lua

Lines changed: 0 additions & 10 deletions
This file was deleted.

lua/nvim-tree/api.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local events = require("nvim-tree.events")
77
local help = require("nvim-tree.help")
88
local keymap = require("nvim-tree.keymap")
99
local notify = require("nvim-tree.notify")
10+
local Explorer = require("nvim-tree.explorer")
1011

1112
local DirectoryNode = require("nvim-tree.node.directory")
1213
local FileLinkNode = require("nvim-tree.node.file-link")
@@ -159,16 +160,17 @@ end)
159160

160161
Api.tree.change_root_to_node = wrap_node(function(node)
161162
if node.name == ".." or node:is(RootNode) then
162-
actions.root.change_dir.fn("..")
163+
Explorer.change_dir.fn("..")
163164
else
164165
node = node:as(DirectoryNode)
165166
if node then
166-
actions.root.change_dir.fn(node:last_group_node().absolute_path)
167+
Explorer.change_dir.fn(node:last_group_node().absolute_path)
167168
end
168169
end
169170
end)
170171

171-
Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn)
172+
173+
Api.tree.change_root_to_parent = wrap_node(Explorer.dir_up)
172174
Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor")
173175
Api.tree.get_nodes = wrap_explorer("get_nodes")
174176

@@ -274,7 +276,7 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
274276
local dir = node:as(DirectoryNode)
275277

276278
if root or node.name == ".." then
277-
actions.root.change_dir.fn("..")
279+
Explorer.change_dir.fn("..")
278280
elseif dir then
279281
dir:expand_or_collapse(toggle_group)
280282
elseif not toggle_group then

lua/nvim-tree/actions/root/change-dir.lua renamed to lua/nvim-tree/explorer/change-dir.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ end
6666

6767
---@return boolean
6868
local function should_change_dir()
69+
local explorer = core.get_explorer()
6970
return M.options.enable and vim.tbl_isempty(vim.v.event)
7071
end
7172

lua/nvim-tree/actions/root/dir-up.lua renamed to lua/nvim-tree/explorer/dir-up.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ local core = require("nvim-tree.core")
33

44
local M = {}
55

6-
---@param node Node
76
function M.fn(node)
87
if not node or node.name == ".." then
9-
require("nvim-tree.actions.root.change-dir").fn("..")
8+
require("lua.nvim-tree.explorer.change-dir").fn("..")
109
else
1110
local cwd = core.get_cwd()
1211
if cwd == nil then
1312
return
1413
end
1514

1615
local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h")
17-
require("nvim-tree.actions.root.change-dir").fn(newdir)
16+
require("lua.nvim-tree.explorer.change-dir").fn(newdir)
1817
require("nvim-tree.actions.finders.find-file").fn(node.absolute_path)
1918
end
2019
end

lua/nvim-tree/explorer/init.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ local Renderer = require("nvim-tree.renderer")
2323

2424
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
2525

26+
-- local dir_up = require("lua.nvim-tree.explorer.dir-up")
27+
local change_dir = require("nvim-tree.explorer.change-dir")
28+
29+
2630
local config
2731

2832
---@class (exact) Explorer: RootNode
2933
---@field uid_explorer number vim.loop.hrtime() at construction time
3034
---@field opts table user options
3135
---@field augroup_id integer
3236
---@field renderer Renderer
37+
---@field change_dir any
3338
---@field filters Filters
3439
---@field live_filter LiveFilter
3540
---@field sorters Sorter
@@ -665,8 +670,26 @@ function Explorer:get_nodes()
665670
return self:clone()
666671
end
667672

673+
---@param node Node
674+
function Explorer:dir_up(node)
675+
if not node or node.name == ".." then
676+
require("nvim-tree.explorer.change-dir").fn("..")
677+
else
678+
local cwd = core.get_cwd()
679+
if cwd == nil then
680+
return
681+
end
682+
local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h")
683+
require("nvim-tree.explorer.change-dir").fn(newdir)
684+
require("nvim-tree.actions.finders.find-file").fn(node.absolute_path)
685+
end
686+
end
687+
688+
Explorer.change_dir = change_dir
689+
668690
function Explorer:setup(opts)
669691
config = opts
692+
change_dir.setup(opts)
670693
end
671694

672695
return Explorer

lua/nvim-tree/lib.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
---@param cwd string
2626
local function handle_buf_cwd(cwd)
2727
if M.respect_buf_cwd and cwd ~= core.get_cwd() then
28-
require("nvim-tree.actions.root.change-dir").fn(cwd)
28+
require("lua.nvim-tree.explorer.change-dir").fn(cwd)
2929
end
3030
end
3131

0 commit comments

Comments
 (0)