From 833cbca95b3163b3d1c63515ee70633281deccc3 Mon Sep 17 00:00:00 2001 From: aamunger Date: Wed, 17 Aug 2022 16:13:01 -0700 Subject: [PATCH 1/4] enabled core cell delete for IW and undo operations --- src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts | 3 ++- .../contrib/notebook/browser/controller/cellOperations.ts | 2 +- .../contrib/notebook/browser/controller/editActions.ts | 5 ++--- .../notebook/browser/viewModel/notebookViewModelImpl.ts | 6 ------ 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts b/src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts index 0e870b7b6392e..c2dc0915eada9 100644 --- a/src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts +++ b/src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts @@ -72,7 +72,8 @@ export class BulkCellEdits { // apply edits const edits = group.map(entry => entry.cellEdit); - ref.object.notebook.applyEdits(edits, true, undefined, () => undefined, this._undoRedoGroup, true); + const computeUndo = !ref.object.isReadonly; + ref.object.notebook.applyEdits(edits, true, undefined, () => undefined, this._undoRedoGroup, computeUndo); ref.dispose(); this._progress.report(undefined); diff --git a/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts b/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts index 08f3f326881dc..fe5975a2c3df4 100644 --- a/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts +++ b/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts @@ -649,6 +649,6 @@ export function insertCellAtIndex(viewModel: NotebookViewModel, index: number, s } ] } - ], synchronous, { kind: SelectionStateType.Index, focus: viewModel.getFocus(), selections: viewModel.getSelections() }, () => endSelections, undefined, pushUndoStop); + ], synchronous, { kind: SelectionStateType.Index, focus: viewModel.getFocus(), selections: viewModel.getSelections() }, () => endSelections, undefined, pushUndoStop && !viewModel.options.isReadOnly); return viewModel.cellAt(index)!; } diff --git a/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts b/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts index 06e089a7c6b21..8efd2d548a864 100644 --- a/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts @@ -134,7 +134,7 @@ registerAction2(class DeleteCellAction extends NotebookCellAction { mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace }, - when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_EDITABLE, ContextKeyExpr.not(InputFocusedContextKey)), + when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)), weight: KeybindingWeight.WorkbenchContrib }, menu: [ @@ -145,7 +145,6 @@ registerAction2(class DeleteCellAction extends NotebookCellAction { }, { id: MenuId.InteractiveCellDelete, - when: NOTEBOOK_EDITOR_EDITABLE, group: CELL_TITLE_CELL_GROUP_ID } ], @@ -154,7 +153,7 @@ registerAction2(class DeleteCellAction extends NotebookCellAction { } async runWithContext(accessor: ServicesAccessor, context: INotebookCellActionContext) { - if (!context.notebookEditor.hasModel() || context.notebookEditor.isReadOnly) { + if (!context.notebookEditor.hasModel()) { return; } diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModelImpl.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModelImpl.ts index 080427cbac2bc..4229a1a96d972 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModelImpl.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModelImpl.ts @@ -954,9 +954,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD } async undo() { - if (this._options.isReadOnly) { - return null; - } const editStack = this._undoService.getElements(this.uri); const element = editStack.past.length ? editStack.past[editStack.past.length - 1] : undefined; @@ -974,9 +971,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD } async redo() { - if (this._options.isReadOnly) { - return null; - } const editStack = this._undoService.getElements(this.uri); const element = editStack.future[0]; From d4094a4204e12b3f61c1496b84c4799c7f70977b Mon Sep 17 00:00:00 2001 From: aamunger Date: Thu, 8 Sep 2022 13:03:36 -0700 Subject: [PATCH 2/4] don't allow undo for read-only notebooks --- .../contrib/notebook/browser/controller/cellOperations.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts b/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts index fe5975a2c3df4..23d16182f30fe 100644 --- a/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts +++ b/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts @@ -123,6 +123,7 @@ export function runDeleteAction(editor: IActiveNotebookEditor, cell: ICellViewMo const targetCellIndex = editor.getCellIndex(cell); const containingSelection = selections.find(selection => selection.start <= targetCellIndex && targetCellIndex < selection.end); + const computeUndoRedo = !editor.isReadOnly || textModel.viewType === 'interactive'; if (containingSelection) { const edits: ICellReplaceEdit[] = selections.reverse().map(selection => ({ editType: CellEditType.Replace, index: selection.start, count: selection.end - selection.start, cells: [] @@ -143,7 +144,7 @@ export function runDeleteAction(editor: IActiveNotebookEditor, cell: ICellViewMo return { kind: SelectionStateType.Index, focus: { start: 0, end: 0 }, selections: [{ start: 0, end: 0 }] }; } } - }, undefined, true); + }, undefined, computeUndoRedo); } else { const focus = editor.getFocus(); const edits: ICellReplaceEdit[] = [{ @@ -169,14 +170,14 @@ export function runDeleteAction(editor: IActiveNotebookEditor, cell: ICellViewMo textModel.applyEdits(edits, true, { kind: SelectionStateType.Index, focus: editor.getFocus(), selections: editor.getSelections() }, () => ({ kind: SelectionStateType.Index, focus: newFocus, selections: finalSelections - }), undefined, true); + }), undefined, computeUndoRedo); } else { // users decide to delete a cell out of current focus/selection const newFocus = focus.start > targetCellIndex ? { start: focus.start - 1, end: focus.end - 1 } : focus; textModel.applyEdits(edits, true, { kind: SelectionStateType.Index, focus: editor.getFocus(), selections: editor.getSelections() }, () => ({ kind: SelectionStateType.Index, focus: newFocus, selections: finalSelections - }), undefined, true); + }), undefined, computeUndoRedo); } } } From 9038464448f46c1430d641fd7f584a23b5c996c0 Mon Sep 17 00:00:00 2001 From: aamunger Date: Thu, 8 Sep 2022 15:49:46 -0700 Subject: [PATCH 3/4] more cases for restricting undo/redo --- .../notebook/browser/controller/editActions.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts b/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts index 8efd2d548a864..12782676c3e5b 100644 --- a/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/controller/editActions.ts @@ -201,7 +201,8 @@ registerAction2(class ClearCellOutputsAction extends NotebookCellAction { return; } - editor.textModel.applyEdits([{ editType: CellEditType.Output, index, outputs: [] }], true, undefined, () => undefined, undefined, true); + const computeUndoRedo = !editor.isReadOnly; + editor.textModel.applyEdits([{ editType: CellEditType.Output, index, outputs: [] }], true, undefined, () => undefined, undefined, computeUndoRedo); const runState = notebookExecutionStateService.getCellExecution(context.cell.uri)?.state; if (runState !== NotebookCellExecutionState.Executing) { @@ -213,7 +214,7 @@ registerAction2(class ClearCellOutputsAction extends NotebookCellAction { executionOrder: null, lastRunSuccess: null } - }], true, undefined, () => undefined, undefined, true); + }], true, undefined, () => undefined, undefined, computeUndoRedo); } } }); @@ -256,10 +257,11 @@ registerAction2(class ClearAllCellOutputsAction extends NotebookAction { return; } + const computeUndoRedo = !editor.isReadOnly; editor.textModel.applyEdits( editor.textModel.cells.map((cell, index) => ({ editType: CellEditType.Output, index, outputs: [] - })), true, undefined, () => undefined, undefined, true); + })), true, undefined, () => undefined, undefined, computeUndoRedo); const clearExecutionMetadataEdits = editor.textModel.cells.map((cell, index) => { const runState = notebookExecutionStateService.getCellExecution(cell.uri)?.state; @@ -278,7 +280,7 @@ registerAction2(class ClearAllCellOutputsAction extends NotebookAction { } }).filter(edit => !!edit) as ICellEditOperation[]; if (clearExecutionMetadataEdits.length) { - context.notebookEditor.textModel.applyEdits(clearExecutionMetadataEdits, true, undefined, () => undefined, undefined, true); + context.notebookEditor.textModel.applyEdits(clearExecutionMetadataEdits, true, undefined, () => undefined, undefined, computeUndoRedo); } } }); @@ -498,7 +500,7 @@ async function setCellToLanguage(languageId: string, context: IChangeCellContext const index = context.notebookEditor.textModel.cells.indexOf(context.cell.model); context.notebookEditor.textModel.applyEdits( [{ editType: CellEditType.CellLanguage, index, language: languageId }], - true, undefined, () => undefined, undefined, true + true, undefined, () => undefined, undefined, !context.notebookEditor.isReadOnly ); } } From 0f48a3598aa949358ed1f635938ee7f33a63a611 Mon Sep 17 00:00:00 2001 From: aamunger Date: Thu, 8 Sep 2022 15:55:06 -0700 Subject: [PATCH 4/4] don't add initialization edits to undo stack --- .../contrib/notebook/common/model/notebookTextModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts b/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts index a6f4d5253d0d1..a3b8945877ebf 100644 --- a/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts +++ b/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts @@ -402,7 +402,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel true, undefined, () => undefined, undefined, - true + false ); }