refactor(#2942): multi instance: move find_node to Explorer
This commit is contained in:
parent
570b905c78
commit
3a3565ed84
@ -1,6 +1,4 @@
|
|||||||
local view = require("nvim-tree.view")
|
local view = require("nvim-tree.view")
|
||||||
local utils = require("nvim-tree.utils")
|
|
||||||
|
|
||||||
local DirectoryNode = require("nvim-tree.node.directory")
|
local DirectoryNode = require("nvim-tree.node.directory")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
@ -29,7 +27,7 @@ function M.fn(should_close)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local _, line = utils.find_node(parent.explorer.nodes, function(n)
|
local _, line = parent.explorer:find_node(function(n)
|
||||||
return n.absolute_path == parent.absolute_path
|
return n.absolute_path == parent.absolute_path
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
@ -601,7 +601,7 @@ end
|
|||||||
|
|
||||||
---@param path string
|
---@param path string
|
||||||
function Explorer:focus_file(path)
|
function Explorer:focus_file(path)
|
||||||
local _, i = utils.find_node(self.nodes, function(node)
|
local _, i = self:find_node(function(node)
|
||||||
return node.absolute_path == path
|
return node.absolute_path == path
|
||||||
end)
|
end)
|
||||||
view.set_cursor({ i + 1, 1 })
|
view.set_cursor({ i + 1, 1 })
|
||||||
@ -614,7 +614,7 @@ end
|
|||||||
function Explorer:focus_node_or_parent(node)
|
function Explorer:focus_node_or_parent(node)
|
||||||
|
|
||||||
while node do
|
while node do
|
||||||
local found_node, i = utils.find_node(self.nodes, function(node_)
|
local found_node, i = self:find_node(function(node_)
|
||||||
return node_.absolute_path == node.absolute_path
|
return node_.absolute_path == node.absolute_path
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -627,6 +627,25 @@ function Explorer:focus_node_or_parent(node)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Get the node and index of the node from the tree that matches the predicate.
|
||||||
|
--- The explored nodes are those displayed on the view.
|
||||||
|
---@param fn fun(node: Node): boolean
|
||||||
|
---@return table|nil
|
||||||
|
---@return number
|
||||||
|
function Explorer:find_node(fn)
|
||||||
|
local node, i = Iterator.builder(self.nodes)
|
||||||
|
:matcher(fn)
|
||||||
|
:recursor(function(node)
|
||||||
|
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
|
||||||
|
end)
|
||||||
|
:iterate()
|
||||||
|
i = view.is_root_folder_visible() and i or i - 1
|
||||||
|
if node and node.explorer.live_filter.filter then
|
||||||
|
i = i + 1
|
||||||
|
end
|
||||||
|
return node, i
|
||||||
|
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()
|
||||||
|
|||||||
@ -114,26 +114,6 @@ end
|
|||||||
|
|
||||||
M.path_separator = path_separator
|
M.path_separator = path_separator
|
||||||
|
|
||||||
--- Get the node and index of the node from the tree that matches the predicate.
|
|
||||||
--- The explored nodes are those displayed on the view.
|
|
||||||
---@param nodes Node[]
|
|
||||||
---@param fn fun(node: Node): boolean
|
|
||||||
---@return table|nil
|
|
||||||
---@return number
|
|
||||||
function M.find_node(nodes, fn)
|
|
||||||
local node, i = Iterator.builder(nodes)
|
|
||||||
:matcher(fn)
|
|
||||||
:recursor(function(node)
|
|
||||||
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
|
|
||||||
end)
|
|
||||||
:iterate()
|
|
||||||
i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1
|
|
||||||
if node and node.explorer.live_filter.filter then
|
|
||||||
i = i + 1
|
|
||||||
end
|
|
||||||
return node, i
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param extmarks vim.api.keyset.get_extmark_item[] as per vim.api.nvim_buf_get_extmarks
|
---@param extmarks vim.api.keyset.get_extmark_item[] as per vim.api.nvim_buf_get_extmarks
|
||||||
---@return number
|
---@return number
|
||||||
function M.extmarks_length(extmarks)
|
function M.extmarks_length(extmarks)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user