fix: changed search_node action to determine correct index (#1022)

This commit is contained in:
Andreas Bissinger
2022-03-01 19:56:43 +01:00
committed by GitHub
parent 61a59ffae1
commit 1b8757e530

View File

@@ -15,40 +15,57 @@ function M.fn()
input_path input_path
}) })
local tree_altered = false local function count_visible_nodes(nodes)
local visible_nodes = 0
local function search_node(nodes)
-- first search for absolute match
local index_absolute_match = 0
for _, node in ipairs(nodes) do for _, node in ipairs(nodes) do
index_absolute_match = index_absolute_match + 1 visible_nodes = visible_nodes + 1
if absolute_input_path == node.absolute_path then if node.open and node.nodes then
return index_absolute_match visible_nodes = visible_nodes + count_visible_nodes(node.nodes)
end end
end end
-- if no absolute match in current directory, then search for partial match return visible_nodes
local index_partial_match = 0 end
local tree_altered = false
local found_something = false
local function search_node(nodes)
local index = 0
for _, node in ipairs(nodes) do for _, node in ipairs(nodes) do
index_partial_match = index_partial_match + 1 index = index + 1
if absolute_input_path == node.absolute_path then
found_something = true
return index
end
if node.nodes then if node.nodes then
local matches = utils.str_find(absolute_input_path, node.absolute_path) -- e.g. user searches for "/foo/bar.txt", than directory "/foo/bar" should not match with filename
local matches = utils.str_find(absolute_input_path, node.absolute_path .. '/')
if matches then if matches then
found_something = true
-- if node is not open -> open it
if not node.open then if not node.open then
node.open = true node.open = true
TreeExplorer:expand(node) TreeExplorer:expand(node)
tree_altered = true tree_altered = true
end end
return index_partial_match + search_node(node.nodes) return index + search_node(node.nodes)
end end
end end
if node.open then
index = index + count_visible_nodes(node.nodes)
end
end end
return 0 return index
end end
local index = search_node(TreeExplorer.nodes) local index = search_node(TreeExplorer.nodes)
@@ -57,7 +74,7 @@ function M.fn()
renderer.draw() renderer.draw()
end end
if index > 0 and view.is_visible() then if found_something and view.is_visible() then
if TreeExplorer.cwd ~= '/' and not view.View.hide_root_folder then if TreeExplorer.cwd ~= '/' and not view.View.hide_root_folder then
index = index + 1 index = index + 1
end end