refactor(actions): split movements into multiple modules
This commit is contained in:
@@ -27,16 +27,16 @@ local Actions = {
|
|||||||
rename = require("nvim-tree.actions.fs.rename-file").fn(false),
|
rename = require("nvim-tree.actions.fs.rename-file").fn(false),
|
||||||
|
|
||||||
-- Movements in tree
|
-- Movements in tree
|
||||||
close_node = require("nvim-tree.actions.moves.movements").parent_node(true),
|
close_node = require("nvim-tree.actions.moves.parent").fn(true),
|
||||||
first_sibling = require("nvim-tree.actions.moves.movements").sibling(-math.huge),
|
first_sibling = require("nvim-tree.actions.moves.sibling").fn(-math.huge),
|
||||||
last_sibling = require("nvim-tree.actions.moves.movements").sibling(math.huge),
|
last_sibling = require("nvim-tree.actions.moves.sibling").fn(math.huge),
|
||||||
next_diag_item = require("nvim-tree.actions.moves.movements").find_item("next", "diag"),
|
next_diag_item = require("nvim-tree.actions.moves.item").fn("next", "diag"),
|
||||||
next_git_item = require("nvim-tree.actions.moves.movements").find_item("next", "git"),
|
next_git_item = require("nvim-tree.actions.moves.item").fn("next", "git"),
|
||||||
next_sibling = require("nvim-tree.actions.moves.movements").sibling(1),
|
next_sibling = require("nvim-tree.actions.moves.sibling").fn(1),
|
||||||
parent_node = require("nvim-tree.actions.moves.movements").parent_node(false),
|
parent_node = require("nvim-tree.actions.moves.parent").fn(false),
|
||||||
prev_diag_item = require("nvim-tree.actions.moves.movements").find_item("prev", "diag"),
|
prev_diag_item = require("nvim-tree.actions.moves.item").fn("prev", "diag"),
|
||||||
prev_git_item = require("nvim-tree.actions.moves.movements").find_item("prev", "git"),
|
prev_git_item = require("nvim-tree.actions.moves.item").fn("prev", "git"),
|
||||||
prev_sibling = require("nvim-tree.actions.moves.movements").sibling(-1),
|
prev_sibling = require("nvim-tree.actions.moves.sibling").fn(-1),
|
||||||
|
|
||||||
-- Other types
|
-- Other types
|
||||||
refresh = require("nvim-tree.actions.reloaders.reloaders").reload_explorer,
|
refresh = require("nvim-tree.actions.reloaders.reloaders").reload_explorer,
|
||||||
|
|||||||
55
lua/nvim-tree/actions/moves/item.lua
Normal file
55
lua/nvim-tree/actions/moves/item.lua
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
local utils = require "nvim-tree.utils"
|
||||||
|
local view = require "nvim-tree.view"
|
||||||
|
local core = require "nvim-tree.core"
|
||||||
|
local lib = require "nvim-tree.lib"
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.fn(where, what)
|
||||||
|
return function()
|
||||||
|
local node_cur = lib.get_node_at_cursor()
|
||||||
|
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())
|
||||||
|
|
||||||
|
local cur, first, prev, nex = nil, nil, nil, nil
|
||||||
|
for line, node in pairs(nodes_by_line) do
|
||||||
|
local valid = false
|
||||||
|
if what == "git" then
|
||||||
|
valid = node.git_status ~= nil
|
||||||
|
elseif what == "diag" then
|
||||||
|
valid = node.diag_status ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if not first and valid then
|
||||||
|
first = line
|
||||||
|
end
|
||||||
|
|
||||||
|
if node == node_cur then
|
||||||
|
cur = line
|
||||||
|
elseif valid then
|
||||||
|
if not cur then
|
||||||
|
prev = line
|
||||||
|
end
|
||||||
|
if cur and not nex then
|
||||||
|
nex = line
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if where == "prev" then
|
||||||
|
if prev then
|
||||||
|
view.set_cursor { prev, 0 }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if cur then
|
||||||
|
if nex then
|
||||||
|
view.set_cursor { nex, 0 }
|
||||||
|
end
|
||||||
|
elseif first then
|
||||||
|
view.set_cursor { first, 0 }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
local utils = require "nvim-tree.utils"
|
|
||||||
local view = require "nvim-tree.view"
|
|
||||||
local renderer = require "nvim-tree.renderer"
|
|
||||||
local core = require "nvim-tree.core"
|
|
||||||
local lib = require "nvim-tree.lib"
|
|
||||||
|
|
||||||
local M = {}
|
|
||||||
|
|
||||||
local function get_index_of(node, nodes)
|
|
||||||
local node_path = node.absolute_path
|
|
||||||
local line = 1
|
|
||||||
|
|
||||||
for _, _node in ipairs(nodes) do
|
|
||||||
if not _node.hidden then
|
|
||||||
local n = lib.get_last_group_node(_node)
|
|
||||||
if node_path == n.absolute_path then
|
|
||||||
return line
|
|
||||||
end
|
|
||||||
|
|
||||||
line = line + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.parent_node(should_close)
|
|
||||||
should_close = should_close or false
|
|
||||||
|
|
||||||
return function(node)
|
|
||||||
if should_close and node.open then
|
|
||||||
node.open = false
|
|
||||||
return renderer.draw()
|
|
||||||
end
|
|
||||||
|
|
||||||
local parent = node.parent
|
|
||||||
|
|
||||||
if renderer.config.group_empty and parent then
|
|
||||||
while parent.parent and parent.parent.group_next do
|
|
||||||
parent = parent.parent
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not parent or not parent.parent then
|
|
||||||
return view.set_cursor { 1, 0 }
|
|
||||||
end
|
|
||||||
|
|
||||||
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
|
|
||||||
return n.absolute_path == parent.absolute_path
|
|
||||||
end)
|
|
||||||
|
|
||||||
view.set_cursor { line + 1, 0 }
|
|
||||||
if should_close then
|
|
||||||
parent.open = false
|
|
||||||
renderer.draw()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.sibling(direction)
|
|
||||||
return function(node)
|
|
||||||
if node.name == ".." or not direction then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local parent = node.parent or core.get_explorer()
|
|
||||||
local parent_nodes = vim.tbl_filter(function(n)
|
|
||||||
return not n.hidden
|
|
||||||
end, parent.nodes)
|
|
||||||
|
|
||||||
local node_index = get_index_of(node, parent_nodes)
|
|
||||||
|
|
||||||
local target_idx = node_index + direction
|
|
||||||
if target_idx < 1 then
|
|
||||||
target_idx = 1
|
|
||||||
elseif target_idx > #parent_nodes then
|
|
||||||
target_idx = #parent_nodes
|
|
||||||
end
|
|
||||||
|
|
||||||
local target_node = parent_nodes[target_idx]
|
|
||||||
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
|
|
||||||
return n.absolute_path == target_node.absolute_path
|
|
||||||
end)
|
|
||||||
|
|
||||||
view.set_cursor { line + 1, 0 }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.find_item(where, what)
|
|
||||||
return function()
|
|
||||||
local node_cur = lib.get_node_at_cursor()
|
|
||||||
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())
|
|
||||||
|
|
||||||
local cur, first, prev, nex = nil, nil, nil, nil
|
|
||||||
for line, node in pairs(nodes_by_line) do
|
|
||||||
local valid = false
|
|
||||||
if what == "git" then
|
|
||||||
valid = node.git_status ~= nil
|
|
||||||
elseif what == "diag" then
|
|
||||||
valid = node.diag_status ~= nil
|
|
||||||
end
|
|
||||||
|
|
||||||
if not first and valid then
|
|
||||||
first = line
|
|
||||||
end
|
|
||||||
|
|
||||||
if node == node_cur then
|
|
||||||
cur = line
|
|
||||||
elseif valid then
|
|
||||||
if not cur then
|
|
||||||
prev = line
|
|
||||||
end
|
|
||||||
if cur and not nex then
|
|
||||||
nex = line
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if where == "prev" then
|
|
||||||
if prev then
|
|
||||||
view.set_cursor { prev, 0 }
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if cur then
|
|
||||||
if nex then
|
|
||||||
view.set_cursor { nex, 0 }
|
|
||||||
end
|
|
||||||
elseif first then
|
|
||||||
view.set_cursor { first, 0 }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
|
||||||
41
lua/nvim-tree/actions/moves/parent.lua
Normal file
41
lua/nvim-tree/actions/moves/parent.lua
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
local renderer = require "nvim-tree.renderer"
|
||||||
|
local view = require "nvim-tree.view"
|
||||||
|
local utils = require "nvim-tree.utils"
|
||||||
|
local core = require "nvim-tree.core"
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.fn(should_close)
|
||||||
|
should_close = should_close or false
|
||||||
|
|
||||||
|
return function(node)
|
||||||
|
if should_close and node.open then
|
||||||
|
node.open = false
|
||||||
|
return renderer.draw()
|
||||||
|
end
|
||||||
|
|
||||||
|
local parent = node.parent
|
||||||
|
|
||||||
|
if renderer.config.group_empty and parent then
|
||||||
|
while parent.parent and parent.parent.group_next do
|
||||||
|
parent = parent.parent
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not parent or not parent.parent then
|
||||||
|
return view.set_cursor { 1, 0 }
|
||||||
|
end
|
||||||
|
|
||||||
|
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
|
||||||
|
return n.absolute_path == parent.absolute_path
|
||||||
|
end)
|
||||||
|
|
||||||
|
view.set_cursor { line + 1, 0 }
|
||||||
|
if should_close then
|
||||||
|
parent.open = false
|
||||||
|
renderer.draw()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
53
lua/nvim-tree/actions/moves/sibling.lua
Normal file
53
lua/nvim-tree/actions/moves/sibling.lua
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
local utils = require "nvim-tree.utils"
|
||||||
|
local view = require "nvim-tree.view"
|
||||||
|
local core = require "nvim-tree.core"
|
||||||
|
local lib = require "nvim-tree.lib"
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local function get_index_of(node, nodes)
|
||||||
|
local node_path = node.absolute_path
|
||||||
|
local line = 1
|
||||||
|
|
||||||
|
for _, _node in ipairs(nodes) do
|
||||||
|
if not _node.hidden then
|
||||||
|
local n = lib.get_last_group_node(_node)
|
||||||
|
if node_path == n.absolute_path then
|
||||||
|
return line
|
||||||
|
end
|
||||||
|
|
||||||
|
line = line + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.fn(direction)
|
||||||
|
return function(node)
|
||||||
|
if node.name == ".." or not direction then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local parent = node.parent or core.get_explorer()
|
||||||
|
local parent_nodes = vim.tbl_filter(function(n)
|
||||||
|
return not n.hidden
|
||||||
|
end, parent.nodes)
|
||||||
|
|
||||||
|
local node_index = get_index_of(node, parent_nodes)
|
||||||
|
|
||||||
|
local target_idx = node_index + direction
|
||||||
|
if target_idx < 1 then
|
||||||
|
target_idx = 1
|
||||||
|
elseif target_idx > #parent_nodes then
|
||||||
|
target_idx = #parent_nodes
|
||||||
|
end
|
||||||
|
|
||||||
|
local target_node = parent_nodes[target_idx]
|
||||||
|
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
|
||||||
|
return n.absolute_path == target_node.absolute_path
|
||||||
|
end)
|
||||||
|
|
||||||
|
view.set_cursor { line + 1, 0 }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
Reference in New Issue
Block a user