diff --git a/lua/nvim-tree/actions/tree/modifiers/collapse.lua b/lua/nvim-tree/actions/tree/modifiers/collapse.lua index 62da5f9a..51a15f3d 100644 --- a/lua/nvim-tree/actions/tree/modifiers/collapse.lua +++ b/lua/nvim-tree/actions/tree/modifiers/collapse.lua @@ -56,7 +56,7 @@ local function collapse(node, opts) :iterate() explorer.renderer:draw() - utils.focus_node_or_parent(node_at_cursor) + explorer:focus_node_or_parent(node_at_cursor) end diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 62230687..fda0a79f 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -280,7 +280,7 @@ function Filters:toggle(type) local node = self.explorer:get_node_at_cursor() self.explorer:reload_explorer() if node then - utils.focus_node_or_parent(node) + self.explorer:focus_node_or_parent(node) end end diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 5a23f824..4565b9c1 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -607,6 +607,26 @@ function Explorer:focus_file(path) view.set_cursor({ i + 1, 1 }) end +---Focus node passed as parameter if visible, otherwise focus first visible parent. +---If none of the parents is visible focus root. +---If node is nil do nothing. +---@param node Node? node to focus +function Explorer:focus_node_or_parent(node) + + while node do + local found_node, i = utils.find_node(self.nodes, function(node_) + return node_.absolute_path == node.absolute_path + end) + + if found_node or node.parent == nil then + require("nvim-tree.view").set_cursor({ i + 1, 1 }) + break + end + + node = node.parent + end +end + ---Api.tree.get_nodes ---@return nvim_tree.api.Node function Explorer:get_nodes() diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index 54f751c4..5fb3e920 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -227,9 +227,9 @@ function Marks:navigate(up) end if up then - utils.focus_node_or_parent(prev or last) + self.explorer:focus_node_or_parent(prev or last) else - utils.focus_node_or_parent(next or first) + self.explorer:focus_node_or_parent(next or first) end end diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 23bd2392..d4d15488 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -447,31 +447,6 @@ function M.debounce(context, timeout, callback) end) end ----Focus node passed as parameter if visible, otherwise focus first visible parent. ----If none of the parents is visible focus root. ----If node is nil do nothing. ----@param node Node? node to focus -function M.focus_node_or_parent(node) - local explorer = require("nvim-tree.core").get_explorer() - - if explorer == nil then - return - end - - while node do - local found_node, i = M.find_node(explorer.nodes, function(node_) - return node_.absolute_path == node.absolute_path - end) - - if found_node or node.parent == nil then - require("nvim-tree.view").set_cursor({ i + 1, 1 }) - break - end - - node = node.parent - end -end - ---@param path string ---@return integer|nil ---@return integer|nil