Path: blob/main/extensions/copilot/src/platform/notebook/common/notebookDiff.ts
13401 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 { DetailedLineRangeMapping } from '../../../util/vs/editor/common/diff/rangeMapping';678export type CellDiffInfo = {9originalCellIndex: number;10modifiedCellIndex: number;11type: 'unchanged';12} |13{14originalCellIndex: number;15type: 'delete';16} |17{18modifiedCellIndex: number;19type: 'insert';20};2122export function computeDiff(originalModel: string[], modifiedModel: string[], cellChanges: readonly DetailedLineRangeMapping[]) {23const cellDiffInfo: CellDiffInfo[] = [];24let originalCellIndex = 0;25let modifiedCellIndex = 0;2627for (let i = 0; i < cellChanges.length; i++) {28const change = cellChanges[i];29// common cells3031for (let j = 0; j < change.original.startLineNumber - 1 - originalCellIndex; j++) {32cellDiffInfo.push({33originalCellIndex: originalCellIndex + j,34modifiedCellIndex: modifiedCellIndex + j,35type: 'unchanged'36});37}3839const modifiedLCS = computeModifiedLCS(originalModel, modifiedModel, change);4041cellDiffInfo.push(...modifiedLCS);42originalCellIndex = change.original.startLineNumber - 1 + change.original.length;43modifiedCellIndex = change.modified.startLineNumber - 1 + change.modified.length;44}4546for (let i = originalCellIndex; i < originalModel.length; i++) {47cellDiffInfo.push({48originalCellIndex: i,49modifiedCellIndex: i - originalCellIndex + modifiedCellIndex,50type: 'unchanged'51});52}5354return cellDiffInfo;55}5657function computeModifiedLCS(original: string[], modified: string[], change: DetailedLineRangeMapping) {58const result: CellDiffInfo[] = [];59// modified cells60const modifiedLen = Math.min(change.original.length, change.modified.length);6162for (let j = 0; j < modifiedLen; j++) {63const originalCell = original[change.original.startLineNumber - 1 + j];64const modifiedCell = modified[change.modified.startLineNumber - 1 + j];65if (originalCell !== modifiedCell) {66result.push({67originalCellIndex: change.original.startLineNumber - 1 + j,68type: 'delete'69});70result.push({71modifiedCellIndex: change.modified.startLineNumber - 1 + j,72type: 'insert'73});74} else {75result.push({76originalCellIndex: change.original.startLineNumber - 1 + j,77modifiedCellIndex: change.modified.startLineNumber - 1 + j,78type: 'unchanged'79});80}81}8283for (let j = modifiedLen; j < change.original.length; j++) {84// deletion85result.push({86originalCellIndex: change.original.startLineNumber - 1 + j,87type: 'delete'88});89}9091for (let j = modifiedLen; j < change.modified.length; j++) {92result.push({93modifiedCellIndex: change.modified.startLineNumber - 1 + j,94type: 'insert'95});96}9798return result;99}100101102