feat(movement): allow circular movement for sibling next and prev (#1416)

This commit is contained in:
Kiyan 2022-07-16 10:39:24 +02:00 committed by GitHub
parent 449b5bd0cb
commit b32c88333f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 38 deletions

View File

@ -28,15 +28,15 @@ local Actions = {
-- Movements in tree
close_node = require("nvim-tree.actions.moves.parent").fn(true),
first_sibling = require("nvim-tree.actions.moves.sibling").fn(-math.huge),
last_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 "last",
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_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),
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_sibling = require("nvim-tree.actions.moves.sibling").fn(-1),
prev_sibling = require("nvim-tree.actions.moves.sibling").fn "prev",
-- Other types
refresh = require("nvim-tree.actions.reloaders.reloaders").reload_explorer,

View File

@ -1,52 +1,50 @@
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 Iterator = require "nvim-tree.iterators.node-iterator"
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 first, last, next, prev = nil, nil, nil, nil
local found = false
local parent = node.parent or core.get_explorer()
local parent_nodes = vim.tbl_filter(function(n)
return not n.hidden
end, parent.nodes)
Iterator.builder(parent.nodes)
:recursor(function()
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_idx = node_index + direction
if target_idx < 1 then
target_idx = 1
elseif target_idx > #parent_nodes then
target_idx = #parent_nodes
local target_node
if direction == "first" then
target_node = first
elseif direction == "last" then
target_node = last
elseif direction == "next" then
target_node = next or first
else
target_node = prev or last
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 }
if target_node then
utils.focus_file(target_node.absolute_path)
end
end
end