Skip to content

Commit

Permalink
Fixes no draft notes, winbar issue (#275)
Browse files Browse the repository at this point in the history
fix: refresh issues with empty state when missing all draft notes or all regular notes
  • Loading branch information
harrisoncramer authored Apr 20, 2024
1 parent e71e848 commit adc20bf
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 62 deletions.
70 changes: 47 additions & 23 deletions lua/gitlab/actions/common.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- This module contains code shared between at least two modules. This includes
-- actions common to multiple tree types, as well as general utility functions
-- that are specific to actions (like jumping to a file or opening a URL)
local List = require("gitlab.utils.list")
local u = require("gitlab.utils")
local reviewer = require("gitlab.reviewer")
local common_indicators = require("gitlab.indicators.common")
Expand Down Expand Up @@ -38,29 +39,52 @@ M.build_content = function(content)
return description_lines
end

---@class TitleArg
---@field bufnr integer
---@field title string
---@field data table

---@param title_args TitleArg[]
M.add_empty_titles = function(title_args)
for _, v in ipairs(title_args) do
M.switch_can_edit_bufs(true, v.bufnr)
local ns_id = vim.api.nvim_create_namespace("GitlabNamespace")
vim.cmd("highlight default TitleHighlight guifg=#787878")

-- Set empty title if applicable
if type(v.data) ~= "table" or #v.data == 0 then
vim.api.nvim_buf_set_lines(v.bufnr, 0, 1, false, { v.title })
local linnr = 1
vim.api.nvim_buf_set_extmark(
v.bufnr,
ns_id,
linnr - 1,
0,
{ end_row = linnr - 1, end_col = string.len(v.title), hl_group = "TitleHighlight" }
)
M.add_empty_titles = function()
local draft_notes = require("gitlab.actions.draft_notes")
local discussions = require("gitlab.actions.discussions")
local linked, unlinked, drafts =
List.new(u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.discussions)),
List.new(u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.unlinked_discussions)),
List.new(u.ensure_table(state.DRAFT_NOTES))

local position_drafts = drafts:filter(function(note)
return draft_notes.has_position(note)
end)
local non_positioned_drafts = drafts:filter(function(note)
return not draft_notes.has_position(note)
end)

local fields = {
{
bufnr = discussions.linked_bufnr,
count = #linked + #position_drafts,
title = "No Discussions for this MR",
},
{
bufnr = discussions.unlinked_bufnr,
count = #unlinked + #non_positioned_drafts,
title = "No Notes (Unlinked Discussions) for this MR",
},
}

for _, v in ipairs(fields) do
if v.bufnr ~= nil then
M.switch_can_edit_bufs(true, v.bufnr)
local ns_id = vim.api.nvim_create_namespace("GitlabNamespace")
vim.cmd("highlight default TitleHighlight guifg=#787878")

-- Set empty title if applicable
if v.count == 0 then
vim.api.nvim_buf_set_lines(v.bufnr, 0, 1, false, { v.title })
local linnr = 1
vim.api.nvim_buf_set_extmark(
v.bufnr,
ns_id,
linnr - 1,
0,
{ end_row = linnr - 1, end_col = string.len(v.title), hl_group = "TitleHighlight" }
)
end
end
end
end
Expand Down
41 changes: 9 additions & 32 deletions lua/gitlab/actions/discussions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ local M = {
---@param callback function|nil
M.load_discussions = function(callback)
job.run_job("/mr/discussions/list", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
state.DISCUSSION_DATA.discussions = data.discussions ~= vim.NIL and data.discussions or {}
state.DISCUSSION_DATA.unlinked_discussions = data.unlinked_discussions ~= vim.NIL and data.unlinked_discussions
or {}
state.DISCUSSION_DATA.emojis = data.emojis ~= vim.NIL and data.emojis or {}
state.DISCUSSION_DATA.discussions = u.ensure_table(data.discussions)
state.DISCUSSION_DATA.unlinked_discussions = u.ensure_table(data.unlinked_discussions)
state.DISCUSSION_DATA.emojis = u.ensure_table(data.emojis)
if type(callback) == "function" then
callback()
end
Expand Down Expand Up @@ -91,12 +90,11 @@ end

--- Take existing data and refresh the diagnostics, the winbar, and the signs
M.refresh_view = function()
if state.settings.discussion_signs.enabled and state.DISCUSSION_DATA then
if state.settings.discussion_signs.enabled then
diagnostics.refresh_diagnostics()
end
if M.split_visible and state.DISCUSSION_DATA then
winbar.update_winbar()
end
winbar.update_winbar()
common.add_empty_titles()
end

---Opens the discussion tree, sets the keybindings. It also
Expand All @@ -108,17 +106,9 @@ M.toggle = function(callback)
return
end

if
type(state.DISCUSSION_DATA.discussions) ~= "table"
and type(state.DISCUSSION_DATA.unlinked_discussions) ~= "table"
and type(state.DISCUSSION_DATA.draft_notes) ~= "table"
then
u.notify("No discussions, notes, or draft notes for this MR", vim.log.levels.WARN)
if M.split ~= nil then
vim.api.nvim_buf_set_lines(M.split.bufnr, 0, -1, false, { "" })
end
return
end
state.DISCUSSION_DATA.discussions = u.ensure_table(state.DISCUSSION_DATA.discussions)
state.DISCUSSION_DATA.unlinked_discussions = u.ensure_table(state.DISCUSSION_DATA.unlinked_discussions)
state.DRAFT_NOTES = u.ensure_table(state.DRAFT_NOTES)

-- Make buffers, get and set buffer numbers, set filetypes
local split, linked_bufnr, unlinked_bufnr = M.create_split_and_bufs()
Expand All @@ -145,19 +135,6 @@ M.toggle = function(callback)
M.rebuild_discussion_tree()
M.rebuild_unlinked_discussion_tree()

common.add_empty_titles({
{
bufnr = M.linked_bufnr,
data = state.DISCUSSION_DATA.discussions,
title = "No Discussions for this MR",
},
{
bufnr = M.unlinked_bufnr,
data = state.DISCUSSION_DATA.unlinked_discussions,
title = "No Notes (Unlinked Discussions) for this MR",
},
})

-- Set default buffer
local default_buffer = winbar.bufnr_map[state.settings.discussion_tree.default_view]
vim.api.nvim_set_current_buf(default_buffer)
Expand Down
18 changes: 14 additions & 4 deletions lua/gitlab/actions/discussions/winbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,21 @@ end
---This function updates the winbar
M.update_winbar = function()
local d = require("gitlab.actions.discussions")
local winId = d.split.winid
local c = content()
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
Expand Down
3 changes: 2 additions & 1 deletion lua/gitlab/actions/draft_notes/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local M = {}
---Adds a draft note to the draft notes state, then rebuilds the view
---@param opts AddDraftNoteOpts
M.add_draft_note = function(opts)
local new_draft_notes = state.DRAFT_NOTES
local new_draft_notes = u.ensure_table(state.DRAFT_NOTES)
table.insert(new_draft_notes, opts.draft_note)
state.DRAFT_NOTES = new_draft_notes
local discussions = require("gitlab.actions.discussions")
Expand Down Expand Up @@ -153,6 +153,7 @@ M.send_deletion = function(tree)
end

winbar.update_winbar()
common.add_empty_titles()
end)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/gitlab/indicators/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ end
---Filter all discussions which are relevant for currently visible signs and diagnostics.
---@return Discussion|DraftNote[]
M.filter_placeable_discussions = function()
local discussions = state.DISCUSSION_DATA.discussions
local discussions = u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.discussions or {})
if type(discussions) ~= "table" then
discussions = {}
end

local draft_notes = state.DRAFT_NOTES
local draft_notes = u.ensure_table(state.DRAFT_NOTES)
if type(draft_notes) ~= "table" then
draft_notes = {}
end
Expand Down
7 changes: 7 additions & 0 deletions lua/gitlab/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -759,4 +759,11 @@ M.trim_slash = function(s)
return (s:gsub("/+$", ""))
end

M.ensure_table = function(data)
if data == vim.NIL or data == nil then
return {}
end
return data
end

return M

0 comments on commit adc20bf

Please sign in to comment.