-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathiterFormatHunk.ts
More file actions
49 lines (43 loc) · 1.52 KB
/
iterFormatHunk.ts
File metadata and controls
49 lines (43 loc) · 1.52 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
import { Context } from './context';
import { T, FormattedString } from './formattedString';
import { getChangesInLines } from './highlightChangesInLine';
import { iterFitTextToWidth } from './iterFitTextToWidth';
import { iterFormatHunkSplit } from './iterFormatHunkSplit';
import {
iterFormatCombinedDiffHunkUnified,
iterFormatUnifiedDiffHunkUnified,
} from './iterFormatHunkUnified';
export type HunkPart = {
fileName: string;
startLineNo: number;
lines: (string | null)[];
};
export async function* iterFormatHunk(
context: Context,
diffType: 'unified-diff' | 'combined-diff',
hunkHeaderLine: string,
hunkParts: HunkPart[]
): AsyncIterable<FormattedString> {
const { HUNK_HEADER_COLOR, SCREEN_WIDTH, MIN_LINE_WIDTH } = context;
yield* iterFitTextToWidth(
context,
T().appendString(hunkHeaderLine),
SCREEN_WIDTH,
HUNK_HEADER_COLOR
);
// TODO: Fix to handle multiple hunk parts
const changes = getChangesInLines(
context,
hunkParts[0].lines,
hunkParts[1].lines
);
// Only split diffs if there's enough room
const splitDiffs = SCREEN_WIDTH >= MIN_LINE_WIDTH * hunkParts.length;
if (splitDiffs) {
yield* iterFormatHunkSplit(context, hunkParts, changes);
} else if (diffType === 'unified-diff') {
yield* iterFormatUnifiedDiffHunkUnified(context, hunkParts, changes);
} else if (diffType === 'combined-diff') {
yield* iterFormatCombinedDiffHunkUnified(context, hunkParts, changes);
}
}