-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevisionHistory.js
More file actions
445 lines (395 loc) · 13.5 KB
/
RevisionHistory.js
File metadata and controls
445 lines (395 loc) · 13.5 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import DiffMatchPatch from 'diff-match-patch';
/**
* RevisionHistory class handles content revisions with memory-efficient diff storage
*/
class RevisionManager {
/**
* Creates a new RevisionManager instance
*/
constructor() {
this.initialContent = '';
this.initialContentType = ''; // Store the initial content type separately
this.contentType = ''; // Current content type
this.diffs = [];
this.currentIndex = -1;
this.diffEngine = new DiffMatchPatch();
}
/**
* Initializes the revision manager with initial content
*
* @param {string} content - The initial content
* @param {string} contentType - The content type
*/
initialize(content, contentType) {
this.initialContent = content;
this.initialContentType = contentType; // Store initial content type
this.contentType = contentType; // Current content type
this.diffs = [];
this.currentIndex = -1;
}
/**
* Adds a new revision
*
* @param {string} newContent - The new content to add as a revision
* @param {string} contentType - The content type of the revision
*/
addRevision(newContent, contentType) {
// Calculate diff between current state and new state
const currentContent = this.getCurrentContent();
const diff = this.diffEngine.diff_main(currentContent, newContent);
this.diffEngine.diff_cleanupSemantic(diff);
const patchText = this.diffEngine.patch_toText(
this.diffEngine.patch_make(currentContent, diff)
);
// Remove any forward history if we're branching
if (this.currentIndex < this.diffs.length - 1) {
this.diffs = this.diffs.slice(0, this.currentIndex + 1);
}
// Add new diff and update content type if it changed
this.diffs.push({
diff: patchText,
contentType: contentType !== this.contentType ? contentType : undefined
});
// Update the content type if it changed
if (contentType !== this.contentType) {
this.contentType = contentType;
}
this.currentIndex = this.diffs.length - 1;
}
/**
* Moves backward in the revision history
*
* @returns {Object|null} The previous revision state or null if at the beginning
*/
undo() {
if (this.currentIndex >= 0) {
this.currentIndex--;
return {
content: this.getCurrentContent(),
contentType: this.getCurrentContentType()
};
}
return null;
}
/**
* Moves forward in the revision history
*
* @returns {Object|null} The next revision state or null if at the end
*/
redo() {
if (this.currentIndex < this.diffs.length - 1) {
this.currentIndex++;
return {
content: this.getCurrentContent(),
contentType: this.getCurrentContentType()
};
}
return null;
}
/**
* Gets the content at the current revision point
*
* @returns {string} The current content
*/
getCurrentContent() {
if (this.currentIndex < 0) return this.initialContent;
// Apply all diffs up to currentIndex
let content = this.initialContent;
for (let i = 0; i <= this.currentIndex; i++) {
const patches = this.diffEngine.patch_fromText(this.diffs[i].diff);
const [patchedText] = this.diffEngine.patch_apply(patches, content);
content = patchedText;
}
return content;
}
/**
* Gets the content type at the current revision point
*
* @returns {string} The current content type
*/
getCurrentContentType() {
if (this.currentIndex < 0) return this.initialContentType;
// Find the last content type change up to the current index
let currentType = this.initialContentType;
for (let i = 0; i <= this.currentIndex; i++) {
if (this.diffs[i].contentType !== undefined) {
currentType = this.diffs[i].contentType;
}
}
return currentType;
}
/**
* Sets the revision to a specific index
*
* @param {number} index - The revision index to set
* @returns {Object|null} The revision state at the index or null if invalid
*/
setRevision(index) {
if (index >= -1 && index < this.diffs.length) {
this.currentIndex = index;
return {
content: this.getCurrentContent(),
contentType: this.getCurrentContentType()
};
}
return null;
}
/**
* Returns the total number of revisions
*
* @returns {number} The number of revisions
*/
getRevisionCount() {
return this.diffs.length;
}
/**
* Returns the current index in the revision history
*
* @returns {number} The current revision index
*/
getCurrentIndex() {
return this.currentIndex;
}
/**
* Gets the content at a specific revision index
*
* @param {number} index - Revision index (-1 for initial, 0 to n-1 for revisions)
* @returns {Object} Object with { content: string, contentType: string }
*/
getContentAtRevision(index) {
// Validate index
if (index < -1 || index >= this.diffs.length) {
throw new Error(`Invalid revision index: ${index}. Valid range: -1 to ${this.diffs.length - 1}`);
}
// Return initial content if index is -1
if (index === -1) {
return {
content: this.initialContent,
contentType: this.initialContentType
};
}
// Apply diffs up to the specified index to get content
let content = this.initialContent;
for (let i = 0; i <= index; i++) {
const patches = this.diffEngine.patch_fromText(this.diffs[i].diff);
const [patchedText] = this.diffEngine.patch_apply(patches, content);
content = patchedText;
}
// Find the content type at this revision
let currentType = this.initialContentType;
for (let i = 0; i <= index; i++) {
if (this.diffs[i].contentType !== undefined) {
currentType = this.diffs[i].contentType;
}
}
return {
content: content,
contentType: currentType
};
}
/**
* Computes diff between two revisions
*
* @param {number} fromIndex - Starting revision index (null for current)
* @param {number} toIndex - Ending revision index (null for current)
* @returns {Array} DiffMatchPatch diff array
*/
computeDiff(fromIndex = null, toIndex = null) {
// Default to comparing previous revision to current if not specified
if (fromIndex === null) {
fromIndex = this.currentIndex - 1;
}
if (toIndex === null) {
toIndex = this.currentIndex;
}
// Validate indices
if (fromIndex < -1 || fromIndex >= this.diffs.length) {
throw new Error(`Invalid fromIndex: ${fromIndex}. Valid range: -1 to ${this.diffs.length - 1}`);
}
if (toIndex < -1 || toIndex >= this.diffs.length) {
throw new Error(`Invalid toIndex: ${toIndex}. Valid range: -1 to ${this.diffs.length - 1}`);
}
if (fromIndex > toIndex) {
throw new Error(`fromIndex (${fromIndex}) cannot be greater than toIndex (${toIndex})`);
}
// Get content at both revisions
const fromContent = this.getContentAtRevision(fromIndex).content;
const toContent = this.getContentAtRevision(toIndex).content;
// Compute and return the diff
const diff = this.diffEngine.diff_main(fromContent, toContent);
this.diffEngine.diff_cleanupSemantic(diff);
return diff;
}
/**
* Computes line-based diff between revisions
*
* @param {number} fromIndex - Starting revision index (null for current - 1)
* @param {number} toIndex - Ending revision index (null for current)
* @returns {Array} Array of line diff objects with { type, content, oldLineNum, newLineNum }
*/
computeLineDiff(fromIndex = null, toIndex = null) {
// Default to comparing previous revision to current if not specified
if (fromIndex === null) {
fromIndex = this.currentIndex - 1;
}
if (toIndex === null) {
toIndex = this.currentIndex;
}
// Validate indices
if (fromIndex < -1 || fromIndex >= this.diffs.length) {
throw new Error(`Invalid fromIndex: ${fromIndex}. Valid range: -1 to ${this.diffs.length - 1}`);
}
if (toIndex < -1 || toIndex >= this.diffs.length) {
throw new Error(`Invalid toIndex: ${toIndex}. Valid range: -1 to ${this.diffs.length - 1}`);
}
if (fromIndex > toIndex) {
throw new Error(`fromIndex (${fromIndex}) cannot be greater than toIndex (${toIndex})`);
}
// Get the full content to properly handle line numbers
const fromContent = this.getContentAtRevision(fromIndex).content;
const toContent = this.getContentAtRevision(toIndex).content;
// Split content into lines for reference
const fromLines = fromContent.split('\n');
const toLines = toContent.split('\n');
// Use diff_match_patch to compute line-level diffs
const lineDiff = this.diffEngine.diff_linesToChars_(fromContent, toContent);
const diffs = this.diffEngine.diff_main(lineDiff.chars1, lineDiff.chars2, false);
this.diffEngine.diff_charsToLines_(diffs, lineDiff.lineArray);
// Convert to our format
const lineDiffs = [];
let oldLineNum = 1;
let newLineNum = 1;
for (const [operation, text] of diffs) {
const lines = text.split('\n').filter((line, idx, arr) =>
// Keep all lines except empty last one (which is just the trailing newline)
idx < arr.length - 1 || line !== ''
);
for (const line of lines) {
if (operation === DiffMatchPatch.DIFF_EQUAL) {
lineDiffs.push({
type: 'unchanged',
content: line,
oldLineNum: oldLineNum,
newLineNum: newLineNum
});
oldLineNum++;
newLineNum++;
} else if (operation === DiffMatchPatch.DIFF_DELETE) {
lineDiffs.push({
type: 'removed',
content: line,
oldLineNum: oldLineNum,
newLineNum: null
});
oldLineNum++;
} else if (operation === DiffMatchPatch.DIFF_INSERT) {
lineDiffs.push({
type: 'added',
content: line,
oldLineNum: null,
newLineNum: newLineNum
});
newLineNum++;
}
}
}
return lineDiffs;
}
/**
* Gets statistics about changes between revisions
*
* @param {number} fromIndex - Starting revision index (null for current - 1)
* @param {number} toIndex - Ending revision index (null for current)
* @returns {Object} Stats object with { additions, deletions, modifications, totalChanges }
*/
getDiffStats(fromIndex = null, toIndex = null) {
// Default to comparing previous revision to current if not specified
if (fromIndex === null) {
fromIndex = this.currentIndex - 1;
}
if (toIndex === null) {
toIndex = this.currentIndex;
}
// Validate indices (same validation as computeLineDiff)
if (fromIndex < -1 || fromIndex >= this.diffs.length) {
throw new Error(`Invalid fromIndex: ${fromIndex}. Valid range: -1 to ${this.diffs.length - 1}`);
}
if (toIndex < -1 || toIndex >= this.diffs.length) {
throw new Error(`Invalid toIndex: ${toIndex}. Valid range: -1 to ${this.diffs.length - 1}`);
}
if (fromIndex > toIndex) {
throw new Error(`fromIndex (${fromIndex}) cannot be greater than toIndex (${toIndex})`);
}
// Get the line diff
const lineDiff = this.computeLineDiff(fromIndex, toIndex);
// Calculate statistics
const stats = {
additions: 0,
deletions: 0,
modifications: 0,
totalChanges: 0
};
// Count different types of changes
for (let i = 0; i < lineDiff.length; i++) {
const line = lineDiff[i];
if (line.type === 'added') {
stats.additions++;
stats.totalChanges++;
} else if (line.type === 'removed') {
stats.deletions++;
stats.totalChanges++;
// Check if this is part of a modification (removed line followed by added line)
// This is a simplified heuristic - a more sophisticated approach would use
// line similarity analysis
if (i + 1 < lineDiff.length && lineDiff[i + 1].type === 'added') {
stats.modifications++;
// Don't double count the deletion and addition as separate changes
stats.totalChanges--;
}
}
}
return stats;
}
/**
* Gets metadata about a revision
*
* @param {number} index - Revision index (-1 for initial, 0+ for revisions)
* @returns {Object} Revision metadata including index, contentType, size, and other info
*/
getRevisionInfo(index) {
// Validate index
if (index < -1 || index >= this.diffs.length) {
throw new Error(`Invalid revision index: ${index}. Valid range: -1 to ${this.diffs.length - 1}`);
}
// Get the content at this revision
const { content, contentType } = this.getContentAtRevision(index);
// Build revision info object
const info = {
index: index,
contentType: contentType,
contentSize: content.length,
lineCount: content.split('\n').length,
isCurrent: index === this.currentIndex
};
// Add diff-specific info for non-initial revisions
if (index >= 0) {
info.diffSize = this.diffs[index].diff.length;
info.hasContentTypeChange = this.diffs[index].contentType !== undefined;
// Add stats comparing to previous revision
const stats = this.getDiffStats(index - 1, index);
info.changeStats = stats;
} else {
// Initial revision has no diff
info.diffSize = 0;
info.hasContentTypeChange = false;
info.changeStats = {
additions: 0,
deletions: 0,
modifications: 0,
totalChanges: 0
};
}
return info;
}
}
export { RevisionManager as RevisionHistory };