Skip to content

Commit

Permalink
Revert "Port #6025 to release (#6103)"
Browse files Browse the repository at this point in the history
This reverts commit 06f396a.
  • Loading branch information
joyceerhl committed Jun 4, 2021
1 parent 945916a commit 28bf740
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 5 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
([#5954](https://github.com/Microsoft/vscode-jupyter/issues/5954))
1. Tweak variable view fit and finish to match VS Code.
([#5955](https://github.com/Microsoft/vscode-jupyter/issues/5955))
1. Replace 'Run cells above' and 'Run cell and below' commands and cell toolbar buttons with VS Code's built-in 'Execute Above Cells' and 'Execute Cell And Below' commands and unified run button.
([#6025](https://github.com/microsoft/vscode-jupyter/issues/6025))

### Fixes

Expand Down
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@
},
"enablement": "notebookType == jupyter-notebook && isWorkspaceTrusted"
},
{
"command": "jupyter.notebookeditor.runallcellsabove",
"title": "%DataScience.runAbove%",
"category": "Notebook",
"icon": "$(run-above)",
"enablement": "notebookType == jupyter-notebook && isWorkspaceTrusted"
},
{
"command": "jupyter.notebookeditor.runcellandallbelow",
"title": "%DataScience.runBelow%",
"category": "Notebook",
"icon": "$(run-below)",
"enablement": "notebookType == jupyter-notebook && isWorkspaceTrusted"
},
{
"command": "jupyter.export",
"title": "%DataScience.notebookExportAs%",
Expand Down Expand Up @@ -844,6 +858,20 @@
"when": "notebookType == 'jupyter-notebook' && isWorkspaceTrusted"
}
],
"notebook/cell/title": [
{
"command": "jupyter.notebookeditor.runallcellsabove",
"title": "%DataScience.runAbove%",
"group": "inline/cell@0",
"when": "notebookType == 'jupyter-notebook' && isWorkspaceTrusted"
},
{
"command": "jupyter.notebookeditor.runcellandallbelow",
"title": "%DataScience.runBelow%",
"group": "inline/cell@0",
"when": "notebookType == 'jupyter-notebook' && isWorkspaceTrusted"
}
],
"notebook/toolbar": [
{
"command": "jupyter.notebookeditor.restartkernel",
Expand Down Expand Up @@ -894,6 +922,16 @@
"title": "%DataScience.toggleCellOutput%",
"when": "notebookType == jupyter-notebook"
},
{
"command": "jupyter.notebookeditor.runallcellsabove",
"title": "%DataScience.runAbove%",
"when": "notebookEditorFocused && notebookType == 'jupyter-notebook' && isWorkspaceTrusted"
},
{
"command": "jupyter.notebookeditor.runcellandallbelow",
"title": "%DataScience.runBelow%",
"when": "notebookEditorFocused && notebookType == 'jupyter-notebook' && isWorkspaceTrusted"
},
{
"command": "jupyter.exportAsPythonScript",
"title": "%jupyter.command.jupyter.exportAsPythonScript.title%",
Expand Down
2 changes: 2 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@
"DataScience.insertAbove": "Insert cell above",
"DataScience.addCell": "Add cell",
"DataScience.runAll": "Run all cells",
"DataScience.runAbove": "Run cells above",
"DataScience.runBelow": "Run cell and below",
"DataScience.toggleCellOutput": "Toggle Cell Output",
"DataScience.convertingToPythonFile": "Converting ipynb to python file",
"DataScience.untitledNotebookMessage": "Your changes will be lost if you don't save them.",
Expand Down
4 changes: 3 additions & 1 deletion src/client/common/application/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

'use strict';

import { CancellationToken, Position, TextDocument, Uri } from 'vscode';
import { CancellationToken, NotebookCell, Position, TextDocument, Uri } from 'vscode';
import { Commands as DSCommands } from '../../datascience/constants';
import { IShowDataViewerFromVariablePanel } from '../../datascience/interactive-common/interactiveWindowTypes';
import { KernelConnectionMetadata } from '../../datascience/jupyter/kernels/types';
Expand Down Expand Up @@ -93,6 +93,8 @@ export interface ICommandNameArgumentTypeMapping extends ICommandNameWithoutArgu
[DSCommands.RunCell]: [Uri, number, number, number, number];
[DSCommands.RunAllCellsAbove]: [Uri, number, number];
[DSCommands.RunCellAndAllBelow]: [Uri, number, number];
[DSCommands.NativeNotebookRunAllCellsAbove]: [NotebookCell];
[DSCommands.NativeNotebookRunCellAndAllBelow]: [NotebookCell];
[DSCommands.RunAllCellsAbovePalette]: [];
[DSCommands.RunCellAndAllBelowPalette]: [];
[DSCommands.DebugCurrentCellPalette]: [];
Expand Down
2 changes: 2 additions & 0 deletions src/client/datascience/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export namespace Commands {
export const RunAllCells = 'jupyter.runallcells';
export const RunAllCellsAbove = 'jupyter.runallcellsabove';
export const RunCellAndAllBelow = 'jupyter.runcellandallbelow';
export const NativeNotebookRunAllCellsAbove = 'jupyter.notebookeditor.runallcellsabove';
export const NativeNotebookRunCellAndAllBelow = 'jupyter.notebookeditor.runcellandallbelow';
export const SetJupyterKernel = 'jupyter.setKernel';
export const SwitchJupyterKernel = 'jupyter.switchKernel';
export const RunAllCellsAbovePalette = 'jupyter.runallcellsabove.palette';
Expand Down
7 changes: 7 additions & 0 deletions src/client/datascience/interactive-ipynb/nativeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,13 @@ export class NativeEditor extends InteractiveBase implements INotebookEditor {
throw Error('Not implemented Exception');
}

public runAbove(): void {
throw Error('Not implemented Exception');
}
public runCellAndBelow(): void {
throw Error('Not implemented Exception');
}

protected addSysInfo(reason: SysInfoReason): Promise<void> {
// We need to send a message when restarting
if (reason === SysInfoReason.Restart || reason === SysInfoReason.New) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import '../../common/extensions';

import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Uri } from 'vscode';
import { NotebookCell, Uri } from 'vscode';

import { ICommandManager } from '../../common/application/types';
import { ICommandManager, IVSCodeNotebook } from '../../common/application/types';
import { traceError, traceInfo } from '../../common/logger';
import { IDisposableRegistry } from '../../common/types';
import { captureTelemetry } from '../../telemetry';
Expand All @@ -20,6 +20,7 @@ export class NativeEditorCommandListener implements IDataScienceCommandListener
constructor(
@inject(IDisposableRegistry) private disposableRegistry: IDisposableRegistry,
@inject(INotebookEditorProvider) private provider: INotebookEditorProvider,
@inject(IVSCodeNotebook) private readonly notebook: IVSCodeNotebook,
@inject(IDataScienceErrorHandler) private dataScienceErrorHandler: IDataScienceErrorHandler
) {}

Expand Down Expand Up @@ -54,6 +55,14 @@ export class NativeEditorCommandListener implements IDataScienceCommandListener
this.disposableRegistry.push(
commandManager.registerCommand(Commands.NotebookEditorAddCellBelow, () => this.addCellBelow())
);
this.disposableRegistry.push(
commandManager.registerCommand(Commands.NativeNotebookRunAllCellsAbove, (cell) => this.runAbove(cell))
);
this.disposableRegistry.push(
commandManager.registerCommand(Commands.NativeNotebookRunCellAndAllBelow, (cell) =>
this.runCellAndBelow(cell)
)
);
}

private runAllCells() {
Expand Down Expand Up @@ -138,4 +147,28 @@ export class NativeEditorCommandListener implements IDataScienceCommandListener
}
}
}

private runAbove(cell: NotebookCell | undefined): void {
const activeEditor = this.provider.activeEditor;
cell = cell || this.getActiveCell(); // When called from command palette, we don't have a cell as the first argument.
if (activeEditor && cell) {
activeEditor.runAbove(cell);
}
}
private getActiveCell() {
const document = this.notebook.activeNotebookEditor?.document;
const selection = this.notebook.activeNotebookEditor?.selections;
if (!selection || !document) {
return;
}
const cells = document.getCells(selection[0]);
return cells.length ? cells[0] : undefined;
}
private runCellAndBelow(cell: NotebookCell | undefined): void {
const activeEditor = this.provider.activeEditor;
cell = cell || this.getActiveCell(); // When called from command palette, we don't have a cell as the first argument.
if (activeEditor && cell) {
activeEditor.runCellAndBelow(cell);
}
}
}
19 changes: 19 additions & 0 deletions src/client/datascience/notebook/notebookEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ConfigurationTarget,
Event,
EventEmitter,
NotebookCell,
NotebookCellKind,
NotebookRange,
NotebookDocument,
Expand Down Expand Up @@ -288,6 +289,24 @@ export class NotebookEditor implements INotebookEditor {
this._closed.fire(this);
}

public runAbove(cell: NotebookCell | undefined): void {
if (cell && cell.index > 0) {
void this.commandManager.executeCommand(
'notebook.cell.execute',
{ start: 0, end: cell.index },
cell.notebook.uri
);
}
}
public runCellAndBelow(cell: NotebookCell | undefined): void {
if (cell && cell.index >= 0) {
void this.commandManager.executeCommand(
'notebook.cell.execute',
{ start: cell.index, end: cell.notebook.cellCount },
cell.notebook.uri
);
}
}
private onClosedDocument(e?: NotebookDocument) {
if (this.document === e) {
this._closed.fire(this);
Expand Down
2 changes: 2 additions & 0 deletions src/client/datascience/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ export interface INotebookEditor extends Disposable, IInteractiveBase {
interruptKernel(): Promise<void>;
restartKernel(): Promise<void>;
syncAllCells(): Promise<void>;
runAbove(cell: NotebookCell | undefined): void;
runCellAndBelow(cell: NotebookCell | undefined): void;
}

export const INotebookExtensibility = Symbol('INotebookExtensibility');
Expand Down
58 changes: 58 additions & 0 deletions src/test/datascience/notebook/notebookEditor.vscode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
'use strict';

import { assert } from 'chai';
import { NotebookCellExecutionState } from 'vscode';
import { ICommandManager, IVSCodeNotebook } from '../../../client/common/application/types';
import { traceInfo } from '../../../client/common/logger';
import { IDisposable } from '../../../client/common/types';
import { Commands } from '../../../client/datascience/constants';
import { hasErrorOutput, NotebookCellStateTracker } from '../../../client/datascience/notebook/helpers/helpers';
import { IExtensionTestApi, waitForCondition } from '../../common';
import { closeActiveWindows, initialize } from '../../initialize';
import {
Expand Down Expand Up @@ -84,4 +86,60 @@ suite('Notebook Editor tests', function () {
'Outputs were not collapsed'
);
});

test('Run cells above', async function () {
return this.skip();
// add some cells
await insertCodeCell('print("0")', { index: 0 });
await insertCodeCell('print("1")', { index: 1 });
await insertCodeCell('print("2")', { index: 2 });

// select second cell
await selectCell(vscodeNotebook.activeNotebookEditor?.document!, 1, 1);

// run command
await commandManager.executeCommand(
Commands.NativeNotebookRunAllCellsAbove,
vscodeNotebook.activeNotebookEditor?.document.cellAt(1)!
);

const firstCell = vscodeNotebook.activeNotebookEditor?.document.cellAt(0)!;
await waitForExecutionCompletedSuccessfully(firstCell);
const thirdCell = vscodeNotebook.activeNotebookEditor?.document.getCells()![2]!;

// The first cell should have a runState of Success
assert.strictEqual(NotebookCellStateTracker.getCellState(firstCell), NotebookCellExecutionState.Idle);
assert.isFalse(hasErrorOutput(firstCell.outputs));

// The third cell should have an undefined runState
assert.strictEqual(NotebookCellStateTracker.getCellState(thirdCell), undefined);
});

test('Run cells below', async function () {
return this.skip();
// add some cells
await insertCodeCell('print("0")', { index: 0 });
await insertCodeCell('print("1")', { index: 1 });
await insertCodeCell('print("2")', { index: 2 });

// select second cell
await selectCell(vscodeNotebook.activeNotebookEditor?.document!, 1, 1);

// run command
await commandManager.executeCommand(
Commands.NativeNotebookRunCellAndAllBelow,
vscodeNotebook.activeNotebookEditor?.document.cellAt(1)!
);

const firstCell = vscodeNotebook.activeNotebookEditor?.document.cellAt(0)!;
const thirdCell = vscodeNotebook.activeNotebookEditor?.document.getCells()![2]!;
await waitForExecutionCompletedSuccessfully(thirdCell);

// The first cell should have an undefined runState
assert.strictEqual(NotebookCellStateTracker.getCellState(firstCell), undefined);

// The third cell should have a runState of Success
assert.strictEqual(NotebookCellStateTracker.getCellState(thirdCell), NotebookCellExecutionState.Idle);
assert.isFalse(hasErrorOutput(thirdCell.outputs));
});
});

0 comments on commit 28bf740

Please sign in to comment.