Open History to see the last commit
1import type { LineAnnotation, SelectedLineRange } from "@pierre/diffs";
2import { msg } from "gt-react";
3import { m, type MessageResolver } from "~/i18n/messages";
4
5export interface FileCommentAnnotationEntry {
6 id: string;
7 kind: "draft" | "comment";
8 startLine: number;
9 endLine: number;
10 text: string;
11}
12
13export interface FileCommentAnnotationGroup {
14 entries: FileCommentAnnotationEntry[];
15}
16
17export type FileCommentLineAnnotation = LineAnnotation<FileCommentAnnotationGroup>;
18
19let fileCommentSequence = 0;
20
21export function nextFileCommentId(): string {
22 fileCommentSequence += 1;
23 return `file-comment-${Date.now()}-${fileCommentSequence}`;
24}
25
26export 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
36export 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
46export 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. */
65export 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}