chore(refacto): move parent and sibling functions into actions
BREAKING CHANGE: - move parent_node and sibling functions from lib to actions/movements.lua.
This commit is contained in:
@@ -65,15 +65,15 @@ local keypress_funcs = {
|
|||||||
cut = require'nvim-tree.actions.copy-paste'.cut,
|
cut = require'nvim-tree.actions.copy-paste'.cut,
|
||||||
paste = require'nvim-tree.actions.copy-paste'.paste,
|
paste = require'nvim-tree.actions.copy-paste'.paste,
|
||||||
close_node = lib.close_node,
|
close_node = lib.close_node,
|
||||||
parent_node = lib.parent_node,
|
parent_node = require'nvim-tree.actions.movements'.parent_node,
|
||||||
toggle_ignored = lib.toggle_ignored,
|
toggle_ignored = lib.toggle_ignored,
|
||||||
toggle_dotfiles = lib.toggle_dotfiles,
|
toggle_dotfiles = lib.toggle_dotfiles,
|
||||||
toggle_help = lib.toggle_help,
|
toggle_help = lib.toggle_help,
|
||||||
refresh = lib.refresh_tree,
|
refresh = lib.refresh_tree,
|
||||||
first_sibling = function(node) lib.sibling(node, -math.huge) end,
|
first_sibling = require'nvim-tree.actions.movements'.sibling(-math.huge),
|
||||||
last_sibling = function(node) lib.sibling(node, math.huge) end,
|
last_sibling = require'nvim-tree.actions.movements'.sibling(math.huge),
|
||||||
prev_sibling = function(node) lib.sibling(node, -1) end,
|
prev_sibling = require'nvim-tree.actions.movements'.sibling(-1),
|
||||||
next_sibling = function(node) lib.sibling(node, 1) end,
|
next_sibling = require'nvim-tree.actions.movements'.sibling(1),
|
||||||
prev_git_item = go_to('prev_git_item'),
|
prev_git_item = go_to('prev_git_item'),
|
||||||
next_git_item = go_to('next_git_item'),
|
next_git_item = go_to('next_git_item'),
|
||||||
dir_up = lib.dir_up,
|
dir_up = lib.dir_up,
|
||||||
|
|||||||
104
lua/nvim-tree/actions/movements.lua
Normal file
104
lua/nvim-tree/actions/movements.lua
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
local utils = require'nvim-tree.utils'
|
||||||
|
local view = require'nvim-tree.view'
|
||||||
|
local diagnostics = require'nvim-tree.diagnostics'
|
||||||
|
local lib = function() return require'nvim-tree.lib' end
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
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
|
||||||
|
local n = lib().get_last_group_node(entry)
|
||||||
|
if node_path == n.absolute_path 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.parent_node(node, should_close)
|
||||||
|
if node.name == '..' then return end
|
||||||
|
|
||||||
|
should_close = should_close or false
|
||||||
|
local altered_tree = false
|
||||||
|
|
||||||
|
local iter = get_line_from_node(node, true)
|
||||||
|
if node.open == true and should_close then
|
||||||
|
node.open = false
|
||||||
|
altered_tree = true
|
||||||
|
else
|
||||||
|
local line, parent = iter(lib().Tree.entries, true)
|
||||||
|
if parent == nil then
|
||||||
|
line = 1
|
||||||
|
elseif should_close then
|
||||||
|
parent.open = false
|
||||||
|
altered_tree = true
|
||||||
|
end
|
||||||
|
line = view.View.hide_root_folder and line - 1 or line
|
||||||
|
view.set_cursor({line, 0})
|
||||||
|
end
|
||||||
|
|
||||||
|
if altered_tree then
|
||||||
|
diagnostics.update()
|
||||||
|
lib().redraw()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.sibling(direction)
|
||||||
|
return function(node)
|
||||||
|
if node.name == '..' or not direction then return end
|
||||||
|
|
||||||
|
local iter = get_line_from_node(node, true)
|
||||||
|
local node_path = node.absolute_path
|
||||||
|
|
||||||
|
local line = 0
|
||||||
|
local parent, _
|
||||||
|
|
||||||
|
-- Check if current node is already at root entries
|
||||||
|
for index, entry in ipairs(lib().Tree.entries) do
|
||||||
|
if node_path == entry.absolute_path then
|
||||||
|
line = index
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if line > 0 then
|
||||||
|
parent = lib().Tree
|
||||||
|
else
|
||||||
|
_, parent = iter(lib().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)(lib().Tree.entries, true)
|
||||||
|
view.set_cursor({line, 0})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
@@ -63,31 +63,6 @@ local function get_node_at_line(line)
|
|||||||
return iter
|
return iter
|
||||||
end
|
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
|
|
||||||
local n = M.get_last_group_node(entry)
|
|
||||||
if node_path == n.absolute_path 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()
|
function M.get_node_at_cursor()
|
||||||
local winnr = view.get_winnr()
|
local winnr = view.get_winnr()
|
||||||
local hide_root_folder = view.View.hide_root_folder
|
local hide_root_folder = view.View.hide_root_folder
|
||||||
@@ -309,78 +284,10 @@ function M.open()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.sibling(node, direction)
|
|
||||||
if node.name == '..' or not direction then return end
|
|
||||||
|
|
||||||
local iter = get_line_from_node(node, true)
|
|
||||||
local node_path = node.absolute_path
|
|
||||||
|
|
||||||
local line = 0
|
|
||||||
local parent, _
|
|
||||||
|
|
||||||
-- Check if current node is already at root entries
|
|
||||||
for index, entry in ipairs(M.Tree.entries) do
|
|
||||||
if node_path == entry.absolute_path then
|
|
||||||
line = index
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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)
|
|
||||||
view.set_cursor({line, 0})
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.close_node(node)
|
function M.close_node(node)
|
||||||
M.parent_node(node, true)
|
M.parent_node(node, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.parent_node(node, should_close)
|
|
||||||
if node.name == '..' then return end
|
|
||||||
|
|
||||||
should_close = should_close or false
|
|
||||||
local altered_tree = false
|
|
||||||
|
|
||||||
local iter = get_line_from_node(node, true)
|
|
||||||
if node.open == true and should_close then
|
|
||||||
node.open = false
|
|
||||||
altered_tree = true
|
|
||||||
else
|
|
||||||
local line, parent = iter(M.Tree.entries, true)
|
|
||||||
if parent == nil then
|
|
||||||
line = 1
|
|
||||||
elseif should_close then
|
|
||||||
parent.open = false
|
|
||||||
altered_tree = true
|
|
||||||
end
|
|
||||||
line = require'nvim-tree.view'.View.hide_root_folder and line - 1 or line
|
|
||||||
view.set_cursor({line, 0})
|
|
||||||
end
|
|
||||||
|
|
||||||
if altered_tree then
|
|
||||||
diagnostics.update()
|
|
||||||
M.redraw()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.toggle_ignored()
|
function M.toggle_ignored()
|
||||||
explorer.config.filter_ignored = not explorer.config.filter_ignored
|
explorer.config.filter_ignored = not explorer.config.filter_ignored
|
||||||
return M.refresh_tree()
|
return M.refresh_tree()
|
||||||
|
|||||||
Reference in New Issue
Block a user