Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/diff/common/diffWorker.ts
13400 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 { DefaultLinesDiffComputer } from '../../../util/vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer';
7
import { ILinesDiffComputerOptions } from '../../../util/vs/editor/common/diff/linesDiffComputer';
8
import { DetailedLineRangeMapping } from '../../../util/vs/editor/common/diff/rangeMapping';
9
10
11
export async function computeDiff(original: string, modified: string, options: ILinesDiffComputerOptions): Promise<IDiffComputationResult> {
12
return computeDiffSync(original, modified, options);
13
}
14
15
export function computeDiffSync(original: string, modified: string, options: ILinesDiffComputerOptions): IDiffComputationResult {
16
const originalLines = original.split(/\r\n|\r|\n/);
17
const modifiedLines = modified.split(/\r\n|\r|\n/);
18
const diffComputer = new DefaultLinesDiffComputer();
19
const result = diffComputer.computeDiff(originalLines, modifiedLines, options);
20
21
const identical = (result.changes.length > 0 ? false : original === modified);
22
23
function getLineChanges(changes: readonly DetailedLineRangeMapping[]): ILineChange[] {
24
return changes.map(m => ([m.original.startLineNumber, m.original.endLineNumberExclusive, m.modified.startLineNumber, m.modified.endLineNumberExclusive, m.innerChanges?.map(m => [
25
m.originalRange.startLineNumber,
26
m.originalRange.startColumn,
27
m.originalRange.endLineNumber,
28
m.originalRange.endColumn,
29
m.modifiedRange.startLineNumber,
30
m.modifiedRange.startColumn,
31
m.modifiedRange.endLineNumber,
32
m.modifiedRange.endColumn,
33
])]));
34
}
35
36
return {
37
identical,
38
quitEarly: result.hitTimeout,
39
changes: getLineChanges(result.changes),
40
moves: result.moves.map(m => ([
41
m.lineRangeMapping.original.startLineNumber,
42
m.lineRangeMapping.original.endLineNumberExclusive,
43
m.lineRangeMapping.modified.startLineNumber,
44
m.lineRangeMapping.modified.endLineNumberExclusive,
45
getLineChanges(m.changes)
46
])),
47
};
48
}
49
50
export interface IDiffComputationResult {
51
quitEarly: boolean;
52
changes: ILineChange[];
53
identical: boolean;
54
moves: ITextMove[];
55
}
56
57
export type ILineChange = [
58
originalStartLine: number,
59
originalEndLine: number,
60
modifiedStartLine: number,
61
modifiedEndLine: number,
62
charChanges: ICharChange[] | undefined,
63
];
64
65
export type ICharChange = [
66
originalStartLine: number,
67
originalStartColumn: number,
68
originalEndLine: number,
69
originalEndColumn: number,
70
71
modifiedStartLine: number,
72
modifiedStartColumn: number,
73
modifiedEndLine: number,
74
modifiedEndColumn: number,
75
];
76
77
export type ITextMove = [
78
originalStartLine: number,
79
originalEndLine: number,
80
modifiedStartLine: number,
81
modifiedEndLine: number,
82
changes: ILineChange[],
83
];
84
85