Skip to content

Commit 5f657fd

Browse files
authored
Fix: Restore cursor when updating from outside of tree (#406)
1 parent e4c9dbe commit 5f657fd

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

lua/gitlab/actions/comment.lua

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
4141
local body = { discussion_id = discussion_id, reply = text, draft = is_draft }
4242
job.run_job("/mr/reply", "POST", body, function()
4343
u.notify("Sent reply!", vim.log.levels.INFO)
44-
if is_draft then
45-
draft_notes.load_draft_notes(function()
46-
discussions.rebuild_view(unlinked)
47-
end)
48-
else
49-
discussions.rebuild_view(unlinked)
50-
end
44+
discussions.rebuild_view(unlinked)
5145
end)
5246
return
5347
end
@@ -69,8 +63,6 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
6963
return
7064
end
7165

72-
vim.print("Here: ", unlinked, discussion_id)
73-
7466
local reviewer_data = reviewer.get_reviewer_data()
7567
if reviewer_data == nil then
7668
u.notify("Error getting reviewer data", vim.log.levels.ERROR)
@@ -102,7 +94,7 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
10294
job.run_job("/mr/draft_notes/", "POST", body, function()
10395
u.notify("Draft reply created!", vim.log.levels.INFO)
10496
draft_notes.load_draft_notes(function()
105-
discussions.rebuild_view(false, true)
97+
discussions.rebuild_view(unlinked)
10698
end)
10799
end)
108100
return

lua/gitlab/actions/discussions/init.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ M.open = function(callback)
139139
local current_window = vim.api.nvim_get_current_win() -- Save user's current window in case they switched while content was loading
140140
vim.api.nvim_set_current_win(M.split.winid)
141141

142-
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unliked_bufnr)
142+
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
143143
M.rebuild_discussion_tree()
144144
M.rebuild_unlinked_discussion_tree()
145145

@@ -432,6 +432,9 @@ M.rebuild_discussion_tree = function()
432432
if M.linked_bufnr == nil then
433433
return
434434
end
435+
436+
local current_node = discussions_tree.get_node_at_cursor(M.discussion_tree, M.last_node_at_cursor)
437+
435438
local expanded_node_ids = M.gather_expanded_node_ids(M.discussion_tree)
436439
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
437440

@@ -447,12 +450,14 @@ M.rebuild_discussion_tree = function()
447450
bufnr = M.linked_bufnr,
448451
prepare_node = tree_utils.nui_tree_prepare_node,
449452
})
453+
450454
-- Re-expand already expanded nodes
451455
for _, id in ipairs(expanded_node_ids) do
452456
tree_utils.open_node_by_id(discussion_tree, id)
453457
end
454-
455458
discussion_tree:render()
459+
discussions_tree.restore_cursor_position(M.split.winid, discussion_tree, current_node)
460+
456461
M.set_tree_keymaps(discussion_tree, M.linked_bufnr, false)
457462
M.discussion_tree = discussion_tree
458463
common.switch_can_edit_bufs(false, M.linked_bufnr, M.unlinked_bufnr)
@@ -466,6 +471,9 @@ M.rebuild_unlinked_discussion_tree = function()
466471
if M.unlinked_bufnr == nil then
467472
return
468473
end
474+
475+
local current_node = discussions_tree.get_node_at_cursor(M.unlinked_discussion_tree, M.last_node_at_cursor)
476+
469477
local expanded_node_ids = M.gather_expanded_node_ids(M.unlinked_discussion_tree)
470478
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
471479
vim.api.nvim_buf_set_lines(M.unlinked_bufnr, 0, -1, false, {})
@@ -487,6 +495,7 @@ M.rebuild_unlinked_discussion_tree = function()
487495
tree_utils.open_node_by_id(unlinked_discussion_tree, id)
488496
end
489497
unlinked_discussion_tree:render()
498+
discussions_tree.restore_cursor_position(M.split.winid, unlinked_discussion_tree, current_node)
490499

491500
M.set_tree_keymaps(unlinked_discussion_tree, M.unlinked_bufnr, true)
492501
M.unlinked_discussion_tree = unlinked_discussion_tree
@@ -535,6 +544,14 @@ M.create_split_and_bufs = function()
535544
buffer = linked_bufnr,
536545
callback = function()
537546
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
538555
end,
539556
})
540557

lua/gitlab/actions/discussions/tree.lua

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,36 @@ M.toggle_nodes = function(winid, tree, unlinked, opts)
422422
M.restore_cursor_position(winid, tree, current_node, root_node)
423423
end
424424

425+
-- Get current node for restoring cursor position
426+
---@param tree NuiTree The inline discussion tree or the unlinked discussion tree
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
433+
return tree:get_node()
434+
else
435+
return last_node
436+
end
437+
end
438+
425439
---Restore cursor position to the original node if possible
440+
---@param winid integer Window number of the discussions split
441+
---@param tree NuiTree The inline discussion tree or the unlinked discussion tree
442+
---@param original_node NuiTree.Node|nil The last node with the cursor
443+
---@param root_node NuiTree.Node|nil The root node of the last node with the cursor
426444
M.restore_cursor_position = function(winid, tree, original_node, root_node)
445+
if original_node == nil or tree == nil then
446+
return
447+
end
427448
local _, line_number = tree:get_node("-" .. tostring(original_node.id))
428-
-- If current_node is has been collapsed, get line number of root node instead
429-
if line_number == nil and root_node then
430-
_, line_number = tree:get_node("-" .. tostring(root_node.id))
449+
-- If current_node has been collapsed, try to get line number of root node instead
450+
if line_number == nil then
451+
root_node = root_node and root_node or common.get_root_node(tree, original_node)
452+
if root_node ~= nil then
453+
_, line_number = tree:get_node("-" .. tostring(root_node.id))
454+
end
431455
end
432456
if line_number ~= nil then
433457
vim.api.nvim_win_set_cursor(winid, { line_number, 0 })

0 commit comments

Comments
 (0)