Path: blob/main/src/vs/platform/agentHost/node/diffWorkerMain.ts
13394 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { parentPort } from 'worker_threads';6import { DefaultLinesDiffComputer } from '../../../editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer.js';7import type { ILinesDiffComputerOptions } from '../../../editor/common/diff/linesDiffComputer.js';8import type { IDiffCountResult } from '../common/diffComputeService.js';910export function computeDiffCounts(originalText: string, modifiedText: string, timeoutMs: number): IDiffCountResult {11const originalLines = originalText.split(/\r\n|\r|\n/);12const modifiedLines = modifiedText.split(/\r\n|\r|\n/);13const diffComputer = new DefaultLinesDiffComputer();14const options: ILinesDiffComputerOptions = {15ignoreTrimWhitespace: true,16maxComputationTimeMs: timeoutMs,17computeMoves: false,18};19const result = diffComputer.computeDiff(originalLines, modifiedLines, options);2021let added = 0;22let removed = 0;23for (const change of result.changes) {24removed += change.original.length;25added += change.modified.length;26}2728return { added, removed };29}3031function main() {32const port = parentPort;33if (!port) {34throw new Error('This module should only be used in a worker thread.');35}3637port.on('message', ({ id, fn, args }: { id: number; fn: string; args: unknown[] }) => {38try {39if (fn === 'computeDiffCounts') {40const res = computeDiffCounts(args[0] as string, args[1] as string, args[2] as number);41port.postMessage({ id, res });42} else {43port.postMessage({ id, err: { message: `Unknown function: ${fn}` } });44}45} catch (err) {46const message = err instanceof Error ? err.message : String(err);47const stack = err instanceof Error ? err.stack : undefined;48port.postMessage({ id, err: { message, stack } });49}50});51}5253main();545556