|
1 | 1 | local utils = require "nvim-tree.utils"
|
2 |
| -local view = require "nvim-tree.view" |
3 | 2 | local core = require "nvim-tree.core"
|
4 |
| -local lib = require "nvim-tree.lib" |
| 3 | +local Iterator = require "nvim-tree.iterators.node-iterator" |
5 | 4 |
|
6 | 5 | local M = {}
|
7 | 6 |
|
8 |
| -local function get_index_of(node, nodes) |
9 |
| - local node_path = node.absolute_path |
10 |
| - local line = 1 |
11 |
| - |
12 |
| - for _, _node in ipairs(nodes) do |
13 |
| - if not _node.hidden then |
14 |
| - local n = lib.get_last_group_node(_node) |
15 |
| - if node_path == n.absolute_path then |
16 |
| - return line |
17 |
| - end |
18 |
| - |
19 |
| - line = line + 1 |
20 |
| - end |
21 |
| - end |
22 |
| -end |
23 |
| - |
24 | 7 | function M.fn(direction)
|
25 | 8 | return function(node)
|
26 | 9 | if node.name == ".." or not direction then
|
27 | 10 | return
|
28 | 11 | end
|
29 | 12 |
|
| 13 | + local first, last, next, prev = nil, nil, nil, nil |
| 14 | + local found = false |
30 | 15 | local parent = node.parent or core.get_explorer()
|
31 |
| - local parent_nodes = vim.tbl_filter(function(n) |
32 |
| - return not n.hidden |
33 |
| - end, parent.nodes) |
34 |
| - |
35 |
| - local node_index = get_index_of(node, parent_nodes) |
36 |
| - |
37 |
| - local target_idx = node_index + direction |
38 |
| - if target_idx < 1 then |
39 |
| - target_idx = 1 |
40 |
| - elseif target_idx > #parent_nodes then |
41 |
| - target_idx = #parent_nodes |
| 16 | + Iterator.builder(parent.nodes) |
| 17 | + :recursor(function() |
| 18 | + return nil |
| 19 | + end) |
| 20 | + :applier(function(n) |
| 21 | + first = first or n |
| 22 | + last = n |
| 23 | + if n.absolute_path == node.absolute_path then |
| 24 | + found = true |
| 25 | + return |
| 26 | + end |
| 27 | + prev = not found and n or prev |
| 28 | + if found and not next then |
| 29 | + next = n |
| 30 | + end |
| 31 | + end) |
| 32 | + :iterate() |
| 33 | + |
| 34 | + local target_node |
| 35 | + if direction == "first" then |
| 36 | + target_node = first |
| 37 | + elseif direction == "last" then |
| 38 | + target_node = last |
| 39 | + elseif direction == "next" then |
| 40 | + target_node = next or first |
| 41 | + else |
| 42 | + target_node = prev or last |
42 | 43 | end
|
43 | 44 |
|
44 |
| - local target_node = parent_nodes[target_idx] |
45 |
| - local _, line = utils.find_node(core.get_explorer().nodes, function(n) |
46 |
| - return n.absolute_path == target_node.absolute_path |
47 |
| - end) |
48 |
| - |
49 |
| - view.set_cursor { line + 1, 0 } |
| 45 | + if target_node then |
| 46 | + utils.focus_file(target_node.absolute_path) |
| 47 | + end |
50 | 48 | end
|
51 | 49 | end
|
52 | 50 |
|
|
0 commit comments