diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index d1b72cac46f..058cebad5b7 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -1356,8 +1356,8 @@ declare module 'vscode' { export interface NotebookEditorCellEdit { replaceCells(from: number, to: number, cells: NotebookCellData[]): void; - replaceOutputs(index: number, outputs: CellOutput[]): void; + replaceMetadata(index: number, metadata: NotebookCellMetadata): void; /** @deprecated */ insert(index: number, content: string | string[], language: string, type: CellKind, outputs: CellOutput[], metadata: NotebookCellMetadata | undefined): void; diff --git a/src/vs/workbench/api/common/extHostNotebook.ts b/src/vs/workbench/api/common/extHostNotebook.ts index a2a995327a2..d7b304c7eee 100644 --- a/src/vs/workbench/api/common/extHostNotebook.ts +++ b/src/vs/workbench/api/common/extHostNotebook.ts @@ -511,6 +511,15 @@ export class NotebookEditorCellEditBuilder implements vscode.NotebookEditorCellE } } + replaceMetadata(index: number, metadata: vscode.NotebookCellMetadata): void { + this._throwIfFinalized(); + this._collectedEdits.push({ + editType: CellEditType.Metadata, + index, + metadata + }); + } + replaceOutputs(index: number, outputs: vscode.CellOutput[]): void { this._throwIfFinalized(); this._collectedEdits.push({ diff --git a/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts b/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts index 272b1ba7c29..905dae17035 100644 --- a/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts +++ b/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts @@ -8,20 +8,12 @@ import { Emitter, Event } from 'vs/base/common/event'; import { Disposable, IDisposable } from 'vs/base/common/lifecycle'; import { URI } from 'vs/base/common/uri'; import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel'; -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'; +import { INotebookTextModel, NotebookCellOutputsSplice, NotebookCellTextModelSplice, NotebookDocumentMetadata, NotebookCellMetadata, ICellEditOperation, CellEditType, CellUri, NotebookCellsChangedEvent, CellKind, IProcessedOutput, notebookDocumentMetadataDefaults, diff, NotebookCellsChangeType, ICellDto2, IMainCellDto } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { ITextSnapshot } from 'vs/editor/common/model'; import { IUndoRedoService, UndoRedoElementType, IUndoRedoElement, IResourceUndoRedoElement } from 'vs/platform/undoRedo/common/undoRedo'; import { InsertCellEdit, DeleteCellEdit, MoveCellEdit, SpliceCellsEdit } from 'vs/workbench/contrib/notebook/common/model/cellEdit'; import { ITextModelService } from 'vs/editor/common/services/resolverService'; -function compareRangesUsingEnds(a: [number, number], b: [number, number]): number { - if (a[1] === b[1]) { - return a[1] - b[1]; - - } - return a[1] - b[1]; -} - export class NotebookTextModelSnapshot implements ITextSnapshot { // private readonly _pieces: Ce[] = []; private _index: number = -1; @@ -235,54 +227,36 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel const oldViewCells = this.cells.slice(0); const oldMap = new Map(this._mapping); - let operations: ({ sortIndex: number; start: number; end: number; } & ICellEditOperation)[] = []; - for (let i = 0; i < rawEdits.length; i++) { - if (rawEdits[i].editType === CellEditType.Insert) { - const edit = rawEdits[i] as ICellInsertEdit; - operations.push({ - sortIndex: i, - start: edit.index, - end: edit.index, - ...edit - }); - } else { - const edit = rawEdits[i] as ICellDeleteEdit; - operations.push({ - sortIndex: i, - start: edit.index, - end: edit.index + edit.count, - ...edit - }); - } - } - - // const edits - operations = operations.sort((a, b) => { - const r = compareRangesUsingEnds([a.start, a.end], [b.start, b.end]); - if (r === 0) { - return b.sortIndex - a.sortIndex; - } - return -r; + const edits = rawEdits.map((edit, index) => { + return { + edit, + end: edit.editType === CellEditType.Delete ? edit.index + edit.count : edit.index, + originalIndex: index, + }; + }).sort((a, b) => { + return b.end - a.end || b.originalIndex - a.originalIndex; }); - for (let i = 0; i < operations.length; i++) { - switch (operations[i].editType) { + for (const { edit } of edits) { + switch (edit.editType) { case CellEditType.Insert: - const insertEdit = operations[i] as ICellInsertEdit; - const mainCells = insertEdit.cells.map(cell => { + const mainCells = edit.cells.map(cell => { const cellHandle = this._cellhandlePool++; const cellUri = CellUri.generate(this.uri, cellHandle); return new NotebookCellTextModel(cellUri, cellHandle, cell.source, cell.language, cell.cellKind, cell.outputs || [], cell.metadata, this._modelService); }); - this.insertNewCell(insertEdit.index, mainCells, false); + this.insertNewCell(edit.index, mainCells, false); break; case CellEditType.Delete: - this.removeCell(operations[i].index, operations[i].end - operations[i].start, false); + this.removeCell(edit.index, edit.count, false); break; case CellEditType.Output: //TODO@joh,@rebornix no event, no undo stop (?) - const cell = this.cells[operations[i].index]; - this.spliceNotebookCellOutputs(cell.handle, [[0, cell.outputs.length, (operations[i]).outputs]]); + const cell = this.cells[edit.index]; + this.spliceNotebookCellOutputs(cell.handle, [[0, cell.outputs.length, edit.outputs]]); + break; + case CellEditType.Metadata: + this.changeCellMetadata(this.cells[edit.index].handle, edit.metadata); break; } } diff --git a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts index ff722330fc1..98d437930ee 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts @@ -403,6 +403,7 @@ export const enum CellEditType { Insert = 1, Delete = 2, Output = 3, + Metadata = 4, } export interface ICellDto2 { @@ -427,11 +428,17 @@ export interface ICellDeleteEdit { export interface ICellOutputEdit { editType: CellEditType.Output; - index: number, - outputs: IProcessedOutput[] + index: number; + outputs: IProcessedOutput[]; } -export type ICellEditOperation = ICellInsertEdit | ICellDeleteEdit | ICellOutputEdit; +export interface ICellMetadataEdit { + editType: CellEditType.Metadata; + index: number; + metadata: NotebookCellMetadata; +} + +export type ICellEditOperation = ICellInsertEdit | ICellDeleteEdit | ICellOutputEdit | ICellMetadataEdit; export interface INotebookEditData { documentVersionId: number;