-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathiterFormatHunkUnified.ts
More file actions
135 lines (125 loc) · 4.14 KB
/
iterFormatHunkUnified.ts
File metadata and controls
135 lines (125 loc) · 4.14 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Change } from 'diff';
import { Context } from './context';
import { formatAndFitHunkLine } from './formatAndFitHunkLine';
import { FormattedString } from './formattedString';
import { HunkPart } from './iterFormatHunk';
/**
* Formats a "unified diff" hunk i.e. a hunk from a traditional diff.
*/
export async function* iterFormatUnifiedDiffHunkUnified(
context: Context,
hunkParts: HunkPart[],
lineChanges: (Change[] | null)[]
): AsyncIterable<FormattedString> {
const lineWidth = context.SCREEN_WIDTH;
const [
{ fileName: fileNameA, lines: hunkLinesA },
{ fileName: fileNameB, lines: hunkLinesB },
] = hunkParts;
let [{ startLineNo: lineNoA }, { startLineNo: lineNoB }] = hunkParts;
let indexA = 0,
indexB = 0;
while (indexA < hunkLinesA.length) {
const hunkLineA = hunkLinesA[indexA];
const prefixA = hunkLineA?.slice(0, 1) ?? null;
switch (prefixA) {
case null:
// Ignore the missing lines we insert to match up indexes
break;
case '-':
yield* formatAndFitHunkLine(
context,
lineWidth,
fileNameA,
lineNoA,
hunkLineA,
lineChanges[indexA]
);
lineNoA++;
break;
default:
// indexA is pointing to an unmodified line, so yield all the
// inserted lines from indexB up to this line
while (indexB < indexA) {
const hunkLineB = hunkLinesB[indexB];
if (hunkLineB !== null) {
yield* formatAndFitHunkLine(
context,
lineWidth,
fileNameB,
lineNoB,
hunkLineB,
lineChanges[indexB]
);
lineNoB++;
}
indexB++;
}
// now yield the unmodified line, which should be present in both
yield* formatAndFitHunkLine(
context,
lineWidth,
fileNameA,
lineNoA,
hunkLineA,
lineChanges[indexB]
);
lineNoA++;
lineNoB++;
indexB++;
}
indexA++;
}
// yield any remaining lines in hunk B, which can happen if there were more
// insertions at the end of the hunk
while (indexB < hunkLinesB.length) {
const hunkLineB = hunkLinesB[indexB];
if (hunkLineB !== null) {
yield* formatAndFitHunkLine(
context,
lineWidth,
fileNameB,
lineNoB,
hunkLineB,
lineChanges[indexB]
);
lineNoB++;
}
indexB++;
}
}
/**
* Formats a "combined diff" hunk i.e. a hunk from a combined diff, generated by
* --cc or --combined options, which are defaults for merge commits.
*/
export async function* iterFormatCombinedDiffHunkUnified(
context: Context,
hunkParts: HunkPart[],
lineChanges: (Change[] | null)[]
): AsyncIterable<FormattedString> {
const lineWidth = context.SCREEN_WIDTH;
// The final hunk part shows the current state of the file, so we just
// display that with additions and deletions highlighted.
const { fileName, lines, startLineNo } = hunkParts[hunkParts.length - 1];
let lineNo = startLineNo;
let numDeletes = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const prefix = line?.slice(0, 1) ?? null;
if (prefix == '-') {
numDeletes++;
} else {
lineNo -= numDeletes;
numDeletes = 0;
}
yield* formatAndFitHunkLine(
context,
lineWidth,
fileName,
lineNo,
line,
lineChanges[i]
);
lineNo++;
}
}