The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { LineAnnotation, SelectedLineRange } from "@pierre/diffs"; |
| 2 | import { msg } from "gt-react"; |
| 3 | import { m, type MessageResolver } from "~/i18n/messages"; |
| 4 | |
| 5 | export interface FileCommentAnnotationEntry { |
| 6 | id: string; |
| 7 | kind: "draft" | "comment"; |
| 8 | startLine: number; |
| 9 | endLine: number; |
| 10 | text: string; |
| 11 | } |
| 12 | |
| 13 | export interface FileCommentAnnotationGroup { |
| 14 | entries: FileCommentAnnotationEntry[]; |
| 15 | } |
| 16 | |
| 17 | export type FileCommentLineAnnotation = LineAnnotation<FileCommentAnnotationGroup>; |
| 18 | |
| 19 | let fileCommentSequence = 0; |
| 20 | |
| 21 | export function nextFileCommentId(): string { |
| 22 | fileCommentSequence += 1; |
| 23 | return `file-comment-${Date.now()}-${fileCommentSequence}`; |
| 24 | } |
| 25 | |
| 26 | export function normalizeFileCommentRange(range: SelectedLineRange): { |
| 27 | startLine: number; |
| 28 | endLine: number; |
| 29 | } { |
| 30 | return { |
| 31 | startLine: Math.min(range.start, range.end), |
| 32 | endLine: Math.max(range.start, range.end), |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | export function formatFileCommentRange( |
| 37 | startLine: number, |
| 38 | endLine: number, |
| 39 | resolve: MessageResolver = m, |
| 40 | ): string { |
| 41 | return startLine === endLine |
| 42 | ? `L${startLine}` |
| 43 | : resolve(msg("L{start} to L{end}", { start: String(startLine), end: String(endLine) })); |
| 44 | } |
| 45 | |
| 46 | export function remapFileCommentAnnotations( |
| 47 | annotations: ReadonlyArray<FileCommentLineAnnotation>, |
| 48 | ): FileCommentLineAnnotation[] { |
| 49 | return annotations.map((annotation) => ({ |
| 50 | ...annotation, |
| 51 | metadata: { |
| 52 | entries: annotation.metadata.entries.map((entry) => { |
| 53 | const lineCount = entry.endLine - entry.startLine; |
| 54 | return { |
| 55 | ...entry, |
| 56 | endLine: annotation.lineNumber, |
| 57 | startLine: Math.max(1, annotation.lineNumber - lineCount), |
| 58 | }; |
| 59 | }), |
| 60 | }, |
| 61 | })); |
| 62 | } |
| 63 | |
| 64 | /** Drafts and the comments still in the prompt; sent or removed ones are not shown or re-added. */ |
| 65 | export function promptFileCommentEntries( |
| 66 | entries: ReadonlyArray<FileCommentAnnotationEntry>, |
| 67 | promptCommentIds: ReadonlySet<string>, |
| 68 | ): FileCommentAnnotationEntry[] { |
| 69 | return entries.filter((entry) => entry.kind === "draft" || promptCommentIds.has(entry.id)); |
| 70 | } |