(
context: Context,
hunkParts: HunkPart[],
lineChanges: (Change[] | null)[]
)
| 8 | * Formats a "unified diff" hunk i.e. a hunk from a traditional diff. |
| 9 | */ |
| 10 | export async function* iterFormatUnifiedDiffHunkUnified( |
| 11 | context: Context, |
| 12 | hunkParts: HunkPart[], |
| 13 | lineChanges: (Change[] | null)[] |
| 14 | ): AsyncIterable<FormattedString> { |
| 15 | const lineWidth = context.SCREEN_WIDTH; |
| 16 | |
| 17 | const [ |
| 18 | { fileName: fileNameA, lines: hunkLinesA }, |
| 19 | { fileName: fileNameB, lines: hunkLinesB }, |
| 20 | ] = hunkParts; |
| 21 | let [{ startLineNo: lineNoA }, { startLineNo: lineNoB }] = hunkParts; |
| 22 | |
| 23 | let indexA = 0, |
| 24 | indexB = 0; |
| 25 | while (indexA < hunkLinesA.length) { |
| 26 | const hunkLineA = hunkLinesA[indexA]; |
| 27 | const prefixA = hunkLineA?.slice(0, 1) ?? null; |
| 28 | |
| 29 | switch (prefixA) { |
| 30 | case null: |
| 31 | // Ignore the missing lines we insert to match up indexes |
| 32 | break; |
| 33 | case '-': |
| 34 | yield* formatAndFitHunkLine( |
| 35 | context, |
| 36 | lineWidth, |
| 37 | fileNameA, |
| 38 | lineNoA, |
| 39 | hunkLineA, |
| 40 | lineChanges[indexA] |
| 41 | ); |
| 42 | lineNoA++; |
| 43 | break; |
| 44 | default: |
| 45 | // indexA is pointing to an unmodified line, so yield all the |
| 46 | // inserted lines from indexB up to this line |
| 47 | while (indexB < indexA) { |
| 48 | const hunkLineB = hunkLinesB[indexB]; |
| 49 | if (hunkLineB !== null) { |
| 50 | yield* formatAndFitHunkLine( |
| 51 | context, |
| 52 | lineWidth, |
| 53 | fileNameB, |
| 54 | lineNoB, |
| 55 | hunkLineB, |
| 56 | lineChanges[indexB] |
| 57 | ); |
| 58 | lineNoB++; |
| 59 | } |
| 60 | indexB++; |
| 61 | } |
| 62 | |
| 63 | // now yield the unmodified line, which should be present in both |
| 64 | yield* formatAndFitHunkLine( |
| 65 | context, |
| 66 | lineWidth, |
| 67 | fileNameA, |
no test coverage detected