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