Skip to content

Commit 1701e41

Browse files
authored
refactor(#2988): multi-instance change-dir (#3233)
* chore: moving root action dir-up to explorer class * chore(explorer): moving requires to top level * wip(explorer): moved all change-dir functions to explorer class * wip(explorer): rename M.current_tab to self.current_tab * chore(explorer): move change_dir root action to explorer and replace all places using change_dir to use it from explorer * refactor(explorer): changing _foldername to folder_name * wip(explorer): rename expand method to expand_dir_node and all places using it * wip(explorer): mark private methods and remove add_profiling_to method * fix: allow changing root to file`s parent directory and fix restrict_above_cwd config path * refactor(api): moving change_root_to_node to Explorer:change_dir_to_node * fix(explorer): get new explorer before redrawing on <C-]> * docs(explorer): adding link to PR discussion for more context * fix(explorer): correctly check config.actions.change_dir for change_dir related methods * fix: correctly pass explorer as first arg when calling explorer methods in lua/nvim-tree * fix: correctly pass args to change_dir * fix: check for explorer before force_dirchange in open_on_directory * fix: check if force_dirchange should call core.init
1 parent 5757bcf commit 1701e41

File tree

10 files changed

+159
-160
lines changed

10 files changed

+159
-160
lines changed

lua/nvim-tree.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ local M = {
1111
init_root = "",
1212
}
1313

14+
--- Helper function to execute some explorer method safely
15+
---@param fn string # key of explorer
16+
---@param ... any|nil
17+
---@return function|nil
18+
local function explorer_fn(fn, ...)
19+
local explorer = core.get_explorer()
20+
if explorer then
21+
return explorer[fn](explorer, ...)
22+
end
23+
end
24+
1425
--- Update the tree root to a directory or the directory containing
1526
---@param path string relative or absolute
1627
---@param bufnr number|nil
@@ -47,7 +58,7 @@ function M.change_root(path, bufnr)
4758
-- test if in vim_cwd
4859
if utils.path_relative(path, vim_cwd) ~= path then
4960
if vim_cwd ~= cwd then
50-
actions.root.change_dir.fn(vim_cwd)
61+
explorer_fn("change_dir", vim_cwd)
5162
end
5263
return
5364
end
@@ -58,19 +69,19 @@ function M.change_root(path, bufnr)
5869

5970
-- otherwise test M.init_root
6071
if _config.prefer_startup_root and utils.path_relative(path, M.init_root) ~= path then
61-
actions.root.change_dir.fn(M.init_root)
72+
explorer_fn("change_dir", M.init_root)
6273
return
6374
end
6475
-- otherwise root_dirs
6576
for _, dir in pairs(_config.root_dirs) do
6677
dir = vim.fn.fnamemodify(dir, ":p")
6778
if utils.path_relative(path, dir) ~= path then
68-
actions.root.change_dir.fn(dir)
79+
explorer_fn("change_dir", dir)
6980
return
7081
end
7182
end
7283
-- finally fall back to the folder containing the file
73-
actions.root.change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
84+
explorer_fn("change_dir", vim.fn.fnamemodify(path, ":p:h"))
7485
end
7586

7687
function M.tab_enter()
@@ -110,7 +121,13 @@ function M.open_on_directory()
110121
return
111122
end
112123

113-
actions.root.change_dir.force_dirchange(bufname, true)
124+
125+
local explorer = core.get_explorer()
126+
if not explorer then
127+
core.init(bufname)
128+
end
129+
130+
explorer_fn("force_dirchange", bufname, true, false)
114131
end
115132

116133
---@return table
@@ -134,7 +151,7 @@ end
134151
---@param name string|nil
135152
function M.change_dir(name)
136153
if name then
137-
actions.root.change_dir.fn(name)
154+
explorer_fn("change_dir", name)
138155
end
139156

140157
if _config.update_focused_file.update_root.enable then

lua/nvim-tree/actions/finders/find-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function M.fn(path)
6666
dir.open = true
6767
end
6868
if #dir.nodes == 0 then
69-
core.get_explorer():expand(dir)
69+
core.get_explorer():expand_dir_node(dir)
7070
if dir.group_next and incremented_line then
7171
line = line - 1
7272
end

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/change-dir.lua

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

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

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

lua/nvim-tree/actions/tree/modifiers/expand.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local function expand(node)
2323
node = node:last_group_node()
2424
node.open = true
2525
if #node.nodes == 0 then
26-
core.get_explorer():expand(node)
26+
core.get_explorer():expand_dir_node(node)
2727
end
2828
end
2929

@@ -66,7 +66,7 @@ local function should_expand(expansion_count, node, should_descend)
6666

6767
if not dir.open and should_descend(expansion_count, node) then
6868
if #node.nodes == 0 then
69-
core.get_explorer():expand(dir) -- populate node.group_next
69+
core.get_explorer():expand_dir_node(dir) -- populate node.group_next
7070
end
7171

7272
if dir.group_next then

lua/nvim-tree/api.lua

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ local keymap = require("nvim-tree.keymap")
99
local notify = require("nvim-tree.notify")
1010

1111
local DirectoryNode = require("nvim-tree.node.directory")
12-
local FileNode = require("nvim-tree.node.file")
1312
local FileLinkNode = require("nvim-tree.node.file-link")
1413
local RootNode = require("nvim-tree.node.root")
1514
local UserDecorator = require("nvim-tree.renderer.decorator.user")
@@ -158,23 +157,7 @@ Api.tree.change_root = wrap(function(...)
158157
require("nvim-tree").change_dir(...)
159158
end)
160159

161-
Api.tree.change_root_to_node = wrap_node(function(node)
162-
if node.name == ".." or node:is(RootNode) then
163-
actions.root.change_dir.fn("..")
164-
return
165-
end
166-
167-
if node:is(FileNode) and node.parent ~= nil then
168-
actions.root.change_dir.fn(node.parent:last_group_node().absolute_path)
169-
return
170-
end
171-
172-
if node:is(DirectoryNode) then
173-
actions.root.change_dir.fn(node:last_group_node().absolute_path)
174-
return
175-
end
176-
end)
177-
160+
Api.tree.change_root_to_node = wrap_node(wrap_explorer("change_dir_to_node"))
178161
Api.tree.change_root_to_parent = wrap_node(wrap_explorer("dir_up"))
179162
Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor")
180163
Api.tree.get_nodes = wrap_explorer("get_nodes")
@@ -281,7 +264,7 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
281264
local dir = node:as(DirectoryNode)
282265

283266
if root or node.name == ".." then
284-
actions.root.change_dir.fn("..")
267+
wrap_explorer("change_dir")("..")
285268
elseif dir then
286269
dir:expand_or_collapse(toggle_group)
287270
elseif not toggle_group then

0 commit comments

Comments
 (0)