Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fileCommentAnnotations.ts

54 lines1.4 KB

1import type { LineAnnotation, SelectedLineRange } from "@pierre/diffs";
2​
3export interface FileCommentAnnotationEntry {
4 id: string;
5 kind: "draft" | "comment";
6 startLine: number;
7 endLine: number;
8 text: string;
9}
10​
11export interface FileCommentAnnotationGroup {
12 entries: FileCommentAnnotationEntry[];
14​
15export type FileCommentLineAnnotation = LineAnnotation<FileCommentAnnotationGroup>;
16​
17let fileCommentSequence = 0;
18​
19export function nextFileCommentId(): string {
20 fileCommentSequence += 1;
21 return `file-comment-${Date.now()}-${fileCommentSequence}`;
23​
24export 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 };
33​
34export function formatFileCommentRange(startLine: number, endLine: number): string {
35 return startLine === endLine ? `L${startLine}` : `L${startLine} to L${endLine}`;
37​
38export 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 }));