4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
6
import { CancellationToken } from '../../../../../base/common/cancellation.js' ;
7
- import { IDiffResult , IDiffChange } from '../../../../../base/common/diff/diff.js' ;
7
+ import { IDiffResult } from '../../../../../base/common/diff/diff.js' ;
8
8
import { Emitter , type IValueWithChangeEvent } from '../../../../../base/common/event.js' ;
9
9
import { Disposable , DisposableStore , dispose } from '../../../../../base/common/lifecycle.js' ;
10
10
import { Schemas } from '../../../../../base/common/network.js' ;
@@ -17,11 +17,12 @@ import { DiffElementCellViewModelBase, DiffElementPlaceholderViewModel, IDiffEle
17
17
import { NotebookDiffEditorEventDispatcher } from './eventDispatcher.js' ;
18
18
import { INotebookDiffViewModel , INotebookDiffViewModelUpdateEvent , NOTEBOOK_DIFF_ITEM_DIFF_STATE , NOTEBOOK_DIFF_ITEM_KIND } from './notebookDiffEditorBrowser.js' ;
19
19
import { NotebookTextModel } from '../../common/model/notebookTextModel.js' ;
20
- import { CellUri , INotebookDiffEditorModel , INotebookDiffResult } from '../../common/notebookCommon.js' ;
20
+ import { CellUri , INotebookDiffEditorModel } from '../../common/notebookCommon.js' ;
21
21
import { INotebookService } from '../../common/notebookService.js' ;
22
22
import { INotebookEditorWorkerService } from '../../common/services/notebookWorkerService.js' ;
23
23
import { IDiffEditorHeightCalculatorService } from './editorHeightCalculator.js' ;
24
24
import { raceCancellation } from '../../../../../base/common/async.js' ;
25
+ import { computeDiff } from '../../common/notebookDiff.js' ;
25
26
26
27
export class NotebookDiffViewModel extends Disposable implements INotebookDiffViewModel , IValueWithChangeEvent < readonly MultiDiffEditorItem [ ] > {
27
28
private readonly placeholderAndRelatedCells = new Map < DiffElementPlaceholderViewModel , DiffElementCellViewModelBase [ ] > ( ) ;
@@ -415,62 +416,7 @@ export type CellDiffInfo = {
415
416
modifiedCellIndex : number ;
416
417
type : 'insert' ;
417
418
} ;
418
- export function computeDiff ( originalModel : NotebookTextModel , modifiedModel : NotebookTextModel , diffResult : INotebookDiffResult ) {
419
- const cellChanges = diffResult . cellsDiff . changes ;
420
- const cellDiffInfo : CellDiffInfo [ ] = [ ] ;
421
- let originalCellIndex = 0 ;
422
- let modifiedCellIndex = 0 ;
423
-
424
- let firstChangeIndex = - 1 ;
425
-
426
- for ( let i = 0 ; i < cellChanges . length ; i ++ ) {
427
- const change = cellChanges [ i ] ;
428
- // common cells
429
-
430
- for ( let j = 0 ; j < change . originalStart - originalCellIndex ; j ++ ) {
431
- const originalCell = originalModel . cells [ originalCellIndex + j ] ;
432
- const modifiedCell = modifiedModel . cells [ modifiedCellIndex + j ] ;
433
- if ( originalCell . getHashValue ( ) === modifiedCell . getHashValue ( ) ) {
434
- cellDiffInfo . push ( {
435
- originalCellIndex : originalCellIndex + j ,
436
- modifiedCellIndex : modifiedCellIndex + j ,
437
- type : 'unchanged'
438
- } ) ;
439
- } else {
440
- if ( firstChangeIndex === - 1 ) {
441
- firstChangeIndex = cellDiffInfo . length ;
442
- }
443
- cellDiffInfo . push ( {
444
- originalCellIndex : originalCellIndex + j ,
445
- modifiedCellIndex : modifiedCellIndex + j ,
446
- type : 'modified'
447
- } ) ;
448
- }
449
- }
450
419
451
- const modifiedLCS = computeModifiedLCS ( change , originalModel , modifiedModel ) ;
452
- if ( modifiedLCS . length && firstChangeIndex === - 1 ) {
453
- firstChangeIndex = cellDiffInfo . length ;
454
- }
455
-
456
- cellDiffInfo . push ( ...modifiedLCS ) ;
457
- originalCellIndex = change . originalStart + change . originalLength ;
458
- modifiedCellIndex = change . modifiedStart + change . modifiedLength ;
459
- }
460
-
461
- for ( let i = originalCellIndex ; i < originalModel . cells . length ; i ++ ) {
462
- cellDiffInfo . push ( {
463
- originalCellIndex : i ,
464
- modifiedCellIndex : i - originalCellIndex + modifiedCellIndex ,
465
- type : 'unchanged'
466
- } ) ;
467
- }
468
-
469
- return {
470
- cellDiffInfo,
471
- firstChangeIndex
472
- } ;
473
- }
474
420
function isEqual ( cellDiffInfo : CellDiffInfo [ ] , viewModels : IDiffElementViewModelBase [ ] , model : INotebookDiffEditorModel ) {
475
421
if ( cellDiffInfo . length !== viewModels . length ) {
476
422
return false ;
@@ -510,40 +456,6 @@ function isEqual(cellDiffInfo: CellDiffInfo[], viewModels: IDiffElementViewModel
510
456
511
457
return true ;
512
458
}
513
-
514
- function computeModifiedLCS ( change : IDiffChange , originalModel : NotebookTextModel , modifiedModel : NotebookTextModel ) {
515
- const result : CellDiffInfo [ ] = [ ] ;
516
- // modified cells
517
- const modifiedLen = Math . min ( change . originalLength , change . modifiedLength ) ;
518
-
519
- for ( let j = 0 ; j < modifiedLen ; j ++ ) {
520
- const isTheSame = originalModel . cells [ change . originalStart + j ] . equal ( modifiedModel . cells [ change . modifiedStart + j ] ) ;
521
- result . push ( {
522
- originalCellIndex : change . originalStart + j ,
523
- modifiedCellIndex : change . modifiedStart + j ,
524
- type : isTheSame ? 'unchanged' : 'modified'
525
- } ) ;
526
- }
527
-
528
- for ( let j = modifiedLen ; j < change . originalLength ; j ++ ) {
529
- // deletion
530
- result . push ( {
531
- originalCellIndex : change . originalStart + j ,
532
- type : 'delete'
533
- } ) ;
534
- }
535
-
536
- for ( let j = modifiedLen ; j < change . modifiedLength ; j ++ ) {
537
- result . push ( {
538
- modifiedCellIndex : change . modifiedStart + j ,
539
- type : 'insert'
540
- } ) ;
541
- }
542
-
543
- return result ;
544
- }
545
-
546
-
547
459
export abstract class NotebookMultiDiffEditorItem extends MultiDiffEditorItem {
548
460
constructor (
549
461
originalUri : URI | undefined ,
0 commit comments