diff --git a/lua/nvim-tree/actions/find-file.lua b/lua/nvim-tree/actions/find-file.lua index f88c8e2acba..fca8c921c1d 100644 --- a/lua/nvim-tree/actions/find-file.lua +++ b/lua/nvim-tree/actions/find-file.lua @@ -1,3 +1,4 @@ +local uv = vim.loop local view = require "nvim-tree.view" local utils = require "nvim-tree.utils" local renderer = require "nvim-tree.renderer" @@ -7,23 +8,38 @@ local M = {} local running = {} +---Find a path in the tree, expand it and focus it +---@param fname string full path function M.fn(fname) if running[fname] or not core.get_explorer() then return end running[fname] = true + -- always match against the real path + local fname_real = uv.fs_realpath(fname) + if not fname_real then + return + end + local i = view.is_root_folder_visible() and 1 or 0 local tree_altered = false local function iterate_nodes(nodes) for _, node in ipairs(nodes) do i = i + 1 - if node.absolute_path == fname then - return i + + if not node.absolute_path or not uv.fs_stat(node.absolute_path) then + break end - local path_matches = node.nodes and vim.startswith(fname, node.absolute_path .. utils.path_separator) + -- match against node absolute and link, as symlinks themselves will differ + if node.absolute_path == fname_real or node.link_to == fname_real then + return i + end + local abs_match = vim.startswith(fname_real, node.absolute_path .. utils.path_separator) + local link_match = node.link_to and vim.startswith(fname_real, node.link_to .. utils.path_separator) + local path_matches = node.nodes and (abs_match or link_match) if path_matches then if not node.open then node.open = true