lody 0.57.1-next.3 → 0.57.1-next.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/diff-line-counts-BLxwWP6r.js +675 -0
- package/dist/chunks/embedded-prompt-D__ujYMd.js +4 -0
- package/dist/chunks/index-B9fKl40V.js +308 -0
- package/dist/chunks/index-CUUne095.js +33 -0
- package/dist/chunks/index-v46gaGAl.js +17 -0
- package/dist/chunks/loro_wasm_bg-N-tMVpou.js +5197 -0
- package/dist/chunks/review-viewer-Dqz_1hWG.js +80 -0
- package/dist/chunks/sparse-text-D7zcV2O5.js +649 -0
- package/dist/chunks/turn-diff-replay-qRat9u1k.js +311 -0
- package/dist/diff-worker.js +11 -0
- package/dist/index.js +18661 -10280
- package/dist/turn-diff-replay-worker.js +16 -0
- package/package.json +23 -16
- package/dist/chunks/loro_wasm_bg-ICD3atfE.js +0 -5083
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
|
|
3
|
+
import os__default from "node:os";
|
|
4
|
+
import path__default from "node:path";
|
|
5
|
+
function Diff() {
|
|
6
|
+
}
|
|
7
|
+
Diff.prototype = {
|
|
8
|
+
diff: function diff(oldString, newString) {
|
|
9
|
+
var _options$timeout;
|
|
10
|
+
var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
11
|
+
var callback = options.callback;
|
|
12
|
+
if (typeof options === "function") {
|
|
13
|
+
callback = options;
|
|
14
|
+
options = {};
|
|
15
|
+
}
|
|
16
|
+
var self = this;
|
|
17
|
+
function done(value) {
|
|
18
|
+
value = self.postProcess(value, options);
|
|
19
|
+
if (callback) {
|
|
20
|
+
setTimeout(function() {
|
|
21
|
+
callback(value);
|
|
22
|
+
}, 0);
|
|
23
|
+
return true;
|
|
24
|
+
} else {
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
oldString = this.castInput(oldString, options);
|
|
29
|
+
newString = this.castInput(newString, options);
|
|
30
|
+
oldString = this.removeEmpty(this.tokenize(oldString, options));
|
|
31
|
+
newString = this.removeEmpty(this.tokenize(newString, options));
|
|
32
|
+
var newLen = newString.length, oldLen = oldString.length;
|
|
33
|
+
var editLength = 1;
|
|
34
|
+
var maxEditLength = newLen + oldLen;
|
|
35
|
+
if (options.maxEditLength != null) {
|
|
36
|
+
maxEditLength = Math.min(maxEditLength, options.maxEditLength);
|
|
37
|
+
}
|
|
38
|
+
var maxExecutionTime = (_options$timeout = options.timeout) !== null && _options$timeout !== void 0 ? _options$timeout : Infinity;
|
|
39
|
+
var abortAfterTimestamp = Date.now() + maxExecutionTime;
|
|
40
|
+
var bestPath = [{
|
|
41
|
+
oldPos: -1,
|
|
42
|
+
lastComponent: void 0
|
|
43
|
+
}];
|
|
44
|
+
var newPos = this.extractCommon(bestPath[0], newString, oldString, 0, options);
|
|
45
|
+
if (bestPath[0].oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
|
|
46
|
+
return done(buildValues(self, bestPath[0].lastComponent, newString, oldString, self.useLongestToken));
|
|
47
|
+
}
|
|
48
|
+
var minDiagonalToConsider = -Infinity, maxDiagonalToConsider = Infinity;
|
|
49
|
+
function execEditLength() {
|
|
50
|
+
for (var diagonalPath = Math.max(minDiagonalToConsider, -editLength); diagonalPath <= Math.min(maxDiagonalToConsider, editLength); diagonalPath += 2) {
|
|
51
|
+
var basePath = void 0;
|
|
52
|
+
var removePath = bestPath[diagonalPath - 1], addPath = bestPath[diagonalPath + 1];
|
|
53
|
+
if (removePath) {
|
|
54
|
+
bestPath[diagonalPath - 1] = void 0;
|
|
55
|
+
}
|
|
56
|
+
var canAdd = false;
|
|
57
|
+
if (addPath) {
|
|
58
|
+
var addPathNewPos = addPath.oldPos - diagonalPath;
|
|
59
|
+
canAdd = addPath && 0 <= addPathNewPos && addPathNewPos < newLen;
|
|
60
|
+
}
|
|
61
|
+
var canRemove = removePath && removePath.oldPos + 1 < oldLen;
|
|
62
|
+
if (!canAdd && !canRemove) {
|
|
63
|
+
bestPath[diagonalPath] = void 0;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (!canRemove || canAdd && removePath.oldPos < addPath.oldPos) {
|
|
67
|
+
basePath = self.addToPath(addPath, true, false, 0, options);
|
|
68
|
+
} else {
|
|
69
|
+
basePath = self.addToPath(removePath, false, true, 1, options);
|
|
70
|
+
}
|
|
71
|
+
newPos = self.extractCommon(basePath, newString, oldString, diagonalPath, options);
|
|
72
|
+
if (basePath.oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
|
|
73
|
+
return done(buildValues(self, basePath.lastComponent, newString, oldString, self.useLongestToken));
|
|
74
|
+
} else {
|
|
75
|
+
bestPath[diagonalPath] = basePath;
|
|
76
|
+
if (basePath.oldPos + 1 >= oldLen) {
|
|
77
|
+
maxDiagonalToConsider = Math.min(maxDiagonalToConsider, diagonalPath - 1);
|
|
78
|
+
}
|
|
79
|
+
if (newPos + 1 >= newLen) {
|
|
80
|
+
minDiagonalToConsider = Math.max(minDiagonalToConsider, diagonalPath + 1);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
editLength++;
|
|
85
|
+
}
|
|
86
|
+
if (callback) {
|
|
87
|
+
(function exec() {
|
|
88
|
+
setTimeout(function() {
|
|
89
|
+
if (editLength > maxEditLength || Date.now() > abortAfterTimestamp) {
|
|
90
|
+
return callback();
|
|
91
|
+
}
|
|
92
|
+
if (!execEditLength()) {
|
|
93
|
+
exec();
|
|
94
|
+
}
|
|
95
|
+
}, 0);
|
|
96
|
+
})();
|
|
97
|
+
} else {
|
|
98
|
+
while (editLength <= maxEditLength && Date.now() <= abortAfterTimestamp) {
|
|
99
|
+
var ret = execEditLength();
|
|
100
|
+
if (ret) {
|
|
101
|
+
return ret;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
addToPath: function addToPath(path, added, removed, oldPosInc, options) {
|
|
107
|
+
var last = path.lastComponent;
|
|
108
|
+
if (last && !options.oneChangePerToken && last.added === added && last.removed === removed) {
|
|
109
|
+
return {
|
|
110
|
+
oldPos: path.oldPos + oldPosInc,
|
|
111
|
+
lastComponent: {
|
|
112
|
+
count: last.count + 1,
|
|
113
|
+
added,
|
|
114
|
+
removed,
|
|
115
|
+
previousComponent: last.previousComponent
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
} else {
|
|
119
|
+
return {
|
|
120
|
+
oldPos: path.oldPos + oldPosInc,
|
|
121
|
+
lastComponent: {
|
|
122
|
+
count: 1,
|
|
123
|
+
added,
|
|
124
|
+
removed,
|
|
125
|
+
previousComponent: last
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
extractCommon: function extractCommon(basePath, newString, oldString, diagonalPath, options) {
|
|
131
|
+
var newLen = newString.length, oldLen = oldString.length, oldPos = basePath.oldPos, newPos = oldPos - diagonalPath, commonCount = 0;
|
|
132
|
+
while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(oldString[oldPos + 1], newString[newPos + 1], options)) {
|
|
133
|
+
newPos++;
|
|
134
|
+
oldPos++;
|
|
135
|
+
commonCount++;
|
|
136
|
+
if (options.oneChangePerToken) {
|
|
137
|
+
basePath.lastComponent = {
|
|
138
|
+
count: 1,
|
|
139
|
+
previousComponent: basePath.lastComponent,
|
|
140
|
+
added: false,
|
|
141
|
+
removed: false
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (commonCount && !options.oneChangePerToken) {
|
|
146
|
+
basePath.lastComponent = {
|
|
147
|
+
count: commonCount,
|
|
148
|
+
previousComponent: basePath.lastComponent,
|
|
149
|
+
added: false,
|
|
150
|
+
removed: false
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
basePath.oldPos = oldPos;
|
|
154
|
+
return newPos;
|
|
155
|
+
},
|
|
156
|
+
equals: function equals(left, right, options) {
|
|
157
|
+
if (options.comparator) {
|
|
158
|
+
return options.comparator(left, right);
|
|
159
|
+
} else {
|
|
160
|
+
return left === right || options.ignoreCase && left.toLowerCase() === right.toLowerCase();
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
removeEmpty: function removeEmpty(array) {
|
|
164
|
+
var ret = [];
|
|
165
|
+
for (var i = 0; i < array.length; i++) {
|
|
166
|
+
if (array[i]) {
|
|
167
|
+
ret.push(array[i]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return ret;
|
|
171
|
+
},
|
|
172
|
+
castInput: function castInput(value) {
|
|
173
|
+
return value;
|
|
174
|
+
},
|
|
175
|
+
tokenize: function tokenize(value) {
|
|
176
|
+
return Array.from(value);
|
|
177
|
+
},
|
|
178
|
+
join: function join(chars) {
|
|
179
|
+
return chars.join("");
|
|
180
|
+
},
|
|
181
|
+
postProcess: function postProcess(changeObjects) {
|
|
182
|
+
return changeObjects;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
function buildValues(diff2, lastComponent, newString, oldString, useLongestToken) {
|
|
186
|
+
var components = [];
|
|
187
|
+
var nextComponent;
|
|
188
|
+
while (lastComponent) {
|
|
189
|
+
components.push(lastComponent);
|
|
190
|
+
nextComponent = lastComponent.previousComponent;
|
|
191
|
+
delete lastComponent.previousComponent;
|
|
192
|
+
lastComponent = nextComponent;
|
|
193
|
+
}
|
|
194
|
+
components.reverse();
|
|
195
|
+
var componentPos = 0, componentLen = components.length, newPos = 0, oldPos = 0;
|
|
196
|
+
for (; componentPos < componentLen; componentPos++) {
|
|
197
|
+
var component = components[componentPos];
|
|
198
|
+
if (!component.removed) {
|
|
199
|
+
if (!component.added && useLongestToken) {
|
|
200
|
+
var value = newString.slice(newPos, newPos + component.count);
|
|
201
|
+
value = value.map(function(value2, i) {
|
|
202
|
+
var oldValue = oldString[oldPos + i];
|
|
203
|
+
return oldValue.length > value2.length ? oldValue : value2;
|
|
204
|
+
});
|
|
205
|
+
component.value = diff2.join(value);
|
|
206
|
+
} else {
|
|
207
|
+
component.value = diff2.join(newString.slice(newPos, newPos + component.count));
|
|
208
|
+
}
|
|
209
|
+
newPos += component.count;
|
|
210
|
+
if (!component.added) {
|
|
211
|
+
oldPos += component.count;
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
component.value = diff2.join(oldString.slice(oldPos, oldPos + component.count));
|
|
215
|
+
oldPos += component.count;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return components;
|
|
219
|
+
}
|
|
220
|
+
function longestCommonPrefix(str1, str2) {
|
|
221
|
+
var i;
|
|
222
|
+
for (i = 0; i < str1.length && i < str2.length; i++) {
|
|
223
|
+
if (str1[i] != str2[i]) {
|
|
224
|
+
return str1.slice(0, i);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return str1.slice(0, i);
|
|
228
|
+
}
|
|
229
|
+
function longestCommonSuffix(str1, str2) {
|
|
230
|
+
var i;
|
|
231
|
+
if (!str1 || !str2 || str1[str1.length - 1] != str2[str2.length - 1]) {
|
|
232
|
+
return "";
|
|
233
|
+
}
|
|
234
|
+
for (i = 0; i < str1.length && i < str2.length; i++) {
|
|
235
|
+
if (str1[str1.length - (i + 1)] != str2[str2.length - (i + 1)]) {
|
|
236
|
+
return str1.slice(-i);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return str1.slice(-i);
|
|
240
|
+
}
|
|
241
|
+
function replacePrefix(string, oldPrefix, newPrefix) {
|
|
242
|
+
if (string.slice(0, oldPrefix.length) != oldPrefix) {
|
|
243
|
+
throw Error("string ".concat(JSON.stringify(string), " doesn't start with prefix ").concat(JSON.stringify(oldPrefix), "; this is a bug"));
|
|
244
|
+
}
|
|
245
|
+
return newPrefix + string.slice(oldPrefix.length);
|
|
246
|
+
}
|
|
247
|
+
function replaceSuffix(string, oldSuffix, newSuffix) {
|
|
248
|
+
if (!oldSuffix) {
|
|
249
|
+
return string + newSuffix;
|
|
250
|
+
}
|
|
251
|
+
if (string.slice(-oldSuffix.length) != oldSuffix) {
|
|
252
|
+
throw Error("string ".concat(JSON.stringify(string), " doesn't end with suffix ").concat(JSON.stringify(oldSuffix), "; this is a bug"));
|
|
253
|
+
}
|
|
254
|
+
return string.slice(0, -oldSuffix.length) + newSuffix;
|
|
255
|
+
}
|
|
256
|
+
function removePrefix(string, oldPrefix) {
|
|
257
|
+
return replacePrefix(string, oldPrefix, "");
|
|
258
|
+
}
|
|
259
|
+
function removeSuffix(string, oldSuffix) {
|
|
260
|
+
return replaceSuffix(string, oldSuffix, "");
|
|
261
|
+
}
|
|
262
|
+
function maximumOverlap(string1, string2) {
|
|
263
|
+
return string2.slice(0, overlapCount(string1, string2));
|
|
264
|
+
}
|
|
265
|
+
function overlapCount(a, b) {
|
|
266
|
+
var startA = 0;
|
|
267
|
+
if (a.length > b.length) {
|
|
268
|
+
startA = a.length - b.length;
|
|
269
|
+
}
|
|
270
|
+
var endB = b.length;
|
|
271
|
+
if (a.length < b.length) {
|
|
272
|
+
endB = a.length;
|
|
273
|
+
}
|
|
274
|
+
var map = Array(endB);
|
|
275
|
+
var k = 0;
|
|
276
|
+
map[0] = 0;
|
|
277
|
+
for (var j = 1; j < endB; j++) {
|
|
278
|
+
if (b[j] == b[k]) {
|
|
279
|
+
map[j] = map[k];
|
|
280
|
+
} else {
|
|
281
|
+
map[j] = k;
|
|
282
|
+
}
|
|
283
|
+
while (k > 0 && b[j] != b[k]) {
|
|
284
|
+
k = map[k];
|
|
285
|
+
}
|
|
286
|
+
if (b[j] == b[k]) {
|
|
287
|
+
k++;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
k = 0;
|
|
291
|
+
for (var i = startA; i < a.length; i++) {
|
|
292
|
+
while (k > 0 && a[i] != b[k]) {
|
|
293
|
+
k = map[k];
|
|
294
|
+
}
|
|
295
|
+
if (a[i] == b[k]) {
|
|
296
|
+
k++;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return k;
|
|
300
|
+
}
|
|
301
|
+
var extendedWordChars = "a-zA-Z0-9_\\u{C0}-\\u{FF}\\u{D8}-\\u{F6}\\u{F8}-\\u{2C6}\\u{2C8}-\\u{2D7}\\u{2DE}-\\u{2FF}\\u{1E00}-\\u{1EFF}";
|
|
302
|
+
var tokenizeIncludingWhitespace = new RegExp("[".concat(extendedWordChars, "]+|\\s+|[^").concat(extendedWordChars, "]"), "ug");
|
|
303
|
+
var wordDiff = new Diff();
|
|
304
|
+
wordDiff.equals = function(left, right, options) {
|
|
305
|
+
if (options.ignoreCase) {
|
|
306
|
+
left = left.toLowerCase();
|
|
307
|
+
right = right.toLowerCase();
|
|
308
|
+
}
|
|
309
|
+
return left.trim() === right.trim();
|
|
310
|
+
};
|
|
311
|
+
wordDiff.tokenize = function(value) {
|
|
312
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
313
|
+
var parts;
|
|
314
|
+
if (options.intlSegmenter) {
|
|
315
|
+
if (options.intlSegmenter.resolvedOptions().granularity != "word") {
|
|
316
|
+
throw new Error('The segmenter passed must have a granularity of "word"');
|
|
317
|
+
}
|
|
318
|
+
parts = Array.from(options.intlSegmenter.segment(value), function(segment) {
|
|
319
|
+
return segment.segment;
|
|
320
|
+
});
|
|
321
|
+
} else {
|
|
322
|
+
parts = value.match(tokenizeIncludingWhitespace) || [];
|
|
323
|
+
}
|
|
324
|
+
var tokens = [];
|
|
325
|
+
var prevPart = null;
|
|
326
|
+
parts.forEach(function(part) {
|
|
327
|
+
if (/\s/.test(part)) {
|
|
328
|
+
if (prevPart == null) {
|
|
329
|
+
tokens.push(part);
|
|
330
|
+
} else {
|
|
331
|
+
tokens.push(tokens.pop() + part);
|
|
332
|
+
}
|
|
333
|
+
} else if (/\s/.test(prevPart)) {
|
|
334
|
+
if (tokens[tokens.length - 1] == prevPart) {
|
|
335
|
+
tokens.push(tokens.pop() + part);
|
|
336
|
+
} else {
|
|
337
|
+
tokens.push(prevPart + part);
|
|
338
|
+
}
|
|
339
|
+
} else {
|
|
340
|
+
tokens.push(part);
|
|
341
|
+
}
|
|
342
|
+
prevPart = part;
|
|
343
|
+
});
|
|
344
|
+
return tokens;
|
|
345
|
+
};
|
|
346
|
+
wordDiff.join = function(tokens) {
|
|
347
|
+
return tokens.map(function(token, i) {
|
|
348
|
+
if (i == 0) {
|
|
349
|
+
return token;
|
|
350
|
+
} else {
|
|
351
|
+
return token.replace(/^\s+/, "");
|
|
352
|
+
}
|
|
353
|
+
}).join("");
|
|
354
|
+
};
|
|
355
|
+
wordDiff.postProcess = function(changes, options) {
|
|
356
|
+
if (!changes || options.oneChangePerToken) {
|
|
357
|
+
return changes;
|
|
358
|
+
}
|
|
359
|
+
var lastKeep = null;
|
|
360
|
+
var insertion = null;
|
|
361
|
+
var deletion = null;
|
|
362
|
+
changes.forEach(function(change) {
|
|
363
|
+
if (change.added) {
|
|
364
|
+
insertion = change;
|
|
365
|
+
} else if (change.removed) {
|
|
366
|
+
deletion = change;
|
|
367
|
+
} else {
|
|
368
|
+
if (insertion || deletion) {
|
|
369
|
+
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, change);
|
|
370
|
+
}
|
|
371
|
+
lastKeep = change;
|
|
372
|
+
insertion = null;
|
|
373
|
+
deletion = null;
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
if (insertion || deletion) {
|
|
377
|
+
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, null);
|
|
378
|
+
}
|
|
379
|
+
return changes;
|
|
380
|
+
};
|
|
381
|
+
function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep) {
|
|
382
|
+
if (deletion && insertion) {
|
|
383
|
+
var oldWsPrefix = deletion.value.match(/^\s*/)[0];
|
|
384
|
+
var oldWsSuffix = deletion.value.match(/\s*$/)[0];
|
|
385
|
+
var newWsPrefix = insertion.value.match(/^\s*/)[0];
|
|
386
|
+
var newWsSuffix = insertion.value.match(/\s*$/)[0];
|
|
387
|
+
if (startKeep) {
|
|
388
|
+
var commonWsPrefix = longestCommonPrefix(oldWsPrefix, newWsPrefix);
|
|
389
|
+
startKeep.value = replaceSuffix(startKeep.value, newWsPrefix, commonWsPrefix);
|
|
390
|
+
deletion.value = removePrefix(deletion.value, commonWsPrefix);
|
|
391
|
+
insertion.value = removePrefix(insertion.value, commonWsPrefix);
|
|
392
|
+
}
|
|
393
|
+
if (endKeep) {
|
|
394
|
+
var commonWsSuffix = longestCommonSuffix(oldWsSuffix, newWsSuffix);
|
|
395
|
+
endKeep.value = replacePrefix(endKeep.value, newWsSuffix, commonWsSuffix);
|
|
396
|
+
deletion.value = removeSuffix(deletion.value, commonWsSuffix);
|
|
397
|
+
insertion.value = removeSuffix(insertion.value, commonWsSuffix);
|
|
398
|
+
}
|
|
399
|
+
} else if (insertion) {
|
|
400
|
+
if (startKeep) {
|
|
401
|
+
insertion.value = insertion.value.replace(/^\s*/, "");
|
|
402
|
+
}
|
|
403
|
+
if (endKeep) {
|
|
404
|
+
endKeep.value = endKeep.value.replace(/^\s*/, "");
|
|
405
|
+
}
|
|
406
|
+
} else if (startKeep && endKeep) {
|
|
407
|
+
var newWsFull = endKeep.value.match(/^\s*/)[0], delWsStart = deletion.value.match(/^\s*/)[0], delWsEnd = deletion.value.match(/\s*$/)[0];
|
|
408
|
+
var newWsStart = longestCommonPrefix(newWsFull, delWsStart);
|
|
409
|
+
deletion.value = removePrefix(deletion.value, newWsStart);
|
|
410
|
+
var newWsEnd = longestCommonSuffix(removePrefix(newWsFull, newWsStart), delWsEnd);
|
|
411
|
+
deletion.value = removeSuffix(deletion.value, newWsEnd);
|
|
412
|
+
endKeep.value = replacePrefix(endKeep.value, newWsFull, newWsEnd);
|
|
413
|
+
startKeep.value = replaceSuffix(startKeep.value, newWsFull, newWsFull.slice(0, newWsFull.length - newWsEnd.length));
|
|
414
|
+
} else if (endKeep) {
|
|
415
|
+
var endKeepWsPrefix = endKeep.value.match(/^\s*/)[0];
|
|
416
|
+
var deletionWsSuffix = deletion.value.match(/\s*$/)[0];
|
|
417
|
+
var overlap = maximumOverlap(deletionWsSuffix, endKeepWsPrefix);
|
|
418
|
+
deletion.value = removeSuffix(deletion.value, overlap);
|
|
419
|
+
} else if (startKeep) {
|
|
420
|
+
var startKeepWsSuffix = startKeep.value.match(/\s*$/)[0];
|
|
421
|
+
var deletionWsPrefix = deletion.value.match(/^\s*/)[0];
|
|
422
|
+
var _overlap = maximumOverlap(startKeepWsSuffix, deletionWsPrefix);
|
|
423
|
+
deletion.value = removePrefix(deletion.value, _overlap);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
var wordWithSpaceDiff = new Diff();
|
|
427
|
+
wordWithSpaceDiff.tokenize = function(value) {
|
|
428
|
+
var regex = new RegExp("(\\r?\\n)|[".concat(extendedWordChars, "]+|[^\\S\\n\\r]+|[^").concat(extendedWordChars, "]"), "ug");
|
|
429
|
+
return value.match(regex) || [];
|
|
430
|
+
};
|
|
431
|
+
var lineDiff = new Diff();
|
|
432
|
+
lineDiff.tokenize = function(value, options) {
|
|
433
|
+
if (options.stripTrailingCr) {
|
|
434
|
+
value = value.replace(/\r\n/g, "\n");
|
|
435
|
+
}
|
|
436
|
+
var retLines = [], linesAndNewlines = value.split(/(\n|\r\n)/);
|
|
437
|
+
if (!linesAndNewlines[linesAndNewlines.length - 1]) {
|
|
438
|
+
linesAndNewlines.pop();
|
|
439
|
+
}
|
|
440
|
+
for (var i = 0; i < linesAndNewlines.length; i++) {
|
|
441
|
+
var line = linesAndNewlines[i];
|
|
442
|
+
if (i % 2 && !options.newlineIsToken) {
|
|
443
|
+
retLines[retLines.length - 1] += line;
|
|
444
|
+
} else {
|
|
445
|
+
retLines.push(line);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return retLines;
|
|
449
|
+
};
|
|
450
|
+
lineDiff.equals = function(left, right, options) {
|
|
451
|
+
if (options.ignoreWhitespace) {
|
|
452
|
+
if (!options.newlineIsToken || !left.includes("\n")) {
|
|
453
|
+
left = left.trim();
|
|
454
|
+
}
|
|
455
|
+
if (!options.newlineIsToken || !right.includes("\n")) {
|
|
456
|
+
right = right.trim();
|
|
457
|
+
}
|
|
458
|
+
} else if (options.ignoreNewlineAtEof && !options.newlineIsToken) {
|
|
459
|
+
if (left.endsWith("\n")) {
|
|
460
|
+
left = left.slice(0, -1);
|
|
461
|
+
}
|
|
462
|
+
if (right.endsWith("\n")) {
|
|
463
|
+
right = right.slice(0, -1);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return Diff.prototype.equals.call(this, left, right, options);
|
|
467
|
+
};
|
|
468
|
+
function diffLines(oldStr, newStr, callback) {
|
|
469
|
+
return lineDiff.diff(oldStr, newStr, callback);
|
|
470
|
+
}
|
|
471
|
+
var sentenceDiff = new Diff();
|
|
472
|
+
sentenceDiff.tokenize = function(value) {
|
|
473
|
+
return value.split(/(\S.+?[.!?])(?=\s+|$)/);
|
|
474
|
+
};
|
|
475
|
+
var cssDiff = new Diff();
|
|
476
|
+
cssDiff.tokenize = function(value) {
|
|
477
|
+
return value.split(/([{}:;,]|\s+)/);
|
|
478
|
+
};
|
|
479
|
+
function _typeof(o) {
|
|
480
|
+
"@babel/helpers - typeof";
|
|
481
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o2) {
|
|
482
|
+
return typeof o2;
|
|
483
|
+
} : function(o2) {
|
|
484
|
+
return o2 && "function" == typeof Symbol && o2.constructor === Symbol && o2 !== Symbol.prototype ? "symbol" : typeof o2;
|
|
485
|
+
}, _typeof(o);
|
|
486
|
+
}
|
|
487
|
+
var jsonDiff = new Diff();
|
|
488
|
+
jsonDiff.useLongestToken = true;
|
|
489
|
+
jsonDiff.tokenize = lineDiff.tokenize;
|
|
490
|
+
jsonDiff.castInput = function(value, options) {
|
|
491
|
+
var undefinedReplacement = options.undefinedReplacement, _options$stringifyRep = options.stringifyReplacer, stringifyReplacer = _options$stringifyRep === void 0 ? function(k, v) {
|
|
492
|
+
return typeof v === "undefined" ? undefinedReplacement : v;
|
|
493
|
+
} : _options$stringifyRep;
|
|
494
|
+
return typeof value === "string" ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), stringifyReplacer, " ");
|
|
495
|
+
};
|
|
496
|
+
jsonDiff.equals = function(left, right, options) {
|
|
497
|
+
return Diff.prototype.equals.call(jsonDiff, left.replace(/,([\r\n])/g, "$1"), right.replace(/,([\r\n])/g, "$1"), options);
|
|
498
|
+
};
|
|
499
|
+
function canonicalize(obj, stack, replacementStack, replacer, key) {
|
|
500
|
+
stack = stack || [];
|
|
501
|
+
replacementStack = replacementStack || [];
|
|
502
|
+
if (replacer) {
|
|
503
|
+
obj = replacer(key, obj);
|
|
504
|
+
}
|
|
505
|
+
var i;
|
|
506
|
+
for (i = 0; i < stack.length; i += 1) {
|
|
507
|
+
if (stack[i] === obj) {
|
|
508
|
+
return replacementStack[i];
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
var canonicalizedObj;
|
|
512
|
+
if ("[object Array]" === Object.prototype.toString.call(obj)) {
|
|
513
|
+
stack.push(obj);
|
|
514
|
+
canonicalizedObj = new Array(obj.length);
|
|
515
|
+
replacementStack.push(canonicalizedObj);
|
|
516
|
+
for (i = 0; i < obj.length; i += 1) {
|
|
517
|
+
canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack, replacer, key);
|
|
518
|
+
}
|
|
519
|
+
stack.pop();
|
|
520
|
+
replacementStack.pop();
|
|
521
|
+
return canonicalizedObj;
|
|
522
|
+
}
|
|
523
|
+
if (obj && obj.toJSON) {
|
|
524
|
+
obj = obj.toJSON();
|
|
525
|
+
}
|
|
526
|
+
if (_typeof(obj) === "object" && obj !== null) {
|
|
527
|
+
stack.push(obj);
|
|
528
|
+
canonicalizedObj = {};
|
|
529
|
+
replacementStack.push(canonicalizedObj);
|
|
530
|
+
var sortedKeys = [], _key;
|
|
531
|
+
for (_key in obj) {
|
|
532
|
+
if (Object.prototype.hasOwnProperty.call(obj, _key)) {
|
|
533
|
+
sortedKeys.push(_key);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
sortedKeys.sort();
|
|
537
|
+
for (i = 0; i < sortedKeys.length; i += 1) {
|
|
538
|
+
_key = sortedKeys[i];
|
|
539
|
+
canonicalizedObj[_key] = canonicalize(obj[_key], stack, replacementStack, replacer, _key);
|
|
540
|
+
}
|
|
541
|
+
stack.pop();
|
|
542
|
+
replacementStack.pop();
|
|
543
|
+
} else {
|
|
544
|
+
canonicalizedObj = obj;
|
|
545
|
+
}
|
|
546
|
+
return canonicalizedObj;
|
|
547
|
+
}
|
|
548
|
+
var arrayDiff = new Diff();
|
|
549
|
+
arrayDiff.tokenize = function(value) {
|
|
550
|
+
return value.slice();
|
|
551
|
+
};
|
|
552
|
+
arrayDiff.join = arrayDiff.removeEmpty = function(value) {
|
|
553
|
+
return value;
|
|
554
|
+
};
|
|
555
|
+
const MAX_EDIT_LENGTH = 600;
|
|
556
|
+
const MAX_LINES_FOR_JSDIFF = 2e5;
|
|
557
|
+
const GIT_NUMSTAT_MAX_BUFFER = 64 * 1024 * 1024;
|
|
558
|
+
function countTextLines(text) {
|
|
559
|
+
if (text.length === 0) return 0;
|
|
560
|
+
return text.endsWith("\n") ? text.split("\n").length - 1 : text.split("\n").length;
|
|
561
|
+
}
|
|
562
|
+
function splitTextForDiffStats(text) {
|
|
563
|
+
if (text.length === 0) {
|
|
564
|
+
return [];
|
|
565
|
+
}
|
|
566
|
+
const lines = text.split("\n");
|
|
567
|
+
if (text.endsWith("\n")) {
|
|
568
|
+
lines.pop();
|
|
569
|
+
}
|
|
570
|
+
return lines;
|
|
571
|
+
}
|
|
572
|
+
function prefixSuffixLineCounts(oldText, newText) {
|
|
573
|
+
const oldLines = splitTextForDiffStats(oldText);
|
|
574
|
+
const newLines = splitTextForDiffStats(newText);
|
|
575
|
+
let prefix = 0;
|
|
576
|
+
while (prefix < oldLines.length && prefix < newLines.length && oldLines[prefix] === newLines[prefix]) {
|
|
577
|
+
prefix += 1;
|
|
578
|
+
}
|
|
579
|
+
let suffix = 0;
|
|
580
|
+
while (suffix < oldLines.length - prefix && suffix < newLines.length - prefix && oldLines[oldLines.length - 1 - suffix] === newLines[newLines.length - 1 - suffix]) {
|
|
581
|
+
suffix += 1;
|
|
582
|
+
}
|
|
583
|
+
return [newLines.length - prefix - suffix, oldLines.length - prefix - suffix];
|
|
584
|
+
}
|
|
585
|
+
function jsdiffLineCounts(oldText, newText) {
|
|
586
|
+
const totalLines = countTextLines(oldText) + countTextLines(newText);
|
|
587
|
+
if (totalLines > MAX_LINES_FOR_JSDIFF) {
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
const parts = diffLines(oldText, newText, { maxEditLength: MAX_EDIT_LENGTH });
|
|
591
|
+
if (!parts) {
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
let add = 0;
|
|
595
|
+
let del = 0;
|
|
596
|
+
for (const part of parts) {
|
|
597
|
+
if (part.added) add += part.count ?? 0;
|
|
598
|
+
else if (part.removed) del += part.count ?? 0;
|
|
599
|
+
}
|
|
600
|
+
return [add, del];
|
|
601
|
+
}
|
|
602
|
+
let gitAvailable;
|
|
603
|
+
function isGitAvailable() {
|
|
604
|
+
if (gitAvailable === void 0) {
|
|
605
|
+
try {
|
|
606
|
+
gitAvailable = spawnSync("git", ["--version"], { stdio: "ignore" }).status === 0;
|
|
607
|
+
} catch {
|
|
608
|
+
gitAvailable = false;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return gitAvailable;
|
|
612
|
+
}
|
|
613
|
+
const NUMSTAT_LINE = /^(\d+|-)\t(\d+|-)\t/m;
|
|
614
|
+
function gitNumstatLineCounts(oldText, newText) {
|
|
615
|
+
if (!isGitAvailable()) {
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
let dir;
|
|
619
|
+
try {
|
|
620
|
+
dir = mkdtempSync(path__default.join(os__default.tmpdir(), "lody-diff-"));
|
|
621
|
+
const oldPath = path__default.join(dir, "old");
|
|
622
|
+
const newPath = path__default.join(dir, "new");
|
|
623
|
+
writeFileSync(oldPath, oldText);
|
|
624
|
+
writeFileSync(newPath, newText);
|
|
625
|
+
const result = spawnSync("git", ["diff", "--no-index", "--numstat", "--", oldPath, newPath], {
|
|
626
|
+
encoding: "utf8",
|
|
627
|
+
maxBuffer: GIT_NUMSTAT_MAX_BUFFER
|
|
628
|
+
});
|
|
629
|
+
if (result.status !== 0 && result.status !== 1) {
|
|
630
|
+
return null;
|
|
631
|
+
}
|
|
632
|
+
const match = NUMSTAT_LINE.exec(result.stdout ?? "");
|
|
633
|
+
if (!match) {
|
|
634
|
+
return result.status === 0 ? [0, 0] : null;
|
|
635
|
+
}
|
|
636
|
+
if (match[1] === "-" || match[2] === "-") {
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
const add = Number.parseInt(match[1] ?? "", 10);
|
|
640
|
+
const del = Number.parseInt(match[2] ?? "", 10);
|
|
641
|
+
if (!Number.isFinite(add) || !Number.isFinite(del)) {
|
|
642
|
+
return null;
|
|
643
|
+
}
|
|
644
|
+
return [add, del];
|
|
645
|
+
} catch {
|
|
646
|
+
return null;
|
|
647
|
+
} finally {
|
|
648
|
+
if (dir) {
|
|
649
|
+
try {
|
|
650
|
+
rmSync(dir, { recursive: true, force: true });
|
|
651
|
+
} catch {
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function computeTextLineCounts(oldText, newText) {
|
|
657
|
+
return jsdiffLineCounts(oldText, newText) ?? gitNumstatLineCounts(oldText, newText) ?? prefixSuffixLineCounts(oldText, newText);
|
|
658
|
+
}
|
|
659
|
+
function computeLineCounts(oldText, newText) {
|
|
660
|
+
if (oldText !== null && newText !== null) {
|
|
661
|
+
return computeTextLineCounts(oldText, newText);
|
|
662
|
+
}
|
|
663
|
+
if (oldText === null && newText !== null) {
|
|
664
|
+
return [countTextLines(newText), 0];
|
|
665
|
+
}
|
|
666
|
+
if (oldText !== null && newText === null) {
|
|
667
|
+
return [0, countTextLines(oldText)];
|
|
668
|
+
}
|
|
669
|
+
return [0, 0];
|
|
670
|
+
}
|
|
671
|
+
export {
|
|
672
|
+
countTextLines as a,
|
|
673
|
+
computeLineCounts as c,
|
|
674
|
+
prefixSuffixLineCounts as p
|
|
675
|
+
};
|