Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/node/diffWorkerMain.ts
13394 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 { parentPort } from 'worker_threads';
7
import { DefaultLinesDiffComputer } from '../../../editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer.js';
8
import type { ILinesDiffComputerOptions } from '../../../editor/common/diff/linesDiffComputer.js';
9
import type { IDiffCountResult } from '../common/diffComputeService.js';
10
11
export function computeDiffCounts(originalText: string, modifiedText: string, timeoutMs: number): IDiffCountResult {
12
const originalLines = originalText.split(/\r\n|\r|\n/);
13
const modifiedLines = modifiedText.split(/\r\n|\r|\n/);
14
const diffComputer = new DefaultLinesDiffComputer();
15
const options: ILinesDiffComputerOptions = {
16
ignoreTrimWhitespace: true,
17
maxComputationTimeMs: timeoutMs,
18
computeMoves: false,
19
};
20
const result = diffComputer.computeDiff(originalLines, modifiedLines, options);
21
22
let added = 0;
23
let removed = 0;
24
for (const change of result.changes) {
25
removed += change.original.length;
26
added += change.modified.length;
27
}
28
29
return { added, removed };
30
}
31
32
function main() {
33
const port = parentPort;
34
if (!port) {
35
throw new Error('This module should only be used in a worker thread.');
36
}
37
38
port.on('message', ({ id, fn, args }: { id: number; fn: string; args: unknown[] }) => {
39
try {
40
if (fn === 'computeDiffCounts') {
41
const res = computeDiffCounts(args[0] as string, args[1] as string, args[2] as number);
42
port.postMessage({ id, res });
43
} else {
44
port.postMessage({ id, err: { message: `Unknown function: ${fn}` } });
45
}
46
} catch (err) {
47
const message = err instanceof Error ? err.message : String(err);
48
const stack = err instanceof Error ? err.stack : undefined;
49
port.postMessage({ id, err: { message, stack } });
50
}
51
});
52
}
53
54
main();
55
56