Skip to content

Commit

Permalink
Fix: Toggle modified notes (#188)
Browse files Browse the repository at this point in the history
* Fix: Toggle discussion nodes correctly
* Feat: Show Help keymap in discussion tree winbar
  • Loading branch information
jakubbortlik authored Feb 24, 2024
1 parent faf2a25 commit f6a5238
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions lua/gitlab/actions/discussions/annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
---@field resolved_discussions number
---@field resolvable_notes number
---@field resolved_notes number
---@field help_keymap string
---
---@class SignTable
---@field name string
Expand Down
36 changes: 28 additions & 8 deletions lua/gitlab/actions/discussions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -401,33 +401,49 @@ end
---This function (settings.discussion_tree.toggle_nodes) expands/collapses all nodes and their children according to the opts.
---@param tree NuiTree
---@param opts ToggleNodesOptions
M.toggle_nodes = function(tree, opts)
M.toggle_nodes = function(tree, unlinked, opts)
local current_node = tree:get_node()
if current_node == nil then
return
end
local root_node = M.get_root_node(tree, current_node)
for _, node in ipairs(tree:get_nodes()) do
if opts.toggle_resolved then
if state.resolved_expanded then
if
(unlinked and state.unlinked_discussion_tree.resolved_expanded)
or (not unlinked and state.discussion_tree.resolved_expanded)
then
M.collapse_recursively(tree, node, root_node, opts.keep_current_open, true)
else
M.expand_recursively(tree, node, true)
end
end
if opts.toggle_unresolved then
if state.unresolved_expanded then
if
(unlinked and state.unlinked_discussion_tree.unresolved_expanded)
or (not unlinked and state.discussion_tree.unresolved_expanded)
then
M.collapse_recursively(tree, node, root_node, opts.keep_current_open, false)
else
M.expand_recursively(tree, node, false)
end
end
end
-- Reset states of resolved discussions after toggling
if opts.toggle_resolved then
state.resolved_expanded = not state.resolved_expanded
if unlinked then
state.unlinked_discussion_tree.resolved_expanded = not state.unlinked_discussion_tree.resolved_expanded
else
state.discussion_tree.resolved_expanded = not state.discussion_tree.resolved_expanded
end
end
-- Reset states of unresolved discussions after toggling
if opts.toggle_unresolved then
state.unresolved_expanded = not state.unresolved_expanded
if unlinked then
state.unlinked_discussion_tree.unresolved_expanded = not state.unlinked_discussion_tree.unresolved_expanded
else
state.discussion_tree.unresolved_expanded = not state.discussion_tree.unresolved_expanded
end
end
tree:render()
M.restore_cursor_position(tree, current_node, root_node)
Expand Down Expand Up @@ -543,6 +559,8 @@ M.rebuild_discussion_tree = function()
M.discussion_tree = discussion_tree
M.switch_can_edit_bufs(false)
vim.api.nvim_set_option_value("filetype", "gitlab", { buf = M.linked_bufnr })
state.discussion_tree.resolved_expanded = false
state.discussion_tree.unresolved_expanded = false
end

M.rebuild_unlinked_discussion_tree = function()
Expand All @@ -561,6 +579,8 @@ M.rebuild_unlinked_discussion_tree = function()
M.set_tree_keymaps(unlinked_discussion_tree, M.unlinked_bufnr, true)
M.unlinked_discussion_tree = unlinked_discussion_tree
M.switch_can_edit_bufs(false)
state.unlinked_discussion_tree.resolved_expanded = false
state.unlinked_discussion_tree.unresolved_expanded = false
end

M.switch_can_edit_bufs = function(bool)
Expand Down Expand Up @@ -662,21 +682,21 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
M.toggle_node(tree)
end, { buffer = bufnr, desc = "Toggle node" })
vim.keymap.set("n", state.settings.discussion_tree.toggle_all_discussions, function()
M.toggle_nodes(tree, {
M.toggle_nodes(tree, unlinked, {
toggle_resolved = true,
toggle_unresolved = true,
keep_current_open = state.settings.discussion_tree.keep_current_open,
})
end, { buffer = bufnr, desc = "Toggle all nodes" })
vim.keymap.set("n", state.settings.discussion_tree.toggle_resolved_discussions, function()
M.toggle_nodes(tree, {
M.toggle_nodes(tree, unlinked, {
toggle_resolved = true,
toggle_unresolved = false,
keep_current_open = state.settings.discussion_tree.keep_current_open,
})
end, { buffer = bufnr, desc = "Toggle resolved nodes" })
vim.keymap.set("n", state.settings.discussion_tree.toggle_unresolved_discussions, function()
M.toggle_nodes(tree, {
M.toggle_nodes(tree, unlinked, {
toggle_resolved = false,
toggle_unresolved = true,
keep_current_open = state.settings.discussion_tree.keep_current_open,
Expand Down
3 changes: 2 additions & 1 deletion lua/gitlab/actions/discussions/winbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ local function content(discussions, unlinked_discussions, file_name)
resolved_discussions = resolved_discussions,
resolvable_notes = resolvable_notes,
resolved_notes = resolved_notes,
help_keymap = state.settings.help,
}

return state.settings.discussion_tree.winbar(t)
end

---This function sends the edited comment to the Go server
---This function updates the winbar
---@param discussions Discussion[]
---@param unlinked_discussions UnlinkedDiscussion[]
---@param base_title string
Expand Down
13 changes: 12 additions & 1 deletion lua/gitlab/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ M.settings = {
discussions_content = "%#Comment#" .. discussions_content
notes_content = "%#Text#" .. notes_content
end
return " " .. discussions_content .. " %#Comment#| " .. notes_content
local help = "%#Comment#%=Help: " .. t.help_keymap:gsub(" ", "<space>") .. " "
return " " .. discussions_content .. " %#Comment#| " .. notes_content .. help
end,
},
merge = {
Expand Down Expand Up @@ -169,6 +170,16 @@ M.settings = {
},
}

-- These are the initial states of the discussion trees
M.discussion_tree = {
resolved_expanded = false,
unresolved_expanded = false,
}
M.unlinked_discussion_tree = {
resolved_expanded = false,
unresolved_expanded = false,
}

-- Merges user settings into the default settings, overriding them
M.merge_settings = function(args)
M.settings = u.merge(M.settings, args)
Expand Down

0 comments on commit f6a5238

Please sign in to comment.