From 762968a3cb91308af4f5e3ee40478ab19faa825b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 25 Aug 2024 12:11:17 +1000 Subject: [PATCH 1/2] fix(#2878): nowrapscan prevents move from root --- lua/nvim-tree/actions/moves/item.lua | 5 +++++ lua/nvim-tree/lib.lua | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 3803a06fe44..72c1d3c4397 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -38,6 +38,11 @@ local function move(where, what, skip_gitignored) 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 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. From 14458e45c4f48f9c2efac1b11bf569678cc8f5fd Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 25 Aug 2024 12:20:37 +1000 Subject: [PATCH 2/2] fix(#2878): nowrapscan prevents move from root --- lua/nvim-tree/actions/moves/item.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 72c1d3c4397..9714b3b5001 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -33,7 +33,6 @@ 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 @@ -57,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