Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions lua/neo-tree/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -688,16 +688,17 @@ end
---Saves a window position to be restored later
---@param state neotree.State
---@param force boolean?
---@return boolean saved
M.position.save = function(state, force)
if not force and state.position.topline and state.position.lnum then
log.debug("There's already a position saved to be restored. Cannot save another.")
return
return false
end
if not state.tree then
return
return false
end
if not M.window_exists(state) then
return
return false
end

local win_state = vim.api.nvim_win_call(state.winid, vim.fn.winsaveview)
Expand All @@ -713,6 +714,7 @@ M.position.save = function(state, force)
local b = vim.fn.getpos("v")
state.position.visual_selection = { a, b }
end
return true
end

---Queues a node to focus
Expand Down Expand Up @@ -1161,22 +1163,26 @@ M.acquire_window = function(state)
vim.api.nvim_buf_set_name(state.bufnr, bufname)
vim.api.nvim_set_current_win(state.winid)
-- Used to track the position of the cursor within the tree as it gains and loses focus
local restored_after_window_change = false
win:on({ "CursorMoved", "ModeChanged" }, function(args)
if win.winid == vim.api.nvim_get_current_win() and restored_after_window_change then
M.position.save(state, true)
local wait_for_save = true
win:on({ "CursorMoved", "ModeChanged" }, function()
if state.winid == vim.api.nvim_get_current_win() then
if not wait_for_save then
M.position.save(state, true)
elseif M.position.save(state) then
wait_for_save = false
end
end
end)
win:on({ "BufDelete" }, function()
M.position.save(state)
end)
win:on({ "WinEnter" }, function(args)
win:on({ "WinEnter" }, function()
M.position.restore_selection(state)
if win.winid == vim.api.nvim_get_current_win() then
if state.winid == vim.api.nvim_get_current_win() then
M.position.restore(state)
restored_after_window_change = true
wait_for_save = false
else
restored_after_window_change = false
wait_for_save = true
end
end)
win:on({ "BufDelete" }, function()
Expand Down
9 changes: 9 additions & 0 deletions tests/neo-tree/command/command_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ describe("Command", function()
-- It seems like in headless mode, CursorMoved is not emitted.
vim.api.nvim_exec_autocmds("CursorMoved", {})
verify.filesystem_tree_node_is(testfile)

-- toggle on and off
require("neo-tree.command").execute({
toggle = true,
})
require("neo-tree.command").execute({
toggle = true,
})
verify.filesystem_tree_node_is(testfile)
end)
end)

Expand Down
Loading