@@ -6,6 +6,7 @@ import type {
66 PullRequestDiffSide ,
77 PullRequestOmittedFileStat ,
88 PullRequestRef ,
9+ PullRequestReviewPosition ,
910 PullRequestReviewThread ,
1011} from "@t3tools/contracts" ;
1112import {
@@ -43,7 +44,11 @@ import {
4344} from "~/lib/diffRendering" ;
4445import { cn } from "~/lib/utils" ;
4546import { createPullRequestDiffFileContentsLoader } from "~/lib/diffFileContents" ;
46- import { buildDiffReviewComment , type ReviewCommentContext } from "~/reviewCommentContext" ;
47+ import {
48+ buildDiffReviewComment ,
49+ resolveDiffReviewPosition ,
50+ type ReviewCommentContext ,
51+ } from "~/reviewCommentContext" ;
4752import { pullRequestEnvironment } from "~/state/pullRequests" ;
4853import { useEnvironmentQuery } from "~/state/query" ;
4954import { useAtomCommand } from "~/state/use-atom-command" ;
@@ -129,8 +134,7 @@ interface DraftAnchor {
129134 readonly path : string ;
130135 /** What the file was called before the change, for the hosts that resolve a position by both. */
131136 readonly oldPath : string | null ;
132- readonly line : number ;
133- readonly side : PullRequestDiffSide ;
137+ readonly position : PullRequestReviewPosition ;
134138 /** The whole selection, which the comment collapses to one line but a question keeps. */
135139 readonly range : SelectedLineRange ;
136140}
@@ -148,8 +152,21 @@ function toViewerSide(side: PullRequestDiffSide) {
148152 return side === "left" ? ( "deletions" as const ) : ( "additions" as const ) ;
149153}
150154
151- function fromViewerSide ( side : string | undefined ) : PullRequestDiffSide {
152- return side === "deletions" ? "left" : "right" ;
155+ function getReviewPositionAnchor ( position : PullRequestReviewPosition ) : {
156+ line : number ;
157+ side : PullRequestDiffSide ;
158+ } {
159+ switch ( position . kind ) {
160+ case "added" :
161+ return { line : position . newLine , side : "right" } ;
162+ case "deleted" :
163+ return { line : position . oldLine , side : "left" } ;
164+ case "context" :
165+ return {
166+ line : position . side === "left" ? position . oldLine : position . newLine ,
167+ side : position . side ,
168+ } ;
169+ }
153170}
154171
155172/**
@@ -442,10 +459,14 @@ export function PullRequestCodeTab({
442459 if ( commit === null ) {
443460 for ( const comment of pendingComments ) {
444461 if ( comment . path !== path ) continue ;
445- groupAt ( comment . side , comment . line ) . pending . push ( comment ) ;
462+ const anchor = getReviewPositionAnchor ( comment . position ) ;
463+ groupAt ( anchor . side , anchor . line ) . pending . push ( comment ) ;
446464 }
447465 }
448- if ( draft ?. fileKey === fileKey ) groupAt ( draft . side , draft . line ) . draft = true ;
466+ if ( draft ?. fileKey === fileKey ) {
467+ const anchor = getReviewPositionAnchor ( draft . position ) ;
468+ groupAt ( anchor . side , anchor . line ) . draft = true ;
469+ }
449470
450471 const collapsed = isFileDiffCollapsed ( fileKey , foldOverride , toggledFiles ) ;
451472
@@ -594,12 +615,13 @@ export function PullRequestCodeTab({
594615 // that silently lost its first line on the other hosts would be worse than one line.
595616 const path = resolveFileDiffPath ( file ) ;
596617 const previousPath = resolveFileDiffPreviousPath ( file ) ;
618+ const position = resolveDiffReviewPosition ( file , range . end , range . endSide ?? range . side ) ;
619+ if ( position === null ) return ;
597620 setDraft ( {
598621 fileKey : item . id ,
599622 path,
600623 oldPath : previousPath === path ? null : previousPath ,
601- line : range . end ,
602- side : fromViewerSide ( range . endSide ?? range . side ) ,
624+ position,
603625 range,
604626 } ) ;
605627 } ,
@@ -842,7 +864,7 @@ export function PullRequestCodeTab({
842864 { annotation . metadata . draft && draft ? (
843865 < DiffCommentAnnotation
844866 kind = "draft"
845- rangeLabel = { `${ draft . path } :${ draft . line } ` }
867+ rangeLabel = { `${ draft . path } :${ getReviewPositionAnchor ( draft . position ) . line } ` }
846868 text = ""
847869 submitLabel = "Add to review"
848870 { ...( onAskAboutSelection
@@ -864,8 +886,7 @@ export function PullRequestCodeTab({
864886 id : nextPendingReviewCommentId ( ) ,
865887 path : draft . path ,
866888 ...( draft . oldPath === null ? { } : { oldPath : draft . oldPath } ) ,
867- line : draft . line ,
868- side : draft . side ,
889+ position : draft . position ,
869890 body,
870891 } ) ;
871892 setDraft ( null ) ;
0 commit comments