nvim-tree.lua/lua/nvim-tree/actions/find-file.lua
kiyan f977e5c05a refacto: make git module interface to wait for job to finish
allows simplify the explore/reload/find/initialization by making the
whole code synchronous. No more callback needed.
2022-02-21 19:12:16 +01:00

61 lines
1.3 KiB
Lua

local view = require'nvim-tree.view'
local utils = require'nvim-tree.utils'
local renderer = require"nvim-tree.renderer"
local M = {}
local function get_index_offset()
local hide_root_folder = view.View.hide_root_folder
if TreeExplorer.cwd == '/' or hide_root_folder then
return 0
else
return 1
end
end
local running = {}
function M.fn(fname)
if running[fname] or not TreeExplorer then return end
running[fname] = true
local i = get_index_offset()
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
end
local path_matches = node.nodes and utils.str_find(fname, node.absolute_path..utils.path_separator)
if path_matches then
if not node.open then
node.open = true
tree_altered = true
TreeExplorer:expand(node)
end
if iterate_nodes(node.nodes) ~= nil then
return i
end
-- mandatory to iterate i
elseif node.open then
iterate_nodes(node.nodes)
end
end
end
local index = iterate_nodes(TreeExplorer.nodes)
if tree_altered then
renderer.draw()
end
if index and view.is_visible() then
view.set_cursor({index, 0})
end
running[fname] = false
end
return M