| 1 | import type { LineAnnotation, SelectedLineRange } from "@pierre/diffs"; |
| 2 | |
| 3 | export interface FileCommentAnnotationEntry { |
| 4 | id: string; |
| 5 | kind: "draft" | "comment"; |
| 6 | startLine: number; |
| 7 | endLine: number; |
| 8 | text: string; |
| 9 | } |
| 10 | |
| 11 | export interface FileCommentAnnotationGroup { |
| 12 | entries: FileCommentAnnotationEntry[]; |
| 13 | } |
| 14 | |
| 15 | export type FileCommentLineAnnotation = LineAnnotation<FileCommentAnnotationGroup>; |
| 16 | |
| 17 | let fileCommentSequence = 0; |
| 18 | |
| 19 | export function nextFileCommentId(): string { |
| 20 | fileCommentSequence += 1; |
| 21 | return `file-comment-${Date.now()}-${fileCommentSequence}`; |
| 22 | } |
| 23 | |
| 24 | export function normalizeFileCommentRange(range: SelectedLineRange): { |
| 25 | startLine: number; |
| 26 | endLine: number; |
| 27 | } { |
| 28 | return { |
| 29 | startLine: Math.min(range.start, range.end), |
| 30 | endLine: Math.max(range.start, range.end), |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | export function formatFileCommentRange(startLine: number, endLine: number): string { |
| 35 | return startLine === endLine ? `L${startLine}` : `L${startLine} to L${endLine}`; |
| 36 | } |
| 37 | |
| 38 | export function remapFileCommentAnnotations( |
| 39 | annotations: ReadonlyArray<FileCommentLineAnnotation>, |
| 40 | ): FileCommentLineAnnotation[] { |
| 41 | return annotations.map((annotation) => ({ |
| 42 | ...annotation, |
| 43 | metadata: { |
| 44 | entries: annotation.metadata.entries.map((entry) => { |
| 45 | const lineCount = entry.endLine - entry.startLine; |
| 46 | return { |
| 47 | ...entry, |
| 48 | endLine: annotation.lineNumber, |
| 49 | startLine: Math.max(1, annotation.lineNumber - lineCount), |
| 50 | }; |
| 51 | }), |
| 52 | }, |
| 53 | })); |
| 54 | } |