Path: blob/main/extensions/copilot/src/platform/diff/common/diffWorker.ts
13400 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 { DefaultLinesDiffComputer } from '../../../util/vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer';6import { ILinesDiffComputerOptions } from '../../../util/vs/editor/common/diff/linesDiffComputer';7import { DetailedLineRangeMapping } from '../../../util/vs/editor/common/diff/rangeMapping';8910export async function computeDiff(original: string, modified: string, options: ILinesDiffComputerOptions): Promise<IDiffComputationResult> {11return computeDiffSync(original, modified, options);12}1314export function computeDiffSync(original: string, modified: string, options: ILinesDiffComputerOptions): IDiffComputationResult {15const originalLines = original.split(/\r\n|\r|\n/);16const modifiedLines = modified.split(/\r\n|\r|\n/);17const diffComputer = new DefaultLinesDiffComputer();18const result = diffComputer.computeDiff(originalLines, modifiedLines, options);1920const identical = (result.changes.length > 0 ? false : original === modified);2122function getLineChanges(changes: readonly DetailedLineRangeMapping[]): ILineChange[] {23return changes.map(m => ([m.original.startLineNumber, m.original.endLineNumberExclusive, m.modified.startLineNumber, m.modified.endLineNumberExclusive, m.innerChanges?.map(m => [24m.originalRange.startLineNumber,25m.originalRange.startColumn,26m.originalRange.endLineNumber,27m.originalRange.endColumn,28m.modifiedRange.startLineNumber,29m.modifiedRange.startColumn,30m.modifiedRange.endLineNumber,31m.modifiedRange.endColumn,32])]));33}3435return {36identical,37quitEarly: result.hitTimeout,38changes: getLineChanges(result.changes),39moves: result.moves.map(m => ([40m.lineRangeMapping.original.startLineNumber,41m.lineRangeMapping.original.endLineNumberExclusive,42m.lineRangeMapping.modified.startLineNumber,43m.lineRangeMapping.modified.endLineNumberExclusive,44getLineChanges(m.changes)45])),46};47}4849export interface IDiffComputationResult {50quitEarly: boolean;51changes: ILineChange[];52identical: boolean;53moves: ITextMove[];54}5556export type ILineChange = [57originalStartLine: number,58originalEndLine: number,59modifiedStartLine: number,60modifiedEndLine: number,61charChanges: ICharChange[] | undefined,62];6364export type ICharChange = [65originalStartLine: number,66originalStartColumn: number,67originalEndLine: number,68originalEndColumn: number,6970modifiedStartLine: number,71modifiedStartColumn: number,72modifiedEndLine: number,73modifiedEndColumn: number,74];7576export type ITextMove = [77originalStartLine: number,78originalEndLine: number,79modifiedStartLine: number,80modifiedEndLine: number,81changes: ILineChange[],82];838485