Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lua/nvim-tree/actions/dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 33 additions & 35 deletions lua/nvim-tree/actions/moves/sibling.lua
Original file line number Diff line number Diff line change
@@ -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)

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
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 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

Expand Down