Skip to content

Commit 5830585

Browse files
committed
move place_cursor_on_node to Explorer
1 parent 8994c1e commit 5830585

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

lua/nvim-tree.lua

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,6 @@ function M.open_on_directory()
114114
actions.root.change_dir.force_dirchange(bufname, true)
115115
end
116116

117-
function M.place_cursor_on_node()
118-
local ok, search = pcall(vim.fn.searchcount)
119-
if ok and search and search.exact_match == 1 then
120-
return
121-
end
122-
123-
local explorer = core.get_explorer()
124-
if not explorer then
125-
return
126-
end
127-
128-
local node = explorer:get_node_at_cursor()
129-
if not node or node.name == ".." then
130-
return
131-
end
132-
node = node:get_parent_of_group() or node
133-
134-
local line = vim.api.nvim_get_current_line()
135-
local cursor = vim.api.nvim_win_get_cursor(0)
136-
local idx = vim.fn.stridx(line, node.name)
137-
138-
if idx >= 0 then
139-
vim.api.nvim_win_set_cursor(0, { cursor[1], idx })
140-
end
141-
end
142-
143117
---@return table
144118
function M.get_config()
145119
return M.config
@@ -270,7 +244,10 @@ local function setup_autocommands(opts)
270244
pattern = "NvimTree_*",
271245
callback = function()
272246
if utils.is_nvim_tree_buf(0) then
273-
M.place_cursor_on_node()
247+
local explorer = core.get_explorer()
248+
if explorer then
249+
explorer:place_cursor_on_node()
250+
end
274251
end
275252
end,
276253
})

lua/nvim-tree/explorer/init.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,27 @@ function Explorer:get_node_at_cursor()
401401
return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]]
402402
end
403403

404+
function Explorer:place_cursor_on_node()
405+
local ok, search = pcall(vim.fn.searchcount)
406+
if ok and search and search.exact_match == 1 then
407+
return
408+
end
409+
410+
local node = self:get_node_at_cursor()
411+
if not node or node.name == ".." then
412+
return
413+
end
414+
node = node:get_parent_of_group() or node
415+
416+
local line = vim.api.nvim_get_current_line()
417+
local cursor = vim.api.nvim_win_get_cursor(0)
418+
local idx = vim.fn.stridx(line, node.name)
419+
420+
if idx >= 0 then
421+
vim.api.nvim_win_set_cursor(0, { cursor[1], idx })
422+
end
423+
end
424+
404425
---Api.tree.get_nodes
405426
---@return Node
406427
function Explorer:get_nodes()

0 commit comments

Comments
 (0)