From 5830585318bc16e104dbf050354dcd225fa58821 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Oct 2024 11:56:10 +1100 Subject: [PATCH] move place_cursor_on_node to Explorer --- lua/nvim-tree.lua | 31 ++++--------------------------- lua/nvim-tree/explorer/init.lua | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a45c5c6c..d5cbba60 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -114,32 +114,6 @@ function M.open_on_directory() actions.root.change_dir.force_dirchange(bufname, true) end -function M.place_cursor_on_node() - local ok, search = pcall(vim.fn.searchcount) - if ok and search and search.exact_match == 1 then - return - end - - local explorer = core.get_explorer() - if not explorer then - return - end - - local node = explorer:get_node_at_cursor() - if not node or node.name == ".." then - return - end - node = node:get_parent_of_group() or node - - local line = vim.api.nvim_get_current_line() - local cursor = vim.api.nvim_win_get_cursor(0) - local idx = vim.fn.stridx(line, node.name) - - if idx >= 0 then - vim.api.nvim_win_set_cursor(0, { cursor[1], idx }) - end -end - ---@return table function M.get_config() return M.config @@ -270,7 +244,10 @@ local function setup_autocommands(opts) pattern = "NvimTree_*", callback = function() if utils.is_nvim_tree_buf(0) then - M.place_cursor_on_node() + local explorer = core.get_explorer() + if explorer then + explorer:place_cursor_on_node() + end end end, }) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 7535605d..54fbb512 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -401,6 +401,27 @@ function Explorer:get_node_at_cursor() return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]] end +function Explorer:place_cursor_on_node() + local ok, search = pcall(vim.fn.searchcount) + if ok and search and search.exact_match == 1 then + return + end + + local node = self:get_node_at_cursor() + if not node or node.name == ".." then + return + end + node = node:get_parent_of_group() or node + + local line = vim.api.nvim_get_current_line() + local cursor = vim.api.nvim_win_get_cursor(0) + local idx = vim.fn.stridx(line, node.name) + + if idx >= 0 then + vim.api.nvim_win_set_cursor(0, { cursor[1], idx }) + end +end + ---Api.tree.get_nodes ---@return Node function Explorer:get_nodes()