diff --git a/lua/nvim-tree/actions/search-node.lua b/lua/nvim-tree/actions/search-node.lua index 3ad6a698..485806a3 100644 --- a/lua/nvim-tree/actions/search-node.lua +++ b/lua/nvim-tree/actions/search-node.lua @@ -15,40 +15,57 @@ function M.fn() input_path }) - local tree_altered = false - - local function search_node(nodes) - -- first search for absolute match - local index_absolute_match = 0 + local function count_visible_nodes(nodes) + local visible_nodes = 0 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 - return index_absolute_match + if node.open and node.nodes then + visible_nodes = visible_nodes + count_visible_nodes(node.nodes) end end - -- if no absolute match in current directory, then search for partial match - local index_partial_match = 0 + return visible_nodes + end + + local tree_altered = false + local found_something = false + + local function search_node(nodes) + local index = 0 + 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 - 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 + found_something = true + + -- if node is not open -> open it if not node.open then node.open = true TreeExplorer:expand(node) tree_altered = true end - return index_partial_match + search_node(node.nodes) + return index + search_node(node.nodes) end end + + if node.open then + index = index + count_visible_nodes(node.nodes) + end end - return 0 + return index end local index = search_node(TreeExplorer.nodes) @@ -57,7 +74,7 @@ function M.fn() renderer.draw() 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 index = index + 1 end