Skip to content

Commit

Permalink
feat: open multiple comments on 1 line
Browse files Browse the repository at this point in the history
  • Loading branch information
noortw01 committed Dec 16, 2024
1 parent ca1332c commit 71a0d09
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lua/adopure/quickfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function M.render_quickfix(pull_request_threads, _)
filename = file_path.filename,
lnum = vim.F.if_nil(context.rightFileStart.line, 1),
col = vim.F.if_nil(context.rightFileStart.offset, 1),
text = "[" .. pull_request_thread.status .. "] - " .. pull_request_thread.comments[1].content,
text = require("adopure.utils").pull_request_thread_title(pull_request_thread),
}
table.insert(entries, entry)
end
Expand Down
54 changes: 40 additions & 14 deletions lua/adopure/thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,56 @@ end
---@class adopure.OpenThreadWindowOpts
---@field thread_id number|nil

---@param opts adopure.OpenThreadWindowOpts
---@return number[]
local function _get_extmark_ids(opts)
if opts.thread_id then
return { opts.thread_id }
end
return vim.iter(require("adopure.marker").get_extmarks_at_position())
:map(function(extmark)
return extmark[1]
end)
:totable()
end

---Open an existing comment thread in a window.
---Can be called if there is an extmark indicating an available comment thread.
---@param opts adopure.OpenThreadWindowOpts
function M.open_thread_window(state, opts)
local extmark_id = opts.thread_id
if not extmark_id then
local extmarks = require("adopure.marker").get_extmarks_at_position()
local first_extmark = extmarks[1]
extmark_id = first_extmark[1]
end
local extmark_ids = _get_extmark_ids(opts)

---@type adopure.Thread|nil
local thread_to_open
for _, pull_request_thread in pairs(state.pull_request_threads) do
if pull_request_thread.id == extmark_id then
thread_to_open = pull_request_thread
---@param pull_request_thread adopure.Thread|nil
local function _render_thread_window(pull_request_thread)
if not pull_request_thread then
vim.notify("Did not choose thread to open;", 3)
return
end
local bufnr, mark_id = require("adopure.render").render_reply_thread(pull_request_thread)
add_comment_reply(bufnr, mark_id, state, pull_request_thread)
end
if not thread_to_open then

---@type adopure.Thread[]
local threads_to_open = vim.iter(state.pull_request_threads)
:filter(function(pull_request_thread)
return vim.iter(extmark_ids):any(function(extmark_id)
return extmark_id == pull_request_thread.id
end)
end)
:totable()
if #threads_to_open == 0 then
vim.notify("Did not find thread to open;", 3)
return
end
local bufnr, mark_id = require("adopure.render").render_reply_thread(thread_to_open)
add_comment_reply(bufnr, mark_id, state, thread_to_open)
if #threads_to_open == 1 then
_render_thread_window(threads_to_open[1])
return
end

vim.ui.select(threads_to_open, {
prompt = "Select pull request thread to open;",
format_item = require("adopure.utils").pull_request_thread_title,
}, _render_thread_window)
end

---@param bufnr number
Expand Down
14 changes: 14 additions & 0 deletions lua/adopure/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ function M.await_result(job)
end
end

--- Create pull_request_thread descriptive line
--- @param pull_request_thread adopure.Thread
--- @return string
function M.pull_request_thread_title(pull_request_thread)
return table.concat({
"[",
pull_request_thread.id,
" - ",
pull_request_thread.status,
"] ",
pull_request_thread.comments[1].content,
})
end

return M

0 comments on commit 71a0d09

Please sign in to comment.