Skip to content

Commit 660c42a

Browse files
committed
refactor(#2942): multi instance: move get_nodes_by_line to Explorer
1 parent 3a3565e commit 660c42a

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

lua/nvim-tree/actions/moves/item.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local utils = require("nvim-tree.utils")
21
local view = require("nvim-tree.view")
32
local core = require("nvim-tree.core")
43
local diagnostics = require("nvim-tree.diagnostics")
@@ -36,7 +35,7 @@ end
3635
---@param skip_gitignored boolean? default false
3736
local function move(explorer, where, what, skip_gitignored)
3837
local first_node_line = core.get_nodes_starting_line()
39-
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line)
38+
local nodes_by_line = explorer:get_nodes_by_line(first_node_line)
4039
local iter_start, iter_end, iter_step, cur, first, nex
4140

4241
local cursor = explorer:get_cursor_position()

lua/nvim-tree/explorer/init.lua

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ function Explorer:get_node_at_cursor()
527527
return self
528528
end
529529

530-
return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]]
530+
return self:get_nodes_by_line(core.get_nodes_starting_line())[cursor[1]]
531531
end
532532

533533
function Explorer:place_cursor_on_node()
@@ -560,7 +560,7 @@ function Explorer:find_node_line(node)
560560
end
561561

562562
local first_node_line = core.get_nodes_starting_line()
563-
local nodes_by_line = utils.get_nodes_by_line(self.nodes, first_node_line)
563+
local nodes_by_line = self:get_nodes_by_line(first_node_line)
564564
local iter_start, iter_end = first_node_line, #nodes_by_line
565565

566566
for line = iter_start, iter_end, 1 do
@@ -646,6 +646,29 @@ function Explorer:find_node(fn)
646646
return node, i
647647
end
648648

649+
--- Return visible nodes indexed by line
650+
---@param line_start number
651+
---@return table
652+
function Explorer:get_nodes_by_line(line_start)
653+
local nodes_by_line = {}
654+
local line = line_start
655+
656+
Iterator.builder(self.nodes)
657+
:applier(function(node)
658+
if node.group_next then
659+
return
660+
end
661+
nodes_by_line[line] = node
662+
line = line + 1
663+
end)
664+
:recursor(function(node)
665+
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
666+
end)
667+
:iterate()
668+
669+
return nodes_by_line
670+
end
671+
649672
---Api.tree.get_nodes
650673
---@return nvim_tree.api.Node
651674
function Explorer:get_nodes()

lua/nvim-tree/utils.lua

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local Iterator = require("nvim-tree.iterators.node-iterator")
2-
31
local M = {
42
debouncers = {},
53
}
@@ -149,30 +147,6 @@ M.default_format_hidden_count = function(hidden_count, simple)
149147
return nil
150148
end
151149

152-
--- Return visible nodes indexed by line
153-
---@param nodes_all Node[]
154-
---@param line_start number
155-
---@return table
156-
function M.get_nodes_by_line(nodes_all, line_start)
157-
local nodes_by_line = {}
158-
local line = line_start
159-
160-
Iterator.builder(nodes_all)
161-
:applier(function(node)
162-
if node.group_next then
163-
return
164-
end
165-
nodes_by_line[line] = node
166-
line = line + 1
167-
end)
168-
:recursor(function(node)
169-
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
170-
end)
171-
:iterate()
172-
173-
return nodes_by_line
174-
end
175-
176150
function M.rename_loaded_buffers(old_path, new_path)
177151
-- delete new if it exists
178152
for _, buf in pairs(vim.api.nvim_list_bufs()) do

0 commit comments

Comments
 (0)