Skip to content

Commit 532db9d

Browse files
committed
fix: restore cursor when updating from outside of tree
1 parent 6d23978 commit 532db9d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lua/gitlab/actions/discussions/init.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ M.rebuild_discussion_tree = function()
433433
return
434434
end
435435

436-
local current_node = discussions_tree.get_node_at_cursor(M.split.winid, M.linked_bufnr, M.discussion_tree)
436+
local current_node = discussions_tree.get_node_at_cursor(M.discussion_tree, M.last_node_at_cursor)
437437

438438
local expanded_node_ids = M.gather_expanded_node_ids(M.discussion_tree)
439439
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
@@ -472,8 +472,7 @@ M.rebuild_unlinked_discussion_tree = function()
472472
return
473473
end
474474

475-
-- save current node for restoring cursor position
476-
local current_node = discussions_tree.get_node_at_cursor(M.split.winid, M.unlinked_bufnr, M.unlinked_discussion_tree)
475+
local current_node = discussions_tree.get_node_at_cursor(M.unlinked_discussion_tree, M.last_node_at_cursor)
477476

478477
local expanded_node_ids = M.gather_expanded_node_ids(M.unlinked_discussion_tree)
479478
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
@@ -545,6 +544,14 @@ M.create_split_and_bufs = function()
545544
buffer = linked_bufnr,
546545
callback = function()
547546
M.last_row, M.last_column = unpack(vim.api.nvim_win_get_cursor(0))
547+
M.last_node_at_cursor = M.discussion_tree and M.discussion_tree:get_node() or nil
548+
end,
549+
})
550+
551+
vim.api.nvim_create_autocmd("WinLeave", {
552+
buffer = unlinked_bufnr,
553+
callback = function()
554+
M.last_node_at_cursor = M.unlinked_discussion_tree and M.unlinked_discussion_tree:get_node() or nil
548555
end,
549556
})
550557

lua/gitlab/actions/discussions/tree.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,16 @@ M.toggle_nodes = function(winid, tree, unlinked, opts)
423423
end
424424

425425
-- Get current node for restoring cursor position
426-
---@param winid integer Window number of the discussions split
427-
---@param bufnr integer Buffer number of the current tree
428426
---@param tree NuiTree The inline discussion tree or the unlinked discussion tree
429-
M.get_node_at_cursor = function(winid, bufnr, tree)
430-
if vim.api.nvim_get_current_win() == winid and vim.api.nvim_get_current_buf() == bufnr and tree ~= nil then
427+
---@param last_node NuiTree.Node|nil The last active discussion tree node in case we are not in any of the discussion trees
428+
M.get_node_at_cursor = function(tree, last_node)
429+
if tree == nil then
430+
return
431+
end
432+
if vim.api.nvim_get_current_win() == vim.fn.win_findbuf(tree.bufnr)[1] then
431433
return tree:get_node()
434+
else
435+
return last_node
432436
end
433437
end
434438

0 commit comments

Comments
 (0)