| 1 | import type { Hunk } from '../git/diff/diff.js'; |
| 2 | |
| 3 | type DiffSide |
| 4 | |
| 5 | // Rows of the diff shown above a comment's first line, enough to read it in the conversation the way GitHub shows a diff hunk. |
| 6 | const CONTEXT_LINES = 3 |
| 7 | |
| 8 | /** The part of the diff a line comment points at, as a hunk with its own `@@` header: a few lines of lead-in, then its lines. Null when the diff does not show that line. */ |
| 9 | export function diffHunkFor( |
| 10 | hunks: Hunk |
| 11 | { |
| 12 | side, |
| 13 | line, |
| 14 | startSide, |
| 15 | startLine, |
| 16 | }: { |
| 17 | side: DiffSide |
| 18 | line: number |
| 19 | startSide?: DiffSide |
| 20 | startLine?: number |
| 21 | }, |
| 22 | ): string | null |
| 23 | const at = |
| 24 | (onSide: DiffSide |
| 25 | onSide === 'additions' |
| 26 | ? row.kind |
| 27 | : row.kind |
| 28 | |
| 29 | for (const hunk |
| 30 | const end = |
| 31 | if (end === |
| 32 | const start = |
| 33 | at(startSide ?? |
| 34 | ); |
| 35 | const rows = |
| 36 | Math.max((start |
| 37 | end + 1, |
| 38 | ); |
| 39 | const oldCount = |
| 40 | const newCount = |
| 41 | return [ |
| 42 | `@@ -${rows[0] |
| 43 | ...rows.map( |
| 44 | ].join('\n' |
| 45 | } |
| 46 | return null; |
| 47 | } |