Skip to content

Commit

Permalink
applyEdit supports begin/end selections.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Sep 10, 2020
1 parent 2103630 commit f88fca5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/vs/workbench/api/browser/mainThreadNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
return false;
}
this._notebookService.transformEditsOutputs(textModel, cellEdits);
return textModel.applyEdit(modelVersionId, cellEdits, true);
return textModel.applyEdit(modelVersionId, cellEdits, true, undefined, () => undefined);
}

private _isDeltaEmpty(delta: INotebookDocumentsAndEditorsDelta) {
Expand Down Expand Up @@ -460,7 +460,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
this._notebookService.transformEditsOutputs(mainthreadTextModel, edits);
await new Promise(resolve => {
DOM.scheduleAtNextAnimationFrame(() => {
const ret = mainthreadTextModel!.applyEdit(mainthreadTextModel!.versionId, edits, true);
const ret = mainthreadTextModel!.applyEdit(mainthreadTextModel!.versionId, edits, true, undefined, () => undefined);
resolve(ret);
});
});
Expand Down Expand Up @@ -583,7 +583,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
index: textModel.cells.indexOf(cell),
splices
}
], true);
], true, undefined, () => undefined);
}

async $postMessage(editorId: string, forRendererId: string | undefined, value: any): Promise<boolean> {
Expand Down Expand Up @@ -618,7 +618,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
{
editType: CellEditType.Unknown
}
], true);
], true, undefined, () => undefined);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class BulkCellEdits {
// apply edits
const edits = group.map(entry => entry.cellEdit);
this._notebookService.transformEditsOutputs(ref.object.notebook, edits);
ref.object.notebook.applyEdit(ref.object.notebook.versionId, edits, true);
ref.object.notebook.applyEdit(ref.object.notebook.versionId, edits, true, undefined, () => undefined);
ref.dispose();

this._progress.report(undefined);
Expand Down
10 changes: 5 additions & 5 deletions src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ registerAction2(class extends NotebookCellAction {
return;
}

editor.viewModel.notebookDocument.applyEdit(editor.viewModel.notebookDocument.versionId, [{ editType: CellEditType.Output, index, outputs: [] }], true);
editor.viewModel.notebookDocument.applyEdit(editor.viewModel.notebookDocument.versionId, [{ editType: CellEditType.Output, index, outputs: [] }], true, undefined, () => undefined);

if (context.cell.metadata && context.cell.metadata?.runState !== NotebookCellRunState.Running) {
context.notebookEditor.viewModel!.notebookDocument.applyEdit(context.notebookEditor.viewModel!.notebookDocument.versionId, [{
Expand All @@ -1308,7 +1308,7 @@ registerAction2(class extends NotebookCellAction {
lastRunDuration: undefined,
statusMessage: undefined
}
}], true);
}], true, undefined, () => undefined);
}
}
});
Expand Down Expand Up @@ -1391,7 +1391,7 @@ export class ChangeCellLanguageAction extends NotebookCellAction {
context.notebookEditor.viewModel!.notebookDocument.applyEdit(
context.notebookEditor.viewModel!.notebookDocument.versionId,
[{ editType: CellEditType.CellLanguage, index, language: selection.languageId }],
true
true, undefined, () => undefined
);
}
}
Expand Down Expand Up @@ -1442,7 +1442,7 @@ registerAction2(class extends NotebookAction {
editor.viewModel.notebookDocument.applyEdit(editor.viewModel.notebookDocument.versionId,
editor.viewModel.notebookDocument.cells.map((cell, index) => ({
editType: CellEditType.Output, index, outputs: []
})), true);
})), true, undefined, () => undefined);
}
});

Expand Down Expand Up @@ -1576,7 +1576,7 @@ abstract class ChangeNotebookCellMetadataAction extends NotebookCellAction {
return;
}

textModel.applyEdit(textModel.versionId, [{ editType: CellEditType.Metadata, index, metadata: { ...context.cell.metadata, ...this.getMetadataDelta() } }], true);
textModel.applyEdit(textModel.versionId, [{ editType: CellEditType.Metadata, index, metadata: { ...context.cell.metadata, ...this.getMetadataDelta() } }], true, undefined, () => undefined);
}

abstract getMetadataDelta(): NotebookCellMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ abstract class AbstractCellRenderer extends Disposable {
this.notebookEditor.textModel!.applyEdit(
this.notebookEditor.textModel!.versionId,
[{ editType: CellEditType.CellLanguage, index, language: newLangauge }],
true
true,
undefined,
() => undefined
);
}

Expand All @@ -481,7 +483,7 @@ abstract class AbstractCellRenderer extends Disposable {

this.notebookEditor.textModel!.applyEdit(this.notebookEditor.textModel!.versionId, [
{ editType: CellEditType.Metadata, index, metadata: result }
], true);
], true, undefined, () => undefined);
} catch {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,11 @@ abstract class AbstractCellRenderer {
if (templateData.currentRenderedCell.metadata?.inputCollapsed) {
textModel.applyEdit(textModel.versionId, [
{ editType: CellEditType.Metadata, index, metadata: { ...templateData.currentRenderedCell.metadata, inputCollapsed: false } }
], true);
], true, undefined, () => undefined);
} else if (templateData.currentRenderedCell.metadata?.outputCollapsed) {
textModel.applyEdit(textModel.versionId, [
{ editType: CellEditType.Metadata, index, metadata: { ...templateData.currentRenderedCell.metadata, outputCollapsed: false } }
], true);
], true, undefined, () => undefined);
}
}));
}
Expand Down
56 changes: 34 additions & 22 deletions src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,39 @@ class StackOperation implements IResourceUndoRedoElement {
type: UndoRedoElementType.Resource;

private _operations: IUndoRedoElement[] = [];
private _beginSelectionState: number[] | undefined = undefined;
private _resultSelectionState: number[] | undefined = undefined;

constructor(readonly resource: URI, readonly label: string, private _delayedEmitter: DelayedEmitter) {
constructor(readonly resource: URI, readonly label: string, private _delayedEmitter: DelayedEmitter, selectionState: number[] | undefined) {
this.type = UndoRedoElementType.Resource;
this._beginSelectionState = selectionState;
}

pushEditOperation(element: IUndoRedoElement) {
pushEndSelectionState(selectionState: number[] | undefined) {
this._resultSelectionState = selectionState;
}
pushEditOperation(element: IUndoRedoElement, beginSelectionState: number[] | undefined, resultSelectionState: number[] | undefined) {
if (this._operations.length === 0) {
this._beginSelectionState = this._beginSelectionState || beginSelectionState;
}
this._operations.push(element);
this._resultSelectionState = resultSelectionState;
}

async undo(): Promise<void> {
this._delayedEmitter.beginDeferredEmit();
for (let i = this._operations.length - 1; i >= 0; i--) {
await this._operations[i].undo();
}
this._delayedEmitter.endDeferredEmit();
this._delayedEmitter.endDeferredEmit(this._beginSelectionState);
}

async redo(): Promise<void> {
this._delayedEmitter.beginDeferredEmit();
for (let i = 0; i < this._operations.length; i++) {
await this._operations[i].redo();
}
this._delayedEmitter.endDeferredEmit();
this._delayedEmitter.endDeferredEmit(this._resultSelectionState);
}
}

Expand All @@ -90,19 +100,20 @@ export class NotebookOperationManager {

}

pushStackElement(label: string) {
pushStackElement(label: string, selectionState: number[] | undefined) {
if (this._pendingStackOperation) {
this._pendingStackOperation.pushEndSelectionState(selectionState);
this._undoService.pushElement(this._pendingStackOperation);
this._pendingStackOperation = null;
return;
}

this._pendingStackOperation = new StackOperation(this._resource, label, this._delayedEmitter);
this._pendingStackOperation = new StackOperation(this._resource, label, this._delayedEmitter, selectionState);
}

pushEditOperation(element: IUndoRedoElement) {
pushEditOperation(element: IUndoRedoElement, beginSelectionState: number[] | undefined, resultSelectionState: number[] | undefined) {
if (this._pendingStackOperation) {
this._pendingStackOperation.pushEditOperation(element);
this._pendingStackOperation.pushEditOperation(element, beginSelectionState, resultSelectionState);
return;
}

Expand All @@ -126,7 +137,7 @@ class DelayedEmitter {
this._deferredCnt++;
}

endDeferredEmit(): void {
endDeferredEmit(endSelections: number[] | undefined): void {
this._deferredCnt--;
if (this._deferredCnt === 0) {
this._computeEndState();
Expand All @@ -136,7 +147,7 @@ class DelayedEmitter {
{
rawEvents: this._notebookTextModelChangedEvent.rawEvents,
versionId: this._textModel.versionId,
endSelections: this._notebookTextModelChangedEvent.endSelections,
endSelections: endSelections || this._notebookTextModelChangedEvent.endSelections,
synchronous: this._notebookTextModelChangedEvent.synchronous
}
);
Expand Down Expand Up @@ -287,17 +298,17 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
return new NotebookCellTextModel(cellUri, cellHandle, source, language, cellKind, outputs || [], metadata || {}, this.transientOptions, this._modelService);
}

pushStackElement(label: string) {
this._operationManager.pushStackElement(label);
pushStackElement(label: string, selectionState: number[] | undefined) {
this._operationManager.pushStackElement(label, selectionState);
}

applyEdit(modelVersionId: number, rawEdits: ICellEditOperation[], synchronous: boolean): boolean {
applyEdit(modelVersionId: number, rawEdits: ICellEditOperation[], synchronous: boolean, beginSelectionState: number[] | undefined, endSelectionsComputer: () => number[] | undefined): boolean {
if (modelVersionId !== this._versionId) {
return false;
}

this._eventEmitter.beginDeferredEmit();
this.pushStackElement('edit');
this.pushStackElement('edit', beginSelectionState);

const edits = rawEdits.map((edit, index) => {
return {
Expand Down Expand Up @@ -357,8 +368,9 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
}
}

this.pushStackElement('edit');
this._eventEmitter.endDeferredEmit();
const endSelections = endSelectionsComputer();
this.pushStackElement('edit', endSelections);
this._eventEmitter.endDeferredEmit(endSelections);
return true;
}

Expand All @@ -377,7 +389,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
redo: async () => {
redo();
},
});
}, undefined, undefined);

this._eventEmitter.emit({
kind: NotebookCellsChangeType.Unknown,
Expand Down Expand Up @@ -442,7 +454,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this._operationManager.pushEditOperation(new SpliceCellsEdit(this.uri, undoDiff, {
insertCell: (index, cell, endSelections?: number[]) => { this._insertNewCell(index, [cell], true, endSelections); },
deleteCell: (index, endSelections?: number[]) => { this._removeCell(index, 1, true, endSelections); },
}, undefined, undefined));
}, undefined, undefined), undefined, undefined);

// should be deferred
this._eventEmitter.emit({
Expand Down Expand Up @@ -542,7 +554,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
}
this._changeCellMetadata(cell.handle, newMetadata, false);
}
}));
}), undefined, undefined);
}
// should be deferred
cell.metadata = metadata;
Expand Down Expand Up @@ -589,7 +601,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
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), beforeSelections, endSelections);
}

this._insertNewCell(index, [cell], synchronous, endSelections);
Expand All @@ -601,7 +613,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this._operationManager.pushEditOperation(new DeleteCellEdit(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), beforeSelections, endSelections);
}

this._removeCell(index, 1, synchronous, endSelections);
Expand All @@ -613,7 +625,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
moveCell: (fromIndex: number, length: number, toIndex: number, beforeSelections: number[] | undefined, endSelections: number[] | undefined) => {
this.moveCellToIdx(fromIndex, length, toIndex, true, false, beforeSelections, endSelections);
},
}, beforeSelections, endSelections));
}, beforeSelections, endSelections), beforeSelections, endSelections);
}

this._assertIndex(index);
Expand Down
Loading

0 comments on commit f88fca5

Please sign in to comment.