Skip to content

Commit 6d23978

Browse files
committed
Refactor: Move duplicated code to common functions
1 parent 7a7b9bf commit 6d23978

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

lua/gitlab/actions/discussions/init.lua

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

436-
-- save current node for restoring cursor position
437-
local current_node
438-
if
439-
vim.api.nvim_get_current_win() == M.split.winid
440-
and vim.api.nvim_get_current_buf() == M.linked_bufnr
441-
and M.discussion_tree ~= nil
442-
then
443-
current_node = M.discussion_tree:get_node()
444-
end
436+
local current_node = discussions_tree.get_node_at_cursor(M.split.winid, M.linked_bufnr, M.discussion_tree)
445437

446438
local expanded_node_ids = M.gather_expanded_node_ids(M.discussion_tree)
447439
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
@@ -464,11 +456,7 @@ M.rebuild_discussion_tree = function()
464456
tree_utils.open_node_by_id(discussion_tree, id)
465457
end
466458
discussion_tree:render()
467-
468-
if current_node ~= nil then
469-
local root_node = common.get_root_node(M.discussion_tree, current_node)
470-
discussions_tree.restore_cursor_position(M.split.winid, discussion_tree, current_node, root_node)
471-
end
459+
discussions_tree.restore_cursor_position(M.split.winid, discussion_tree, current_node)
472460

473461
M.set_tree_keymaps(discussion_tree, M.linked_bufnr, false)
474462
M.discussion_tree = discussion_tree
@@ -485,14 +473,7 @@ M.rebuild_unlinked_discussion_tree = function()
485473
end
486474

487475
-- save current node for restoring cursor position
488-
local current_node
489-
if
490-
vim.api.nvim_get_current_win() == M.split.winid
491-
and vim.api.nvim_get_current_buf() == M.unlinked_bufnr
492-
and M.unlinked_discussion_tree ~= nil
493-
then
494-
current_node = M.unlinked_discussion_tree:get_node()
495-
end
476+
local current_node = discussions_tree.get_node_at_cursor(M.split.winid, M.unlinked_bufnr, M.unlinked_discussion_tree)
496477

497478
local expanded_node_ids = M.gather_expanded_node_ids(M.unlinked_discussion_tree)
498479
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
@@ -515,11 +496,7 @@ M.rebuild_unlinked_discussion_tree = function()
515496
tree_utils.open_node_by_id(unlinked_discussion_tree, id)
516497
end
517498
unlinked_discussion_tree:render()
518-
519-
if current_node ~= nil then
520-
local root_node = common.get_root_node(M.unlinked_discussion_tree, current_node)
521-
discussions_tree.restore_cursor_position(M.split.winid, unlinked_discussion_tree, current_node, root_node)
522-
end
499+
discussions_tree.restore_cursor_position(M.split.winid, unlinked_discussion_tree, current_node)
523500

524501
M.set_tree_keymaps(unlinked_discussion_tree, M.unlinked_bufnr, true)
525502
M.unlinked_discussion_tree = unlinked_discussion_tree

lua/gitlab/actions/discussions/tree.lua

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,32 @@ 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 winid integer Window number of the discussions split
427+
---@param bufnr integer Buffer number of the current tree
428+
---@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
431+
return tree:get_node()
432+
end
433+
end
434+
425435
---Restore cursor position to the original node if possible
436+
---@param winid integer Window number of the discussions split
437+
---@param tree NuiTree The inline discussion tree or the unlinked discussion tree
438+
---@param original_node NuiTree.Node|nil The last node with the cursor
439+
---@param root_node NuiTree.Node|nil The root node of the last node with the cursor
426440
M.restore_cursor_position = function(winid, tree, original_node, root_node)
441+
if original_node == nil or tree == nil then
442+
return
443+
end
427444
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))
445+
-- If current_node has been collapsed, try to get line number of root node instead
446+
if line_number == nil then
447+
root_node = root_node and root_node or common.get_root_node(tree, original_node)
448+
if root_node ~= nil then
449+
_, line_number = tree:get_node("-" .. tostring(root_node.id))
450+
end
431451
end
432452
if line_number ~= nil then
433453
vim.api.nvim_win_set_cursor(winid, { line_number, 0 })

0 commit comments

Comments
 (0)