Skip to content

feat(#2305): find file refreshes up the tree when node is not present #2358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2023
Merged
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
9 changes: 6 additions & 3 deletions lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local renderer = require "nvim-tree.renderer"
local reload = require "nvim-tree.explorer.reload"
local core = require "nvim-tree.core"
local Iterator = require "nvim-tree.iterators.node-iterator"

Expand Down Expand Up @@ -29,8 +30,10 @@ function M.fn(path)

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

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

local line = core.get_nodes_starting_line()

Expand All @@ -57,7 +60,7 @@ function M.fn(path)
if not node.group_next then
node.open = true
end
if #node.nodes == 0 or not node_present then
if #node.nodes == 0 then
core.get_explorer():expand(node)
end
end
Expand Down
39 changes: 39 additions & 0 deletions lua/nvim-tree/explorer/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local live_filter = require "nvim-tree.live-filter"
local git = require "nvim-tree.git"
local log = require "nvim-tree.log"

local NodeIterator = require "nvim-tree.iterators.node-iterator"
local Watcher = require "nvim-tree.watcher"

local M = {}
Expand Down Expand Up @@ -160,6 +161,44 @@ function M.refresh_node(node, callback)
end)
end

---Refresh contents of all nodes to a path: actual directory and links.
---Groups will be expanded if needed.
---@param path string absolute path
function M.refresh_parent_nodes_for_path(path)
local explorer = require("nvim-tree.core").get_explorer()
if not explorer then
return
end

local profile = log.profile_start("refresh_parent_nodes_for_path %s", path)

-- collect parent nodes from the top down
local parent_nodes = {}
NodeIterator.builder({ explorer })
:recursor(function(node)
return node.nodes
end)
:applier(function(node)
local abs_contains = node.absolute_path and path:find(node.absolute_path, 1, true) == 1
local link_contains = node.link_to and path:find(node.link_to, 1, true) == 1
if abs_contains or link_contains then
table.insert(parent_nodes, node)
end
end)
:iterate()

-- refresh in order; this will expand groups as needed
for _, node in ipairs(parent_nodes) do
local project_root = git.get_project_root(node.absolute_path)
local project = git.get_project(project_root) or {}

M.reload(node, project)
update_parent_statuses(node, project, project_root)
end

log.profile_end(profile)
end

function M.setup(opts)
M.config = opts.renderer
end
Expand Down