Skip to content

Commit

Permalink
Change cell execution/cancel API
Browse files Browse the repository at this point in the history
Fix #99203
  • Loading branch information
roblourens committed Jul 20, 2020
1 parent 357fe69 commit 90363c2
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export function smokeTestActivate(context: vscode.ExtensionContext): any {
}];
}
},
executeCell: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell | undefined, _token: vscode.CancellationToken) => {
cancelAllCellsExecution: async () => { },
executeCell: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell | undefined) => {
if (!_cell) {
_cell = _document.cells[0];
}
Expand All @@ -92,6 +93,7 @@ export function smokeTestActivate(context: vscode.ExtensionContext): any {
}];
return;
},
cancelCellExecution: async () => { }
}));

context.subscriptions.push(vscode.commands.registerCommand('vscode-notebook-tests.debugAction', async (cell: vscode.NotebookCell) => {
Expand Down
8 changes: 5 additions & 3 deletions extensions/vscode-notebook-tests/src/notebookTestMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function activate(context: vscode.ExtensionContext): any {

context.subscriptions.push(vscode.notebook.registerNotebookKernel('notebookKernelTest', ['*.vsctestnb'], {
label: 'Notebook Test Kernel',
executeAllCells: async (_document: vscode.NotebookDocument, _token: vscode.CancellationToken) => {
executeAllCells: async (_document: vscode.NotebookDocument) => {
let cell = _document.cells[0];

cell.outputs = [{
Expand All @@ -73,7 +73,8 @@ export function activate(context: vscode.ExtensionContext): any {
}];
return;
},
executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined, _token: vscode.CancellationToken) => {
cancelAllCellsExecution: async (_document: vscode.NotebookDocument) => { },
executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined) => {
if (!cell) {
cell = document.cells[0];
}
Expand Down Expand Up @@ -113,7 +114,8 @@ export function activate(context: vscode.ExtensionContext): any {
}
});
return;
}
},
cancelCellExecution: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell) => { }
}));

const preloadUri = vscode.Uri.file(path.resolve(__dirname, '../src/customRenderer.js'));
Expand Down
16 changes: 14 additions & 2 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,11 @@ declare module 'vscode' {
Error = 4
}

export enum NotebookRunState {
Running = 1,
Idle = 2
}

export interface NotebookCellMetadata {
/**
* Controls if the content of a cell is editable or not.
Expand Down Expand Up @@ -1525,6 +1530,11 @@ declare module 'vscode' {
* Additional attributes of the document metadata.
*/
custom?: { [key: string]: any };

/**
* The document's current run state
*/
runState?: NotebookRunState;
}

export interface NotebookDocument {
Expand Down Expand Up @@ -1832,8 +1842,10 @@ declare module 'vscode' {
description?: string;
isPreferred?: boolean;
preloads?: Uri[];
executeCell(document: NotebookDocument, cell: NotebookCell, token: CancellationToken): Promise<void>;
executeAllCells(document: NotebookDocument, token: CancellationToken): Promise<void>;
executeCell(document: NotebookDocument, cell: NotebookCell): void;
cancelCellExecution(document: NotebookDocument, cell: NotebookCell): void;
executeAllCells(document: NotebookDocument): void;
cancelAllCellsExecution(document: NotebookDocument): void;
}

export interface NotebookDocumentFilter {
Expand Down
30 changes: 20 additions & 10 deletions src/vs/workbench/api/browser/mainThreadNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,23 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
resolveNotebookEditor: async (viewType: string, uri: URI, editorId: string) => {
await this._proxy.$resolveNotebookEditor(viewType, uri, editorId);
},
executeNotebookByAttachedKernel: async (viewType: string, uri: URI, token: CancellationToken) => {
return this.executeNotebookByAttachedKernel(viewType, uri, token);
executeNotebookByAttachedKernel: async (viewType: string, uri: URI) => {
return this.executeNotebookByAttachedKernel(viewType, uri);
},
cancelNotebookByAttachedKernel: async (viewType: string, uri: URI) => {
return this.cancelNotebookByAttachedKernel(viewType, uri);
},
onDidReceiveMessage: (editorId: string, rendererType: string | undefined, message: unknown) => {
this._proxy.$onDidReceiveMessage(editorId, rendererType, message);
},
removeNotebookDocument: async (uri: URI) => {
return this.removeNotebookTextModel(uri);
},
executeNotebookCell: async (uri: URI, handle: number, token: CancellationToken) => {
return this._proxy.$executeNotebookByAttachedKernel(_viewType, uri, handle, token);
executeNotebookCell: async (uri: URI, handle: number) => {
return this._proxy.$executeNotebookByAttachedKernel(_viewType, uri, handle);
},
cancelNotebookCell: async (uri: URI, handle: number) => {
return this._proxy.$cancelNotebookByAttachedKernel(_viewType, uri, handle);
},
save: async (uri: URI, token: CancellationToken) => {
return this._proxy.$saveNotebook(_viewType, uri, token);
Expand Down Expand Up @@ -537,8 +543,8 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
resolveKernel: (editorId: string, uri: URI, kernelId: string, token: CancellationToken) => {
return that._proxy.$resolveNotebookKernel(handle, editorId, uri, kernelId, token);
},
executeNotebook: (uri: URI, kernelId: string, cellHandle: number | undefined, token: CancellationToken) => {
return that._proxy.$executeNotebookKernelFromProvider(handle, uri, kernelId, cellHandle, token);
executeNotebook: (uri: URI, kernelId: string, cellHandle: number | undefined) => {
return that._proxy.$executeNotebookKernelFromProvider(handle, uri, kernelId, cellHandle);
}
});
this._notebookKernelProviders.set(handle, {
Expand Down Expand Up @@ -590,8 +596,12 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
}
}

async executeNotebookByAttachedKernel(viewType: string, uri: URI, token: CancellationToken): Promise<void> {
return this._proxy.$executeNotebookByAttachedKernel(viewType, uri, undefined, token);
async executeNotebookByAttachedKernel(viewType: string, uri: URI): Promise<void> {
return this._proxy.$executeNotebookByAttachedKernel(viewType, uri, undefined);
}

async cancelNotebookByAttachedKernel(viewType: string, uri: URI): Promise<void> {
return this._proxy.$cancelNotebookByAttachedKernel(viewType, uri, undefined);
}

async $postMessage(editorId: string, forRendererId: string | undefined, value: any): Promise<boolean> {
Expand Down Expand Up @@ -634,8 +644,8 @@ export class MainThreadNotebookKernel implements INotebookKernelInfo {
) {
}

async executeNotebook(viewType: string, uri: URI, handle: number | undefined, token: CancellationToken): Promise<void> {
return this._proxy.$executeNotebook2(this.id, viewType, uri, handle, token);
async executeNotebook(viewType: string, uri: URI, handle: number | undefined): Promise<void> {
return this._proxy.$executeNotebook2(this.id, viewType, uri, handle);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,8 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
TimelineItem: extHostTypes.TimelineItem,
CellKind: extHostTypes.CellKind,
CellOutputKind: extHostTypes.CellOutputKind,
NotebookCellRunState: extHostTypes.NotebookCellRunState
NotebookCellRunState: extHostTypes.NotebookCellRunState,
NotebookRunState: extHostTypes.NotebookRunState
};
};
}
Expand Down
7 changes: 4 additions & 3 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1616,9 +1616,10 @@ export interface ExtHostNotebookShape {
$resolveNotebookEditor(viewType: string, uri: UriComponents, editorId: string): Promise<void>;
$provideNotebookKernels(handle: number, uri: UriComponents, token: CancellationToken): Promise<INotebookKernelInfoDto2[]>;
$resolveNotebookKernel(handle: number, editorId: string, uri: UriComponents, kernelId: string, token: CancellationToken): Promise<void>;
$executeNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined, token: CancellationToken): Promise<void>;
$executeNotebookKernelFromProvider(handle: number, uri: UriComponents, kernelId: string, cellHandle: number | undefined, token: CancellationToken): Promise<void>;
$executeNotebook2(kernelId: string, viewType: string, uri: UriComponents, cellHandle: number | undefined, token: CancellationToken): Promise<void>;
$executeNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void>;
$cancelNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void>;
$executeNotebookKernelFromProvider(handle: number, uri: UriComponents, kernelId: string, cellHandle: number | undefined): Promise<void>;
$executeNotebook2(kernelId: string, viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void>;
$saveNotebook(viewType: string, uri: UriComponents, token: CancellationToken): Promise<boolean>;
$saveNotebookAs(viewType: string, uri: UriComponents, target: UriComponents, token: CancellationToken): Promise<boolean>;
$backup(viewType: string, uri: UriComponents, cancellation: CancellationToken): Promise<string | undefined>;
Expand Down
69 changes: 57 additions & 12 deletions src/vs/workbench/api/common/extHostNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
import { CellEditType, diff, ICellEditOperation, ICellInsertEdit, INotebookDisplayOrder, INotebookEditData, NotebookCellsChangedEvent, NotebookCellsSplice2, ICellDeleteEdit, notebookDocumentMetadataDefaults, NotebookCellsChangeType, NotebookDataDto, IOutputRenderRequest, IOutputRenderResponse, IOutputRenderResponseOutputInfo, IOutputRenderResponseCellInfo, IRawOutput, CellOutputKind, IProcessedOutput, INotebookKernelInfoDto2, IMainCellDto } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
import { CancellationToken } from 'vs/base/common/cancellation';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { ExtHostDocumentData } from 'vs/workbench/api/common/extHostDocumentData';
import { NotImplementedProxy } from 'vs/base/common/types';
import * as typeConverters from 'vs/workbench/api/common/extHostTypeConverters';
Expand Down Expand Up @@ -830,19 +830,43 @@ export class ExtHostNotebookKernelProviderAdapter extends Disposable {
}
}

async executeNotebook(kernelId: string, document: ExtHostNotebookDocument, cell: ExtHostCell | undefined, token: CancellationToken) {
async executeNotebook(kernelId: string, document: ExtHostNotebookDocument, cell: ExtHostCell | undefined) {
const kernel = this._idToKernel.get(kernelId);

if (!kernel) {
return;
}

if (cell) {
return kernel.executeCell(document, cell, token);
return withToken(token => (kernel.executeCell as any)(document, cell, token));
} else {
return kernel.executeAllCells(document, token);
return withToken(token => (kernel.executeAllCells as any)(document, token));
}
}

async cancelNotebook(kernelId: string, document: ExtHostNotebookDocument, cell: ExtHostCell | undefined) {
const kernel = this._idToKernel.get(kernelId);

if (!kernel) {
return;
}

if (cell) {
return kernel.cancelCellExecution(document, cell);
} else {
return kernel.cancelAllCellsExecution(document);
}
}
}

// TODO@roblou remove 'token' passed to all execute APIs once extensions are updated
async function withToken(cb: (token: CancellationToken) => any) {
const source = new CancellationTokenSource();
try {
await cb(source.token);
} finally {
source.dispose();
}
}

export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostNotebookOutputRenderingHandler {
Expand Down Expand Up @@ -1220,7 +1244,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
}

async $executeNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined, token: CancellationToken): Promise<void> {
async $executeNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void> {
let document = this._documents.get(URI.revive(uri).toString());

if (!document) {
Expand All @@ -1233,23 +1257,44 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN

if (provider.kernel) {
if (cell) {
return provider.kernel.executeCell(document, cell, token);
return withToken(token => (provider.kernel!.executeCell as any)(document, cell, token));
} else {
return withToken(token => (provider.kernel!.executeAllCells as any)(document, token));
}
}
}
}

async $cancelNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void> {
const document = this._documents.get(URI.revive(uri).toString());

if (!document) {
return;
}

if (this._notebookContentProviders.has(viewType)) {
const cell = cellHandle !== undefined ? document.getCell(cellHandle) : undefined;
const provider = this._notebookContentProviders.get(viewType)!.provider;

if (provider.kernel) {
if (cell) {
return provider.kernel.cancelCellExecution(document, cell);
} else {
return provider.kernel.executeAllCells(document, token);
return provider.kernel.cancelAllCellsExecution(document);
}
}
}
}

async $executeNotebookKernelFromProvider(handle: number, uri: UriComponents, kernelId: string, cellHandle: number | undefined, token: CancellationToken): Promise<void> {
async $executeNotebookKernelFromProvider(handle: number, uri: UriComponents, kernelId: string, cellHandle: number | undefined): Promise<void> {
await this._withAdapter(handle, uri, async (adapter, document) => {
let cell = cellHandle !== undefined ? document.getCell(cellHandle) : undefined;

return adapter.executeNotebook(kernelId, document, cell, token);
return adapter.executeNotebook(kernelId, document, cell);
});
}

async $executeNotebook2(kernelId: string, viewType: string, uri: UriComponents, cellHandle: number | undefined, token: CancellationToken): Promise<void> {
async $executeNotebook2(kernelId: string, viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void> {
let document = this._documents.get(URI.revive(uri).toString());

if (!document || document.viewType !== viewType) {
Expand All @@ -1265,9 +1310,9 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
let cell = cellHandle !== undefined ? document.getCell(cellHandle) : undefined;

if (cell) {
return kernelInfo.kernel.executeCell(document, cell, token);
return withToken(token => (kernelInfo!.kernel.executeCell as any)(document, cell, token));
} else {
return kernelInfo.kernel.executeAllCells(document, token);
return withToken(token => (kernelInfo!.kernel.executeAllCells as any)(document, token));
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,11 @@ export enum NotebookCellRunState {
Error = 4
}

export enum NotebookRunState {
Running = 1,
Idle = 2
}

//#endregion

//#region Timeline
Expand Down
2 changes: 0 additions & 2 deletions src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { IListEvent, IListMouseEvent } from 'vs/base/browser/ui/list/list';
import { IListOptions, IListStyles } from 'vs/base/browser/ui/list/listWidget';
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { Event } from 'vs/base/common/event';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { ScrollEvent } from 'vs/base/common/scrollable';
Expand Down Expand Up @@ -117,7 +116,6 @@ export interface ICellViewModel {
language: string;
cellKind: CellKind;
editState: CellEditState;
currentTokenSource: CancellationTokenSource | undefined;
focusMode: CellFocusMode;
getText(): string;
getTextLength(): number;
Expand Down
Loading

0 comments on commit 90363c2

Please sign in to comment.