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
15 changes: 10 additions & 5 deletions doc/guh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ R Refresh the current thing. In a PR-related buffer, refreshes all
dd View PR diff + comments. |guh-diff|
<Plug>(guh-diff)

dz Toggle resolved comment threads in the PR diff. Hidden by default,
and after "R" (refresh). |guh-diff|
<Plug>(guh-resolved)

dl View the most-recent CI logs for all job-names in the CI matrix.
<Plug>(guh-logs)

Expand Down Expand Up @@ -188,7 +192,7 @@ View PR diff *guh-diff*
`dd` on a PR view opens the diff + comments in 'scrollbind' split windows.
Comments are also provided as diagnostics (and loaded into the quickfix list).

- Only UNRESOLVED comments are shown.
- RESOLVED comments are hidden; "dz" toggles them.
- Comments on VIEWED files are never shown, even if Unresolved.
- Unresolved comments on OUTDATED code (i.e. code from an old push), are
anchored to pseudo-diff sections above the "current" PR diff, with
Expand All @@ -206,8 +210,8 @@ PR diff:
- Shows VIEWED files as a single line (diff/comments not loaded): >
(viewed) path/to/file
<
- The diff overlay shows: >
PR diff (Viewed files: 3/12, Resolved comments: 4/20).
- The 'winbar' shows: >
PR DIFF | Files: 12 (Viewed: 3) | Unresolved: 5 | Resolved: 4 (hidden)
<
- Refresh ("R") to update after marking/unmarking a file as VIEWED.

Expand All @@ -220,7 +224,8 @@ PR comments:
comment bodies in-order. If a comment can't fit, it shows `(truncated)`. The
full comment is always accessible via the diagnostic peek (`<C-w>d`) or the
quickfix list.
- as diagnostics on the diff buffer (severity INFO, source "guh.nvim"):
- as diagnostics on the diff buffer (severity INFO, or HINT for resolved
threads; source "guh.nvim"):
- Navigate using `vim.diagnostic.jump` (|]d| |[d|).
- See comment details using `<C-w>d` |CTRL-W_d-default|.
- in the quickfix list ("Guh comments") via |vim.diagnostic.setqflist()|.
Expand All @@ -234,7 +239,7 @@ Comment on issues, PRs, and diffs *guh-comment*

`cr` on a comment (thread), presents a menu:
1. Reply
2. Resolve
2. Resolve/Unresolve

See |:GuhComment|.

Expand Down
39 changes: 24 additions & 15 deletions lua/guh/comments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,12 @@ function M.load_pr_comments(id, repo, diff_buf, pr_data, comments_list)

local thread_comments = thread.comments
or { { user = thread.user, updated_at = thread.updated_at, body = thread.body } }
local tag = filename:match(outdated_prefix_pat) and '(outdated) '
or filename:match(outside_prefix_pat) and '(outside) '
or ''
local tag = ('%s%s'):format(
thread_comments[1].resolved and '(resolved) ' or '',
filename:match(outdated_prefix_pat) and '(outdated) '
or filename:match(outside_prefix_pat) and '(outside) '
or ''
)

local comment_blocks = entries[start_idx] or {}
for ci, c in ipairs(thread_comments) do
Expand Down Expand Up @@ -319,7 +322,7 @@ function M.load_pr_comments(id, repo, diff_buf, pr_data, comments_list)
col = 0,
end_col = 0,
message = body,
severity = severity.INFO,
severity = thread_comments[1].resolved and severity.HINT or severity.INFO,
source = 'guh.nvim',
user_data = { comments = thread_comments },
})
Expand Down Expand Up @@ -408,6 +411,7 @@ function M.load_pr_comments(id, repo, diff_buf, pr_data, comments_list)
vim.api.nvim_buf_call(buf, function()
vim.cmd([[syntax match GuhHeading /^▎ \zs.*$/ containedin=ALL]])
vim.cmd([[syntax match GuhWarning /(outdated)\|(outside)\|(truncated)/ containedin=ALL]])
vim.cmd([[syntax match GuhDimmed /(resolved)/ containedin=ALL]])
end)

return buf
Expand Down Expand Up @@ -525,6 +529,7 @@ end
--- (Example: "outdated-3271868956:runtime/lua/vim/_core/options.lua")
--- - "outside" threads (anchored outside of the PR diff).
--- - Collapses VIEWED files.
--- - Hides resolved threads, unless `pr_data.show_resolved`.
--- - Groups comments into per-file threads (`to_threads`).
---
--- @param pr_data PullRequest Note: `raw_comments` is mutated in-place to rewrite paths.
Expand All @@ -538,18 +543,21 @@ function M.render_diff(pr_data, diff_stdout)
local diff_lines, n_files = prepare_pr_diff(diff_stdout, viewed)
local line_map = to_line_map(diff_lines)
local viewed_threads = {} ---@type table<integer, true>
for _, c in ipairs(pr_data.raw_comments) do
local shown = vim.tbl_filter(function(c)
return pr_data.show_resolved or not c.resolved
end, pr_data.raw_comments)
for _, c in ipairs(shown) do
-- Anchor thread to an off-diff quasi-file when: `isOutdated` (GitHub flag), or outside the diff
-- and not in "Viewed" file. "outside" if it anchors to current-HEAD, else "outdated".
if c.thread_id and (c.outdated or (not viewed[c.path] and not in_diff(line_map, c))) then
c.path = ('%s-%d:%s'):format(c.in_head and 'outside' or 'outdated', c.thread_id, c.path)
end
-- Count threads hidden by "viewed". After the `c.path` assignment above, it matches how `viewed` is keyed.
if viewed[c.path] and c.thread_id then
-- Count unresolved threads hidden by "viewed". After the `c.path` rewrite above, it matches how `viewed` is keyed.
if viewed[c.path] and c.thread_id and not c.resolved then
viewed_threads[c.thread_id] = true
end
end
local threads = M.to_threads(pr_data.raw_comments)
local threads = M.to_threads(shown)
local lines = vim
.iter({
to_offdiff_section(threads, 'outdated', viewed),
Expand Down Expand Up @@ -726,7 +734,7 @@ function M.update_comment(linenr)
end)
end

--- Acts on a comment thread (Reply or Resolve).
--- Acts on a comment thread (Reply, Resolve/Unresolve).
---
--- @param linenr integer 1-indexed line number.
function M.reply_or_resolve(linenr)
Expand All @@ -744,7 +752,8 @@ function M.reply_or_resolve(linenr)
if not c then
return
end
local action = util.await_select({ 'Reply', 'Resolve' }, {
local resolve = not c.resolved
local action = util.await_select({ 'Reply', resolve and 'Resolve' or 'Unresolve' }, {
prompt = ('Thread on %s:%d:'):format(c.path or '?', c.end_line or 0),
})
if action == 'Reply' then
Expand All @@ -760,17 +769,17 @@ function M.reply_or_resolve(linenr)
else
progress('failed', nil, 'Failed to send reply.')
end
elseif action == 'Resolve' then
elseif action == 'Resolve' or action == 'Unresolve' then
if not c.thread_node_id then
return util.msg('Missing thread_node_id (refresh and retry)', vim.log.levels.WARN)
end
local progress = util.new_progress_report('Resolving thread...', buf)
local resp = async.await(2, gh.resolve_thread, c.thread_node_id)
local progress = util.new_progress_report(('%s thread...'):format(resolve and 'Resolving' or 'Unresolving'), buf)
local resp = async.await(3, gh.resolve_thread, c.thread_node_id, resolve)
if resp['errors'] == nil then
progress('success', nil, 'Thread resolved.')
progress('success', nil, ('Thread %s.'):format(resolve and 'resolved' or 'unresolved'))
require('guh.pr').refresh({ feat = 'pr', id = prnum, repo = repo })
else
progress('failed', nil, 'Failed to resolve thread.')
progress('failed', nil, ('Failed to %s thread.'):format(action:lower()))
end
end
end)
Expand Down
107 changes: 53 additions & 54 deletions lua/guh/gh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,53 +135,50 @@ local function flatten_threads_to_comments(threads)
local out = {}
local n_resolved = 0
for _, thread in ipairs(threads or {}) do
if thread.isResolved then
-- Drop resolved threads entirely.
n_resolved = n_resolved + 1
else
local nodes = vim.tbl_get(thread, 'comments', 'nodes') or {}
local thread_id = nodes[1] and nodes[1].databaseId
-- GraphQL global node id (e.g. "PRRT_kw…"). Needed by the resolveReviewThread mutation.
local thread_node_id = thread.id
-- Reply comments on outdated threads carry no originalLine of their own;
-- inherit from the head comment so they aren't filtered out below.
local head_line = nodes[1] and nodes[1].originalLine
local head_start = nodes[1] and nodes[1].originalStartLine
local head_path = nodes[1] and nodes[1].path
-- Thread-level: head comment anchors to current-HEAD. Decides "outside" vs "outdated" (no HEAD line).
local in_head = (nodes[1] and nodes[1].line) ~= nil
-- `diffSide` is a thread-level property in GraphQL; every comment in the thread inherits it.
local c_side = thread.diffSide
for _, c in ipairs(nodes) do
local c_path = c.path ~= nil and c.path or head_path
-- LEFT-side (deleted-line) comments, and outdated threads, have a null `line` on HEAD; fallback to `originalLine` then.
local effective_end_line = c.line ~= nil and c.line or c.originalLine ~= nil and c.originalLine or head_line
local effective_start = c.startLine ~= nil and c.startLine
or c.originalStartLine ~= nil and c.originalStartLine
or head_start
if effective_end_line ~= nil and c_path ~= nil then
local reply_to
if c.replyTo ~= nil and c.replyTo.databaseId then
reply_to = c.replyTo.databaseId
end
table.insert(out, {
id = c.databaseId,
html_url = c.url,
user = { login = c.author ~= nil and c.author.login or '?' },
body = c.body or '',
diff_hunk = c.diffHunk or '',
path = c_path,
end_line = effective_end_line,
start_line = effective_start,
side = c_side,
updated_at = c.updatedAt,
in_reply_to_id = reply_to,
outdated = thread.isOutdated or false,
in_head = in_head,
thread_id = thread_id,
thread_node_id = thread_node_id,
})
n_resolved = n_resolved + (thread.isResolved and 1 or 0)
local nodes = vim.tbl_get(thread, 'comments', 'nodes') or {}
local thread_id = nodes[1] and nodes[1].databaseId
-- GraphQL global node id (e.g. "PRRT_kw…"). Needed by the resolveReviewThread mutation.
local thread_node_id = thread.id
-- Reply comments on outdated threads carry no originalLine of their own;
-- inherit from the head comment so they aren't filtered out below.
local head_line = nodes[1] and nodes[1].originalLine
local head_start = nodes[1] and nodes[1].originalStartLine
local head_path = nodes[1] and nodes[1].path
-- Thread-level: head comment anchors to current-HEAD. Decides "outside" vs "outdated" (no HEAD line).
local in_head = (nodes[1] and nodes[1].line) ~= nil
-- `diffSide` is a thread-level property in GraphQL; every comment in the thread inherits it.
local c_side = thread.diffSide
for _, c in ipairs(nodes) do
local c_path = c.path ~= nil and c.path or head_path
-- LEFT-side (deleted-line) comments, and outdated threads, have a null `line` on HEAD; fallback to `originalLine` then.
local effective_end_line = c.line ~= nil and c.line or c.originalLine ~= nil and c.originalLine or head_line
local effective_start = c.startLine ~= nil and c.startLine
or c.originalStartLine ~= nil and c.originalStartLine
or head_start
if effective_end_line ~= nil and c_path ~= nil then
local reply_to
if c.replyTo ~= nil and c.replyTo.databaseId then
reply_to = c.replyTo.databaseId
end
table.insert(out, {
id = c.databaseId,
html_url = c.url,
user = { login = c.author ~= nil and c.author.login or '?' },
body = c.body or '',
diff_hunk = c.diffHunk or '',
path = c_path,
end_line = effective_end_line,
start_line = effective_start,
side = c_side,
updated_at = c.updatedAt,
in_reply_to_id = reply_to,
outdated = thread.isOutdated or false,
resolved = thread.isResolved or false,
in_head = in_head,
thread_id = thread_id,
thread_node_id = thread_node_id,
})
end
end
end
Expand Down Expand Up @@ -304,7 +301,7 @@ end

--- Gets PR data via a single GraphQL query: metadata + comments + per-file "Viewed" state.
---
--- - Drops resolved threads.
--- - Provides a per-comment `resolved` field (see `Comment.resolved`).
--- - Provides a per-comment `outdated` field, so the caller doesn't need to walk the "thread".
--- - Provides a map of `viewed` files.
--- - Threads and files are limited to 100.
Expand Down Expand Up @@ -591,26 +588,28 @@ function M.delete_comment(comment_id, repo, on_done)
end)
end

--- Resolves a review comment-thread.
--- Resolves or unresolves a review comment-thread.
---
--- Note: `thread_node_id` is the GraphQL global node id (e.g. `PRRT_kw…`), not the REST
--- `databaseId`.
---
--- @param thread_node_id string
--- @param cb fun(resp: table)
function M.resolve_thread(thread_node_id, cb)
--- @param resolve boolean false = unresolve.
--- @param on_done fun(resp: table)
function M.resolve_thread(thread_node_id, resolve, on_done)
vim.validate('thread_node_id', thread_node_id, 'string')
local query = [[
local op = resolve and 'resolveReviewThread' or 'unresolveReviewThread'
local query = ([[
mutation($thread:ID!){
resolveReviewThread(input:{threadId:$thread}){
%s(input:{threadId:$thread}){
thread{ id isResolved }
}
}
]]
]]):format(op)
gh_api('resolve_thread', 'POST', 'graphql', {
{ '-F', 'thread=' .. thread_node_id },
{ '-f', 'query=' .. query },
}, cb)
}, on_done)
end

--- Marks a file as "Viewed"/"Unviewed" for the current user on a PR.
Expand Down
15 changes: 15 additions & 0 deletions lua/guh/pr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,21 @@ function M.toggle_viewed()
end
end

--- Toggles rendering of resolved threads in the prdiff/ + prcomments/ buffers.
function M.toggle_resolved()
local _, id, repo = require_pr()
local pr_data, pr_buf = state.get_pr_data(repo, id)
if not pr_data then
return util.msg(('PR #%s not loaded? ("R" to refresh)'):format(id), vim.log.levels.ERROR)
end
local show = not pr_data.show_resolved
state.set_b_key(pr_buf, { 'guh', 'pr_data', 'show_resolved' }, show)
-- Re-render from cache.
M.load_pr({ id = id, repo = repo }, function()
util.msg(('Resolved threads (%d): %s'):format(pr_data.n_resolved or 0, show and 'shown' or 'hidden'))
end)
end

--- Reruns CI for the current PR. Shows a vim.ui.select picker unless `[count]` was given.
---
--- @param opts? { id?: integer|string, repo?: string, args?: string }
Expand Down
4 changes: 4 additions & 0 deletions lua/guh/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ local bufs = {
local info_fields = {
branch = '', -- Non-default base branch to flag (pr).
n_files = 0,
n_resolved = 0,
n_viewed = 0,
n_viewed_threads = 0,
n_visible_threads = 0,
show_resolved = false,
status = '', -- PR state label, or CI job icon (prlogs).
status_hl = '',
title = '', -- PR title, edit prompt, or CI job name (prlogs).
Expand Down Expand Up @@ -402,6 +404,8 @@ function M.update_info(buf)
b.n_viewed = vim.tbl_count(pr.viewed or {})
b.n_viewed_threads = n_viewed_threads
b.n_visible_threads = (pr.n_threads or 0) - (pr.n_resolved or 0) - n_viewed_threads
b.n_resolved = pr.n_resolved or 0
b.show_resolved = pr.show_resolved or false
end
elseif feat == 'prlogs' and pr then
-- `b:guh.id` is the PR number; the job's `databaseId` is the trailing bufname segment. Reuse the
Expand Down
4 changes: 4 additions & 0 deletions lua/guh/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
--- "Info fields: denormalized values for use by 'winbar' (but possibly other consumers later).
--- @field branch string Target branch (if non-default).
--- @field n_files integer Diff file count.
--- @field n_resolved integer Resolved thread count.
--- @field n_viewed integer "Viewed" file count.
--- @field n_viewed_threads integer Unresolved threads hidden in "Viewed" files.
--- @field n_visible_threads integer Unresolved threads visible in the diff.
--- @field show_resolved boolean Resolved threads are rendered (see `PullRequest.show_resolved`).
--- @field status string PR status label ("Open"/"Draft"/…), or CI job icon (prlogs).
--- @field status_hl string Highlight group for `status`.
--- @field title string Feat-specific title.
Expand All @@ -56,6 +58,7 @@
--- @field url string
--- @field user string
--- @field outdated? boolean true if GraphQL marked the thread `isOutdated`.
--- @field resolved? boolean true if GraphQL marked the thread `isResolved`. Hidden unless `PullRequest.show_resolved`.
--- @field in_head? boolean Thread anchors to a current-HEAD line (decides "outside" vs "outdated").
--- @field thread_id? number GraphQL thread id (root comment databaseId).
--- @field thread_node_id? string GraphQL global node id of the thread (e.g. `PRRT_kw…`). Needed by the resolveReviewThread mutation.
Expand Down Expand Up @@ -126,6 +129,7 @@
--- @field reactions { content: string, reactors: { totalCount: integer } }[] Reaction groups (incl. zero-count).
--- @field reviewDecision string
--- @field reviews table
--- @field show_resolved? boolean Render resolved threads ("dz"). Set by `pr.toggle_resolved`.
--- @field state 'OPEN'|'CLOSED'|'MERGED'
--- @field title string
--- @field url string
Expand Down
1 change: 1 addition & 0 deletions lua/guh/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ function M.set_default_keymaps(buf)
M.map_default(buf, 'n', 'R', '<Plug>(guh-refresh)', 'Refresh this guh:// buffer')
M.map_default(buf, 'n', 'gX', '<Plug>(guh-web)', 'Open this view in web browser')
M.map_default(buf, 'n', 'dd', '<Plug>(guh-diff)', 'View the PR diff')
M.map_default(buf, 'n', 'dz', '<Plug>(guh-resolved)', 'Toggle resolved threads in the PR diff')
M.map_default(buf, 'n', 'dl', '<Plug>(guh-logs)', 'View the CI logs for this PR')
M.map_default(buf, 'n', ']f', '<Plug>(guh-next)', 'View the next PR commit / CI job')
M.map_default(buf, 'n', '[f', '<Plug>(guh-prev)', 'View the previous PR commit / CI job')
Expand Down
Loading
Loading