Skip to content

Commit ace6422

Browse files
authored
feat(#2305): find file refreshes up the tree when node is not present (#2358)
1 parent 116b885 commit ace6422

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local log = require "nvim-tree.log"
22
local view = require "nvim-tree.view"
33
local utils = require "nvim-tree.utils"
44
local renderer = require "nvim-tree.renderer"
5+
local reload = require "nvim-tree.explorer.reload"
56
local core = require "nvim-tree.core"
67
local Iterator = require "nvim-tree.iterators.node-iterator"
78

@@ -29,8 +30,10 @@ function M.fn(path)
2930

3031
local profile = log.profile_start("find file %s", path_real)
3132

32-
-- force a re-expand when the node is not in the tree
33-
local node_present = utils.get_node_from_path(path_real) ~= nil
33+
-- refresh the contents of all parents, expanding groups as needed
34+
if utils.get_node_from_path(path_real) == nil then
35+
reload.refresh_parent_nodes_for_path(vim.fn.fnamemodify(path_real, ":h"))
36+
end
3437

3538
local line = core.get_nodes_starting_line()
3639

@@ -57,7 +60,7 @@ function M.fn(path)
5760
if not node.group_next then
5861
node.open = true
5962
end
60-
if #node.nodes == 0 or not node_present then
63+
if #node.nodes == 0 then
6164
core.get_explorer():expand(node)
6265
end
6366
end

lua/nvim-tree/explorer/reload.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local live_filter = require "nvim-tree.live-filter"
77
local git = require "nvim-tree.git"
88
local log = require "nvim-tree.log"
99

10+
local NodeIterator = require "nvim-tree.iterators.node-iterator"
1011
local Watcher = require "nvim-tree.watcher"
1112

1213
local M = {}
@@ -160,6 +161,44 @@ function M.refresh_node(node, callback)
160161
end)
161162
end
162163

164+
---Refresh contents of all nodes to a path: actual directory and links.
165+
---Groups will be expanded if needed.
166+
---@param path string absolute path
167+
function M.refresh_parent_nodes_for_path(path)
168+
local explorer = require("nvim-tree.core").get_explorer()
169+
if not explorer then
170+
return
171+
end
172+
173+
local profile = log.profile_start("refresh_parent_nodes_for_path %s", path)
174+
175+
-- collect parent nodes from the top down
176+
local parent_nodes = {}
177+
NodeIterator.builder({ explorer })
178+
:recursor(function(node)
179+
return node.nodes
180+
end)
181+
:applier(function(node)
182+
local abs_contains = node.absolute_path and path:find(node.absolute_path, 1, true) == 1
183+
local link_contains = node.link_to and path:find(node.link_to, 1, true) == 1
184+
if abs_contains or link_contains then
185+
table.insert(parent_nodes, node)
186+
end
187+
end)
188+
:iterate()
189+
190+
-- refresh in order; this will expand groups as needed
191+
for _, node in ipairs(parent_nodes) do
192+
local project_root = git.get_project_root(node.absolute_path)
193+
local project = git.get_project(project_root) or {}
194+
195+
M.reload(node, project)
196+
update_parent_statuses(node, project, project_root)
197+
end
198+
199+
log.profile_end(profile)
200+
end
201+
163202
function M.setup(opts)
164203
M.config = opts.renderer
165204
end

0 commit comments

Comments
 (0)