feat(movement): allow circular movement for sibling next and prev (#1416)
This commit is contained in:
@@ -28,15 +28,15 @@ local Actions = {
|
|||||||
|
|
||||||
-- Movements in tree
|
-- Movements in tree
|
||||||
close_node = require("nvim-tree.actions.moves.parent").fn(true),
|
close_node = require("nvim-tree.actions.moves.parent").fn(true),
|
||||||
first_sibling = require("nvim-tree.actions.moves.sibling").fn(-math.huge),
|
first_sibling = require("nvim-tree.actions.moves.sibling").fn "first",
|
||||||
last_sibling = require("nvim-tree.actions.moves.sibling").fn(math.huge),
|
last_sibling = require("nvim-tree.actions.moves.sibling").fn "last",
|
||||||
next_diag_item = require("nvim-tree.actions.moves.item").fn("next", "diag"),
|
next_diag_item = require("nvim-tree.actions.moves.item").fn("next", "diag"),
|
||||||
next_git_item = require("nvim-tree.actions.moves.item").fn("next", "git"),
|
next_git_item = require("nvim-tree.actions.moves.item").fn("next", "git"),
|
||||||
next_sibling = require("nvim-tree.actions.moves.sibling").fn(1),
|
next_sibling = require("nvim-tree.actions.moves.sibling").fn "next",
|
||||||
parent_node = require("nvim-tree.actions.moves.parent").fn(false),
|
parent_node = require("nvim-tree.actions.moves.parent").fn(false),
|
||||||
prev_diag_item = require("nvim-tree.actions.moves.item").fn("prev", "diag"),
|
prev_diag_item = require("nvim-tree.actions.moves.item").fn("prev", "diag"),
|
||||||
prev_git_item = require("nvim-tree.actions.moves.item").fn("prev", "git"),
|
prev_git_item = require("nvim-tree.actions.moves.item").fn("prev", "git"),
|
||||||
prev_sibling = require("nvim-tree.actions.moves.sibling").fn(-1),
|
prev_sibling = require("nvim-tree.actions.moves.sibling").fn "prev",
|
||||||
|
|
||||||
-- Other types
|
-- Other types
|
||||||
refresh = require("nvim-tree.actions.reloaders.reloaders").reload_explorer,
|
refresh = require("nvim-tree.actions.reloaders.reloaders").reload_explorer,
|
||||||
|
|||||||
@@ -1,52 +1,50 @@
|
|||||||
local utils = require "nvim-tree.utils"
|
local utils = require "nvim-tree.utils"
|
||||||
local view = require "nvim-tree.view"
|
|
||||||
local core = require "nvim-tree.core"
|
local core = require "nvim-tree.core"
|
||||||
local lib = require "nvim-tree.lib"
|
local Iterator = require "nvim-tree.iterators.node-iterator"
|
||||||
|
|
||||||
local M = {}
|
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)
|
function M.fn(direction)
|
||||||
return function(node)
|
return function(node)
|
||||||
if node.name == ".." or not direction then
|
if node.name == ".." or not direction then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local first, last, next, prev = nil, nil, nil, nil
|
||||||
|
local found = false
|
||||||
local parent = node.parent or core.get_explorer()
|
local parent = node.parent or core.get_explorer()
|
||||||
local parent_nodes = vim.tbl_filter(function(n)
|
Iterator.builder(parent.nodes)
|
||||||
return not n.hidden
|
:recursor(function()
|
||||||
end, parent.nodes)
|
return nil
|
||||||
|
end)
|
||||||
|
:applier(function(n)
|
||||||
|
first = first or n
|
||||||
|
last = n
|
||||||
|
if n.absolute_path == node.absolute_path then
|
||||||
|
found = true
|
||||||
|
return
|
||||||
|
end
|
||||||
|
prev = not found and n or prev
|
||||||
|
if found and not next then
|
||||||
|
next = n
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
:iterate()
|
||||||
|
|
||||||
local node_index = get_index_of(node, parent_nodes)
|
local target_node
|
||||||
|
if direction == "first" then
|
||||||
local target_idx = node_index + direction
|
target_node = first
|
||||||
if target_idx < 1 then
|
elseif direction == "last" then
|
||||||
target_idx = 1
|
target_node = last
|
||||||
elseif target_idx > #parent_nodes then
|
elseif direction == "next" then
|
||||||
target_idx = #parent_nodes
|
target_node = next or first
|
||||||
|
else
|
||||||
|
target_node = prev or last
|
||||||
end
|
end
|
||||||
|
|
||||||
local target_node = parent_nodes[target_idx]
|
if target_node then
|
||||||
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
|
utils.focus_file(target_node.absolute_path)
|
||||||
return n.absolute_path == target_node.absolute_path
|
end
|
||||||
end)
|
|
||||||
|
|
||||||
view.set_cursor { line + 1, 0 }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user