(
context: Context,
hunkParts: HunkPart[],
lineChanges: (Change[] | null)[]
)
| 6 | import { HunkPart } from './iterFormatHunk'; |
| 7 | |
| 8 | export async function* iterFormatHunkSplit( |
| 9 | context: Context, |
| 10 | hunkParts: HunkPart[], |
| 11 | lineChanges: (Change[] | null)[] |
| 12 | ): AsyncIterable<FormattedString> { |
| 13 | const { MISSING_LINE_COLOR } = context; |
| 14 | const lineWidth = Math.floor(context.SCREEN_WIDTH / hunkParts.length); |
| 15 | const blankLine = ''.padStart(lineWidth); |
| 16 | |
| 17 | const lineNos = hunkParts.map((part) => part.startLineNo); |
| 18 | const numDeletes = hunkParts.map(() => 0); |
| 19 | |
| 20 | for (const [changes, ...hunkPartLines] of zip( |
| 21 | lineChanges, |
| 22 | ...hunkParts.map((part) => part.lines) |
| 23 | )) { |
| 24 | // Count deletions and adjust line numbers for previous deletions |
| 25 | hunkPartLines.forEach((hunkPartLine, i) => { |
| 26 | const prefix = hunkPartLine?.slice(0, 1) ?? null; |
| 27 | if (prefix === '-') { |
| 28 | numDeletes[i]++; |
| 29 | } else { |
| 30 | lineNos[i] -= numDeletes[i]; |
| 31 | numDeletes[i] = 0; |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | const formattedLineIterables = hunkPartLines.map((hunkPartLine, i) => |
| 36 | formatAndFitHunkLine( |
| 37 | context, |
| 38 | lineWidth, |
| 39 | hunkParts[i].fileName, |
| 40 | lineNos[i], |
| 41 | hunkPartLine ?? null, |
| 42 | changes ?? null |
| 43 | ) |
| 44 | ); |
| 45 | |
| 46 | const missingLine = T().appendString(blankLine, MISSING_LINE_COLOR); |
| 47 | |
| 48 | for await (const formattedLines of zipAsync( |
| 49 | ...formattedLineIterables |
| 50 | )) { |
| 51 | const formattedLine = T(); |
| 52 | for (const line of formattedLines) { |
| 53 | formattedLine.appendSpannedString(line ?? missingLine); |
| 54 | } |
| 55 | yield formattedLine; |
| 56 | } |
| 57 | |
| 58 | hunkPartLines.forEach((hunkPartLine, i) => { |
| 59 | if (hunkPartLine !== null && hunkPartLine !== undefined) { |
| 60 | lineNos[i]++; |
| 61 | } |
| 62 | }); |
| 63 | } |
| 64 | } |
no test coverage detected