Release 2.5.1 (#271)
* feat: Support for custom authentication provider functions (#270) * feat: Support for adding "draft" notes to the review, and publishing them, either individually or all at once. Addresses feature request #223. * feat: Lets users select + checkout a merge request directly within Neovim, without exiting to the terminal * fix: Checks that the remote feature branch exists and is up-to-date before creating a MR, starting a review, or opening the MR summary (#278) * docs: We require some state from Diffview, this shows how to load that state prior to installing w/ Packer. Fixes #94. This is a #MINOR release. --------- Co-authored-by: Jakub F. Bortlík <jakub.bortlik@proton.me> Co-authored-by: sunfuze <sunfuze.1989@gmail.com> Co-authored-by: Patrick Pichler <mail@patrickpichler.dev>
This commit is contained in:
committed by
GitHub
parent
f10c4ebb8f
commit
cf6ccddce3
@@ -79,9 +79,11 @@
|
||||
---@field moji string
|
||||
|
||||
---@class WinbarTable
|
||||
---@field name string
|
||||
---@field view_type string
|
||||
---@field resolvable_discussions number
|
||||
---@field resolved_discussions number
|
||||
---@field inline_draft_notes number
|
||||
---@field unlinked_draft_notes number
|
||||
---@field resolvable_notes number
|
||||
---@field resolved_notes number
|
||||
---@field help_keymap string
|
||||
@@ -120,3 +122,14 @@
|
||||
---@field old_line integer | nil
|
||||
---@field new_line integer | nil
|
||||
---@field line_range ReviewerRangeInfo|nil
|
||||
|
||||
---@class DraftNote
|
||||
---@field note string
|
||||
---@field id integer
|
||||
---@field author_id integer
|
||||
---@field merge_request_id integer
|
||||
---@field resolve_discussion boolean
|
||||
---@field discussion_id string -- This will always be ""
|
||||
---@field commit_id string -- This will always be ""
|
||||
---@field line_code string
|
||||
---@field position NotePosition
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,149 +1,22 @@
|
||||
local state = require("gitlab.state")
|
||||
-- This module contains tree code specific to the discussion tree, that
|
||||
-- is not used in the draft notes tree
|
||||
local u = require("gitlab.utils")
|
||||
local common = require("gitlab.actions.common")
|
||||
local state = require("gitlab.state")
|
||||
local NuiTree = require("nui.tree")
|
||||
local NuiLine = require("nui.line")
|
||||
|
||||
local M = {}
|
||||
|
||||
local attach_uuid = function(str)
|
||||
return { text = str, id = u.uuid() }
|
||||
end
|
||||
|
||||
---Create path node
|
||||
---@param relative_path string
|
||||
---@param full_path string
|
||||
---@param child_nodes NuiTree.Node[]?
|
||||
---@return NuiTree.Node
|
||||
local function create_path_node(relative_path, full_path, child_nodes)
|
||||
return NuiTree.Node({
|
||||
text = relative_path,
|
||||
path = full_path,
|
||||
id = full_path,
|
||||
type = "path",
|
||||
icon = " ",
|
||||
icon_hl = "GitlabDirectoryIcon",
|
||||
text_hl = "GitlabDirectory",
|
||||
}, child_nodes or {})
|
||||
end
|
||||
|
||||
---Create file name node
|
||||
---@param file_name string
|
||||
---@param full_file_path string
|
||||
---@param child_nodes NuiTree.Node[]?
|
||||
---@return NuiTree.Node
|
||||
local function create_file_name_node(file_name, full_file_path, child_nodes)
|
||||
local icon, icon_hl = u.get_icon(file_name)
|
||||
return NuiTree.Node({
|
||||
text = file_name,
|
||||
file_name = full_file_path,
|
||||
id = full_file_path,
|
||||
type = "file_name",
|
||||
icon = icon,
|
||||
icon_hl = icon_hl,
|
||||
text_hl = "GitlabFileName",
|
||||
}, child_nodes or {})
|
||||
end
|
||||
|
||||
---Sort list of nodes (in place) of type "path" or "file_name"
|
||||
---@param nodes NuiTree.Node[]
|
||||
local function sort_nodes(nodes)
|
||||
table.sort(nodes, function(node1, node2)
|
||||
if node1.type == "path" and node2.type == "path" then
|
||||
return node1.path < node2.path
|
||||
elseif node1.type == "file_name" and node2.type == "file_name" then
|
||||
return node1.file_name < node2.file_name
|
||||
elseif node1.type == "path" and node2.type == "file_name" then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---Merge path nodes which have only single path child
|
||||
---@param node NuiTree.Node
|
||||
local function flatten_nodes(node)
|
||||
if node.type ~= "path" then
|
||||
return
|
||||
end
|
||||
for _, child in ipairs(node.__children) do
|
||||
flatten_nodes(child)
|
||||
end
|
||||
if #node.__children == 1 and node.__children[1].type == "path" then
|
||||
local child = node.__children[1]
|
||||
node.__children = child.__children
|
||||
node.id = child.id
|
||||
node.path = child.path
|
||||
node.text = node.text .. u.path_separator .. child.text
|
||||
end
|
||||
sort_nodes(node.__children)
|
||||
end
|
||||
|
||||
---Build note header from note.
|
||||
---@param note Note
|
||||
---@return string
|
||||
M.build_note_header = function(note)
|
||||
return "@" .. note.author.username .. " " .. u.time_since(note.created_at)
|
||||
end
|
||||
|
||||
---Build note node body
|
||||
---@param note Note
|
||||
---@param resolve_info table?
|
||||
---@return string
|
||||
---@return NuiTree.Node[]
|
||||
local function build_note_body(note, resolve_info)
|
||||
local text_nodes = {}
|
||||
for bodyLine in u.split_by_new_lines(note.body) do
|
||||
local line = attach_uuid(bodyLine)
|
||||
table.insert(
|
||||
text_nodes,
|
||||
NuiTree.Node({
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
text = line.text,
|
||||
id = line.id,
|
||||
type = "note_body",
|
||||
}, {})
|
||||
)
|
||||
end
|
||||
|
||||
local resolve_symbol = ""
|
||||
if resolve_info ~= nil and resolve_info.resolvable then
|
||||
resolve_symbol = resolve_info.resolved and state.settings.discussion_tree.resolved
|
||||
or state.settings.discussion_tree.unresolved
|
||||
end
|
||||
|
||||
local noteHeader = M.build_note_header(note) .. " " .. resolve_symbol
|
||||
|
||||
return noteHeader, text_nodes
|
||||
end
|
||||
|
||||
---Build note node
|
||||
---@param note Note
|
||||
---@param resolve_info table?
|
||||
---@return NuiTree.Node
|
||||
---@return string
|
||||
---@return NuiTree.Node[]
|
||||
M.build_note = function(note, resolve_info)
|
||||
local text, text_nodes = build_note_body(note, resolve_info)
|
||||
local note_node = NuiTree.Node({
|
||||
text = text,
|
||||
id = note.id,
|
||||
file_name = (type(note.position) == "table" and note.position.new_path),
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
url = state.INFO.web_url .. "#note_" .. note.id,
|
||||
type = "note",
|
||||
}, text_nodes)
|
||||
|
||||
return note_node, text, text_nodes
|
||||
end
|
||||
|
||||
---Create nodes for NuiTree from discussions
|
||||
---@param items Discussion[]
|
||||
---@param unlinked boolean? False or nil means that discussions are linked to code lines
|
||||
---@return NuiTree.Node[]
|
||||
M.add_discussions_to_table = function(items, unlinked)
|
||||
local t = {}
|
||||
if items == vim.NIL then
|
||||
items = {}
|
||||
end
|
||||
for _, discussion in ipairs(items) do
|
||||
local discussion_children = {}
|
||||
|
||||
@@ -206,10 +79,85 @@ M.add_discussions_to_table = function(items, unlinked)
|
||||
return t
|
||||
end
|
||||
|
||||
return M.create_node_list_by_file_name(t)
|
||||
end
|
||||
|
||||
---Create path node
|
||||
---@param relative_path string
|
||||
---@param full_path string
|
||||
---@param child_nodes NuiTree.Node[]?
|
||||
---@return NuiTree.Node
|
||||
local function create_path_node(relative_path, full_path, child_nodes)
|
||||
return NuiTree.Node({
|
||||
text = relative_path,
|
||||
path = full_path,
|
||||
id = full_path,
|
||||
type = "path",
|
||||
icon = " ",
|
||||
icon_hl = "GitlabDirectoryIcon",
|
||||
text_hl = "GitlabDirectory",
|
||||
}, child_nodes or {})
|
||||
end
|
||||
|
||||
---Sort list of nodes (in place) of type "path" or "file_name"
|
||||
---@param nodes NuiTree.Node[]
|
||||
local function sort_nodes(nodes)
|
||||
table.sort(nodes, function(node1, node2)
|
||||
if node1.type == "path" and node2.type == "path" then
|
||||
return node1.path < node2.path
|
||||
elseif node1.type == "file_name" and node2.type == "file_name" then
|
||||
return node1.file_name < node2.file_name
|
||||
elseif node1.type == "path" and node2.type == "file_name" then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---Merge path nodes which have only single path child
|
||||
---@param node NuiTree.Node
|
||||
local function flatten_nodes(node)
|
||||
if node.type ~= "path" then
|
||||
return
|
||||
end
|
||||
for _, child in ipairs(node.__children) do
|
||||
flatten_nodes(child)
|
||||
end
|
||||
if #node.__children == 1 and node.__children[1].type == "path" then
|
||||
local child = node.__children[1]
|
||||
node.__children = child.__children
|
||||
node.id = child.id
|
||||
node.path = child.path
|
||||
node.text = node.text .. u.path_separator .. child.text
|
||||
end
|
||||
sort_nodes(node.__children)
|
||||
end
|
||||
|
||||
---Create file name node
|
||||
---@param file_name string
|
||||
---@param full_file_path string
|
||||
---@param child_nodes NuiTree.Node[]?
|
||||
---@return NuiTree.Node
|
||||
local function create_file_name_node(file_name, full_file_path, child_nodes)
|
||||
local icon, icon_hl = u.get_icon(file_name)
|
||||
return NuiTree.Node({
|
||||
text = file_name,
|
||||
file_name = full_file_path,
|
||||
id = full_file_path,
|
||||
type = "file_name",
|
||||
icon = icon,
|
||||
icon_hl = icon_hl,
|
||||
text_hl = "GitlabFileName",
|
||||
}, child_nodes or {})
|
||||
end
|
||||
|
||||
local create_disscussions_by_file_name = function(node_list)
|
||||
-- Create all the folder and file name nodes.
|
||||
local discussion_by_file_name = {}
|
||||
local top_level_path_to_node = {}
|
||||
for _, node in ipairs(t) do
|
||||
|
||||
for _, node in ipairs(node_list) do
|
||||
local path = ""
|
||||
local parent_node = nil
|
||||
local path_parts = u.split_path(node.file_name)
|
||||
@@ -274,13 +222,280 @@ M.add_discussions_to_table = function(items, unlinked)
|
||||
end
|
||||
end
|
||||
|
||||
return discussion_by_file_name
|
||||
end
|
||||
|
||||
M.create_node_list_by_file_name = function(node_list)
|
||||
-- Create all the folder and file name nodes.
|
||||
local discussion_by_file_name = create_disscussions_by_file_name(node_list)
|
||||
|
||||
-- Flatten empty folders
|
||||
for _, node in ipairs(discussion_by_file_name) do
|
||||
flatten_nodes(node)
|
||||
end
|
||||
|
||||
sort_nodes(discussion_by_file_name)
|
||||
|
||||
return discussion_by_file_name
|
||||
end
|
||||
|
||||
local attach_uuid = function(str)
|
||||
return { text = str, id = u.uuid() }
|
||||
end
|
||||
|
||||
---Build note node body
|
||||
---@param note Note|DraftNote
|
||||
---@param resolve_info table?
|
||||
---@return string
|
||||
---@return NuiTree.Node[]
|
||||
local function build_note_body(note, resolve_info)
|
||||
local text_nodes = {}
|
||||
for bodyLine in u.split_by_new_lines(note.body or note.note) do
|
||||
local line = attach_uuid(bodyLine)
|
||||
table.insert(
|
||||
text_nodes,
|
||||
NuiTree.Node({
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
text = line.text,
|
||||
id = line.id,
|
||||
type = "note_body",
|
||||
}, {})
|
||||
)
|
||||
end
|
||||
|
||||
local resolve_symbol = ""
|
||||
if resolve_info ~= nil and resolve_info.resolvable then
|
||||
resolve_symbol = resolve_info.resolved and state.settings.discussion_tree.resolved
|
||||
or state.settings.discussion_tree.unresolved
|
||||
end
|
||||
|
||||
local noteHeader = common.build_note_header(note) .. " " .. resolve_symbol
|
||||
|
||||
return noteHeader, text_nodes
|
||||
end
|
||||
|
||||
---Build note node
|
||||
---@param note Note|DraftNote
|
||||
---@param resolve_info table?
|
||||
---@return NuiTree.Node
|
||||
---@return string
|
||||
---@return NuiTree.Node[]
|
||||
M.build_note = function(note, resolve_info)
|
||||
local text, text_nodes = build_note_body(note, resolve_info)
|
||||
local note_node = NuiTree.Node({
|
||||
text = text,
|
||||
is_draft = note.note ~= nil,
|
||||
id = note.id,
|
||||
file_name = (type(note.position) == "table" and note.position.new_path),
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
url = state.INFO.web_url .. "#note_" .. note.id,
|
||||
type = "note",
|
||||
}, text_nodes)
|
||||
|
||||
return note_node, text, text_nodes
|
||||
end
|
||||
|
||||
---Inspired by default func https://github.com/MunifTanjim/nui.nvim/blob/main/lua/nui/tree/util.lua#L38
|
||||
M.nui_tree_prepare_node = function(node)
|
||||
if not node.text then
|
||||
error("missing node.text")
|
||||
end
|
||||
|
||||
local texts = node.text
|
||||
if type(node.text) ~= "table" or node.text.content then
|
||||
texts = { node.text }
|
||||
end
|
||||
|
||||
local lines = {}
|
||||
|
||||
for i, text in ipairs(texts) do
|
||||
local line = NuiLine()
|
||||
|
||||
line:append(string.rep(" ", node._depth - 1))
|
||||
|
||||
if i == 1 and node:has_children() then
|
||||
line:append(node:is_expanded() and " " or " ")
|
||||
if node.icon then
|
||||
line:append(node.icon .. " ", node.icon_hl)
|
||||
end
|
||||
else
|
||||
line:append(" ")
|
||||
end
|
||||
|
||||
line:append(text, node.text_hl)
|
||||
|
||||
local note_id = tostring(node.is_root and node.root_note_id or node.id)
|
||||
|
||||
local e = require("gitlab.emoji")
|
||||
|
||||
---@type Emoji[]
|
||||
local emojis = state.DISCUSSION_DATA.emojis[note_id]
|
||||
local placed_emojis = {}
|
||||
if emojis ~= nil then
|
||||
for _, v in ipairs(emojis) do
|
||||
local icon = e.emoji_map[v.name]
|
||||
if icon ~= nil and not u.contains(placed_emojis, icon.moji) then
|
||||
line:append(" ")
|
||||
line:append(icon.moji)
|
||||
table.insert(placed_emojis, icon.moji)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(lines, line)
|
||||
end
|
||||
|
||||
return lines
|
||||
end
|
||||
|
||||
---@class ToggleNodesOptions
|
||||
---@field toggle_resolved boolean Whether to toggle resolved discussions.
|
||||
---@field toggle_unresolved boolean Whether to toggle unresolved discussions.
|
||||
---@field keep_current_open boolean Whether to keep the current discussion open even if it should otherwise be closed.
|
||||
|
||||
---This function (settings.discussion_tree.toggle_nodes) expands/collapses all nodes and their children according to the opts.
|
||||
---@param tree NuiTree
|
||||
---@param winid integer
|
||||
---@param unlinked boolean
|
||||
---@param opts ToggleNodesOptions
|
||||
M.toggle_nodes = function(winid, tree, unlinked, opts)
|
||||
local current_node = tree:get_node()
|
||||
if current_node == nil then
|
||||
return
|
||||
end
|
||||
local root_node = common.get_root_node(tree, current_node)
|
||||
for _, node in ipairs(tree:get_nodes()) do
|
||||
if opts.toggle_resolved 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
|
||||
(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
|
||||
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
|
||||
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(winid, tree, current_node, root_node)
|
||||
end
|
||||
|
||||
---Restore cursor position to the original node if possible
|
||||
M.restore_cursor_position = function(winid, tree, original_node, root_node)
|
||||
local _, line_number = tree:get_node("-" .. tostring(original_node.id))
|
||||
-- If current_node is has been collapsed, get line number of root node instead
|
||||
if line_number == nil and root_node then
|
||||
_, line_number = tree:get_node("-" .. tostring(root_node.id))
|
||||
end
|
||||
if line_number ~= nil then
|
||||
vim.api.nvim_win_set_cursor(winid, { line_number, 0 })
|
||||
end
|
||||
end
|
||||
|
||||
---This function (settings.discussion_tree.expand_recursively) expands a node and its children.
|
||||
---@param tree NuiTree
|
||||
---@param node NuiTree.Node
|
||||
---@param is_resolved boolean If true, expand resolved discussions. If false, expand unresolved discussions.
|
||||
M.expand_recursively = function(tree, node, is_resolved)
|
||||
if node == nil then
|
||||
return
|
||||
end
|
||||
if common.is_node_note(node) and common.get_root_node(tree, node).resolved == is_resolved then
|
||||
node:expand()
|
||||
end
|
||||
local children = node:get_child_ids()
|
||||
for _, child in ipairs(children) do
|
||||
M.expand_recursively(tree, tree:get_node(child), is_resolved)
|
||||
end
|
||||
end
|
||||
|
||||
---This function (settings.discussion_tree.collapse_recursively) collapses a node and its children.
|
||||
---@param tree NuiTree
|
||||
---@param node NuiTree.Node
|
||||
---@param current_root_node NuiTree.Node The root node of the current node.
|
||||
---@param keep_current_open boolean If true, the current node stays open, even if it should otherwise be collapsed.
|
||||
---@param is_resolved boolean If true, collapse resolved discussions. If false, collapse unresolved discussions.
|
||||
M.collapse_recursively = function(tree, node, current_root_node, keep_current_open, is_resolved)
|
||||
if node == nil then
|
||||
return
|
||||
end
|
||||
local root_node = common.get_root_node(tree, node)
|
||||
if common.is_node_note(node) and root_node.resolved == is_resolved then
|
||||
if keep_current_open and root_node == current_root_node then
|
||||
return
|
||||
end
|
||||
node:collapse()
|
||||
end
|
||||
local children = node:get_child_ids()
|
||||
for _, child in ipairs(children) do
|
||||
M.collapse_recursively(tree, tree:get_node(child), current_root_node, keep_current_open, is_resolved)
|
||||
end
|
||||
end
|
||||
|
||||
-- This function (settings.discussion_tree.toggle_node) expands/collapses the current node and its children
|
||||
M.toggle_node = function(tree)
|
||||
local node = tree:get_node()
|
||||
if node == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- Switch to the "note" node from "note_body" nodes to enable toggling discussions inside comments
|
||||
if node.type == "note_body" then
|
||||
node = tree:get_node(node:get_parent_id())
|
||||
end
|
||||
if node == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local children = node:get_child_ids()
|
||||
if node == nil then
|
||||
return
|
||||
end
|
||||
if node:is_expanded() then
|
||||
node:collapse()
|
||||
if common.is_node_note(node) then
|
||||
for _, child in ipairs(children) do
|
||||
tree:get_node(child):collapse()
|
||||
end
|
||||
end
|
||||
else
|
||||
if common.is_node_note(node) then
|
||||
for _, child in ipairs(children) do
|
||||
tree:get_node(child):expand()
|
||||
end
|
||||
end
|
||||
node:expand()
|
||||
end
|
||||
|
||||
tree:render()
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
local M = {}
|
||||
local state = require("gitlab.state")
|
||||
local List = require("gitlab.utils.list")
|
||||
local state = require("gitlab.state")
|
||||
|
||||
local M = {
|
||||
bufnr_map = {
|
||||
discussions = nil,
|
||||
notes = nil,
|
||||
},
|
||||
current_view_type = state.settings.discussion_tree.default_view,
|
||||
}
|
||||
|
||||
M.set_buffers = function(linked_bufnr, unlinked_bufnr)
|
||||
M.bufnr_map = {
|
||||
discussions = linked_bufnr,
|
||||
notes = unlinked_bufnr,
|
||||
}
|
||||
end
|
||||
|
||||
---@param nodes Discussion[]|UnlinkedDiscussion[]|nil
|
||||
---@return number, number
|
||||
@@ -30,36 +44,131 @@ local get_data = function(nodes)
|
||||
return total_resolvable, total_resolved
|
||||
end
|
||||
|
||||
---@param discussions Discussion[]|nil
|
||||
---@param unlinked_discussions UnlinkedDiscussion[]|nil
|
||||
---@param file_name string
|
||||
local function content(discussions, unlinked_discussions, file_name)
|
||||
local resolvable_discussions, resolved_discussions = get_data(discussions)
|
||||
local resolvable_notes, resolved_notes = get_data(unlinked_discussions)
|
||||
local function content()
|
||||
local resolvable_discussions, resolved_discussions = get_data(state.DISCUSSION_DATA.discussions)
|
||||
local resolvable_notes, resolved_notes = get_data(state.DISCUSSION_DATA.unlinked_discussions)
|
||||
|
||||
local draft_notes = require("gitlab.actions.draft_notes")
|
||||
local inline_draft_notes = List.new(state.DRAFT_NOTES):filter(draft_notes.has_position)
|
||||
local unlinked_draft_notes = List.new(state.DRAFT_NOTES):filter(function(note)
|
||||
return not draft_notes.has_position(note)
|
||||
end)
|
||||
|
||||
local t = {
|
||||
name = file_name,
|
||||
resolvable_discussions = resolvable_discussions,
|
||||
resolved_discussions = resolved_discussions,
|
||||
inline_draft_notes = #inline_draft_notes,
|
||||
unlinked_draft_notes = #unlinked_draft_notes,
|
||||
resolvable_notes = resolvable_notes,
|
||||
resolved_notes = resolved_notes,
|
||||
help_keymap = state.settings.help,
|
||||
}
|
||||
|
||||
return state.settings.discussion_tree.winbar(t)
|
||||
return M.make_winbar(t)
|
||||
end
|
||||
|
||||
---This function updates the winbar
|
||||
---@param discussions Discussion[]
|
||||
---@param unlinked_discussions UnlinkedDiscussion[]
|
||||
---@param base_title string
|
||||
M.update_winbar = function(discussions, unlinked_discussions, base_title)
|
||||
M.update_winbar = function()
|
||||
local d = require("gitlab.actions.discussions")
|
||||
local winId = d.split.winid
|
||||
local c = content(discussions, unlinked_discussions, base_title)
|
||||
if vim.wo[winId] then
|
||||
vim.wo[winId].winbar = c
|
||||
if d.split == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local win_id = d.split.winid
|
||||
if win_id == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if not vim.api.nvim_win_is_valid(win_id) then
|
||||
return
|
||||
end
|
||||
|
||||
local c = content()
|
||||
vim.api.nvim_set_option_value("winbar", c, { scope = "local", win = win_id })
|
||||
end
|
||||
|
||||
---Builds the title string for both sections, using the count of resolvable and draft nodes
|
||||
---@param base_title string
|
||||
---@param resolvable_count integer
|
||||
---@param resolved_count integer
|
||||
---@param drafts_count integer
|
||||
---@return string
|
||||
local add_drafts_and_resolvable = function(base_title, resolvable_count, resolved_count, drafts_count)
|
||||
if resolvable_count ~= 0 then
|
||||
base_title = base_title .. string.format(" (%d/%d resolved", resolvable_count, resolved_count)
|
||||
end
|
||||
if drafts_count ~= 0 then
|
||||
if resolvable_count ~= 0 then
|
||||
base_title = base_title .. string.format("; %d drafts)", drafts_count)
|
||||
else
|
||||
base_title = base_title .. string.format(" (%d drafts)", drafts_count)
|
||||
end
|
||||
elseif resolvable_count ~= 0 then
|
||||
base_title = base_title .. ")"
|
||||
end
|
||||
|
||||
return base_title
|
||||
end
|
||||
|
||||
---@param t WinbarTable
|
||||
M.make_winbar = function(t)
|
||||
local discussion_title =
|
||||
add_drafts_and_resolvable("Inline Comments", t.resolvable_discussions, t.resolved_discussions, t.inline_draft_notes)
|
||||
local notes_title = add_drafts_and_resolvable("Notes", t.resolvable_notes, t.resolved_notes, t.unlinked_draft_notes)
|
||||
|
||||
-- Colorize the active tab
|
||||
if M.current_view_type == "discussions" then
|
||||
discussion_title = "%#Text#" .. discussion_title
|
||||
notes_title = "%#Comment#" .. notes_title
|
||||
elseif M.current_view_type == "notes" then
|
||||
discussion_title = "%#Comment#" .. discussion_title
|
||||
notes_title = "%#Text#" .. notes_title
|
||||
end
|
||||
|
||||
local mode = M.get_mode()
|
||||
|
||||
-- Join everything together and return it
|
||||
local separator = "%#Comment#|"
|
||||
local end_section = "%="
|
||||
local help = "%#Comment#Help: " .. t.help_keymap:gsub(" ", "<space>") .. " "
|
||||
return string.format(
|
||||
" %s %s %s %s %s %s %s",
|
||||
discussion_title,
|
||||
separator,
|
||||
notes_title,
|
||||
end_section,
|
||||
mode,
|
||||
separator,
|
||||
help
|
||||
)
|
||||
end
|
||||
|
||||
---Returns a string for the winbar indicating the mode type, live or draft
|
||||
---@return string
|
||||
M.get_mode = function()
|
||||
if state.settings.discussion_tree.draft_mode then
|
||||
return "%#DiagnosticWarn#Draft Mode"
|
||||
else
|
||||
return "%#DiagnosticOK#Live Mode"
|
||||
end
|
||||
end
|
||||
|
||||
---Sets the current view type (if provided an argument)
|
||||
---and then updates the view
|
||||
---@param override any
|
||||
M.switch_view_type = function(override)
|
||||
if override then
|
||||
M.current_view_type = override
|
||||
else
|
||||
if M.current_view_type == "discussions" then
|
||||
M.current_view_type = "notes"
|
||||
elseif M.current_view_type == "notes" then
|
||||
M.current_view_type = "discussions"
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_set_current_buf(M.bufnr_map[M.current_view_type])
|
||||
M.update_winbar()
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user