From a20c81733aab67b4b72bc8fad9d6f53798b35c42 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 8 Sep 2025 15:21:59 +1000 Subject: [PATCH] refactor(#2942): multi instance: move focus_file to Explorer --- lua/nvim-tree/actions/moves/sibling.lua | 10 +++++++--- lua/nvim-tree/explorer/init.lua | 8 ++++++++ lua/nvim-tree/explorer/live-filter.lua | 4 ++-- lua/nvim-tree/marks/init.lua | 2 +- lua/nvim-tree/utils.lua | 7 ------- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lua/nvim-tree/actions/moves/sibling.lua b/lua/nvim-tree/actions/moves/sibling.lua index cf5b492d..24347db4 100644 --- a/lua/nvim-tree/actions/moves/sibling.lua +++ b/lua/nvim-tree/actions/moves/sibling.lua @@ -1,4 +1,3 @@ -local utils = require("nvim-tree.utils") local core = require("nvim-tree.core") local Iterator = require("nvim-tree.iterators.node-iterator") @@ -12,9 +11,14 @@ function M.fn(direction) return end + local explorer = core.get_explorer() + if not explorer then + return + end + local first, last, next, prev = nil, nil, nil, nil local found = false - local parent = node.parent or core.get_explorer() + local parent = node.parent or explorer Iterator.builder(parent and parent.nodes or {}) :recursor(function() return nil @@ -45,7 +49,7 @@ function M.fn(direction) end if target_node then - utils.focus_file(target_node.absolute_path) + explorer:focus_file(target_node.absolute_path) end end end diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index a3cadd9c..5a23f824 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -599,6 +599,14 @@ function Explorer:get_node_from_path(path) :iterate() end +---@param path string +function Explorer:focus_file(path) + local _, i = utils.find_node(self.nodes, function(node) + return node.absolute_path == path + end) + view.set_cursor({ i + 1, 1 }) +end + ---Api.tree.get_nodes ---@return nvim_tree.api.Node function Explorer:get_nodes() diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 62a7dd9e..df557737 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -220,9 +220,9 @@ function LiveFilter:clear_filter() self.explorer.renderer:draw() if node then - utils.focus_file(node.absolute_path) + self.explorer:focus_file(node.absolute_path) elseif last_node then - utils.focus_file(last_node.absolute_path) + self.explorer:focus_file(last_node.absolute_path) end end diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index c940f999..54f751c4 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -263,7 +263,7 @@ function Marks:navigate_select() if node and not node:is(DirectoryNode) and not utils.get_win_buf_from_path(node.absolute_path) then open_file.fn("edit", node.absolute_path) elseif node then - utils.focus_file(node.absolute_path) + self.explorer:focus_file(node.absolute_path) end end) end diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index b0e54695..23bd2392 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -447,13 +447,6 @@ function M.debounce(context, timeout, callback) end) end -function M.focus_file(path) - local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node) - return node.absolute_path == path - end) - require("nvim-tree.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.