Skip to content

Commit

Permalink
Use the same undo redo group for notebook editing
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Sep 25, 2020
1 parent d026ae2 commit 3222df5
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 173 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 @@ -164,7 +164,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
return false;
}
this._notebookService.transformEditsOutputs(textModel, cellEdits);
return textModel.applyEdits(modelVersionId, cellEdits, true, undefined, () => undefined);
return textModel.applyEdits(modelVersionId, cellEdits, true, undefined, () => undefined, undefined);
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ResourceNotebookCellEdit extends ResourceEdit {
export class BulkCellEdits {

constructor(
undoRedoGroup: UndoRedoGroup,
private _undoRedoGroup: UndoRedoGroup,
private readonly _progress: IProgress<void>,
private readonly _edits: ResourceNotebookCellEdit[],
@INotebookService private readonly _notebookService: INotebookService,
Expand All @@ -53,7 +53,7 @@ export class BulkCellEdits {
// apply edits
const edits = group.map(entry => entry.cellEdit);
this._notebookService.transformEditsOutputs(ref.object.notebook, edits);
ref.object.notebook.applyEdits(ref.object.notebook.versionId, edits, true, undefined, () => undefined);
ref.object.notebook.applyEdits(ref.object.notebook.versionId, edits, true, undefined, () => undefined, this._undoRedoGroup);
ref.dispose();

this._progress.report(undefined);
Expand Down
12 changes: 6 additions & 6 deletions src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ export async function changeCellToKind(kind: CellKind, context: INotebookCellAct
metadata: cell.metadata,
}]
}
], true, undefined, () => undefined, true);
], true, undefined, () => undefined, undefined, true);
const newCell = notebookEditor.viewModel.viewCells[idx];

if (!newCell) {
Expand Down Expand Up @@ -1399,7 +1399,7 @@ registerAction2(class extends NotebookCellAction {
return;
}

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

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

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

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

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

Expand All @@ -505,7 +506,7 @@ abstract class AbstractCellRenderer extends Disposable {

this.notebookEditor.textModel!.applyEdits(this.notebookEditor.textModel!.versionId, [
{ editType: CellEditType.Metadata, index, metadata: result }
], true, undefined, () => undefined);
], true, undefined, () => 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.applyEdits(textModel.versionId, [
{ editType: CellEditType.Metadata, index, metadata: { ...templateData.currentRenderedCell.metadata, inputCollapsed: false } }
], true, undefined, () => undefined);
], true, undefined, () => undefined, undefined);
} else if (templateData.currentRenderedCell.metadata?.outputCollapsed) {
textModel.applyEdits(textModel.versionId, [
{ editType: CellEditType.Metadata, index, metadata: { ...templateData.currentRenderedCell.metadata, outputCollapsed: false } }
], true, undefined, () => undefined);
], true, undefined, () => undefined, undefined);
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class CellDragAndDropController extends Disposable {
}

private copyCells(draggedCells: ICellViewModel[], ontoCell: ICellViewModel, direction: 'above' | 'below') {
this.notebookEditor.textModel!.pushStackElement('Copy Cells', undefined);
this.notebookEditor.textModel!.pushStackElement('Copy Cells', undefined, undefined);
let firstNewCell: ICellViewModel | undefined = undefined;
let firstNewCellState: CellEditState = CellEditState.Preview;
for (let i = 0; i < draggedCells.length; i++) {
Expand All @@ -273,6 +273,6 @@ export class CellDragAndDropController extends Disposable {
this.notebookEditor.focusNotebookCell(firstNewCell, firstNewCellState === CellEditState.Editing ? 'editor' : 'container');
}

this.notebookEditor.textModel!.pushStackElement('Copy Cells', undefined);
this.notebookEditor.textModel!.pushStackElement('Copy Cells', undefined, undefined);
}
}
60 changes: 0 additions & 60 deletions src/vs/workbench/contrib/notebook/browser/viewModel/cellEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,63 +74,3 @@ export class JoinCellEdit implements IResourceUndoRedoElement {
this.cell.focusMode = CellFocusMode.Editor;
}
}


export class SplitCellEdit implements IResourceUndoRedoElement {
type: UndoRedoElementType.Resource = UndoRedoElementType.Resource;
label: string = 'Join Cell';
constructor(
public resource: URI,
private index: number,
private cell: BaseCellViewModel,
private selections: Selection[],
private cellContents: string[],
private language: string,
private cellKind: CellKind,
private editingDelegate: IViewCellEditingDelegate
) {

}

async undo(): Promise<void> {
if (!this.editingDelegate.deleteCell) {
throw new Error('Notebook Delete Cell not implemented for Undo/Redo');
}

await this.cell.resolveTextModel();
this.cell.textModel!.applyEdits([
{
range: this.cell.textModel!.getFullModelRange(),
text: this.cellContents.join('')
}
]);
this.cell.setSelections(this.selections);

for (let j = 1; j < this.cellContents.length; j++) {
this.editingDelegate.deleteCell(this.index + 1, j === this.cellContents.length - 1 ? [this.cell.handle] : undefined);
}

this.cell.focusMode = CellFocusMode.Editor;
}

async redo(): Promise<void> {
if (!this.editingDelegate.createCell) {
throw new Error('Notebook Insert Cell not implemented for Undo/Redo');
}

await this.cell.resolveTextModel();
this.cell.textModel!.applyEdits([
{ range: this.cell.textModel!.getFullModelRange(), text: this.cellContents[0] }
], false);

let insertIndex = this.index + 1;
let lastCell;
for (let j = 1; j < this.cellContents.length; j++, insertIndex++) {
lastCell = this.editingDelegate.createCell(insertIndex, this.cellContents[j], this.language, this.cellKind, {}, []);
}

if (lastCell) {
lastCell.focusMode = CellFocusMode.Editor;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/no
import { MarkdownRenderer } from 'vs/workbench/contrib/notebook/browser/view/renderers/mdRenderer';
import { dirname } from 'vs/base/common/resources';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { SplitCellEdit, JoinCellEdit } from 'vs/workbench/contrib/notebook/browser/viewModel/cellEdit';
import { BaseCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/baseCellViewModel';
import { JoinCellEdit } from 'vs/workbench/contrib/notebook/browser/viewModel/cellEdit';
import { PieceTreeTextBuffer } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBuffer';
import { MultiModelEditStackElement, SingleModelEditStackElement } from 'vs/editor/common/model/editStack';
import { ResourceNotebookCellEdit } from 'vs/workbench/contrib/bulkEdit/browser/bulkCellEdits';

export interface INotebookEditorViewState {
editingCells: { [key: number]: boolean };
Expand Down Expand Up @@ -640,7 +640,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
}
]
}
], synchronous, beforeSelections, () => undefined);
], synchronous, beforeSelections, () => undefined, undefined);
return this._viewCells[index];
}

Expand Down Expand Up @@ -673,6 +673,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
synchronous,
this.selectionHandles,
() => endSelections,
undefined,
pushUndoStop
);
}
Expand All @@ -698,7 +699,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
length,
newIdx
}
], synchronous, undefined, () => [viewCell.handle]);
], synchronous, undefined, () => [viewCell.handle], undefined);
return true;
}

Expand Down Expand Up @@ -782,47 +783,30 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD

const newLinesContents = this._computeCellLinesContents(cell, splitPoints);
if (newLinesContents) {
const editorSelections = cell.getSelections();
const language = cell.language;
const kind = cell.cellKind;
this._notebook.applyEdits(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,
index,
cell,
editorSelections,
newLinesContents,
language,
kind,
{
createCell: (index: number, source: string, language: string, type: CellKind, metadata: NotebookCellMetadata | undefined, outputs: IProcessedOutput[]) => {
return this.createCell(index, source, language, type, metadata, outputs, true, false) as BaseCellViewModel;
},
deleteCell: (index: number) => {
this.deleteCell(index, true, false);
}
}
));

const textModel = await cell.resolveTextModel();
await this._bulkEditService.apply(
[
new ResourceTextEdit(cell.uri, { range: textModel.getFullModelRange(), text: newLinesContents[0] }),
new ResourceNotebookCellEdit(this._notebook.uri,
{
editType: CellEditType.Replace,
index: index + 1,
count: 0,
cells: newLinesContents.slice(1).map(line => ({
cellKind: kind,
language,
source: line,
outputs: [],
metadata: {}
}))
}
)
],
{ quotableLabel: 'Split Notebook Cell' }
);
}
}

Expand Down
Loading

0 comments on commit 3222df5

Please sign in to comment.