diff --git a/apps/desktop/src/state/diff.test.ts b/apps/desktop/src/state/diff.test.ts new file mode 100644 index 000000000..e857409ea --- /dev/null +++ b/apps/desktop/src/state/diff.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, test } from "bun:test"; +import { parsePatch, QUOTED_LINES, rangeLines, reviewMessage } from "./diff"; + +const patch = parsePatch([ + "@@ -1,3 +1,4 @@", + " one", + "-two", + "+TWO", + "+extra", + " three", + "@@ -10,2 +11,2 @@", + " ten", + " eleven", +]); + +describe("review ranges", () => { + test("a range takes the lines of one side, in order, either way round", () => { + expect(rangeLines(patch, "RIGHT", 1, 3)?.map((l) => l.text)).toEqual(["one", "TWO", "extra"]); + expect(rangeLines(patch, "RIGHT", 3, 1)?.map((l) => l.text)).toEqual(["one", "TWO", "extra"]); + expect(rangeLines(patch, "LEFT", 1, 3)?.map((l) => l.text)).toEqual(["one", "two", "three"]); + }); + + test("a range does not cross a hunk or reach a line the diff lacks", () => { + expect(rangeLines(patch, "RIGHT", 3, 11)).toBeNull(); + expect(rangeLines(patch, "RIGHT", 1, 7)).toBeNull(); + }); + + test("the message names a range and quotes its lines, eliding the rest", () => { + const code = Array.from({ length: QUOTED_LINES + 2 }, (_, i) => `line ${i}`).join("\n"); + const message = reviewMessage("branch b", [ + { path: "a.ts", line: 18, startLine: 11, side: "RIGHT", body: "Split this.", code }, + { path: "b.ts", line: 4, side: "LEFT", body: "Keep it.", code: "old();" }, + ]); + expect(message).toContain("- `a.ts:11-18`: Split this.\n > line 0\n"); + expect(message).toContain(` > line ${QUOTED_LINES - 1}\n > …`); + expect(message).not.toContain(`line ${QUOTED_LINES}\n`); + expect(message).toContain("- `b.ts:4` (removed line): Keep it.\n > old();"); + }); +}); diff --git a/apps/desktop/src/state/diff.ts b/apps/desktop/src/state/diff.ts index f0d4a4d71..ec667f0c0 100644 --- a/apps/desktop/src/state/diff.ts +++ b/apps/desktop/src/state/diff.ts @@ -70,18 +70,29 @@ export function splitRows(lines: PatchLine[]): SplitRow[] { export interface ReviewComment { path: string; + /** The last line commented on; with `startLine`, the end of the range. */ line: number; + /** The first line of a range, on the same side. */ + startLine?: number; side: "LEFT" | "RIGHT"; body: string; - /** The line commented on, quoted in the message. */ + /** The lines commented on, quoted in the message. */ code: string; } +/** Lines of a range quoted in a message; the rest is elided. */ +export const QUOTED_LINES = 6; + /** The comments as one message for the agent working on these changes. */ export function reviewMessage(source: string, comments: ReviewComment[], note = ""): string { const items = comments.map((c) => { - const quote = c.code.trim() ? `\n > ${c.code.trim()}` : ""; - return `- \`${c.path}:${c.line}\`${c.side === "LEFT" ? " (removed line)" : ""}: ${c.body.trim()}${quote}`; + const ranged = c.startLine !== undefined && c.startLine !== c.line; + const where = ranged ? `${c.path}:${c.startLine}-${c.line}` : `${c.path}:${c.line}`; + const code = c.code.split("\n"); + const shown = code.slice(0, QUOTED_LINES).map((line) => `\n > ${line.trimEnd()}`); + const quote = c.code.trim() ? shown.join("") + (code.length > QUOTED_LINES ? "\n > …" : "") : ""; + const removed = c.side === "LEFT" ? (ranged ? " (removed lines)" : " (removed line)") : ""; + return `- \`${where}\`${removed}: ${c.body.trim()}${quote}`; }); return [`Review comments on ${source}:`, note.trim(), items.join("\n")].filter(Boolean).join("\n\n"); } @@ -96,3 +107,30 @@ export function fileTree(paths: string[]): { dir: string; files: string[] }[] { } return [...byDir.entries()].map(([dir, files]) => ({ dir, files })); } + +/** + * The lines numbered `from` to `to` on one side of a file's patch, in order, or null when the + * range crosses a hunk header (lines the diff does not show) or a number is missing. + */ +export function rangeLines( + lines: readonly PatchLine[], + side: "LEFT" | "RIGHT", + from: number, + to: number, +): PatchLine[] | null { + const [lo, hi] = from <= to ? [from, to] : [to, from]; + const out: PatchLine[] = []; + let open = false; + for (const line of lines) { + if (line.kind === "hunk") { + if (open) return null; + continue; + } + const n = side === "LEFT" ? line.old : line.new; + if (n === undefined) continue; + if (n === lo) open = true; + if (open) out.push(line); + if (n === hi) return open ? out : null; + } + return null; +} diff --git a/apps/desktop/src/state/reviewDrafts.ts b/apps/desktop/src/state/reviewDrafts.ts index 6d644876a..57d7450a4 100644 --- a/apps/desktop/src/state/reviewDrafts.ts +++ b/apps/desktop/src/state/reviewDrafts.ts @@ -42,6 +42,7 @@ function comment(value: unknown): ReviewComment | null { side: c.side, body: c.body, code: typeof c.code === "string" ? c.code : "", + ...(typeof c.startLine === "number" ? { startLine: c.startLine } : {}), }; } diff --git a/apps/desktop/src/ui/Review.tsx b/apps/desktop/src/ui/Review.tsx index b5fee24c8..ef3c6cb0d 100644 --- a/apps/desktop/src/ui/Review.tsx +++ b/apps/desktop/src/ui/Review.tsx @@ -25,6 +25,7 @@ import { type PatchLine, parsePatch, type ReviewComment, + rangeLines, reviewMessage, splitRows, } from "../state/diff"; @@ -149,21 +150,24 @@ interface LineProps { line: PatchLine; html: string | null | undefined; side: "LEFT" | "RIGHT"; - onComment: () => void; + onComment: (extend: boolean) => void; commented: boolean; + /** Inside the range a comment is being written on. */ + picked?: boolean; } -function Gutter({ line, side, onComment, commented }: Omit) { +function Gutter({ line, side, onComment, commented, picked }: Omit) { const n = side === "LEFT" ? line.old : line.new; return (