@@ -8,20 +8,12 @@ import { Emitter, Event } from 'vs/base/common/event';
88import { Disposable , IDisposable } from 'vs/base/common/lifecycle' ;
99import { URI } from 'vs/base/common/uri' ;
1010import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel' ;
11- import { INotebookTextModel , NotebookCellOutputsSplice , NotebookCellTextModelSplice , NotebookDocumentMetadata , NotebookCellMetadata , ICellEditOperation , CellEditType , CellUri , ICellInsertEdit , NotebookCellsChangedEvent , CellKind , IProcessedOutput , notebookDocumentMetadataDefaults , diff , ICellDeleteEdit , NotebookCellsChangeType , ICellDto2 , IMainCellDto , ICellOutputEdit } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
11+ import { INotebookTextModel , NotebookCellOutputsSplice , NotebookCellTextModelSplice , NotebookDocumentMetadata , NotebookCellMetadata , ICellEditOperation , CellEditType , CellUri , NotebookCellsChangedEvent , CellKind , IProcessedOutput , notebookDocumentMetadataDefaults , diff , NotebookCellsChangeType , ICellDto2 , IMainCellDto } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
1212import { ITextSnapshot } from 'vs/editor/common/model' ;
1313import { IUndoRedoService , UndoRedoElementType , IUndoRedoElement , IResourceUndoRedoElement } from 'vs/platform/undoRedo/common/undoRedo' ;
1414import { InsertCellEdit , DeleteCellEdit , MoveCellEdit , SpliceCellsEdit } from 'vs/workbench/contrib/notebook/common/model/cellEdit' ;
1515import { ITextModelService } from 'vs/editor/common/services/resolverService' ;
1616
17- function compareRangesUsingEnds ( a : [ number , number ] , b : [ number , number ] ) : number {
18- if ( a [ 1 ] === b [ 1 ] ) {
19- return a [ 1 ] - b [ 1 ] ;
20-
21- }
22- return a [ 1 ] - b [ 1 ] ;
23- }
24-
2517export class NotebookTextModelSnapshot implements ITextSnapshot {
2618 // private readonly _pieces: Ce[] = [];
2719 private _index : number = - 1 ;
@@ -235,54 +227,36 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
235227 const oldViewCells = this . cells . slice ( 0 ) ;
236228 const oldMap = new Map ( this . _mapping ) ;
237229
238- let operations : ( { sortIndex : number ; start : number ; end : number ; } & ICellEditOperation ) [ ] = [ ] ;
239- for ( let i = 0 ; i < rawEdits . length ; i ++ ) {
240- if ( rawEdits [ i ] . editType === CellEditType . Insert ) {
241- const edit = rawEdits [ i ] as ICellInsertEdit ;
242- operations . push ( {
243- sortIndex : i ,
244- start : edit . index ,
245- end : edit . index ,
246- ...edit
247- } ) ;
248- } else {
249- const edit = rawEdits [ i ] as ICellDeleteEdit ;
250- operations . push ( {
251- sortIndex : i ,
252- start : edit . index ,
253- end : edit . index + edit . count ,
254- ...edit
255- } ) ;
256- }
257- }
258-
259- // const edits
260- operations = operations . sort ( ( a , b ) => {
261- const r = compareRangesUsingEnds ( [ a . start , a . end ] , [ b . start , b . end ] ) ;
262- if ( r === 0 ) {
263- return b . sortIndex - a . sortIndex ;
264- }
265- return - r ;
230+ const edits = rawEdits . map ( ( edit , index ) => {
231+ return {
232+ edit,
233+ end : edit . editType === CellEditType . Delete ? edit . index + edit . count : edit . index ,
234+ originalIndex : index ,
235+ } ;
236+ } ) . sort ( ( a , b ) => {
237+ return b . end - a . end || b . originalIndex - a . originalIndex ;
266238 } ) ;
267239
268- for ( let i = 0 ; i < operations . length ; i ++ ) {
269- switch ( operations [ i ] . editType ) {
240+ for ( const { edit } of edits ) {
241+ switch ( edit . editType ) {
270242 case CellEditType . Insert :
271- const insertEdit = operations [ i ] as ICellInsertEdit ;
272- const mainCells = insertEdit . cells . map ( cell => {
243+ const mainCells = edit . cells . map ( cell => {
273244 const cellHandle = this . _cellhandlePool ++ ;
274245 const cellUri = CellUri . generate ( this . uri , cellHandle ) ;
275246 return new NotebookCellTextModel ( cellUri , cellHandle , cell . source , cell . language , cell . cellKind , cell . outputs || [ ] , cell . metadata , this . _modelService ) ;
276247 } ) ;
277- this . insertNewCell ( insertEdit . index , mainCells , false ) ;
248+ this . insertNewCell ( edit . index , mainCells , false ) ;
278249 break ;
279250 case CellEditType . Delete :
280- this . removeCell ( operations [ i ] . index , operations [ i ] . end - operations [ i ] . start , false ) ;
251+ this . removeCell ( edit . index , edit . count , false ) ;
281252 break ;
282253 case CellEditType . Output :
283254 //TODO@joh ,@rebornix no event, no undo stop (?)
284- const cell = this . cells [ operations [ i ] . index ] ;
285- this . spliceNotebookCellOutputs ( cell . handle , [ [ 0 , cell . outputs . length , ( < ICellOutputEdit > operations [ i ] ) . outputs ] ] ) ;
255+ const cell = this . cells [ edit . index ] ;
256+ this . spliceNotebookCellOutputs ( cell . handle , [ [ 0 , cell . outputs . length , edit . outputs ] ] ) ;
257+ break ;
258+ case CellEditType . Metadata :
259+ this . changeCellMetadata ( this . cells [ edit . index ] . handle , edit . metadata ) ;
286260 break ;
287261 }
288262 }
0 commit comments