Feat/add advanced navigation (#257)
This commit is contained in:
@@ -67,9 +67,14 @@ function M.get_bindings()
|
||||
["<C-v>"] = M.nvim_tree_callback("vsplit"),
|
||||
["<C-x>"] = M.nvim_tree_callback("split"),
|
||||
["<C-t>"] = M.nvim_tree_callback("tabnew"),
|
||||
["<"] = M.nvim_tree_callback("prev_sibling"),
|
||||
[">"] = M.nvim_tree_callback("next_sibling"),
|
||||
["P"] = M.nvim_tree_callback("parent_node"),
|
||||
["<BS>"] = M.nvim_tree_callback("close_node"),
|
||||
["<S-CR>"] = M.nvim_tree_callback("close_node"),
|
||||
["<Tab>"] = M.nvim_tree_callback("preview"),
|
||||
["K"] = M.nvim_tree_callback("first_sibling"),
|
||||
["J"] = M.nvim_tree_callback("last_sibling"),
|
||||
["I"] = M.nvim_tree_callback("toggle_ignored"),
|
||||
["H"] = M.nvim_tree_callback("toggle_dotfiles"),
|
||||
["R"] = M.nvim_tree_callback("refresh"),
|
||||
|
||||
@@ -78,6 +78,30 @@ local function get_node_at_line(line)
|
||||
return iter
|
||||
end
|
||||
|
||||
local function get_line_from_node(node, find_parent)
|
||||
local node_path = node.absolute_path
|
||||
|
||||
if find_parent then
|
||||
node_path = node.absolute_path:match("(.*)"..utils.path_separator)
|
||||
end
|
||||
|
||||
local line = 2
|
||||
local function iter(entries, recursive)
|
||||
for _, entry in ipairs(entries) do
|
||||
if node_path:match('^'..entry.match_path..'$') ~= nil then
|
||||
return line, entry
|
||||
end
|
||||
|
||||
line = line + 1
|
||||
if entry.open == true and recursive then
|
||||
local _, child = iter(entry.entries, recursive)
|
||||
if child ~= nil then return line, child end
|
||||
end
|
||||
end
|
||||
end
|
||||
return iter
|
||||
end
|
||||
|
||||
function M.get_node_at_cursor()
|
||||
local cursor = api.nvim_win_get_cursor(M.Tree.winnr())
|
||||
local line = cursor[1]
|
||||
@@ -334,37 +358,65 @@ function M.open()
|
||||
api.nvim_command('setlocal '..window_opts.split_command)
|
||||
end
|
||||
|
||||
function M.close_node(node)
|
||||
if node.name == '..' then return end
|
||||
function M.sibling(node, direction)
|
||||
if not direction then return end
|
||||
|
||||
local sep = package.config:sub(1,1)
|
||||
local dname = node.absolute_path:match("(.*"..sep..")")
|
||||
local index = 2
|
||||
local iter = get_line_from_node(node, true)
|
||||
local node_path = node.absolute_path
|
||||
|
||||
local function iter(entries)
|
||||
for _, entry in ipairs(entries) do
|
||||
if dname:match('^'..entry.match_path..sep..'$') ~= nil then
|
||||
return entry
|
||||
end
|
||||
local line, parent = 0, nil
|
||||
|
||||
index = index + 1
|
||||
if entry.open == true then
|
||||
local child = iter(entry.entries)
|
||||
if child ~= nil then return child end
|
||||
end
|
||||
-- Check if current node is already at root entries
|
||||
for index, entry in ipairs(M.Tree.entries) do
|
||||
if node_path:match('^'..entry.match_path..'$') ~= nil then
|
||||
line = index
|
||||
end
|
||||
end
|
||||
|
||||
if node.open == true then
|
||||
if line > 0 then
|
||||
parent = M.Tree
|
||||
else
|
||||
_, parent = iter(M.Tree.entries, true)
|
||||
if parent ~= nil and #parent.entries > 1 then
|
||||
line, _ = get_line_from_node(node)(parent.entries)
|
||||
end
|
||||
|
||||
-- Ignore parent line count
|
||||
line = line - 1
|
||||
end
|
||||
|
||||
local index = line + direction
|
||||
if index < 1 then
|
||||
index = 1
|
||||
elseif index > #parent.entries then
|
||||
index = #parent.entries
|
||||
end
|
||||
local target_node = parent.entries[index]
|
||||
|
||||
line, _ = get_line_from_node(target_node)(M.Tree.entries, true)
|
||||
api.nvim_win_set_cursor(M.Tree.winnr(), {line, 0})
|
||||
renderer.draw(M.Tree, true)
|
||||
end
|
||||
|
||||
function M.close_node(node)
|
||||
M.parent_node(node, true)
|
||||
end
|
||||
|
||||
function M.parent_node(node, should_close)
|
||||
if node.name == '..' then return end
|
||||
should_close = should_close or false
|
||||
|
||||
local iter = get_line_from_node(node, true)
|
||||
if node.open == true and should_close then
|
||||
node.open = false
|
||||
else
|
||||
local parent = iter(M.Tree.entries)
|
||||
local line, parent = iter(M.Tree.entries, true)
|
||||
if parent == nil then
|
||||
index = 1
|
||||
else
|
||||
line = 1
|
||||
elseif should_close then
|
||||
parent.open = false
|
||||
end
|
||||
api.nvim_win_set_cursor(M.Tree.winnr(), {index, 0})
|
||||
api.nvim_win_set_cursor(M.Tree.winnr(), {line, 0})
|
||||
end
|
||||
renderer.draw(M.Tree, true)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user