/
getAlignedDiffs.ts
206 lines (179 loc) · 6.2 KB
/
getAlignedDiffs.ts
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {DIFF_DELETE, DIFF_INSERT, Diff} from './cleanupSemantic';
import {invertChangedSubstrings} from './printDiffs';
// Encapsulate change lines until either a common newline or the end.
class ChangeBuffer {
private op: number;
private line: Array<Diff>; // incomplete line
private lines: Array<Diff>; // complete lines
constructor(op: number) {
this.op = op;
this.line = [];
this.lines = [];
}
private pushSubstring(substring: string): void {
this.pushDiff(new Diff(this.op, substring));
}
private pushLine(): void {
// Assume call only if line has at least one diff,
// therefore an empty line must have a diff which has an empty string.
// If line has multiple diffs, then assume it has a common diff,
// therefore change diffs have change color;
// otherwise then it has line color only.
this.lines.push(
this.line.length !== 1
? new Diff(this.op, invertChangedSubstrings(this.op, this.line))
: this.line[0][0] === this.op
? this.line[0] // can use instance
: new Diff(this.op, this.line[0][1]), // was common diff
);
this.line.length = 0;
}
isLineEmpty() {
return this.line.length === 0;
}
// Minor input to buffer.
pushDiff(diff: Diff): void {
this.line.push(diff);
}
// Main input to buffer.
align(diff: Diff): void {
const string = diff[1];
if (string.includes('\n')) {
const substrings = string.split('\n');
const iLast = substrings.length - 1;
substrings.forEach((substring, i) => {
if (i < iLast) {
// The first substring completes the current change line.
// A middle substring is a change line.
this.pushSubstring(substring);
this.pushLine();
} else if (substring.length !== 0) {
// The last substring starts a change line, if it is not empty.
// Important: This non-empty condition also automatically omits
// the newline appended to the end of expected and received strings.
this.pushSubstring(substring);
}
});
} else {
// Append non-multiline string to current change line.
this.pushDiff(diff);
}
}
// Output from buffer.
moveLinesTo(lines: Array<Diff>): void {
if (!this.isLineEmpty()) {
this.pushLine();
}
lines.push(...this.lines);
this.lines.length = 0;
}
}
// Encapsulate common and change lines.
class CommonBuffer {
private deleteBuffer: ChangeBuffer;
private insertBuffer: ChangeBuffer;
private lines: Array<Diff>;
constructor(deleteBuffer: ChangeBuffer, insertBuffer: ChangeBuffer) {
this.deleteBuffer = deleteBuffer;
this.insertBuffer = insertBuffer;
this.lines = [];
}
private pushDiffCommonLine(diff: Diff): void {
this.lines.push(diff);
}
private pushDiffChangeLines(diff: Diff): void {
const isDiffEmpty = diff[1].length === 0;
// An empty diff string is redundant, unless a change line is empty.
if (!isDiffEmpty || this.deleteBuffer.isLineEmpty()) {
this.deleteBuffer.pushDiff(diff);
}
if (!isDiffEmpty || this.insertBuffer.isLineEmpty()) {
this.insertBuffer.pushDiff(diff);
}
}
private flushChangeLines(): void {
this.deleteBuffer.moveLinesTo(this.lines);
this.insertBuffer.moveLinesTo(this.lines);
}
// Input to buffer.
align(diff: Diff): void {
const op = diff[0];
const string = diff[1];
if (string.includes('\n')) {
const substrings = string.split('\n');
const iLast = substrings.length - 1;
substrings.forEach((substring, i) => {
if (i === 0) {
const subdiff = new Diff(op, substring);
if (
this.deleteBuffer.isLineEmpty() &&
this.insertBuffer.isLineEmpty()
) {
// If both current change lines are empty,
// then the first substring is a common line.
this.flushChangeLines();
this.pushDiffCommonLine(subdiff);
} else {
// If either current change line is non-empty,
// then the first substring completes the change lines.
this.pushDiffChangeLines(subdiff);
this.flushChangeLines();
}
} else if (i < iLast) {
// A middle substring is a common line.
this.pushDiffCommonLine(new Diff(op, substring));
} else if (substring.length !== 0) {
// The last substring starts a change line, if it is not empty.
// Important: This non-empty condition also automatically omits
// the newline appended to the end of expected and received strings.
this.pushDiffChangeLines(new Diff(op, substring));
}
});
} else {
// Append non-multiline string to current change lines.
// Important: It cannot be at the end following empty change lines,
// because newline appended to the end of expected and received strings.
this.pushDiffChangeLines(diff);
}
}
// Output from buffer.
getLines(): Array<Diff> {
this.flushChangeLines();
return this.lines;
}
}
// Given diffs from expected and received strings,
// return new array of diffs split or joined into lines.
//
// To correctly align a change line at the end, the algorithm:
// * assumes that a newline was appended to the strings
// * omits the last newline from the output array
//
// Assume the function is not called:
// * if either expected or received is empty string
// * if neither expected nor received is multiline string
const getAlignedDiffs = (diffs: Array<Diff>): Array<Diff> => {
const deleteBuffer = new ChangeBuffer(DIFF_DELETE);
const insertBuffer = new ChangeBuffer(DIFF_INSERT);
const commonBuffer = new CommonBuffer(deleteBuffer, insertBuffer);
diffs.forEach(diff => {
switch (diff[0]) {
case DIFF_DELETE:
deleteBuffer.align(diff);
break;
case DIFF_INSERT:
insertBuffer.align(diff);
break;
default:
commonBuffer.align(diff);
}
});
return commonBuffer.getLines();
};
export default getAlignedDiffs;