Skip to content

Commit 2c33c7d

Browse files
committed
feat(diff): Enhance hidden comment expansion when sharing link
1 parent 4b85c50 commit 2c33c7d

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

routers/web/repo/compare.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,20 +987,26 @@ func ExcerptBlob(ctx *context.Context) {
987987
for _, line := range section.Lines {
988988
if line.Type == gitdiff.DiffLineSection && line.SectionInfo != nil {
989989
hiddenCommentCount := 0
990+
var hiddenCommentIDs []int64
990991
// Check if there are comments in the hidden range
991-
for commentLineNum := range lineComments {
992+
for commentLineNum, comments := range lineComments {
992993
absLineNum := commentLineNum
993994
if absLineNum < 0 {
994995
absLineNum = -absLineNum
995996
}
996997
// Count comments that are in the hidden range
997998
if int(absLineNum) > line.SectionInfo.LastRightIdx && int(absLineNum) < line.SectionInfo.RightIdx {
998999
hiddenCommentCount++
1000+
// Collect comment IDs
1001+
for _, comment := range comments {
1002+
hiddenCommentIDs = append(hiddenCommentIDs, comment.ID)
1003+
}
9991004
}
10001005
}
10011006
if hiddenCommentCount > 0 {
10021007
line.HasHiddenComments = true
10031008
line.HiddenCommentCount = hiddenCommentCount
1009+
line.HiddenCommentIDs = hiddenCommentIDs
10041010
}
10051011
}
10061012
}

services/gitdiff/gitdiff.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ type DiffLine struct {
8888
Content string
8989
Comments issues_model.CommentList // related PR code comments
9090
SectionInfo *DiffLineSectionInfo
91-
HasHiddenComments bool // indicates if this expand button has comments in hidden lines
92-
HiddenCommentCount int // number of hidden comments in this section
91+
HasHiddenComments bool // indicates if this expand button has comments in hidden lines
92+
HiddenCommentCount int // number of hidden comments in this section
93+
HiddenCommentIDs []int64 // IDs of hidden comments in this section
9394
}
9495

9596
// DiffLineSectionInfo represents diff line section meta data
@@ -491,19 +492,25 @@ func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, c
491492
// Mark expand buttons that have comments in hidden lines
492493
if line.Type == DiffLineSection && line.SectionInfo != nil {
493494
hiddenCommentCount := 0
495+
var hiddenCommentIDs []int64
494496
// Check if there are comments in the hidden range
495-
for commentLineNum := range lineCommits {
497+
for commentLineNum, comments := range lineCommits {
496498
absLineNum := int(commentLineNum)
497499
if commentLineNum < 0 {
498500
absLineNum = int(-commentLineNum)
499501
}
500502
if absLineNum > line.SectionInfo.LastRightIdx && absLineNum < line.SectionInfo.RightIdx {
501503
hiddenCommentCount++
504+
// Collect comment IDs
505+
for _, comment := range comments {
506+
hiddenCommentIDs = append(hiddenCommentIDs, comment.ID)
507+
}
502508
}
503509
}
504510
if hiddenCommentCount > 0 {
505511
line.HasHiddenComments = true
506512
line.HiddenCommentCount = hiddenCommentCount
513+
line.HiddenCommentIDs = hiddenCommentIDs
507514
}
508515
}
509516
}

templates/repo/diff/blob_excerpt.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{$blobExcerptLink := print $.RepoLink (Iif $.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.AfterCommitID) "?" (Iif $.PageIsPullFiles (print "is_pull=true&issue_index=" $.IssueIndex "&") "")}}
1+
{{$blobExcerptLink := print $.RepoLink (Iif $.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.AfterCommitID) (QueryBuild "?" "anchor" $.Anchor) (Iif $.PageIsPullFiles (print "&is_pull=true&issue_index=" $.IssueIndex) "")}}
22
{{if $.IsSplitStyle}}
33
{{range $k, $line := $.section.Lines}}
44
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded" data-line-type="{{.GetHTMLDiffLineType}}">
@@ -87,19 +87,19 @@
8787
<td colspan="2" class="lines-num">
8888
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
8989
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
90-
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=down" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}">
90+
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=down" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}"{{if $line.HiddenCommentIDs}} data-hidden-comment-ids="{{StringUtils.ToString $line.HiddenCommentIDs}}"{{end}}>
9191
{{svg "octicon-fold-down"}}
9292
{{if $line.HasHiddenComments}}<span class="ui mini label">{{$line.HiddenCommentCount}}</span>{{end}}
9393
</button>
9494
{{end}}
9595
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
96-
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=up" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}">
96+
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=up" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}"{{if $line.HiddenCommentIDs}} data-hidden-comment-ids="{{StringUtils.ToString $line.HiddenCommentIDs}}"{{end}}>
9797
{{svg "octicon-fold-up"}}
9898
{{if $line.HasHiddenComments}}<span class="ui mini label">{{$line.HiddenCommentCount}}</span>{{end}}
9999
</button>
100100
{{end}}
101101
{{if eq $expandDirection 2}}
102-
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}">
102+
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}"{{if $line.HiddenCommentIDs}} data-hidden-comment-ids="{{StringUtils.ToString $line.HiddenCommentIDs}}"{{end}}>
103103
{{svg "octicon-fold"}}
104104
{{if $line.HasHiddenComments}}<span class="ui mini label">{{$line.HiddenCommentCount}}</span>{{end}}
105105
</button>

templates/repo/diff/section_unified.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
<td colspan="2" class="lines-num">
1919
<div class="code-expander-buttons" data-expand-direction="{{$expandDirection}}">
2020
{{if or (eq $expandDirection 3) (eq $expandDirection 5)}}
21-
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=down&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}">
21+
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=down&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}"{{if $line.HiddenCommentIDs}} data-hidden-comment-ids="{{StringUtils.ToString $line.HiddenCommentIDs}}"{{end}}>
2222
{{svg "octicon-fold-down"}}
2323
{{if $line.HasHiddenComments}}<span class="ui mini label">{{$line.HiddenCommentCount}}</span>{{end}}
2424
</button>
2525
{{end}}
2626
{{if or (eq $expandDirection 3) (eq $expandDirection 4)}}
27-
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=up&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}">
27+
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&direction=up&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}"{{if $line.HiddenCommentIDs}} data-hidden-comment-ids="{{StringUtils.ToString $line.HiddenCommentIDs}}"{{end}}>
2828
{{svg "octicon-fold-up"}}
2929
{{if $line.HasHiddenComments}}<span class="ui mini label">{{$line.HiddenCommentCount}}</span>{{end}}
3030
</button>
3131
{{end}}
3232
{{if eq $expandDirection 2}}
33-
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}">
33+
<button class="code-expander-button{{if $line.HasHiddenComments}} has-hidden-comments{{end}}" hx-target="closest tr" hx-get="{{$blobExcerptLink}}&{{$line.GetBlobExcerptQuery}}&style=unified&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}" title="{{if $line.HasHiddenComments}}{{$line.HiddenCommentCount}} hidden comment(s){{end}}"{{if $line.HiddenCommentIDs}} data-hidden-comment-ids="{{StringUtils.ToString $line.HiddenCommentIDs}}"{{end}}>
3434
{{svg "octicon-fold"}}
3535
{{if $line.HasHiddenComments}}<span class="ui mini label">{{$line.HiddenCommentCount}}</span>{{end}}
3636
</button>

web_src/js/features/repo-diff.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ async function loadUntilFound() {
233233
return;
234234
}
235235

236+
// If looking for a comment, try to expand the section that contains it
237+
if (hashTargetSelector.startsWith('#issuecomment-')) {
238+
const commentId = hashTargetSelector.substring('#issuecomment-'.length);
239+
const expandButton = findExpandButtonForComment(commentId);
240+
if (expandButton) {
241+
expandButton.click();
242+
// Wait for HTMX to load the content
243+
await new Promise((resolve) => setTimeout(resolve, 500));
244+
continue; // Try again to find the element
245+
}
246+
}
247+
236248
// the button will be refreshed after each "load more", so query it every time
237249
const showMoreButton = document.querySelector('#diff-show-more-files');
238250
if (!showMoreButton) {
@@ -245,6 +257,23 @@ async function loadUntilFound() {
245257
}
246258
}
247259

260+
// Find the expand button that contains the target comment ID
261+
function findExpandButtonForComment(commentId: string): HTMLElement | null {
262+
const expandButtons = document.querySelectorAll('.code-expander-button[data-hidden-comment-ids]');
263+
for (const button of expandButtons) {
264+
const hiddenIds = button.getAttribute('data-hidden-comment-ids');
265+
if (hiddenIds) {
266+
// Parse the comma-separated list of IDs
267+
const ids = hiddenIds.replace(/[[\]]/g, '').split(' ').filter(Boolean);
268+
if (ids.includes(commentId)) {
269+
return button as HTMLElement;
270+
}
271+
}
272+
}
273+
274+
return null;
275+
}
276+
248277
function initRepoDiffHashChangeListener() {
249278
window.addEventListener('hashchange', loadUntilFound);
250279
loadUntilFound();

0 commit comments

Comments
 (0)