refactor(#2942): multi instance: move get_nodes_by_line to Explorer

This commit is contained in:
Alexander Courtis 2025-09-08 16:07:26 +10:00
parent 3a3565ed84
commit 660c42a0ac
3 changed files with 26 additions and 30 deletions

View File

@ -1,4 +1,3 @@
local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view") local view = require("nvim-tree.view")
local core = require("nvim-tree.core") local core = require("nvim-tree.core")
local diagnostics = require("nvim-tree.diagnostics") local diagnostics = require("nvim-tree.diagnostics")
@ -36,7 +35,7 @@ end
---@param skip_gitignored boolean? default false ---@param skip_gitignored boolean? default false
local function move(explorer, where, what, skip_gitignored) local function move(explorer, where, what, skip_gitignored)
local first_node_line = core.get_nodes_starting_line() local first_node_line = core.get_nodes_starting_line()
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line) local nodes_by_line = explorer:get_nodes_by_line(first_node_line)
local iter_start, iter_end, iter_step, cur, first, nex local iter_start, iter_end, iter_step, cur, first, nex
local cursor = explorer:get_cursor_position() local cursor = explorer:get_cursor_position()

View File

@ -527,7 +527,7 @@ function Explorer:get_node_at_cursor()
return self return self
end end
return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]] return self:get_nodes_by_line(core.get_nodes_starting_line())[cursor[1]]
end end
function Explorer:place_cursor_on_node() function Explorer:place_cursor_on_node()
@ -560,7 +560,7 @@ function Explorer:find_node_line(node)
end end
local first_node_line = core.get_nodes_starting_line() local first_node_line = core.get_nodes_starting_line()
local nodes_by_line = utils.get_nodes_by_line(self.nodes, first_node_line) local nodes_by_line = self:get_nodes_by_line(first_node_line)
local iter_start, iter_end = first_node_line, #nodes_by_line local iter_start, iter_end = first_node_line, #nodes_by_line
for line = iter_start, iter_end, 1 do for line = iter_start, iter_end, 1 do
@ -646,6 +646,29 @@ function Explorer:find_node(fn)
return node, i return node, i
end end
--- Return visible nodes indexed by line
---@param line_start number
---@return table
function Explorer:get_nodes_by_line(line_start)
local nodes_by_line = {}
local line = line_start
Iterator.builder(self.nodes)
:applier(function(node)
if node.group_next then
return
end
nodes_by_line[line] = node
line = line + 1
end)
:recursor(function(node)
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
end)
:iterate()
return nodes_by_line
end
---Api.tree.get_nodes ---Api.tree.get_nodes
---@return nvim_tree.api.Node ---@return nvim_tree.api.Node
function Explorer:get_nodes() function Explorer:get_nodes()

View File

@ -1,5 +1,3 @@
local Iterator = require("nvim-tree.iterators.node-iterator")
local M = { local M = {
debouncers = {}, debouncers = {},
} }
@ -149,30 +147,6 @@ M.default_format_hidden_count = function(hidden_count, simple)
return nil return nil
end end
--- Return visible nodes indexed by line
---@param nodes_all Node[]
---@param line_start number
---@return table
function M.get_nodes_by_line(nodes_all, line_start)
local nodes_by_line = {}
local line = line_start
Iterator.builder(nodes_all)
:applier(function(node)
if node.group_next then
return
end
nodes_by_line[line] = node
line = line + 1
end)
:recursor(function(node)
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
end)
:iterate()
return nodes_by_line
end
function M.rename_loaded_buffers(old_path, new_path) function M.rename_loaded_buffers(old_path, new_path)
-- delete new if it exists -- delete new if it exists
for _, buf in pairs(vim.api.nvim_list_bufs()) do for _, buf in pairs(vim.api.nvim_list_bufs()) do