diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 3803a06fe44..9714b3b5001 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -33,11 +33,15 @@ end ---@param what string type of status ---@param skip_gitignored boolean default false local function move(where, what, skip_gitignored) - local node_cur = lib.get_node_at_cursor() local first_node_line = core.get_nodes_starting_line() local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, first_node_line) local iter_start, iter_end, iter_step, cur, first, nex + local cursor = lib.get_cursor_position() + if cursor and cursor[1] < first_node_line then + cur = cursor[1] + end + if where == "next" then iter_start, iter_end, iter_step = first_node_line, #nodes_by_line, 1 elseif where == "prev" then @@ -52,7 +56,7 @@ local function move(where, what, skip_gitignored) first = line end - if node == node_cur then + if cursor and line == cursor[1] then cur = line elseif valid and cur then nex = line diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 8bdf9a97bbc..6efb42e4156 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -15,25 +15,33 @@ local M = { target_winid = nil, } ----@return Node|nil -function M.get_node_at_cursor() +---Cursor position as per vim.api.nvim_win_get_cursor +---@return integer[]|nil +function M.get_cursor_position() if not core.get_explorer() then return end local winnr = view.get_winnr() - if not winnr then + if not winnr or not vim.api.nvim_win_is_valid(winnr) then return end - local cursor = vim.api.nvim_win_get_cursor(winnr) - local line = cursor[1] + return vim.api.nvim_win_get_cursor(winnr) +end + +---@return Node|nil +function M.get_node_at_cursor() + local cursor = M.get_cursor_position() + if not cursor then + return + end - if line == 1 and view.is_root_folder_visible(core.get_cwd()) then + if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then return { name = ".." } end - return utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line] + return utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[cursor[1]] end ---Create a sanitized partial copy of a node, populating children recursively.