Skip to content

Commit adc20bf

Browse files
Fixes no draft notes, winbar issue (#275)
fix: refresh issues with empty state when missing all draft notes or all regular notes
1 parent e71e848 commit adc20bf

File tree

6 files changed

+81
-62
lines changed

6 files changed

+81
-62
lines changed

lua/gitlab/actions/common.lua

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- This module contains code shared between at least two modules. This includes
22
-- actions common to multiple tree types, as well as general utility functions
33
-- that are specific to actions (like jumping to a file or opening a URL)
4+
local List = require("gitlab.utils.list")
45
local u = require("gitlab.utils")
56
local reviewer = require("gitlab.reviewer")
67
local common_indicators = require("gitlab.indicators.common")
@@ -38,29 +39,52 @@ M.build_content = function(content)
3839
return description_lines
3940
end
4041

41-
---@class TitleArg
42-
---@field bufnr integer
43-
---@field title string
44-
---@field data table
45-
46-
---@param title_args TitleArg[]
47-
M.add_empty_titles = function(title_args)
48-
for _, v in ipairs(title_args) do
49-
M.switch_can_edit_bufs(true, v.bufnr)
50-
local ns_id = vim.api.nvim_create_namespace("GitlabNamespace")
51-
vim.cmd("highlight default TitleHighlight guifg=#787878")
52-
53-
-- Set empty title if applicable
54-
if type(v.data) ~= "table" or #v.data == 0 then
55-
vim.api.nvim_buf_set_lines(v.bufnr, 0, 1, false, { v.title })
56-
local linnr = 1
57-
vim.api.nvim_buf_set_extmark(
58-
v.bufnr,
59-
ns_id,
60-
linnr - 1,
61-
0,
62-
{ end_row = linnr - 1, end_col = string.len(v.title), hl_group = "TitleHighlight" }
63-
)
42+
M.add_empty_titles = function()
43+
local draft_notes = require("gitlab.actions.draft_notes")
44+
local discussions = require("gitlab.actions.discussions")
45+
local linked, unlinked, drafts =
46+
List.new(u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.discussions)),
47+
List.new(u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.unlinked_discussions)),
48+
List.new(u.ensure_table(state.DRAFT_NOTES))
49+
50+
local position_drafts = drafts:filter(function(note)
51+
return draft_notes.has_position(note)
52+
end)
53+
local non_positioned_drafts = drafts:filter(function(note)
54+
return not draft_notes.has_position(note)
55+
end)
56+
57+
local fields = {
58+
{
59+
bufnr = discussions.linked_bufnr,
60+
count = #linked + #position_drafts,
61+
title = "No Discussions for this MR",
62+
},
63+
{
64+
bufnr = discussions.unlinked_bufnr,
65+
count = #unlinked + #non_positioned_drafts,
66+
title = "No Notes (Unlinked Discussions) for this MR",
67+
},
68+
}
69+
70+
for _, v in ipairs(fields) do
71+
if v.bufnr ~= nil then
72+
M.switch_can_edit_bufs(true, v.bufnr)
73+
local ns_id = vim.api.nvim_create_namespace("GitlabNamespace")
74+
vim.cmd("highlight default TitleHighlight guifg=#787878")
75+
76+
-- Set empty title if applicable
77+
if v.count == 0 then
78+
vim.api.nvim_buf_set_lines(v.bufnr, 0, 1, false, { v.title })
79+
local linnr = 1
80+
vim.api.nvim_buf_set_extmark(
81+
v.bufnr,
82+
ns_id,
83+
linnr - 1,
84+
0,
85+
{ end_row = linnr - 1, end_col = string.len(v.title), hl_group = "TitleHighlight" }
86+
)
87+
end
6488
end
6589
end
6690
end

lua/gitlab/actions/discussions/init.lua

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ local M = {
3737
---@param callback function|nil
3838
M.load_discussions = function(callback)
3939
job.run_job("/mr/discussions/list", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
40-
state.DISCUSSION_DATA.discussions = data.discussions ~= vim.NIL and data.discussions or {}
41-
state.DISCUSSION_DATA.unlinked_discussions = data.unlinked_discussions ~= vim.NIL and data.unlinked_discussions
42-
or {}
43-
state.DISCUSSION_DATA.emojis = data.emojis ~= vim.NIL and data.emojis or {}
40+
state.DISCUSSION_DATA.discussions = u.ensure_table(data.discussions)
41+
state.DISCUSSION_DATA.unlinked_discussions = u.ensure_table(data.unlinked_discussions)
42+
state.DISCUSSION_DATA.emojis = u.ensure_table(data.emojis)
4443
if type(callback) == "function" then
4544
callback()
4645
end
@@ -91,12 +90,11 @@ end
9190

9291
--- Take existing data and refresh the diagnostics, the winbar, and the signs
9392
M.refresh_view = function()
94-
if state.settings.discussion_signs.enabled and state.DISCUSSION_DATA then
93+
if state.settings.discussion_signs.enabled then
9594
diagnostics.refresh_diagnostics()
9695
end
97-
if M.split_visible and state.DISCUSSION_DATA then
98-
winbar.update_winbar()
99-
end
96+
winbar.update_winbar()
97+
common.add_empty_titles()
10098
end
10199

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

111-
if
112-
type(state.DISCUSSION_DATA.discussions) ~= "table"
113-
and type(state.DISCUSSION_DATA.unlinked_discussions) ~= "table"
114-
and type(state.DISCUSSION_DATA.draft_notes) ~= "table"
115-
then
116-
u.notify("No discussions, notes, or draft notes for this MR", vim.log.levels.WARN)
117-
if M.split ~= nil then
118-
vim.api.nvim_buf_set_lines(M.split.bufnr, 0, -1, false, { "" })
119-
end
120-
return
121-
end
109+
state.DISCUSSION_DATA.discussions = u.ensure_table(state.DISCUSSION_DATA.discussions)
110+
state.DISCUSSION_DATA.unlinked_discussions = u.ensure_table(state.DISCUSSION_DATA.unlinked_discussions)
111+
state.DRAFT_NOTES = u.ensure_table(state.DRAFT_NOTES)
122112

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

148-
common.add_empty_titles({
149-
{
150-
bufnr = M.linked_bufnr,
151-
data = state.DISCUSSION_DATA.discussions,
152-
title = "No Discussions for this MR",
153-
},
154-
{
155-
bufnr = M.unlinked_bufnr,
156-
data = state.DISCUSSION_DATA.unlinked_discussions,
157-
title = "No Notes (Unlinked Discussions) for this MR",
158-
},
159-
})
160-
161138
-- Set default buffer
162139
local default_buffer = winbar.bufnr_map[state.settings.discussion_tree.default_view]
163140
vim.api.nvim_set_current_buf(default_buffer)

lua/gitlab/actions/discussions/winbar.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,21 @@ end
7070
---This function updates the winbar
7171
M.update_winbar = function()
7272
local d = require("gitlab.actions.discussions")
73-
local winId = d.split.winid
74-
local c = content()
75-
if vim.wo[winId] then
76-
vim.wo[winId].winbar = c
73+
if d.split == nil then
74+
return
75+
end
76+
77+
local win_id = d.split.winid
78+
if win_id == nil then
79+
return
7780
end
81+
82+
if not vim.api.nvim_win_is_valid(win_id) then
83+
return
84+
end
85+
86+
local c = content()
87+
vim.api.nvim_set_option_value("winbar", c, { scope = "local", win = win_id })
7888
end
7989

8090
---Builds the title string for both sections, using the count of resolvable and draft nodes

lua/gitlab/actions/draft_notes/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local M = {}
2121
---Adds a draft note to the draft notes state, then rebuilds the view
2222
---@param opts AddDraftNoteOpts
2323
M.add_draft_note = function(opts)
24-
local new_draft_notes = state.DRAFT_NOTES
24+
local new_draft_notes = u.ensure_table(state.DRAFT_NOTES)
2525
table.insert(new_draft_notes, opts.draft_note)
2626
state.DRAFT_NOTES = new_draft_notes
2727
local discussions = require("gitlab.actions.discussions")
@@ -153,6 +153,7 @@ M.send_deletion = function(tree)
153153
end
154154

155155
winbar.update_winbar()
156+
common.add_empty_titles()
156157
end)
157158
end
158159

lua/gitlab/indicators/common.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ end
3131
---Filter all discussions which are relevant for currently visible signs and diagnostics.
3232
---@return Discussion|DraftNote[]
3333
M.filter_placeable_discussions = function()
34-
local discussions = state.DISCUSSION_DATA.discussions
34+
local discussions = u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.discussions or {})
3535
if type(discussions) ~= "table" then
3636
discussions = {}
3737
end
3838

39-
local draft_notes = state.DRAFT_NOTES
39+
local draft_notes = u.ensure_table(state.DRAFT_NOTES)
4040
if type(draft_notes) ~= "table" then
4141
draft_notes = {}
4242
end

lua/gitlab/utils/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,4 +759,11 @@ M.trim_slash = function(s)
759759
return (s:gsub("/+$", ""))
760760
end
761761

762+
M.ensure_table = function(data)
763+
if data == vim.NIL or data == nil then
764+
return {}
765+
end
766+
return data
767+
end
768+
762769
return M

0 commit comments

Comments
 (0)