Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lua/gitlab/actions/discussions/winbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ local function content()
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)
local inline_draft_notes, unlinked_draft_notes = List.new(state.DRAFT_NOTES):partition(function(note)
if note.discussion_id == "" then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing but since this is an early return, it's clearer in my opinion if you remove the else block. Meaning:

if some_cond then
   return some_result
end

-- Do other work

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sure. It's indeed clearer. Fixed.

return draft_notes.has_position(note)
end
for _, discussion in ipairs(state.DISCUSSION_DATA.unlinked_discussions) do
if discussion.id == note.discussion_id then
return false
end
end
return true
end)

local t = {
Expand Down
17 changes: 17 additions & 0 deletions lua/gitlab/utils/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ function List:filter(func)
return result
end

---Partitions a given list into two lists
---@generic T
---@param func fun(v: T, i: integer):boolean
---@return List<T>, List<T> @Returns two lists: the 1st with elements for which func returns true, the 2nd with elements for which it returns false
function List:partition(func)
local result_true = List.new()
local result_false = List.new()
for i, v in ipairs(self) do
if func(v, i) == true then
table.insert(result_true, v)
else
table.insert(result_false, v)
end
end
return result_true, result_false
end

function List:reduce(func, agg)
for i, v in ipairs(self) do
agg = func(agg, v, i)
Expand Down
Loading