add LuaTreeFindFile, doc and option

This commit is contained in:
kyazdani42
2020-02-28 17:24:08 +01:00
parent b9398b285d
commit daff837b23
5 changed files with 83 additions and 8 deletions

View File

@@ -119,6 +119,38 @@ local function refresh_tree()
end
end
local function clone(obj)
if type(obj) ~= 'table' then return obj end
local res = {}
for k, v in pairs(obj) do res[clone(k)] = clone(v) end
return res
end
local function find_file(path)
local relpath = string.sub(path, #ROOT_PATH + 1, -1)
local tree_copy = clone(Tree)
for i, node in pairs(tree_copy) do
if node.relpath and string.match(relpath, node.relpath) then
if node.relpath == relpath then
Tree = clone(tree_copy)
return i
end
if node.dir and not node.open then
local path = node.path .. node.name
node.open = true
local dirs = list_dirs(path)
for j, n in pairs(create_nodes(path, node.relpath, node.depth + 1, dirs)) do
table.insert(tree_copy, i + j, n)
end
end
end
end
return nil
end
local function open_dir(tree_index)
local node = Tree[tree_index];
node.open = not node.open
@@ -152,4 +184,5 @@ return {
open_dir = open_dir;
set_root_path = set_root_path;
get_cwd = get_cwd;
find_file = find_file;
}

View File

@@ -16,6 +16,7 @@ local init_tree = state.init_tree
local open_dir = state.open_dir
local refresh_tree = state.refresh_tree
local set_root_path = state.set_root_path
local find_file = state.find_file
local winutils = require 'lib/winutils'
local update_view = winutils.update_view
@@ -24,6 +25,7 @@ local close = winutils.close
local open = winutils.open
local set_mappings = winutils.set_mappings
local replace_tree = winutils.replace_tree
local get_win = winutils.get_win
local git = require 'lib/git'
local refresh_git = git.refresh_git
@@ -147,6 +149,19 @@ local function check_buffer_and_open()
end
end
local function find()
local line = find_file(api.nvim_buf_get_name(0))
if not line then return end
update_view()
local win = get_win()
if win then
api.nvim_win_set_cursor(win, { line, 0 })
end
end
return {
toggle = toggle;
open_file = open_file;
@@ -155,5 +170,6 @@ return {
check_windows_and_close = check_windows_and_close;
check_buffer_and_open = check_buffer_and_open;
replace_tree = replace_tree;
find = find;
}