Skip to content

Commit

Permalink
remove spliceNotebook api from textmodel.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Sep 11, 2020
1 parent 58d9622 commit 4aefd43
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,28 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
const newLinesContents = this._computeCellLinesContents(cell, splitPoints);
if (newLinesContents) {
const editorSelections = cell.getSelections();
this._notebook.splitNotebookCell(index, newLinesContents, this.selectionHandles);
const language = cell.language;
const kind = cell.cellKind;
this._notebook.applyEdit(this._notebook.versionId, [
{
editType: CellEditType.CellContent,
index,
range: undefined,
text: newLinesContents[0]
},
{
editType: CellEditType.Replace,
index: index + 1,
count: 0,
cells: newLinesContents.slice(1).map(line => ({
cellKind: kind,
language,
source: line,
outputs: [],
metadata: {}
}))
}
], true, undefined, () => this.selectionHandles, false);

this._undoService.pushElement(new SplitCellEdit(
this.uri,
Expand Down
63 changes: 13 additions & 50 deletions src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, dispose, 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, NotebookDocumentMetadata, NotebookCellMetadata, ICellEditOperation, CellEditType, CellUri, CellKind, IProcessedOutput, notebookDocumentMetadataDefaults, diff, NotebookCellsChangeType, ICellDto2, TransientOptions, NotebookTextModelChangedEvent, NotebookRawContentEvent } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookTextModel, NotebookCellOutputsSplice, NotebookDocumentMetadata, NotebookCellMetadata, ICellEditOperation, CellEditType, CellUri, notebookDocumentMetadataDefaults, diff, NotebookCellsChangeType, ICellDto2, TransientOptions, NotebookTextModelChangedEvent, NotebookRawContentEvent } 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, MoveCellEdit, SpliceCellsEdit, CellMetadataEdit } from 'vs/workbench/contrib/notebook/common/model/cellEdit';
import { MoveCellEdit, SpliceCellsEdit, CellMetadataEdit } from 'vs/workbench/contrib/notebook/common/model/cellEdit';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IRange } from 'vs/editor/common/core/range';

export class NotebookTextModelSnapshot implements ITextSnapshot {

Expand Down Expand Up @@ -353,6 +354,10 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
case CellEditType.Move:
this._moveCellToIdx(edit.index, edit.length, edit.newIdx, synchronous, computeUndoRedo, undefined, undefined);
break;
case CellEditType.CellContent:
// TODO@rebornix, _replaceCellContent is async and does not push undo element
this._replaceCellContent(this._cells[edit.index], edit.range, edit.text);
break;
case CellEditType.Unknown:
this._handleUnknownChange();
break;
Expand Down Expand Up @@ -600,62 +605,20 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
return true;
}

private _assertIndex(index: number) {
if (index < 0 || index >= this._cells.length) {
throw new Error(`model index out of range ${index}`);
}
}

//#region Split Cell API, should be replaced with applyEdit later on.

private _insertCell(index: number, cell: NotebookCellTextModel, synchronous: boolean, pushUndoStop: boolean, beforeSelections: number[] | undefined, endSelections: number[] | undefined): void {
if (pushUndoStop) {
this._operationManager.pushEditOperation(new InsertCellEdit(this.uri, index, cell, {
insertCell: (index, cell, endSelections) => { this._insertNewCell(index, [cell], true, endSelections); },
deleteCell: (index, endSelections) => { this._removeCell(index, 1, true, endSelections); },
}, beforeSelections, endSelections), beforeSelections, endSelections);
}

this._insertNewCell(index, [cell], synchronous, endSelections);
}

private _createCellTextModel(
source: string,
language: string,
cellKind: CellKind,
outputs: IProcessedOutput[],
metadata: NotebookCellMetadata | undefined
) {
const cellHandle = this._cellhandlePool++;
const cellUri = CellUri.generate(this.uri, cellHandle);
return new NotebookCellTextModel(cellUri, cellHandle, source, language, cellKind, outputs || [], metadata || {}, this.transientOptions, this._modelService);
}


async splitNotebookCell(index: number, newLinesContents: string[], endSelections: number[]) {
const cell = this._cells[index];

async _replaceCellContent(cell: NotebookCellTextModel, range: IRange | undefined, text: string) {
const ref = await cell.resolveTextModelRef();
const textModel = ref.object.textEditorModel;

textModel.applyEdits([
{ range: textModel.getFullModelRange(), text: newLinesContents[0] }
{ range: range || textModel.getFullModelRange(), text: text }
], false);

ref.dispose();
}

// create new cells based on the new text models
const language = cell.language;
const kind = cell.cellKind;
let insertIndex = index + 1;
const newCells = [];
for (let j = 1; j < newLinesContents.length; j++, insertIndex++) {
const cell = this._createCellTextModel(newLinesContents[j], language, kind, [], undefined);
this._insertCell(insertIndex, cell, true, false, undefined, j === newLinesContents.length - 1 ? endSelections : undefined);
newCells.push(cell);
private _assertIndex(index: number) {
if (index < 0 || index >= this._cells.length) {
throw new Error(`model index out of range ${index}`);
}
}
//#endregion


}
13 changes: 11 additions & 2 deletions src/vs/workbench/contrib/notebook/common/notebookCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IRevertOptions } from 'vs/workbench/common/editor';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { IDisposable } from 'vs/base/common/lifecycle';
import { IFileStatWithMetadata } from 'vs/platform/files/common/files';
import { IRange } from 'vs/editor/common/core/range';

export enum CellKind {
Markdown = 1,
Expand Down Expand Up @@ -424,7 +425,8 @@ export const enum CellEditType {
DocumentMetadata = 5,
OutputsSplice = 6,
Move = 7,
Unknown = 8
Unknown = 8,
CellContent = 9
}

export interface ICellDto2 {
Expand Down Expand Up @@ -479,11 +481,18 @@ export interface ICellMoveEdit {
newIdx: number;
}

export interface ICellContentEdit {
editType: CellEditType.CellContent;
index: number;
range: IRange | undefined;
text: string;
}

export interface IDocumentUnknownEdit {
editType: CellEditType.Unknown;
}

export type ICellEditOperation = ICellReplaceEdit | ICellOutputEdit | ICellMetadataEdit | ICellLanguageEdit | IDocumentMetadataEdit | ICellOutputsSpliceEdit | ICellMoveEdit | IDocumentUnknownEdit;
export type ICellEditOperation = ICellReplaceEdit | ICellOutputEdit | ICellMetadataEdit | ICellLanguageEdit | IDocumentMetadataEdit | ICellOutputsSpliceEdit | ICellMoveEdit | ICellContentEdit | IDocumentUnknownEdit;

export interface INotebookEditData {
documentVersionId: number;
Expand Down

0 comments on commit 4aefd43

Please sign in to comment.