Skip to content

Commit 98dd195

Browse files
committed
hook native undo/redo with notebook
1 parent 215a116 commit 98dd195

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

extensions/vscode-notebook-tests/src/notebook.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ suite('notebook undo redo', () => {
663663

664664

665665
// undo should bring back the deleted cell, and revert to previous content and selection
666-
await vscode.commands.executeCommand('notebook.undo');
666+
await vscode.commands.executeCommand('undo');
667667
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 3);
668668
assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(vscode.notebook.activeNotebookEditor!.selection!), 1);
669669
assert.equal(vscode.notebook.activeNotebookEditor?.selection?.document.getText(), 'var abc = 0;');
@@ -729,7 +729,7 @@ suite('notebook undo redo', () => {
729729
assert.equal(cellOutputsAddedRet.cells[0].outputs.length, 1);
730730

731731
const cellOutputClear = getEventOncePromise<vscode.NotebookCellOutputsChangeEvent>(vscode.notebook.onDidChangeCellOutputs);
732-
await vscode.commands.executeCommand('notebook.undo');
732+
await vscode.commands.executeCommand('undo');
733733
const cellOutputsCleardRet = await cellOutputClear;
734734
assert.deepEqual(cellOutputsCleardRet, {
735735
document: vscode.notebook.activeNotebookEditor!.document,

src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts

-38
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ const EXECUTE_NOTEBOOK_COMMAND_ID = 'notebook.execute';
3030
const CANCEL_NOTEBOOK_COMMAND_ID = 'notebook.cancelExecution';
3131
const NOTEBOOK_FOCUS_TOP = 'notebook.focusTop';
3232
const NOTEBOOK_FOCUS_BOTTOM = 'notebook.focusBottom';
33-
const NOTEBOOK_REDO = 'notebook.redo';
34-
const NOTEBOOK_UNDO = 'notebook.undo';
3533
const NOTEBOOK_FOCUS_PREVIOUS_EDITOR = 'notebook.focusPreviousEditor';
3634
const NOTEBOOK_FOCUS_NEXT_EDITOR = 'notebook.focusNextEditor';
3735
const CLEAR_ALL_CELLS_OUTPUTS_COMMAND_ID = 'notebook.clearAllCellsOutputs';
@@ -1108,42 +1106,6 @@ registerAction2(class extends NotebookCellAction {
11081106
});
11091107

11101108

1111-
registerAction2(class extends NotebookAction {
1112-
constructor() {
1113-
super({
1114-
id: NOTEBOOK_UNDO,
1115-
title: localize('undo', 'Undo'),
1116-
keybinding: {
1117-
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)),
1118-
primary: KeyMod.CtrlCmd | KeyCode.KEY_Z,
1119-
weight: KeybindingWeight.WorkbenchContrib
1120-
}
1121-
});
1122-
}
1123-
1124-
async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
1125-
await context.notebookEditor.viewModel?.undo();
1126-
}
1127-
});
1128-
1129-
registerAction2(class extends NotebookAction {
1130-
constructor() {
1131-
super({
1132-
id: NOTEBOOK_REDO,
1133-
title: localize('redo', 'Redo'),
1134-
keybinding: {
1135-
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)),
1136-
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z,
1137-
weight: KeybindingWeight.WorkbenchContrib
1138-
}
1139-
});
1140-
}
1141-
1142-
async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
1143-
await context.notebookEditor.viewModel?.redo();
1144-
}
1145-
});
1146-
11471109
registerAction2(class extends NotebookAction {
11481110
constructor() {
11491111
super({

src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { CellKind, IProcessedOutput, IRenderOutput, NotebookCellMetadata, Notebo
2626
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
2727
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
2828
import { IMenu } from 'vs/platform/actions/common/actions';
29+
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2930

3031
export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('notebookFindWidgetFocused', false);
3132

@@ -643,3 +644,9 @@ export function getVisibleCells(cells: CellViewModel[], hiddenRanges: ICellRange
643644

644645
return result;
645646
}
647+
648+
export function getActiveNotebookEditor(editorService: IEditorService): INotebookEditor | undefined {
649+
// TODO can `isNotebookEditor` be on INotebookEditor to avoid a circular dependency?
650+
const activeEditorPane = editorService.activeEditorPane as unknown as { isNotebookEditor?: boolean } | undefined;
651+
return activeEditorPane?.isNotebookEditor ? (editorService.activeEditorPane?.getControl() as INotebookEditor) : undefined;
652+
}

src/vs/workbench/contrib/notebook/browser/notebookServiceImpl.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/mode
2121
import { INotebookService, IMainNotebookController } from 'vs/workbench/contrib/notebook/common/notebookService';
2222
import * as glob from 'vs/base/common/glob';
2323
import { basename } from 'vs/base/common/path';
24-
import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
24+
import { getActiveNotebookEditor, INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
2525
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2626
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
2727
import { Memento } from 'vs/workbench/common/memento';
@@ -32,6 +32,7 @@ import { flatten } from 'vs/base/common/arrays';
3232
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
3333
import { NotebookKernelProviderAssociationRegistry, updateNotebookKernelProvideAssociationSchema, NotebookViewTypesExtensionRegistry } from 'vs/workbench/contrib/notebook/browser/notebookKernelAssociation';
3434
import { PureNotebookOutputRenderer } from 'vs/workbench/contrib/notebook/browser/notebookPureOutputRenderer';
35+
import { RedoCommand, UndoCommand } from 'vs/editor/browser/editorExtensions';
3536

3637
function MODEL_ID(resource: URI): string {
3738
return resource.toString();
@@ -321,6 +322,27 @@ export class NotebookService extends Disposable implements INotebookService, ICu
321322
this._register(this._accessibilityService.onDidChangeScreenReaderOptimized(() => {
322323
updateOrder();
323324
}));
325+
326+
const PRIORITY = 105;
327+
this._register(UndoCommand.addImplementation(PRIORITY, () => {
328+
const editor = getActiveNotebookEditor(this._editorService);
329+
if (editor?.viewModel) {
330+
editor?.viewModel.undo();
331+
return true;
332+
}
333+
334+
return false;
335+
}));
336+
337+
this._register(RedoCommand.addImplementation(PRIORITY, () => {
338+
const editor = getActiveNotebookEditor(this._editorService);
339+
if (editor?.viewModel) {
340+
editor?.viewModel.redo();
341+
return true;
342+
}
343+
344+
return false;
345+
}));
324346
}
325347

326348
getViewTypes(): ICustomEditorInfo[] {

0 commit comments

Comments
 (0)