Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/notebook/common/notebookDiff.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { DetailedLineRangeMapping } from '../../../util/vs/editor/common/diff/rangeMapping';
7
8
9
export type CellDiffInfo = {
10
originalCellIndex: number;
11
modifiedCellIndex: number;
12
type: 'unchanged';
13
} |
14
{
15
originalCellIndex: number;
16
type: 'delete';
17
} |
18
{
19
modifiedCellIndex: number;
20
type: 'insert';
21
};
22
23
export function computeDiff(originalModel: string[], modifiedModel: string[], cellChanges: readonly DetailedLineRangeMapping[]) {
24
const cellDiffInfo: CellDiffInfo[] = [];
25
let originalCellIndex = 0;
26
let modifiedCellIndex = 0;
27
28
for (let i = 0; i < cellChanges.length; i++) {
29
const change = cellChanges[i];
30
// common cells
31
32
for (let j = 0; j < change.original.startLineNumber - 1 - originalCellIndex; j++) {
33
cellDiffInfo.push({
34
originalCellIndex: originalCellIndex + j,
35
modifiedCellIndex: modifiedCellIndex + j,
36
type: 'unchanged'
37
});
38
}
39
40
const modifiedLCS = computeModifiedLCS(originalModel, modifiedModel, change);
41
42
cellDiffInfo.push(...modifiedLCS);
43
originalCellIndex = change.original.startLineNumber - 1 + change.original.length;
44
modifiedCellIndex = change.modified.startLineNumber - 1 + change.modified.length;
45
}
46
47
for (let i = originalCellIndex; i < originalModel.length; i++) {
48
cellDiffInfo.push({
49
originalCellIndex: i,
50
modifiedCellIndex: i - originalCellIndex + modifiedCellIndex,
51
type: 'unchanged'
52
});
53
}
54
55
return cellDiffInfo;
56
}
57
58
function computeModifiedLCS(original: string[], modified: string[], change: DetailedLineRangeMapping) {
59
const result: CellDiffInfo[] = [];
60
// modified cells
61
const modifiedLen = Math.min(change.original.length, change.modified.length);
62
63
for (let j = 0; j < modifiedLen; j++) {
64
const originalCell = original[change.original.startLineNumber - 1 + j];
65
const modifiedCell = modified[change.modified.startLineNumber - 1 + j];
66
if (originalCell !== modifiedCell) {
67
result.push({
68
originalCellIndex: change.original.startLineNumber - 1 + j,
69
type: 'delete'
70
});
71
result.push({
72
modifiedCellIndex: change.modified.startLineNumber - 1 + j,
73
type: 'insert'
74
});
75
} else {
76
result.push({
77
originalCellIndex: change.original.startLineNumber - 1 + j,
78
modifiedCellIndex: change.modified.startLineNumber - 1 + j,
79
type: 'unchanged'
80
});
81
}
82
}
83
84
for (let j = modifiedLen; j < change.original.length; j++) {
85
// deletion
86
result.push({
87
originalCellIndex: change.original.startLineNumber - 1 + j,
88
type: 'delete'
89
});
90
}
91
92
for (let j = modifiedLen; j < change.modified.length; j++) {
93
result.push({
94
modifiedCellIndex: change.modified.startLineNumber - 1 + j,
95
type: 'insert'
96
});
97
}
98
99
return result;
100
}
101
102