forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffComputer.ts
More file actions
320 lines (265 loc) · 11.6 KB
/
diffComputer.ts
File metadata and controls
320 lines (265 loc) · 11.6 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { IDiffChange, ISequence, LcsDiff } from 'vs/base/common/diff/diff';
import * as strings from 'vs/base/common/strings';
import { ICharChange, ILineChange } from 'vs/editor/common/editorCommon';
var MAXIMUM_RUN_TIME = 5000; // 5 seconds
var MINIMUM_MATCHING_CHARACTER_LENGTH = 3;
interface IMarker {
lineNumber: number;
column: number;
offset: number;
}
function computeDiff(originalSequence: ISequence, modifiedSequence: ISequence, continueProcessingPredicate: () => boolean): IDiffChange[] {
var diffAlgo = new LcsDiff(originalSequence, modifiedSequence, continueProcessingPredicate);
return diffAlgo.ComputeDiff();
}
class MarkerSequence implements ISequence {
public buffer: string;
public startMarkers: IMarker[];
public endMarkers: IMarker[];
constructor(buffer: string, startMarkers: IMarker[], endMarkers: IMarker[]) {
this.buffer = buffer;
this.startMarkers = startMarkers;
this.endMarkers = endMarkers;
}
public equals(other: any): boolean {
if (!(other instanceof MarkerSequence)) {
return false;
}
var otherMarkerSequence = <MarkerSequence>other;
if (this.getLength() !== otherMarkerSequence.getLength()) {
return false;
}
for (var i = 0, len = this.getLength(); i < len; i++) {
var myElement = this.getElementHash(i);
var otherElement = otherMarkerSequence.getElementHash(i);
if (myElement !== otherElement) {
return false;
}
}
return true;
}
public getLength(): number {
return this.startMarkers.length;
}
public getElementHash(i: number): string {
return this.buffer.substring(this.startMarkers[i].offset, this.endMarkers[i].offset);
}
public getStartLineNumber(i: number): number {
if (i === this.startMarkers.length) {
// This is the special case where a change happened after the last marker
return this.startMarkers[i - 1].lineNumber + 1;
}
return this.startMarkers[i].lineNumber;
}
public getStartColumn(i: number): number {
return this.startMarkers[i].column;
}
public getEndLineNumber(i: number): number {
return this.endMarkers[i].lineNumber;
}
public getEndColumn(i: number): number {
return this.endMarkers[i].column;
}
}
class LineMarkerSequence extends MarkerSequence {
constructor(lines: string[], shouldIgnoreTrimWhitespace: boolean) {
var i: number, length: number, pos: number;
var buffer = '';
var startMarkers: IMarker[] = [], endMarkers: IMarker[] = [], startColumn: number, endColumn: number;
for (pos = 0, i = 0, length = lines.length; i < length; i++) {
buffer += lines[i];
startColumn = 1;
endColumn = lines[i].length + 1;
if (shouldIgnoreTrimWhitespace) {
startColumn = LineMarkerSequence._getFirstNonBlankColumn(lines[i], 1);
endColumn = LineMarkerSequence._getLastNonBlankColumn(lines[i], 1);
}
startMarkers.push({
offset: pos + startColumn - 1,
lineNumber: i + 1,
column: startColumn
});
endMarkers.push({
offset: pos + endColumn - 1,
lineNumber: i + 1,
column: endColumn
});
pos += lines[i].length;
}
super(buffer, startMarkers, endMarkers);
}
private static _getFirstNonBlankColumn(txt: string, defaultValue: number): number {
var r = strings.firstNonWhitespaceIndex(txt);
if (r === -1) {
return defaultValue;
}
return r + 1;
}
private static _getLastNonBlankColumn(txt: string, defaultValue: number): number {
var r = strings.lastNonWhitespaceIndex(txt);
if (r === -1) {
return defaultValue;
}
return r + 2;
}
public getCharSequence(startIndex: number, endIndex: number): MarkerSequence {
var startMarkers: IMarker[] = [], endMarkers: IMarker[] = [], index: number, i: number, startMarker: IMarker, endMarker: IMarker;
for (index = startIndex; index <= endIndex; index++) {
startMarker = this.startMarkers[index];
endMarker = this.endMarkers[index];
for (i = startMarker.offset; i < endMarker.offset; i++) {
startMarkers.push({
offset: i,
lineNumber: startMarker.lineNumber,
column: startMarker.column + (i - startMarker.offset)
});
endMarkers.push({
offset: i + 1,
lineNumber: startMarker.lineNumber,
column: startMarker.column + (i - startMarker.offset) + 1
});
}
}
return new MarkerSequence(this.buffer, startMarkers, endMarkers);
}
}
class CharChange implements ICharChange {
public originalStartLineNumber: number;
public originalStartColumn: number;
public originalEndLineNumber: number;
public originalEndColumn: number;
public modifiedStartLineNumber: number;
public modifiedStartColumn: number;
public modifiedEndLineNumber: number;
public modifiedEndColumn: number;
constructor(diffChange: IDiffChange, originalCharSequence: MarkerSequence, modifiedCharSequence: MarkerSequence) {
if (diffChange.originalLength === 0) {
this.originalStartLineNumber = 0;
this.originalStartColumn = 0;
this.originalEndLineNumber = 0;
this.originalEndColumn = 0;
} else {
this.originalStartLineNumber = originalCharSequence.getStartLineNumber(diffChange.originalStart);
this.originalStartColumn = originalCharSequence.getStartColumn(diffChange.originalStart);
this.originalEndLineNumber = originalCharSequence.getEndLineNumber(diffChange.originalStart + diffChange.originalLength - 1);
this.originalEndColumn = originalCharSequence.getEndColumn(diffChange.originalStart + diffChange.originalLength - 1);
}
if (diffChange.modifiedLength === 0) {
this.modifiedStartLineNumber = 0;
this.modifiedStartColumn = 0;
this.modifiedEndLineNumber = 0;
this.modifiedEndColumn = 0;
} else {
this.modifiedStartLineNumber = modifiedCharSequence.getStartLineNumber(diffChange.modifiedStart);
this.modifiedStartColumn = modifiedCharSequence.getStartColumn(diffChange.modifiedStart);
this.modifiedEndLineNumber = modifiedCharSequence.getEndLineNumber(diffChange.modifiedStart + diffChange.modifiedLength - 1);
this.modifiedEndColumn = modifiedCharSequence.getEndColumn(diffChange.modifiedStart + diffChange.modifiedLength - 1);
}
}
}
function postProcessCharChanges(rawChanges: IDiffChange[]): IDiffChange[] {
if (rawChanges.length <= 1) {
return rawChanges;
}
var result = [rawChanges[0]];
var i: number, len: number, originalMatchingLength: number, modifiedMatchingLength: number, matchingLength: number, prevChange = result[0], currChange: IDiffChange;
for (i = 1, len = rawChanges.length; i < len; i++) {
currChange = rawChanges[i];
originalMatchingLength = currChange.originalStart - (prevChange.originalStart + prevChange.originalLength);
modifiedMatchingLength = currChange.modifiedStart - (prevChange.modifiedStart + prevChange.modifiedLength);
// Both of the above should be equal, but the continueProcessingPredicate may prevent this from being true
matchingLength = Math.min(originalMatchingLength, modifiedMatchingLength);
if (matchingLength < MINIMUM_MATCHING_CHARACTER_LENGTH) {
// Merge the current change into the previous one
prevChange.originalLength = (currChange.originalStart + currChange.originalLength) - prevChange.originalStart;
prevChange.modifiedLength = (currChange.modifiedStart + currChange.modifiedLength) - prevChange.modifiedStart;
} else {
// Add the current change
result.push(currChange);
prevChange = currChange;
}
}
return result;
}
class LineChange implements ILineChange {
public originalStartLineNumber: number;
public originalEndLineNumber: number;
public modifiedStartLineNumber: number;
public modifiedEndLineNumber: number;
public charChanges: CharChange[];
constructor(diffChange: IDiffChange, originalLineSequence: LineMarkerSequence, modifiedLineSequence: LineMarkerSequence, continueProcessingPredicate: () => boolean, shouldPostProcessCharChanges: boolean) {
if (diffChange.originalLength === 0) {
this.originalStartLineNumber = originalLineSequence.getStartLineNumber(diffChange.originalStart) - 1;
this.originalEndLineNumber = 0;
} else {
this.originalStartLineNumber = originalLineSequence.getStartLineNumber(diffChange.originalStart);
this.originalEndLineNumber = originalLineSequence.getEndLineNumber(diffChange.originalStart + diffChange.originalLength - 1);
}
if (diffChange.modifiedLength === 0) {
this.modifiedStartLineNumber = modifiedLineSequence.getStartLineNumber(diffChange.modifiedStart) - 1;
this.modifiedEndLineNumber = 0;
} else {
this.modifiedStartLineNumber = modifiedLineSequence.getStartLineNumber(diffChange.modifiedStart);
this.modifiedEndLineNumber = modifiedLineSequence.getEndLineNumber(diffChange.modifiedStart + diffChange.modifiedLength - 1);
}
if (diffChange.originalLength !== 0 && diffChange.modifiedLength !== 0 && continueProcessingPredicate()) {
var originalCharSequence = originalLineSequence.getCharSequence(diffChange.originalStart, diffChange.originalStart + diffChange.originalLength - 1);
var modifiedCharSequence = modifiedLineSequence.getCharSequence(diffChange.modifiedStart, diffChange.modifiedStart + diffChange.modifiedLength - 1);
var rawChanges = computeDiff(originalCharSequence, modifiedCharSequence, continueProcessingPredicate);
if (shouldPostProcessCharChanges) {
rawChanges = postProcessCharChanges(rawChanges);
}
this.charChanges = [];
for (var i = 0, length = rawChanges.length; i < length; i++) {
this.charChanges.push(new CharChange(rawChanges[i], originalCharSequence, modifiedCharSequence));
}
}
}
}
export interface IDiffComputerOpts {
shouldPostProcessCharChanges: boolean;
shouldIgnoreTrimWhitespace: boolean;
shouldConsiderTrimWhitespaceInEmptyCase: boolean;
}
export class DiffComputer {
private shouldPostProcessCharChanges: boolean;
private shouldIgnoreTrimWhitespace: boolean;
private maximumRunTimeMs: number;
private original: LineMarkerSequence;
private modified: LineMarkerSequence;
private computationStartTime: number;
constructor(originalLines: string[], modifiedLines: string[], opts: IDiffComputerOpts) {
this.shouldPostProcessCharChanges = opts.shouldPostProcessCharChanges;
this.shouldIgnoreTrimWhitespace = opts.shouldIgnoreTrimWhitespace;
this.maximumRunTimeMs = MAXIMUM_RUN_TIME;
this.original = new LineMarkerSequence(originalLines, this.shouldIgnoreTrimWhitespace);
this.modified = new LineMarkerSequence(modifiedLines, this.shouldIgnoreTrimWhitespace);
if (opts.shouldConsiderTrimWhitespaceInEmptyCase && this.shouldIgnoreTrimWhitespace && this.original.equals(this.modified)) {
// Diff would be empty with `shouldIgnoreTrimWhitespace`
this.shouldIgnoreTrimWhitespace = false;
this.original = new LineMarkerSequence(originalLines, this.shouldIgnoreTrimWhitespace);
this.modified = new LineMarkerSequence(modifiedLines, this.shouldIgnoreTrimWhitespace);
}
}
public computeDiff(): ILineChange[] {
this.computationStartTime = (new Date()).getTime();
var rawChanges = computeDiff(this.original, this.modified, this._continueProcessingPredicate.bind(this));
var lineChanges: ILineChange[] = [];
for (var i = 0, length = rawChanges.length; i < length; i++) {
lineChanges.push(new LineChange(rawChanges[i], this.original, this.modified, this._continueProcessingPredicate.bind(this), this.shouldPostProcessCharChanges));
}
return lineChanges;
}
private _continueProcessingPredicate(): boolean {
if (this.maximumRunTimeMs === 0) {
return true;
}
var now = (new Date()).getTime();
return now - this.computationStartTime < this.maximumRunTimeMs;
}
}