mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-25 01:25:36 -05:00
Initial implementation for VSCode Notebook support (#17885)
This commit is contained in:
@@ -17,6 +17,10 @@ import { LocalContentManager } from 'sql/workbench/services/notebook/common/loca
|
||||
import { Deferred } from 'sql/base/common/promise';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import type { FutureInternal } from 'sql/workbench/services/notebook/browser/interfaces';
|
||||
import { Registry } from 'vs/platform/registry/common/platform';
|
||||
import { INotebookProviderRegistry, NotebookProviderRegistryId } from 'sql/workbench/services/notebook/common/notebookRegistry';
|
||||
|
||||
const notebookRegistry = Registry.as<INotebookProviderRegistry>(NotebookProviderRegistryId);
|
||||
|
||||
@extHostNamedCustomer(SqlMainContext.MainThreadNotebook)
|
||||
export class MainThreadNotebook extends Disposable implements MainThreadNotebookShape {
|
||||
@@ -96,7 +100,10 @@ export class MainThreadNotebook extends Disposable implements MainThreadNotebook
|
||||
if (future) {
|
||||
future.onDone(done);
|
||||
}
|
||||
}
|
||||
|
||||
public $updateProviderDescriptionLanguages(providerId: string, languages: string[]): void {
|
||||
notebookRegistry.updateProviderDescriptionLanguages(providerId, languages);
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
207
src/sql/workbench/api/common/adsNotebookController.ts
Normal file
207
src/sql/workbench/api/common/adsNotebookController.ts
Normal file
@@ -0,0 +1,207 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import type * as vscode from 'vscode';
|
||||
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
|
||||
import { INotebookKernelDto2 } from 'vs/workbench/api/common/extHost.protocol';
|
||||
import { Emitter, Event } from 'vs/base/common/event';
|
||||
import * as extHostTypeConverters from 'vs/workbench/api/common/extHostTypeConverters';
|
||||
import { Deferred } from 'sql/base/common/promise';
|
||||
|
||||
type SelectionChangedEvent = { selected: boolean, notebook: vscode.NotebookDocument; };
|
||||
type MessageReceivedEvent = { editor: vscode.NotebookEditor, message: any; };
|
||||
type ExecutionHandler = (cells: vscode.NotebookCell[], notebook: vscode.NotebookDocument, controller: vscode.NotebookController) => void | Thenable<void>;
|
||||
type InterruptHandler = (notebook: vscode.NotebookDocument) => void | Promise<void>;
|
||||
|
||||
/**
|
||||
* A VS Code Notebook Controller that is used as part of converting VS Code notebook extension APIs into ADS equivalents.
|
||||
*/
|
||||
export class ADSNotebookController implements vscode.NotebookController {
|
||||
private readonly _kernelData: INotebookKernelDto2;
|
||||
private _interruptHandler: (notebook: vscode.NotebookDocument) => void | Promise<void>;
|
||||
|
||||
private readonly _onDidChangeSelection = new Emitter<SelectionChangedEvent>();
|
||||
private readonly _onDidReceiveMessage = new Emitter<MessageReceivedEvent>();
|
||||
|
||||
private readonly _languagesAdded = new Deferred<void>();
|
||||
private readonly _executionHandlerAdded = new Deferred<void>();
|
||||
|
||||
constructor(
|
||||
private _extension: IExtensionDescription,
|
||||
private _id: string,
|
||||
private _viewType: string,
|
||||
private _label: string,
|
||||
private _addLanguagesHandler: (providerId, languages) => void,
|
||||
private _handler?: ExecutionHandler,
|
||||
preloads?: vscode.NotebookRendererScript[]
|
||||
) {
|
||||
this._kernelData = {
|
||||
id: `${this._extension.identifier.value}/${this._id}`,
|
||||
notebookType: this._viewType,
|
||||
extensionId: this._extension.identifier,
|
||||
extensionLocation: this._extension.extensionLocation,
|
||||
label: this._label || this._extension.identifier.value,
|
||||
preloads: preloads ? preloads.map(extHostTypeConverters.NotebookRendererScript.from) : []
|
||||
};
|
||||
if (this._handler) {
|
||||
this._executionHandlerAdded.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
public get languagesAdded(): Promise<void> {
|
||||
return this._languagesAdded.promise;
|
||||
}
|
||||
|
||||
public get executionHandlerAdded(): Promise<void> {
|
||||
return this._executionHandlerAdded.promise;
|
||||
}
|
||||
|
||||
public get id(): string { return this._id; }
|
||||
|
||||
public get notebookType(): string { return this._viewType; }
|
||||
|
||||
public get onDidChangeSelectedNotebooks(): Event<SelectionChangedEvent> {
|
||||
return this._onDidChangeSelection.event;
|
||||
}
|
||||
|
||||
public get onDidReceiveMessage(): Event<MessageReceivedEvent> {
|
||||
return this._onDidReceiveMessage.event;
|
||||
}
|
||||
|
||||
public get label(): string {
|
||||
return this._kernelData.label;
|
||||
}
|
||||
|
||||
public set label(value: string) {
|
||||
this._kernelData.label = value ?? this._extension.displayName ?? this._extension.name;
|
||||
}
|
||||
|
||||
public get detail(): string {
|
||||
return this._kernelData.detail ?? '';
|
||||
}
|
||||
|
||||
public set detail(value: string) {
|
||||
this._kernelData.detail = value;
|
||||
}
|
||||
|
||||
public get description(): string {
|
||||
return this._kernelData.description ?? '';
|
||||
}
|
||||
|
||||
public set description(value: string) {
|
||||
this._kernelData.description = value;
|
||||
}
|
||||
|
||||
public get supportedLanguages(): string[] | undefined {
|
||||
return this._kernelData.supportedLanguages;
|
||||
}
|
||||
|
||||
public set supportedLanguages(value: string[]) {
|
||||
this._kernelData.supportedLanguages = value;
|
||||
this._addLanguagesHandler(this._viewType, value);
|
||||
this._languagesAdded.resolve();
|
||||
}
|
||||
|
||||
public get supportsExecutionOrder(): boolean {
|
||||
return this._kernelData.supportsExecutionOrder ?? false;
|
||||
}
|
||||
|
||||
public set supportsExecutionOrder(value: boolean) {
|
||||
this._kernelData.supportsExecutionOrder = value;
|
||||
}
|
||||
|
||||
public get rendererScripts(): vscode.NotebookRendererScript[] {
|
||||
return this._kernelData.preloads ? this._kernelData.preloads.map(extHostTypeConverters.NotebookRendererScript.to) : [];
|
||||
}
|
||||
|
||||
public get executeHandler(): ExecutionHandler {
|
||||
return this._handler;
|
||||
}
|
||||
|
||||
public set executeHandler(value: ExecutionHandler) {
|
||||
this._handler = value;
|
||||
this._executionHandlerAdded.resolve();
|
||||
}
|
||||
|
||||
public get interruptHandler(): InterruptHandler {
|
||||
return this._interruptHandler;
|
||||
}
|
||||
|
||||
public set interruptHandler(value: InterruptHandler) {
|
||||
this._interruptHandler = value;
|
||||
this._kernelData.supportsInterrupt = Boolean(value);
|
||||
}
|
||||
|
||||
public createNotebookCellExecution(cell: vscode.NotebookCell): vscode.NotebookCellExecution {
|
||||
return new ADSNotebookCellExecution(cell);
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public updateNotebookAffinity(notebook: vscode.NotebookDocument, affinity: vscode.NotebookControllerAffinity): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public postMessage(message: any, editor?: vscode.NotebookEditor): Thenable<boolean> {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
public asWebviewUri(localResource: vscode.Uri): vscode.Uri {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
class ADSNotebookCellExecution implements vscode.NotebookCellExecution {
|
||||
private _executionOrder: number;
|
||||
constructor(private readonly _cell: vscode.NotebookCell) {
|
||||
this._executionOrder = this._cell.executionSummary?.executionOrder ?? -1;
|
||||
}
|
||||
|
||||
public get cell(): vscode.NotebookCell {
|
||||
return this._cell;
|
||||
}
|
||||
|
||||
public get token(): vscode.CancellationToken {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public get executionOrder(): number {
|
||||
return this._executionOrder;
|
||||
}
|
||||
|
||||
public set executionOrder(order: number) {
|
||||
this._executionOrder = order;
|
||||
}
|
||||
|
||||
public start(startTime?: number): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public end(success: boolean, endTime?: number): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public async clearOutput(cell?: vscode.NotebookCell): Promise<void> {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public async replaceOutput(out: vscode.NotebookCellOutput | vscode.NotebookCellOutput[], cell?: vscode.NotebookCell): Promise<void> {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public async appendOutput(out: vscode.NotebookCellOutput | vscode.NotebookCellOutput[], cell?: vscode.NotebookCell): Promise<void> {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public async replaceOutputItems(items: vscode.NotebookCellOutputItem | vscode.NotebookCellOutputItem[], output: vscode.NotebookCellOutput): Promise<void> {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public async appendOutputItems(items: vscode.NotebookCellOutputItem | vscode.NotebookCellOutputItem[], output: vscode.NotebookCellOutput): Promise<void> {
|
||||
// No-op
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,8 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as azdata from 'azdata';
|
||||
import * as vscode from 'vscode';
|
||||
import type * as azdata from 'azdata';
|
||||
import type * as vscode from 'vscode';
|
||||
|
||||
import { IMainContext } from 'vs/workbench/api/common/extHost.protocol';
|
||||
import { Disposable } from 'vs/workbench/api/common/extHostTypes';
|
||||
@@ -13,6 +13,10 @@ import { URI, UriComponents } from 'vs/base/common/uri';
|
||||
|
||||
import { ExtHostNotebookShape, MainThreadNotebookShape, SqlMainContext } from 'sql/workbench/api/common/sqlExtHost.protocol';
|
||||
import { IExecuteManagerDetails, INotebookSessionDetails, INotebookKernelDetails, INotebookFutureDetails, FutureMessageType, ISerializationManagerDetails } from 'sql/workbench/api/common/sqlExtHostTypes';
|
||||
import { VSCodeSerializationProvider } from 'sql/workbench/api/common/vscodeSerializationProvider';
|
||||
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
|
||||
import { ADSNotebookController } from 'sql/workbench/api/common/adsNotebookController';
|
||||
import { VSCodeExecuteProvider } from 'sql/workbench/api/common/vscodeExecuteProvider';
|
||||
|
||||
type Adapter = azdata.nb.NotebookSerializationProvider | azdata.nb.SerializationManager | azdata.nb.NotebookExecuteProvider | azdata.nb.ExecuteManager | azdata.nb.ISession | azdata.nb.IKernel | azdata.nb.IFuture;
|
||||
|
||||
@@ -232,7 +236,6 @@ export class ExtHostNotebook implements ExtHostNotebookShape {
|
||||
return sessionManager.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region APIs called by extensions
|
||||
@@ -253,11 +256,22 @@ export class ExtHostNotebook implements ExtHostNotebookShape {
|
||||
this._proxy.$registerSerializationProvider(provider.providerId, handle);
|
||||
return this._createDisposable(handle);
|
||||
}
|
||||
|
||||
registerNotebookSerializer(notebookType: string, serializer: vscode.NotebookSerializer, options?: vscode.NotebookDocumentContentOptions, registration?: vscode.NotebookRegistrationData): vscode.Disposable {
|
||||
let serializationProvider = new VSCodeSerializationProvider(notebookType, serializer);
|
||||
return this.registerSerializationProvider(serializationProvider);
|
||||
}
|
||||
|
||||
createNotebookController(extension: IExtensionDescription, id: string, viewType: string, label: string, handler?: (cells: vscode.NotebookCell[], notebook: vscode.NotebookDocument, controller: vscode.NotebookController) => void | Thenable<void>, rendererScripts?: vscode.NotebookRendererScript[]): vscode.NotebookController {
|
||||
let addLanguagesHandler = (id, languages) => this._proxy.$updateProviderDescriptionLanguages(id, languages);
|
||||
let controller = new ADSNotebookController(extension, id, viewType, label, addLanguagesHandler, handler, extension.enableProposedApi ? rendererScripts : undefined);
|
||||
let executeProvider = new VSCodeExecuteProvider(controller);
|
||||
this.registerExecuteProvider(executeProvider);
|
||||
return controller;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region private methods
|
||||
|
||||
private getAdapters<A>(ctor: { new(...args: any[]): A }): A[] {
|
||||
let matchingAdapters = [];
|
||||
this._adapters.forEach(a => {
|
||||
|
||||
@@ -48,24 +48,20 @@ export interface IExtensionApiFactory {
|
||||
|
||||
export interface IAdsExtensionApiFactory {
|
||||
azdata: IAzdataExtensionApiFactory;
|
||||
extHostNotebook: ExtHostNotebook;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method instantiates and returns the extension API surface
|
||||
*/
|
||||
export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): IExtensionApiFactory {
|
||||
const { azdata } = createAdsApiFactory(accessor);
|
||||
const { azdata, extHostNotebook } = createAdsApiFactory(accessor);
|
||||
return {
|
||||
azdata,
|
||||
vscode: vsApiFactory(accessor)
|
||||
vscode: vsApiFactory(accessor, extHostNotebook)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export interface IAdsExtensionApiFactory {
|
||||
azdata: IAzdataExtensionApiFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method instantiates and returns the extension API surface
|
||||
*/
|
||||
@@ -634,6 +630,7 @@ export function createAdsApiFactory(accessor: ServicesAccessor): IAdsExtensionAp
|
||||
TextType: sqlExtHostTypes.TextType,
|
||||
designers: designers
|
||||
};
|
||||
}
|
||||
},
|
||||
extHostNotebook: extHostNotebook
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import { URI, UriComponents } from 'vs/base/common/uri';
|
||||
|
||||
import { IDisposable } from 'vs/base/common/lifecycle';
|
||||
|
||||
import * as azdata from 'azdata';
|
||||
import * as vscode from 'vscode';
|
||||
import type * as azdata from 'azdata';
|
||||
import type * as vscode from 'vscode';
|
||||
|
||||
import { ITreeComponentItem } from 'sql/workbench/common/views';
|
||||
import { ITaskHandlerDescription } from 'sql/workbench/services/tasks/common/tasks';
|
||||
@@ -887,7 +887,6 @@ export interface MainThreadQueryEditorShape extends IDisposable {
|
||||
}
|
||||
|
||||
export interface ExtHostNotebookShape {
|
||||
|
||||
/**
|
||||
* Looks up a notebook manager for a given notebook URI
|
||||
* @returns handle of the manager to be used when sending
|
||||
@@ -935,6 +934,7 @@ export interface MainThreadNotebookShape extends IDisposable {
|
||||
$unregisterExecuteProvider(handle: number): void;
|
||||
$onFutureMessage(futureId: number, type: FutureMessageType, payload: azdata.nb.IMessage): void;
|
||||
$onFutureDone(futureId: number, done: INotebookFutureDone): void;
|
||||
$updateProviderDescriptionLanguages(providerId: string, languages: string[]): void;
|
||||
}
|
||||
|
||||
export interface INotebookDocumentsAndEditorsDelta {
|
||||
|
||||
316
src/sql/workbench/api/common/vscodeExecuteProvider.ts
Normal file
316
src/sql/workbench/api/common/vscodeExecuteProvider.ts
Normal file
@@ -0,0 +1,316 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import type * as vscode from 'vscode';
|
||||
import type * as azdata from 'azdata';
|
||||
import { ADSNotebookController } from 'sql/workbench/api/common/adsNotebookController';
|
||||
import * as nls from 'vs/nls';
|
||||
|
||||
class VSCodeFuture implements azdata.nb.IFuture {
|
||||
private _inProgress = true;
|
||||
|
||||
constructor(private readonly _executeCompletion: Promise<void>) {
|
||||
}
|
||||
|
||||
dispose() {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public get inProgress(): boolean {
|
||||
return this._inProgress;
|
||||
}
|
||||
|
||||
public set inProgress(value: boolean) {
|
||||
this._inProgress = value;
|
||||
}
|
||||
|
||||
public get msg(): azdata.nb.IMessage | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public get done(): Thenable<azdata.nb.IShellMessage | undefined> {
|
||||
return this._executeCompletion.then(() => {
|
||||
return undefined;
|
||||
}).finally(() => {
|
||||
this._inProgress = false;
|
||||
});
|
||||
}
|
||||
|
||||
setReplyHandler(handler: azdata.nb.MessageHandler<azdata.nb.IShellMessage>): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
setStdInHandler(handler: azdata.nb.MessageHandler<azdata.nb.IStdinMessage>): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
setIOPubHandler(handler: azdata.nb.MessageHandler<azdata.nb.IIOPubMessage>): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
registerMessageHook(hook: (msg: azdata.nb.IIOPubMessage) => boolean | Thenable<boolean>): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
removeMessageHook(hook: (msg: azdata.nb.IIOPubMessage) => boolean | Thenable<boolean>): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
sendInputReply(content: azdata.nb.IInputReply): void {
|
||||
// No-op
|
||||
}
|
||||
}
|
||||
|
||||
class VSCodeKernel implements azdata.nb.IKernel {
|
||||
protected static kernelId = 0;
|
||||
private readonly _id: string;
|
||||
private readonly _name: string;
|
||||
private readonly _info: azdata.nb.IInfoReply;
|
||||
private readonly _kernelSpec: azdata.nb.IKernelSpec;
|
||||
|
||||
constructor(private readonly _controller: ADSNotebookController, private readonly _options: azdata.nb.ISessionOptions, language: string) {
|
||||
this._id = this._options.kernelId ?? (VSCodeKernel.kernelId++).toString();
|
||||
this._name = this._options.kernelName ?? this._controller.notebookType;
|
||||
this._info = {
|
||||
protocol_version: '',
|
||||
implementation: '',
|
||||
implementation_version: '',
|
||||
language_info: {
|
||||
name: language,
|
||||
version: '',
|
||||
},
|
||||
banner: '',
|
||||
help_links: [{
|
||||
text: '',
|
||||
url: ''
|
||||
}]
|
||||
};
|
||||
this._kernelSpec = {
|
||||
name: this._name,
|
||||
language: language,
|
||||
display_name: this._name
|
||||
};
|
||||
}
|
||||
|
||||
public get id(): string {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
public get name(): string {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
public get supportsIntellisense(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
public get requiresConnection(): boolean | undefined {
|
||||
return false;
|
||||
}
|
||||
|
||||
public get isReady(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
public get ready(): Thenable<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
public get info(): azdata.nb.IInfoReply | null {
|
||||
return this._info;
|
||||
}
|
||||
|
||||
getSpec(): Thenable<azdata.nb.IKernelSpec> {
|
||||
return Promise.resolve(this._kernelSpec);
|
||||
}
|
||||
|
||||
requestExecute(content: azdata.nb.IExecuteRequest, disposeOnDone?: boolean): azdata.nb.IFuture {
|
||||
let executePromise: Promise<void>;
|
||||
if (this._controller.executeHandler) {
|
||||
let cell = <vscode.NotebookCell>{
|
||||
document: <vscode.TextDocument>{
|
||||
uri: content.notebookUri,
|
||||
languageId: this._kernelSpec.language,
|
||||
getText: () => Array.isArray(content.code) ? content.code.join('') : content.code,
|
||||
},
|
||||
notebook: <vscode.NotebookDocument>{
|
||||
uri: content.notebookUri
|
||||
}
|
||||
};
|
||||
|
||||
executePromise = Promise.resolve(this._controller.executeHandler([cell], cell.notebook, this._controller));
|
||||
}
|
||||
else {
|
||||
executePromise = Promise.resolve();
|
||||
}
|
||||
|
||||
return new VSCodeFuture(executePromise);
|
||||
}
|
||||
|
||||
requestComplete(content: azdata.nb.ICompleteRequest): Thenable<azdata.nb.ICompleteReplyMsg> {
|
||||
let response: Partial<azdata.nb.ICompleteReplyMsg> = {};
|
||||
return Promise.resolve(response as azdata.nb.ICompleteReplyMsg);
|
||||
}
|
||||
|
||||
public async interrupt(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
class VSCodeSession implements azdata.nb.ISession {
|
||||
private _kernel: VSCodeKernel;
|
||||
private _defaultKernelLoaded = false;
|
||||
constructor(controller: ADSNotebookController, private readonly _options: azdata.nb.ISessionOptions, language: string) {
|
||||
this._kernel = new VSCodeKernel(controller, this._options, language);
|
||||
}
|
||||
|
||||
public set defaultKernelLoaded(value) {
|
||||
this._defaultKernelLoaded = value;
|
||||
}
|
||||
|
||||
public get defaultKernelLoaded(): boolean {
|
||||
return this._defaultKernelLoaded;
|
||||
}
|
||||
|
||||
public get canChangeKernels(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
public get id(): string {
|
||||
return this._options.kernelId || this._kernel ? this._kernel.id : '';
|
||||
}
|
||||
|
||||
public get path(): string {
|
||||
return this._options.path;
|
||||
}
|
||||
|
||||
public get name(): string {
|
||||
return this._options.name || '';
|
||||
}
|
||||
|
||||
public get type(): string {
|
||||
return this._options.type || '';
|
||||
}
|
||||
|
||||
public get status(): azdata.nb.KernelStatus {
|
||||
return 'connected';
|
||||
}
|
||||
|
||||
public get kernel(): azdata.nb.IKernel {
|
||||
return this._kernel;
|
||||
}
|
||||
|
||||
changeKernel(kernelInfo: azdata.nb.IKernelSpec): Thenable<azdata.nb.IKernel> {
|
||||
return Promise.resolve(this._kernel);
|
||||
}
|
||||
|
||||
configureKernel(kernelInfo: azdata.nb.IKernelSpec): Thenable<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
configureConnection(connection: azdata.IConnectionProfile): Thenable<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
class VSCodeSessionManager implements azdata.nb.SessionManager {
|
||||
private _sessions: azdata.nb.ISession[] = [];
|
||||
|
||||
constructor(private readonly _controller: ADSNotebookController) {
|
||||
}
|
||||
|
||||
public get isReady(): boolean {
|
||||
return this._controller.supportedLanguages?.length > 0 && this._controller.executeHandler !== undefined;
|
||||
}
|
||||
|
||||
public get ready(): Thenable<void> {
|
||||
return Promise.all([this._controller.languagesAdded, this._controller.executionHandlerAdded]).then();
|
||||
}
|
||||
|
||||
public get specs(): azdata.nb.IAllKernels {
|
||||
let languages = this._controller.supportedLanguages?.length > 0 ? this._controller.supportedLanguages : [this._controller.label];
|
||||
return {
|
||||
defaultKernel: languages[0],
|
||||
kernels: languages.map<azdata.nb.IKernelSpec>(language => {
|
||||
return {
|
||||
name: language,
|
||||
language: language,
|
||||
display_name: language
|
||||
};
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
public async startNew(options: azdata.nb.ISessionOptions): Promise<azdata.nb.ISession> {
|
||||
if (!this.isReady) {
|
||||
return Promise.reject(new Error(nls.localize('errorStartBeforeReady', "Cannot start a session, the manager is not yet initialized")));
|
||||
}
|
||||
|
||||
let session: azdata.nb.ISession = new VSCodeSession(this._controller, options, this.specs.defaultKernel);
|
||||
let index = this._sessions.findIndex(session => session.path === options.path);
|
||||
if (index > -1) {
|
||||
this._sessions.splice(index);
|
||||
}
|
||||
this._sessions.push(session);
|
||||
return Promise.resolve(session);
|
||||
}
|
||||
|
||||
public shutdown(id: string): Thenable<void> {
|
||||
let index = this._sessions.findIndex(session => session.id === id);
|
||||
if (index > -1) {
|
||||
this._sessions.splice(index);
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
public shutdownAll(): Thenable<void> {
|
||||
return Promise.all(this._sessions.map(session => {
|
||||
return this.shutdown(session.id);
|
||||
})).then();
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
// No-op
|
||||
}
|
||||
}
|
||||
|
||||
class VSCodeExecuteManager implements azdata.nb.ExecuteManager {
|
||||
public readonly providerId: string;
|
||||
private readonly _sessionManager: azdata.nb.SessionManager;
|
||||
|
||||
constructor(controller: ADSNotebookController) {
|
||||
this.providerId = controller.notebookType;
|
||||
this._sessionManager = new VSCodeSessionManager(controller);
|
||||
}
|
||||
|
||||
public get sessionManager(): azdata.nb.SessionManager {
|
||||
return this._sessionManager;
|
||||
}
|
||||
|
||||
public get serverManager(): azdata.nb.ServerManager | undefined {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A Notebook Execute Provider that is used to convert VS Code notebook extension APIs into ADS equivalents.
|
||||
*/
|
||||
export class VSCodeExecuteProvider implements azdata.nb.NotebookExecuteProvider {
|
||||
public readonly providerId: string;
|
||||
private readonly _executeManager: azdata.nb.ExecuteManager;
|
||||
|
||||
constructor(controller: ADSNotebookController) {
|
||||
this._executeManager = new VSCodeExecuteManager(controller);
|
||||
this.providerId = controller.notebookType;
|
||||
}
|
||||
|
||||
public getExecuteManager(notebookUri: vscode.Uri): Thenable<azdata.nb.ExecuteManager> {
|
||||
return Promise.resolve(this._executeManager);
|
||||
}
|
||||
|
||||
public handleNotebookClosed(notebookUri: vscode.Uri): void {
|
||||
// No-op
|
||||
}
|
||||
}
|
||||
150
src/sql/workbench/api/common/vscodeSerializationProvider.ts
Normal file
150
src/sql/workbench/api/common/vscodeSerializationProvider.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import type * as vscode from 'vscode';
|
||||
import type * as azdata from 'azdata';
|
||||
import { VSBuffer } from 'vs/base/common/buffer';
|
||||
import { NotebookCellKind } from 'vs/workbench/api/common/extHostTypes';
|
||||
import { CancellationTokenSource } from 'vs/base/common/cancellation';
|
||||
import { OutputTypes } from 'sql/workbench/services/notebook/common/contracts';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
/**
|
||||
* A Notebook Content Manager that is used as part of converting VS Code notebook extension APIs into ADS equivalents.
|
||||
*/
|
||||
export class VSCodeContentManager implements azdata.nb.ContentManager {
|
||||
constructor(private readonly _serializer: vscode.NotebookSerializer) {
|
||||
}
|
||||
|
||||
public static convertToADSCellOutput(output: vscode.NotebookCellOutput, executionOrder?: number): azdata.nb.IExecuteResult {
|
||||
let outputData = {};
|
||||
for (let item of output.items) {
|
||||
outputData[item.mime] = VSBuffer.wrap(item.data).toString();
|
||||
}
|
||||
return {
|
||||
output_type: 'execute_result',
|
||||
data: outputData,
|
||||
execution_count: executionOrder,
|
||||
metadata: output.metadata,
|
||||
id: output.id
|
||||
};
|
||||
}
|
||||
|
||||
public async deserializeNotebook(contents: string): Promise<azdata.nb.INotebookContents> {
|
||||
let buffer = VSBuffer.fromString(contents);
|
||||
let notebookData = await this._serializer.deserializeNotebook(buffer.buffer, new CancellationTokenSource().token);
|
||||
let result = {
|
||||
cells: notebookData.cells?.map<azdata.nb.ICellContents>(cell => {
|
||||
let executionOrder = cell.executionSummary?.executionOrder;
|
||||
return {
|
||||
cell_type: cell.kind === NotebookCellKind.Code ? 'code' : 'markdown',
|
||||
source: cell.value,
|
||||
metadata: {
|
||||
language: cell.languageId
|
||||
},
|
||||
execution_count: executionOrder,
|
||||
outputs: cell.outputs?.map<azdata.nb.IExecuteResult>(output => VSCodeContentManager.convertToADSCellOutput(output, executionOrder))
|
||||
};
|
||||
}),
|
||||
metadata: notebookData.metadata ?? {},
|
||||
nbformat: notebookData.metadata?.custom?.nbformat ?? NBFORMAT,
|
||||
nbformat_minor: notebookData.metadata?.custom?.nbformat_minor ?? NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
// Clear out extra lingering vscode custom metadata
|
||||
delete result.metadata.custom;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static convertToVSCodeCellOutput(output: azdata.nb.ICellOutput): vscode.NotebookCellOutput {
|
||||
let convertedOutputItems: vscode.NotebookCellOutputItem[];
|
||||
switch (output.output_type) {
|
||||
case OutputTypes.ExecuteResult:
|
||||
case OutputTypes.DisplayData:
|
||||
case OutputTypes.UpdateDisplayData:
|
||||
let displayOutput = output as azdata.nb.IDisplayResult;
|
||||
convertedOutputItems = Object.keys(displayOutput.data).map<vscode.NotebookCellOutputItem>(key => {
|
||||
return {
|
||||
mime: key,
|
||||
data: VSBuffer.fromString(displayOutput.data[key]).buffer
|
||||
};
|
||||
});
|
||||
break;
|
||||
case OutputTypes.Stream:
|
||||
let streamOutput = output as azdata.nb.IStreamResult;
|
||||
convertedOutputItems = [{
|
||||
mime: 'text/html',
|
||||
data: VSBuffer.fromString(Array.isArray(streamOutput.text) ? streamOutput.text.join('') : streamOutput.text).buffer
|
||||
}];
|
||||
break;
|
||||
case OutputTypes.Error:
|
||||
let errorOutput = output as azdata.nb.IErrorResult;
|
||||
let errorString = errorOutput.ename + ': ' + errorOutput.evalue + (errorOutput.traceback ? '\n' + errorOutput.traceback?.join('\n') : '');
|
||||
convertedOutputItems = [{
|
||||
mime: 'text/html',
|
||||
data: VSBuffer.fromString(errorString).buffer
|
||||
}];
|
||||
break;
|
||||
}
|
||||
return {
|
||||
items: convertedOutputItems,
|
||||
metadata: output.metadata,
|
||||
id: output.id
|
||||
};
|
||||
}
|
||||
|
||||
public async serializeNotebook(notebook: azdata.nb.INotebookContents): Promise<string> {
|
||||
let notebookData: vscode.NotebookData = {
|
||||
cells: notebook.cells?.map<vscode.NotebookCellData>(cell => {
|
||||
return {
|
||||
kind: cell.cell_type === 'code' ? NotebookCellKind.Code : NotebookCellKind.Markup,
|
||||
value: Array.isArray(cell.source) ? cell.source.join('\n') : cell.source,
|
||||
languageId: cell.metadata?.language,
|
||||
outputs: cell.outputs?.map<vscode.NotebookCellOutput>(output => VSCodeContentManager.convertToVSCodeCellOutput(output)),
|
||||
executionSummary: {
|
||||
executionOrder: cell.execution_count
|
||||
}
|
||||
};
|
||||
}),
|
||||
metadata: notebook.metadata
|
||||
};
|
||||
notebookData.metadata.custom = {
|
||||
nbformat: notebook.nbformat,
|
||||
nbformat_minor: notebook.nbformat_minor
|
||||
};
|
||||
|
||||
let bytes = await this._serializer.serializeNotebook(notebookData, new CancellationTokenSource().token);
|
||||
let buffer = VSBuffer.wrap(bytes);
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class VSCodeSerializationManager implements azdata.nb.SerializationManager {
|
||||
private _manager: VSCodeContentManager;
|
||||
|
||||
constructor(serializer: vscode.NotebookSerializer) {
|
||||
this._manager = new VSCodeContentManager(serializer);
|
||||
}
|
||||
|
||||
public get contentManager(): azdata.nb.ContentManager {
|
||||
return this._manager;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A Notebook Serialization Provider that is used to convert VS Code notebook extension APIs into ADS equivalents.
|
||||
*/
|
||||
export class VSCodeSerializationProvider implements azdata.nb.NotebookSerializationProvider {
|
||||
private _manager: VSCodeSerializationManager;
|
||||
|
||||
constructor(public readonly providerId: string, serializer: vscode.NotebookSerializer) {
|
||||
this._manager = new VSCodeSerializationManager(serializer);
|
||||
}
|
||||
|
||||
public getSerializationManager(notebookUri: vscode.Uri): Thenable<azdata.nb.SerializationManager> {
|
||||
return Promise.resolve(this._manager);
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,10 @@ export const RESOURCE_VIEWER_TYPEID = 'workbench.editorInput.resourceViewerInput
|
||||
|
||||
export const JUPYTER_PROVIDER_ID = 'jupyter';
|
||||
|
||||
// The version of the notebook file format that we support
|
||||
export const NBFORMAT = 4;
|
||||
export const NBFORMAT_MINOR = 2;
|
||||
|
||||
export const enum NotebookLanguage {
|
||||
Notebook = 'Notebook',
|
||||
Ipynb = 'ipynb'
|
||||
|
||||
@@ -444,10 +444,10 @@ export abstract class NotebookInput extends EditorInput implements INotebookInpu
|
||||
this._providerId = providerIds.filter(provider => provider !== DEFAULT_NOTEBOOK_PROVIDER)[0];
|
||||
this._providers = providerIds;
|
||||
this._standardKernels = [];
|
||||
this._providers.forEach(provider => {
|
||||
let standardKernels = getStandardKernelsForProvider(provider, this.notebookService);
|
||||
for (let provider of this._providers) {
|
||||
let standardKernels = await getStandardKernelsForProvider(provider, this.notebookService);
|
||||
this._standardKernels.push(...standardKernels);
|
||||
});
|
||||
}
|
||||
let serializationProvider = await this.notebookService.getOrCreateSerializationManager(this._providerId, this._resource);
|
||||
this._contentLoader = this.instantiationService.createInstance(NotebookEditorContentLoader, this, serializationProvider.contentManager);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import { ServiceCollection } from 'vs/platform/instantiation/common/serviceColle
|
||||
import { ExecuteManagerStub, SerializationManagerStub } from 'sql/workbench/contrib/notebook/test/stubs';
|
||||
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
|
||||
import { UndoRedoService } from 'vs/platform/undoRedo/common/undoRedoService';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
suite('CellToolbarActions', function (): void {
|
||||
suite('removeDuplicatedAndStartingSeparators', function (): void {
|
||||
@@ -211,8 +212,8 @@ export async function createandLoadNotebookModel(codeContent?: nb.INotebookConte
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let serviceCollection = new ServiceCollection();
|
||||
|
||||
@@ -31,6 +31,7 @@ import { Separator } from 'vs/base/common/actions';
|
||||
import { INotebookView, INotebookViews } from 'sql/workbench/services/notebook/browser/notebookViews/notebookViews';
|
||||
import * as TelemetryKeys from 'sql/platform/telemetry/common/telemetryKeys';
|
||||
import { ITelemetryEventProperties } from 'sql/platform/telemetry/common/telemetry';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
class TestClientSession extends ClientSessionStub {
|
||||
private _errorState: boolean = false;
|
||||
@@ -280,8 +281,8 @@ suite('Notebook Actions', function (): void {
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let mockNotification = TypeMoq.Mock.ofType<INotificationService>(TestNotificationService);
|
||||
@@ -324,8 +325,8 @@ suite('Notebook Actions', function (): void {
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let expectedMsg: string = noParameterCell;
|
||||
|
||||
@@ -365,8 +366,8 @@ suite('Notebook Actions', function (): void {
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let expectedMsg: string = noParametersInCell;
|
||||
|
||||
@@ -410,8 +411,8 @@ suite('Notebook Actions', function (): void {
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let expectedMsg: string = noParametersInCell;
|
||||
|
||||
@@ -456,8 +457,8 @@ suite('Notebook Actions', function (): void {
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let expectedMsg: string = noParametersInCell;
|
||||
|
||||
@@ -505,8 +506,8 @@ suite('Notebook Actions', function (): void {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let expectedMsg: string = kernelNotSupported;
|
||||
|
||||
|
||||
@@ -36,12 +36,12 @@ suite('Notebook Input', function (): void {
|
||||
const mockNotebookService = TypeMoq.Mock.ofType<INotebookService>(NotebookServiceStub);
|
||||
mockNotebookService.setup(s => s.getProvidersForFileType(TypeMoq.It.isAny())).returns(() => [testProvider]);
|
||||
mockNotebookService.setup(s => s.getStandardKernelsForProvider(TypeMoq.It.isAny())).returns(() => {
|
||||
return [{
|
||||
return Promise.resolve([{
|
||||
name: 'TestName',
|
||||
displayName: 'TestDisplayName',
|
||||
connectionProviderIds: ['TestId'],
|
||||
notebookProvider: testProvider
|
||||
}];
|
||||
}]);
|
||||
});
|
||||
let testManager: ISerializationManager = {
|
||||
providerId: testProvider,
|
||||
|
||||
@@ -222,7 +222,8 @@ suite.skip('NotebookService:', function (): void {
|
||||
assert.strictEqual(notebookService.listNotebookEditors().length, 0, 'No notebook editors should be listed');
|
||||
assert.strictEqual(notebookService.getMimeRegistry().mimeTypes.length, 15, 'MIME Types need to have appropriate tests when added or removed');
|
||||
assert.deepStrictEqual(notebookService.getProvidersForFileType('.ipynb'), ['sql'], 'sql provider should be registered for ipynb extension');
|
||||
assert.strictEqual(notebookService.getStandardKernelsForProvider('sql').length, 1, 'SQL kernel should be provided by default');
|
||||
let standardKernels = await notebookService.getStandardKernelsForProvider('sql');
|
||||
assert.strictEqual(standardKernels.length, 1, 'SQL kernel should be provided by default');
|
||||
assert.strictEqual(notebookService.getStandardKernelsForProvider('otherProvider'), undefined, 'Other provider should not have kernels since it has not been added as a provider');
|
||||
assert.deepStrictEqual(notebookService.getSupportedFileExtensions(), ['.ipynb'], 'IPYNB file extension should be supported by default');
|
||||
await notebookService.registrationComplete;
|
||||
@@ -248,7 +249,8 @@ suite.skip('NotebookService:', function (): void {
|
||||
|
||||
assert.deepStrictEqual(notebookService.getProvidersForFileType('.ipynb'), ['sql', 'otherProvider'], 'otherProvider should also be registered for ipynb extension');
|
||||
assert.deepStrictEqual(notebookService.getSupportedFileExtensions(), ['.ipynb'], 'Only IPYNB should be registered as supported file extension');
|
||||
assert.strictEqual(notebookService.getStandardKernelsForProvider('otherProvider').length, 1, 'otherProvider kernel info could not be found');
|
||||
let standardKernels = await notebookService.getStandardKernelsForProvider('otherProvider');
|
||||
assert.strictEqual(standardKernels.length, 1, 'otherProvider kernel info could not be found');
|
||||
assert.deepStrictEqual(notebookService.getStandardKernelsForProvider('otherProvider')[0], otherProviderRegistration.standardKernels[0], 'otherProviderRegistration standard kernels does not match');
|
||||
});
|
||||
|
||||
@@ -557,7 +559,7 @@ suite.skip('NotebookService:', function (): void {
|
||||
await notebookService.registrationComplete;
|
||||
queryManagementService.onHandlerAddedEmitter.fire(SQL_NOTEBOOK_PROVIDER);
|
||||
const connectionTypes = queryManagementService.getRegisteredProviders();
|
||||
const kernels = notebookService.getStandardKernelsForProvider(SQL_NOTEBOOK_PROVIDER);
|
||||
const kernels = await notebookService.getStandardKernelsForProvider(SQL_NOTEBOOK_PROVIDER);
|
||||
for (const kernel of kernels) {
|
||||
assert.strictEqual(kernel.name, notebookConstants.SQL, `kernel name for standard kernels should be ${notebookConstants.SQL}`);
|
||||
assert.strictEqual(kernel.displayName, notebookConstants.SQL, `kernel displayName for standard kernels should be ${notebookConstants.SQL}`);
|
||||
|
||||
@@ -33,6 +33,8 @@ import { TestConfigurationService } from 'sql/platform/connection/test/common/te
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
import { NotebookViewModel } from 'sql/workbench/services/notebook/browser/notebookViews/notebookViewModel';
|
||||
import { isUndefinedOrNull } from 'vs/base/common/types';
|
||||
import { SQL_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
let initialNotebookContent: nb.INotebookContents = {
|
||||
cells: [{
|
||||
@@ -52,8 +54,8 @@ let initialNotebookContent: nb.INotebookContents = {
|
||||
language: 'sql'
|
||||
},
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let notebookContentWithoutMeta: nb.INotebookContents = {
|
||||
@@ -67,8 +69,8 @@ let notebookContentWithoutMeta: nb.INotebookContents = {
|
||||
execution_count: 1
|
||||
}],
|
||||
metadata: {},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let defaultUri = URI.file('/some/path.ipynb');
|
||||
@@ -240,6 +242,7 @@ suite('NotebookViewModel', function (): void {
|
||||
|
||||
function setupServices() {
|
||||
mockSessionManager = TypeMoq.Mock.ofType(SessionManager);
|
||||
executeManagers[0].providerId = SQL_NOTEBOOK_PROVIDER;
|
||||
executeManagers[0].sessionManager = mockSessionManager.object;
|
||||
notificationService = TypeMoq.Mock.ofType<INotificationService>(TestNotificationService, TypeMoq.MockBehavior.Loose);
|
||||
capabilitiesService = TypeMoq.Mock.ofType<ICapabilitiesService>(TestCapabilitiesService);
|
||||
|
||||
@@ -33,6 +33,8 @@ import { TestStorageService } from 'vs/workbench/test/common/workbenchTestServic
|
||||
import sinon = require('sinon');
|
||||
import { InsertCellsModal } from 'sql/workbench/contrib/notebook/browser/notebookViews/insertCellsModal';
|
||||
import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKeybindingService';
|
||||
import { SQL_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
let initialNotebookContent: nb.INotebookContents = {
|
||||
cells: [{
|
||||
@@ -52,8 +54,8 @@ let initialNotebookContent: nb.INotebookContents = {
|
||||
language: 'sql'
|
||||
},
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
suite('Notebook Views Actions', function (): void {
|
||||
@@ -165,6 +167,7 @@ suite('Notebook Views Actions', function (): void {
|
||||
|
||||
function setupServices() {
|
||||
mockSessionManager = TypeMoq.Mock.ofType(SessionManager);
|
||||
executeManagers[0].providerId = SQL_NOTEBOOK_PROVIDER;
|
||||
executeManagers[0].sessionManager = mockSessionManager.object;
|
||||
notificationService = TypeMoq.Mock.ofType<INotificationService>(TestNotificationService, TypeMoq.MockBehavior.Loose);
|
||||
capabilitiesService = TypeMoq.Mock.ofType<ICapabilitiesService>(TestCapabilitiesService);
|
||||
|
||||
@@ -35,6 +35,8 @@ import { TestDialogService } from 'vs/platform/dialogs/test/common/testDialogSer
|
||||
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
|
||||
import { UndoRedoService } from 'vs/platform/undoRedo/common/undoRedoService';
|
||||
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
|
||||
import { SQL_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
let initialNotebookContent: nb.INotebookContents = {
|
||||
cells: [{
|
||||
@@ -54,8 +56,8 @@ let initialNotebookContent: nb.INotebookContents = {
|
||||
language: 'sql'
|
||||
},
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let defaultUri = URI.file('/some/path.ipynb');
|
||||
@@ -151,6 +153,7 @@ suite('NotebookViews', function (): void {
|
||||
|
||||
function setupServices() {
|
||||
mockSessionManager = TypeMoq.Mock.ofType(SessionManager);
|
||||
executeManagers[0].providerId = SQL_NOTEBOOK_PROVIDER;
|
||||
executeManagers[0].sessionManager = mockSessionManager.object;
|
||||
notificationService = TypeMoq.Mock.ofType<INotificationService>(TestNotificationService, TypeMoq.MockBehavior.Loose);
|
||||
capabilitiesService = TypeMoq.Mock.ofType<ICapabilitiesService>(TestCapabilitiesService);
|
||||
|
||||
@@ -17,6 +17,7 @@ import { TestFileService } from 'vs/workbench/test/browser/workbenchTestServices
|
||||
import { IFileService, IReadFileOptions, IFileContent, IWriteFileOptions, IFileStatWithMetadata } from 'vs/platform/files/common/files';
|
||||
import { VSBuffer, VSBufferReadable } from 'vs/base/common/buffer';
|
||||
import { promisify } from 'util';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
let expectedNotebookContent: nb.INotebookContents = {
|
||||
cells: [{
|
||||
@@ -32,8 +33,8 @@ let expectedNotebookContent: nb.INotebookContents = {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 2
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let notebookContentString = JSON.stringify(expectedNotebookContent);
|
||||
@@ -105,8 +106,8 @@ suite('Local Content Manager', function (): void {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 2
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let mimeContentString = JSON.stringify(mimeNotebook);
|
||||
// when I read the content
|
||||
@@ -155,8 +156,8 @@ suite('Local Content Manager', function (): void {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 2
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let markdownNotebookContent = JSON.stringify(expectedNotebookMarkdownContent);
|
||||
// verify that notebooks support markdown cells
|
||||
@@ -189,8 +190,8 @@ suite('Local Content Manager', function (): void {
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 2
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let streamOutputContent = JSON.stringify(expectedNotebookStreamOutputContent);
|
||||
// Verify that the stream output type is supported
|
||||
|
||||
@@ -27,11 +27,13 @@ import { InstantiationService } from 'vs/platform/instantiation/common/instantia
|
||||
import { ClientSession } from 'sql/workbench/services/notebook/browser/models/clientSession';
|
||||
import { TestStorageService } from 'vs/workbench/test/common/workbenchTestServices';
|
||||
import { NotebookEditorContentLoader } from 'sql/workbench/contrib/notebook/browser/models/notebookInput';
|
||||
import { NotebookRange } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NotebookRange, SQL_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NotebookMarkdownRenderer } from 'sql/workbench/contrib/notebook/browser/outputs/notebookMarkdown';
|
||||
import { NullAdsTelemetryService } from 'sql/platform/telemetry/common/adsTelemetryService';
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
import { TestConfigurationService } from 'sql/platform/connection/test/common/testConfigurationService';
|
||||
import { SessionManager } from 'sql/workbench/contrib/notebook/test/emptySessionClasses';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
let expectedNotebookContent: nb.INotebookContents = {
|
||||
cells: [{
|
||||
@@ -52,8 +54,8 @@ let expectedNotebookContent: nb.INotebookContents = {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let defaultUri = URI.file('/some/path.ipynb');
|
||||
@@ -78,6 +80,9 @@ suite('Notebook Find Model', function (): void {
|
||||
let configurationService: IConfigurationService;
|
||||
|
||||
setup(async () => {
|
||||
let mockSessionManager = TypeMoq.Mock.ofType(SessionManager);
|
||||
executeManagers[0].providerId = SQL_NOTEBOOK_PROVIDER;
|
||||
executeManagers[0].sessionManager = mockSessionManager.object;
|
||||
sessionReady = new Deferred<void>();
|
||||
notificationService = TypeMoq.Mock.ofType<INotificationService>(TestNotificationService, TypeMoq.MockBehavior.Loose);
|
||||
capabilitiesService = TypeMoq.Mock.ofType<ICapabilitiesService>(TestCapabilitiesService);
|
||||
@@ -195,8 +200,8 @@ suite('Notebook Find Model', function (): void {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
await initNotebookModel(markdownContent);
|
||||
|
||||
@@ -228,8 +233,8 @@ suite('Notebook Find Model', function (): void {
|
||||
display_name: 'Python'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
await initNotebookModel(codeContent);
|
||||
//initialize find
|
||||
@@ -254,8 +259,8 @@ suite('Notebook Find Model', function (): void {
|
||||
display_name: 'Python'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
await initNotebookModel(codeContent);
|
||||
//initialize find
|
||||
@@ -315,8 +320,8 @@ suite('Notebook Find Model', function (): void {
|
||||
display_name: 'Python'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
await initNotebookModel(codeContent);
|
||||
//initialize find
|
||||
@@ -348,8 +353,8 @@ suite('Notebook Find Model', function (): void {
|
||||
display_name: 'Python'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
await initNotebookModel(codeContent);
|
||||
//initialize find
|
||||
@@ -380,8 +385,8 @@ suite('Notebook Find Model', function (): void {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
await initNotebookModel(markdownContent);
|
||||
|
||||
@@ -435,7 +440,7 @@ suite('Notebook Find Model', function (): void {
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
await initNotebookModel(cellContent);
|
||||
|
||||
@@ -546,7 +551,7 @@ suite('Notebook Find Model', function (): void {
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
max_find_count = 4;
|
||||
await initNotebookModel(cellContent);
|
||||
|
||||
@@ -42,6 +42,8 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
|
||||
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
|
||||
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
|
||||
import { UndoRedoService } from 'vs/platform/undoRedo/common/undoRedoService';
|
||||
import { SQL_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
let expectedNotebookContent: nb.INotebookContents = {
|
||||
cells: [{
|
||||
@@ -64,8 +66,8 @@ let expectedNotebookContent: nb.INotebookContents = {
|
||||
name: 'sql'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let expectedNotebookContentOneCell: nb.INotebookContents = {
|
||||
@@ -82,8 +84,8 @@ let expectedNotebookContentOneCell: nb.INotebookContents = {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let expectedKernelAliasNotebookContentOneCell: nb.INotebookContents = {
|
||||
@@ -103,8 +105,8 @@ let expectedKernelAliasNotebookContentOneCell: nb.INotebookContents = {
|
||||
version: ''
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let expectedParameterizedNotebookContent: nb.INotebookContents = {
|
||||
@@ -126,8 +128,8 @@ let expectedParameterizedNotebookContent: nb.INotebookContents = {
|
||||
display_name: 'Python 3'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let defaultUri = URI.file('/some/path.ipynb');
|
||||
@@ -154,6 +156,7 @@ suite('notebook model', function (): void {
|
||||
const logService = new NullLogService();
|
||||
setup(() => {
|
||||
mockSessionManager = TypeMoq.Mock.ofType(SessionManager);
|
||||
executeManagers[0].providerId = SQL_NOTEBOOK_PROVIDER;
|
||||
executeManagers[0].sessionManager = mockSessionManager.object;
|
||||
sessionReady = new Deferred<void>();
|
||||
notificationService = TypeMoq.Mock.ofType<INotificationService>(TestNotificationService, TypeMoq.MockBehavior.Loose);
|
||||
@@ -207,8 +210,8 @@ suite('notebook model', function (): void {
|
||||
display_name: 'SQL'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
|
||||
let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentLoader);
|
||||
@@ -307,9 +310,9 @@ suite('notebook model', function (): void {
|
||||
|
||||
// Check that the getters return the correct values
|
||||
assert.strictEqual(model.executeManagers.length, 2, 'There should be 2 notebook managers');
|
||||
assert(!isUndefinedOrNull(model.getExecuteManager('SQL')), 'SQL notebook manager is not defined');
|
||||
assert(!isUndefinedOrNull(model.getExecuteManager('jupyter')), 'Jupyter notebook manager is not defined');
|
||||
assert(isUndefinedOrNull(model.getExecuteManager('foo')), 'foo notebook manager is incorrectly defined');
|
||||
assert(model.executeManagers.some(m => m.providerId === 'SQL'), 'SQL notebook manager should be defined');
|
||||
assert(model.executeManagers.some(m => m.providerId === 'jupyter'), 'Jupyter notebook manager should be defined');
|
||||
assert(model.executeManagers.every(m => m.providerId !== 'foo'), 'foo notebook manager should not be defined');
|
||||
|
||||
// Check other properties to ensure that they're returning as expected
|
||||
// No server manager was passed into the notebook manager stub, so expect hasServerManager to return false
|
||||
@@ -626,8 +629,8 @@ suite('notebook model', function (): void {
|
||||
name: 'sql'
|
||||
}
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentLoader);
|
||||
mockContentManager.setup(c => c.loadContent()).returns(() => Promise.resolve(expectedNotebookContentSplitCells));
|
||||
@@ -905,8 +908,8 @@ suite('notebook model', function (): void {
|
||||
metadata: {
|
||||
connection_name: connectionName
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentLoader);
|
||||
mockContentManager.setup(c => c.loadContent()).returns(() => Promise.resolve(notebook));
|
||||
@@ -955,8 +958,8 @@ suite('notebook model', function (): void {
|
||||
metadata: {
|
||||
multi_connection_mode: true
|
||||
},
|
||||
nbformat: 4,
|
||||
nbformat_minor: 5
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
};
|
||||
let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentLoader);
|
||||
mockContentManager.setup(c => c.loadContent()).returns(() => Promise.resolve(notebook));
|
||||
|
||||
@@ -41,13 +41,13 @@ suite('notebookUtils', function (): void {
|
||||
|
||||
// getStandardKernelsForProvider
|
||||
let returnHandler = (provider) => {
|
||||
let result = undefined;
|
||||
if (provider === testProvider) {
|
||||
return [testKernel];
|
||||
result = [testKernel];
|
||||
} else if (provider === SQL_NOTEBOOK_PROVIDER) {
|
||||
return [sqlStandardKernel];
|
||||
} else {
|
||||
return undefined;
|
||||
result = [sqlStandardKernel];
|
||||
}
|
||||
return Promise.resolve(result);
|
||||
};
|
||||
mockNotebookService.setup(n => n.getStandardKernelsForProvider(TypeMoq.It.isAnyString())).returns(returnHandler);
|
||||
mockNotebookService.setup(n => n.getStandardKernelsForProvider(TypeMoq.It.isAnyString())).returns(returnHandler);
|
||||
@@ -91,19 +91,19 @@ suite('notebookUtils', function (): void {
|
||||
test('getStandardKernelsForProvider Test', async function (): Promise<void> {
|
||||
setupMockNotebookService();
|
||||
|
||||
let result = getStandardKernelsForProvider(undefined, undefined);
|
||||
let result = await getStandardKernelsForProvider(undefined, undefined);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
result = getStandardKernelsForProvider(undefined, mockNotebookService.object);
|
||||
result = await getStandardKernelsForProvider(undefined, mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
result = getStandardKernelsForProvider('testProvider', undefined);
|
||||
result = await getStandardKernelsForProvider('testProvider', undefined);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
result = getStandardKernelsForProvider('NotARealProvider', mockNotebookService.object);
|
||||
result = await getStandardKernelsForProvider('NotARealProvider', mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, [Object.assign({ notebookProvider: 'NotARealProvider' }, sqlStandardKernel)]);
|
||||
|
||||
result = getStandardKernelsForProvider('testProvider', mockNotebookService.object);
|
||||
result = await getStandardKernelsForProvider('testProvider', mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, [<IStandardKernelWithProvider>{
|
||||
name: 'testName',
|
||||
displayName: 'testDisplayName',
|
||||
|
||||
@@ -281,7 +281,7 @@ export class NotebookServiceStub implements INotebookService {
|
||||
getProvidersForFileType(fileType: string): string[] {
|
||||
return [];
|
||||
}
|
||||
getStandardKernelsForProvider(provider: string): nb.IStandardKernel[] {
|
||||
getStandardKernelsForProvider(provider: string): Promise<nb.IStandardKernel[]> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
getOrCreateSerializationManager(providerId: string, uri: URI): Promise<ISerializationManager> {
|
||||
|
||||
@@ -611,7 +611,8 @@ export class CellModel extends Disposable implements ICellModel {
|
||||
if (tryMatchCellMagic(this.source[0]) !== ads_execute_command || !this._isCommandExecutionSettingEnabled) {
|
||||
const future = kernel.requestExecute({
|
||||
code: content,
|
||||
stop_on_error: true
|
||||
stop_on_error: true,
|
||||
notebookUri: this.notebookModel.notebookUri
|
||||
}, false);
|
||||
this.setFuture(future as FutureInternal);
|
||||
this.fireExecutionStateChanged();
|
||||
|
||||
@@ -77,6 +77,9 @@ export class ClientSession implements IClientSession {
|
||||
}
|
||||
|
||||
private async startServer(kernelSpec: nb.IKernelSpec): Promise<void> {
|
||||
if (!this._executeManager) {
|
||||
throw new Error(localize('NoExecuteManager', "Server could not start because a provider was not defined for this notebook file type."));
|
||||
}
|
||||
let serverManager = this._executeManager.serverManager;
|
||||
if (serverManager) {
|
||||
await serverManager.startServer(kernelSpec);
|
||||
|
||||
@@ -179,13 +179,6 @@ export class NotebookModel extends Disposable implements INotebookModel {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public getExecuteManager(providerId: string): IExecuteManager | undefined {
|
||||
if (providerId) {
|
||||
return this.executeManagers.find(manager => manager.providerId === providerId);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public get notebookOptions(): INotebookModelOptions {
|
||||
return this._notebookOptions;
|
||||
}
|
||||
@@ -519,7 +512,7 @@ export class NotebookModel extends Disposable implements INotebookModel {
|
||||
|
||||
public async requestModelLoad(): Promise<void> {
|
||||
try {
|
||||
this.setDefaultKernelAndProviderId();
|
||||
await this.setDefaultKernelAndProviderId();
|
||||
this.trySetLanguageFromLangInfo();
|
||||
} catch (error) {
|
||||
this._inErrorState = true;
|
||||
@@ -975,7 +968,15 @@ export class NotebookModel extends Disposable implements INotebookModel {
|
||||
this._activeClientSession = clientSession;
|
||||
}
|
||||
|
||||
public setDefaultKernelAndProviderId() {
|
||||
public async setDefaultKernelAndProviderId(): Promise<void> {
|
||||
if (!this._defaultKernel) {
|
||||
await this.executeManager.sessionManager.ready;
|
||||
if (this.executeManager.sessionManager.specs) {
|
||||
let defaultKernelName = this.executeManager.sessionManager.specs.defaultKernel;
|
||||
this._defaultKernel = this.executeManager.sessionManager.specs.kernels.find(kernel => kernel.name === defaultKernelName);
|
||||
}
|
||||
}
|
||||
|
||||
if (this._capabilitiesService?.providers) {
|
||||
let providers = this._capabilitiesService.providers;
|
||||
for (const server in providers) {
|
||||
@@ -1416,7 +1417,7 @@ export class NotebookModel extends Disposable implements INotebookModel {
|
||||
this._onProviderIdChanged.fire(this._providerId);
|
||||
|
||||
await this.shutdownActiveSession();
|
||||
let manager = this.getExecuteManager(providerId);
|
||||
let manager = this.executeManager;
|
||||
if (manager) {
|
||||
await this.startSession(manager, displayName, false, kernelAlias);
|
||||
} else {
|
||||
|
||||
@@ -35,14 +35,14 @@ export function getProvidersForFileName(fileName: string, notebookService: INote
|
||||
return providers;
|
||||
}
|
||||
|
||||
export function getStandardKernelsForProvider(providerId: string, notebookService: INotebookService): IStandardKernelWithProvider[] {
|
||||
export async function getStandardKernelsForProvider(providerId: string, notebookService: INotebookService): Promise<IStandardKernelWithProvider[]> {
|
||||
if (!providerId || !notebookService) {
|
||||
return [];
|
||||
}
|
||||
let standardKernels = notebookService.getStandardKernelsForProvider(providerId);
|
||||
let standardKernels = await notebookService.getStandardKernelsForProvider(providerId);
|
||||
if (!standardKernels || standardKernels.length === 0) {
|
||||
// Fall back to using SQL provider instead
|
||||
standardKernels = notebookService.getStandardKernelsForProvider(SQL_NOTEBOOK_PROVIDER) ?? [];
|
||||
standardKernels = await notebookService.getStandardKernelsForProvider(SQL_NOTEBOOK_PROVIDER) ?? [];
|
||||
}
|
||||
standardKernels.forEach(kernel => {
|
||||
Object.assign(<IStandardKernelWithProvider>kernel, {
|
||||
|
||||
@@ -74,7 +74,7 @@ export interface INotebookService {
|
||||
|
||||
getProvidersForFileType(fileType: string): string[] | undefined;
|
||||
|
||||
getStandardKernelsForProvider(provider: string): azdata.nb.IStandardKernel[] | undefined;
|
||||
getStandardKernelsForProvider(provider: string): Promise<azdata.nb.IStandardKernel[] | undefined>;
|
||||
|
||||
getOrCreateSerializationManager(providerId: string, uri: URI): Promise<ISerializationManager>;
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editor
|
||||
import { IEditorInput, IEditorPane } from 'vs/workbench/common/editor';
|
||||
import { isINotebookInput } from 'sql/workbench/services/notebook/browser/interface';
|
||||
import { INotebookShowOptions } from 'sql/workbench/api/common/sqlExtHost.protocol';
|
||||
import { NotebookLanguage } from 'sql/workbench/common/constants';
|
||||
import { JUPYTER_PROVIDER_ID, NotebookLanguage } from 'sql/workbench/common/constants';
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
import { SqlSerializationProvider } from 'sql/workbench/services/notebook/browser/sql/sqlSerializationProvider';
|
||||
|
||||
@@ -134,6 +134,31 @@ export class ExecuteProviderDescriptor {
|
||||
}
|
||||
}
|
||||
|
||||
export class StandardKernelsDescriptor {
|
||||
private _instanceReady = new Deferred<nb.IStandardKernel[]>();
|
||||
constructor(private readonly _providerId: string, private _instance?: nb.IStandardKernel[]) {
|
||||
if (_instance) {
|
||||
this._instanceReady.resolve(_instance);
|
||||
}
|
||||
}
|
||||
|
||||
public get providerId(): string {
|
||||
return this._providerId;
|
||||
}
|
||||
|
||||
public get instanceReady(): Promise<nb.IStandardKernel[]> {
|
||||
return this._instanceReady.promise;
|
||||
}
|
||||
|
||||
public get instance(): nb.IStandardKernel[] | undefined {
|
||||
return this._instance;
|
||||
}
|
||||
public set instance(value: nb.IStandardKernel[]) {
|
||||
this._instance = value;
|
||||
this._instanceReady.resolve(value);
|
||||
}
|
||||
}
|
||||
|
||||
export const NotebookUriNotDefined = localize('notebookUriNotDefined', "No URI was passed when creating a notebook manager");
|
||||
export const NotebookServiceNoProviderRegistered = localize('notebookServiceNoProvider', "Notebook provider does not exist");
|
||||
export const FailToSaveTrustState = 'Failed to save trust state to cache';
|
||||
@@ -154,7 +179,7 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
private _onNotebookEditorRename = new Emitter<INotebookEditor>();
|
||||
private _editors = new Map<string, INotebookEditor>();
|
||||
private _fileToProviderDescriptions = new Map<string, ProviderDescriptionRegistration[]>();
|
||||
private _providerToStandardKernels = new Map<string, nb.IStandardKernel[]>();
|
||||
private _providerToStandardKernels = new Map<string, StandardKernelsDescriptor>();
|
||||
private _registrationComplete = new Deferred<void>();
|
||||
private _isRegistrationComplete = false;
|
||||
private _trustedCacheQueue: URI[] = [];
|
||||
@@ -291,13 +316,14 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
let sqlNotebookKernels = this._providerToStandardKernels.get(notebookConstants.SQL);
|
||||
if (sqlNotebookKernels) {
|
||||
let sqlConnectionTypes = this._queryManagementService.getRegisteredProviders();
|
||||
let kernel = sqlNotebookKernels.find(p => p.name === notebookConstants.SQL);
|
||||
let kernel = sqlNotebookKernels.instance.find(p => p.name === notebookConstants.SQL);
|
||||
if (kernel) {
|
||||
this._providerToStandardKernels.set(notebookConstants.SQL, [{
|
||||
let descriptor = new StandardKernelsDescriptor(notebookConstants.SQL, [{
|
||||
name: notebookConstants.SQL,
|
||||
displayName: notebookConstants.SQL,
|
||||
connectionProviderIds: sqlConnectionTypes
|
||||
}]);
|
||||
this._providerToStandardKernels.set(notebookConstants.SQL, descriptor);
|
||||
}
|
||||
}
|
||||
this._isRegistrationComplete = true;
|
||||
@@ -325,6 +351,17 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
this._executeProviders.set(p.id, new ExecuteProviderDescriptor(p.id));
|
||||
}
|
||||
this.addStandardKernels(registration);
|
||||
} else {
|
||||
// Standard kernels might get registered later for VSCode notebooks, so add a descriptor to wait on
|
||||
let descriptor = new StandardKernelsDescriptor(p.id);
|
||||
this._providerToStandardKernels.set(p.id.toUpperCase(), descriptor);
|
||||
}
|
||||
|
||||
// Emit activation event if the provider is not one of the default options
|
||||
if (p.id !== SQL_NOTEBOOK_PROVIDER && p.id !== JUPYTER_PROVIDER_ID) {
|
||||
this._extensionService.whenInstalledExtensionsRegistered().then(() => {
|
||||
this._extensionService.activateByEvent(`onNotebook:${p.id}`).catch(err => onUnexpectedError(err));
|
||||
}).catch(err => onUnexpectedError(err));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,18 +429,24 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
// kernels to the dropdown
|
||||
private addStandardKernels(provider: ProviderDescriptionRegistration) {
|
||||
let providerUpperCase = provider.provider.toUpperCase();
|
||||
let standardKernels = this._providerToStandardKernels.get(providerUpperCase);
|
||||
let descriptor = this._providerToStandardKernels.get(providerUpperCase);
|
||||
if (!descriptor) {
|
||||
descriptor = new StandardKernelsDescriptor(provider.provider);
|
||||
}
|
||||
let standardKernels = descriptor.instance;
|
||||
if (!standardKernels) {
|
||||
standardKernels = [];
|
||||
}
|
||||
provider.standardKernels.forEach(kernel => {
|
||||
standardKernels.push(kernel);
|
||||
});
|
||||
|
||||
// Filter out unusable kernels when running on a SAW
|
||||
if (this.productService.quality === 'saw') {
|
||||
standardKernels = standardKernels.filter(kernel => !kernel.blockedOnSAW);
|
||||
}
|
||||
this._providerToStandardKernels.set(providerUpperCase, standardKernels);
|
||||
descriptor.instance = standardKernels;
|
||||
this._providerToStandardKernels.set(providerUpperCase, descriptor);
|
||||
}
|
||||
|
||||
getSupportedFileExtensions(): string[] {
|
||||
@@ -415,8 +458,12 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
return providers?.map(provider => provider.provider);
|
||||
}
|
||||
|
||||
getStandardKernelsForProvider(provider: string): nb.IStandardKernel[] | undefined {
|
||||
return this._providerToStandardKernels.get(provider.toUpperCase());
|
||||
public async getStandardKernelsForProvider(provider: string): Promise<nb.IStandardKernel[] | undefined> {
|
||||
let descriptor = this._providerToStandardKernels.get(provider.toUpperCase());
|
||||
if (descriptor) {
|
||||
return this.waitOnStandardKernelsAvailability(descriptor);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private shutdown(): void {
|
||||
@@ -588,11 +635,12 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
|
||||
private async getExecuteProviderInstance(providerId: string, timeout?: number): Promise<IExecuteProvider> {
|
||||
let providerDescriptor = this._executeProviders.get(providerId);
|
||||
let kernelDescriptor = this._providerToStandardKernels.get(providerId.toUpperCase());
|
||||
let instance: IExecuteProvider;
|
||||
|
||||
// Try get from actual provider, waiting on its registration
|
||||
if (providerDescriptor) {
|
||||
if (!providerDescriptor.instance) {
|
||||
if (providerDescriptor && kernelDescriptor) {
|
||||
if (!providerDescriptor.instance || !kernelDescriptor.instance) {
|
||||
// Await extension registration before awaiting provider registration
|
||||
try {
|
||||
await this._extensionService.whenInstalledExtensionsRegistered();
|
||||
@@ -600,6 +648,12 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
this._logService.error(error);
|
||||
}
|
||||
instance = await this.waitOnExecuteProviderAvailability(providerDescriptor, timeout);
|
||||
if (instance) {
|
||||
let kernels = await this.waitOnStandardKernelsAvailability(kernelDescriptor, timeout);
|
||||
if (!kernels) {
|
||||
instance = undefined;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
instance = providerDescriptor.instance;
|
||||
}
|
||||
@@ -621,9 +675,12 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
private waitOnSerializationProviderAvailability(providerDescriptor: SerializationProviderDescriptor, timeout?: number): Promise<ISerializationProvider | undefined> {
|
||||
// Wait up to 30 seconds for the provider to be registered
|
||||
timeout = timeout ?? 30000;
|
||||
let promises: Promise<ISerializationProvider>[] = [
|
||||
let promises: Promise<ISerializationProvider | undefined>[] = [
|
||||
providerDescriptor.instanceReady,
|
||||
new Promise<ISerializationProvider>((resolve, reject) => setTimeout(() => resolve(undefined), timeout))
|
||||
new Promise<ISerializationProvider | undefined>((resolve, reject) => setTimeout(() => {
|
||||
onUnexpectedError(localize('serializationProviderTimeout', 'Waiting for Serialization Provider availability timed out for notebook provider \'{0}\'', providerDescriptor.providerId));
|
||||
resolve(undefined);
|
||||
}, timeout))
|
||||
];
|
||||
return Promise.race(promises);
|
||||
}
|
||||
@@ -631,9 +688,25 @@ export class NotebookService extends Disposable implements INotebookService {
|
||||
private waitOnExecuteProviderAvailability(providerDescriptor: ExecuteProviderDescriptor, timeout?: number): Promise<IExecuteProvider | undefined> {
|
||||
// Wait up to 30 seconds for the provider to be registered
|
||||
timeout = timeout ?? 30000;
|
||||
let promises: Promise<IExecuteProvider>[] = [
|
||||
let promises: Promise<IExecuteProvider | undefined>[] = [
|
||||
providerDescriptor.instanceReady,
|
||||
new Promise<IExecuteProvider>((resolve, reject) => setTimeout(() => resolve(undefined), timeout))
|
||||
new Promise<IExecuteProvider | undefined>((resolve, reject) => setTimeout(() => {
|
||||
onUnexpectedError(localize('executeProviderTimeout', 'Waiting for Execute Provider availability timed out for notebook provider \'{0}\'', providerDescriptor.providerId));
|
||||
resolve(undefined);
|
||||
}, timeout))
|
||||
];
|
||||
return Promise.race(promises);
|
||||
}
|
||||
|
||||
private waitOnStandardKernelsAvailability(kernelsDescriptor: StandardKernelsDescriptor, timeout?: number): Promise<nb.IStandardKernel[] | undefined> {
|
||||
// Wait up to 30 seconds for the kernels to be registered
|
||||
timeout = timeout ?? 30000;
|
||||
let promises: Promise<nb.IStandardKernel[] | undefined>[] = [
|
||||
kernelsDescriptor.instanceReady,
|
||||
new Promise<nb.IStandardKernel[] | undefined>((resolve, reject) => setTimeout(() => {
|
||||
onUnexpectedError(localize('standardKernelsTimeout', 'Waiting for Standard Kernels availability timed out for notebook provider \'{0}\'', kernelsDescriptor.providerId));
|
||||
resolve(undefined);
|
||||
}, timeout))
|
||||
];
|
||||
return Promise.race(promises);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import { JSONObject } from 'sql/workbench/services/notebook/common/jsonext';
|
||||
import { OutputTypes } from 'sql/workbench/services/notebook/common/contracts';
|
||||
import { nbversion } from 'sql/workbench/services/notebook/common/notebookConstants';
|
||||
import { nbformat } from 'sql/workbench/services/notebook/common/nbformat';
|
||||
import { NBFORMAT } from 'sql/workbench/common/constants';
|
||||
|
||||
type MimeBundle = { [key: string]: string | string[] | undefined };
|
||||
|
||||
@@ -65,7 +66,7 @@ namespace v4 {
|
||||
let notebook: nb.INotebookContents = {
|
||||
cells: [],
|
||||
metadata: contents.metadata,
|
||||
nbformat: 4,
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: contents.nbformat_minor
|
||||
};
|
||||
|
||||
@@ -206,7 +207,7 @@ namespace v3 {
|
||||
cells: [],
|
||||
metadata: contents.metadata,
|
||||
// Note: upgrading to v4 as we're converting to our codebase
|
||||
nbformat: 4,
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: nbversion.MINOR_VERSION
|
||||
};
|
||||
|
||||
|
||||
@@ -112,6 +112,7 @@ export interface INotebookProviderRegistry {
|
||||
|
||||
readonly onNewDescriptionRegistration: Event<{ id: string, registration: ProviderDescriptionRegistration }>;
|
||||
|
||||
updateProviderDescriptionLanguages(providerId: string, languages: string[]): void;
|
||||
registerProviderDescription(provider: ProviderDescriptionRegistration): void;
|
||||
registerNotebookLanguageMagic(magic: NotebookLanguageMagicRegistration): void;
|
||||
}
|
||||
@@ -123,6 +124,24 @@ class NotebookProviderRegistry implements INotebookProviderRegistry {
|
||||
private _onNewDescriptionRegistration = new Emitter<{ id: string, registration: ProviderDescriptionRegistration }>();
|
||||
public readonly onNewDescriptionRegistration: Event<{ id: string, registration: ProviderDescriptionRegistration }> = this._onNewDescriptionRegistration.event;
|
||||
|
||||
updateProviderDescriptionLanguages(providerId: string, languages: string[]): void {
|
||||
let registration = this._providerDescriptionRegistration.get(providerId);
|
||||
if (!registration) {
|
||||
throw new Error(localize('providerNotInRegistryError', "The specified provider '{0}' is not present in the notebook registry.", providerId));
|
||||
}
|
||||
let kernels = languages.map<azdata.nb.IStandardKernel>(language => {
|
||||
return {
|
||||
name: language,
|
||||
displayName: language,
|
||||
connectionProviderIds: []
|
||||
};
|
||||
});
|
||||
registration.standardKernels = kernels;
|
||||
|
||||
// Update provider description with new info
|
||||
this.registerProviderDescription(registration);
|
||||
}
|
||||
|
||||
registerProviderDescription(registration: ProviderDescriptionRegistration): void {
|
||||
this._providerDescriptionRegistration.set(registration.provider, registration);
|
||||
this._onNewDescriptionRegistration.fire({ id: registration.provider, registration: registration });
|
||||
|
||||
@@ -20,6 +20,8 @@ import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKe
|
||||
import { ExtHostNotebookShape } from 'sql/workbench/api/common/sqlExtHost.protocol';
|
||||
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
|
||||
import { IProductService } from 'vs/platform/product/common/productService';
|
||||
import { Disposable, NotebookCell, NotebookController, NotebookDocument, NotebookDocumentContentOptions, NotebookRegistrationData, NotebookRendererScript, NotebookSerializer } from 'vscode';
|
||||
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
|
||||
|
||||
suite('MainThreadNotebook Tests', () => {
|
||||
|
||||
@@ -181,6 +183,18 @@ suite('MainThreadNotebook Tests', () => {
|
||||
});
|
||||
|
||||
class ExtHostNotebookStub implements ExtHostNotebookShape {
|
||||
$registerExecuteProvider(provider: azdata.nb.NotebookExecuteProvider): Disposable {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
$registerSerializationProvider(provider: azdata.nb.NotebookSerializationProvider): Disposable {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
$registerNotebookSerializer(notebookType: string, serializer: NotebookSerializer, options?: NotebookDocumentContentOptions, registration?: NotebookRegistrationData): Disposable {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
$createNotebookController(extension: IExtensionDescription, id: string, viewType: string, label: string, handler?: (cells: NotebookCell[], notebook: NotebookDocument, controller: NotebookController) => void | Thenable<void>, rendererScripts?: NotebookRendererScript[]): NotebookController {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
$getSerializationManagerDetails(providerHandle: number, notebookUri: UriComponents): Thenable<ISerializationManagerDetails> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,338 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { VSCodeContentManager } from 'sql/workbench/api/common/vscodeSerializationProvider';
|
||||
import type * as vscode from 'vscode';
|
||||
import type * as azdata from 'azdata';
|
||||
import * as sinon from 'sinon';
|
||||
import { NotebookCellKind } from 'vs/workbench/api/common/extHostTypes';
|
||||
import { VSBuffer } from 'vs/base/common/buffer';
|
||||
import * as assert from 'assert';
|
||||
import { OutputTypes } from 'sql/workbench/services/notebook/common/contracts';
|
||||
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
|
||||
|
||||
class MockNotebookSerializer implements vscode.NotebookSerializer {
|
||||
deserializeNotebook(content: Uint8Array, token: vscode.CancellationToken): vscode.NotebookData | Thenable<vscode.NotebookData> {
|
||||
return undefined;
|
||||
}
|
||||
serializeNotebook(data: vscode.NotebookData, token: vscode.CancellationToken): Uint8Array | Thenable<Uint8Array> {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
}
|
||||
|
||||
suite('Notebook Serializer', () => {
|
||||
let contentManager: VSCodeContentManager;
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
let serializeSpy: sinon.SinonSpy;
|
||||
|
||||
const deserializeResult: vscode.NotebookData = {
|
||||
cells: [{
|
||||
kind: NotebookCellKind.Code,
|
||||
value: '1+1',
|
||||
languageId: 'python',
|
||||
outputs: [{
|
||||
id: '1',
|
||||
items: [{
|
||||
mime: 'text/plain',
|
||||
data: VSBuffer.fromString('2').buffer
|
||||
}],
|
||||
metadata: {}
|
||||
}],
|
||||
executionSummary: {
|
||||
executionOrder: 1
|
||||
}
|
||||
}, {
|
||||
kind: NotebookCellKind.Code,
|
||||
value: 'print(1)',
|
||||
languageId: 'python',
|
||||
outputs: [{
|
||||
id: '2',
|
||||
items: [{
|
||||
mime: 'text/plain',
|
||||
data: VSBuffer.fromString('1').buffer
|
||||
}],
|
||||
metadata: {}
|
||||
}],
|
||||
executionSummary: {
|
||||
executionOrder: 2
|
||||
}
|
||||
}],
|
||||
metadata: {
|
||||
kernelspec: {
|
||||
name: 'python3',
|
||||
display_name: 'Python 3',
|
||||
language: 'python'
|
||||
},
|
||||
language_info: {
|
||||
name: 'python',
|
||||
version: '3.8.10',
|
||||
mimetype: 'text/x-python',
|
||||
codemirror_mode: {
|
||||
name: 'ipython',
|
||||
version: '3'
|
||||
}
|
||||
},
|
||||
custom: {
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const expectedDeserializedNotebook: azdata.nb.INotebookContents = {
|
||||
metadata: {
|
||||
kernelspec: {
|
||||
name: 'python3',
|
||||
display_name: 'Python 3',
|
||||
language: 'python'
|
||||
},
|
||||
language_info: {
|
||||
name: 'python',
|
||||
version: '3.8.10',
|
||||
mimetype: 'text/x-python',
|
||||
codemirror_mode: {
|
||||
name: 'ipython',
|
||||
version: '3'
|
||||
}
|
||||
}
|
||||
},
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR,
|
||||
cells: [
|
||||
{
|
||||
cell_type: 'code',
|
||||
source: '1+1',
|
||||
outputs: [
|
||||
{
|
||||
id: '1',
|
||||
output_type: 'execute_result',
|
||||
data: {
|
||||
'text/plain': '2'
|
||||
},
|
||||
metadata: {},
|
||||
execution_count: 1
|
||||
} as azdata.nb.IExecuteResult
|
||||
],
|
||||
execution_count: 1,
|
||||
metadata: {
|
||||
language: 'python'
|
||||
}
|
||||
},
|
||||
{
|
||||
cell_type: 'code',
|
||||
source: 'print(1)',
|
||||
outputs: [
|
||||
{
|
||||
id: '2',
|
||||
output_type: 'execute_result',
|
||||
data: {
|
||||
'text/plain': '1'
|
||||
},
|
||||
metadata: {},
|
||||
execution_count: 2
|
||||
} as azdata.nb.IExecuteResult
|
||||
],
|
||||
execution_count: 2,
|
||||
metadata: {
|
||||
language: 'python'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const expectedSerializeArg: vscode.NotebookData = {
|
||||
cells: [{
|
||||
kind: NotebookCellKind.Code,
|
||||
value: '1+1',
|
||||
languageId: 'python',
|
||||
outputs: [{
|
||||
items: [{
|
||||
mime: 'text/plain',
|
||||
data: VSBuffer.fromString('2').buffer
|
||||
}],
|
||||
metadata: {},
|
||||
id: '1'
|
||||
}],
|
||||
executionSummary: {
|
||||
executionOrder: 1
|
||||
}
|
||||
}, {
|
||||
kind: NotebookCellKind.Code,
|
||||
value: 'print(1)',
|
||||
languageId: 'python',
|
||||
outputs: [{
|
||||
items: [{
|
||||
mime: 'text/plain',
|
||||
data: VSBuffer.fromString('1').buffer
|
||||
}],
|
||||
metadata: {},
|
||||
id: '2'
|
||||
}],
|
||||
executionSummary: {
|
||||
executionOrder: 2
|
||||
}
|
||||
}],
|
||||
metadata: {
|
||||
kernelspec: {
|
||||
name: 'python3',
|
||||
display_name: 'Python 3',
|
||||
language: 'python'
|
||||
},
|
||||
language_info: {
|
||||
name: 'python',
|
||||
version: '3.8.10',
|
||||
mimetype: 'text/x-python',
|
||||
codemirror_mode: {
|
||||
name: 'ipython',
|
||||
version: '3'
|
||||
}
|
||||
},
|
||||
custom: {
|
||||
nbformat: NBFORMAT,
|
||||
nbformat_minor: NBFORMAT_MINOR
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
setup(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
let serializer = new MockNotebookSerializer();
|
||||
sandbox.stub(serializer, 'deserializeNotebook').returns(deserializeResult);
|
||||
serializeSpy = sandbox.spy(serializer, 'serializeNotebook');
|
||||
|
||||
contentManager = new VSCodeContentManager(serializer);
|
||||
});
|
||||
|
||||
teardown(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
|
||||
test('Convert VSCode notebook output to ADS notebook output', async () => {
|
||||
let cellOutput: vscode.NotebookCellOutput = {
|
||||
items: [{
|
||||
mime: 'text/plain',
|
||||
data: VSBuffer.fromString('2').buffer
|
||||
}, {
|
||||
mime: 'text/html',
|
||||
data: VSBuffer.fromString('<i>2</i>').buffer
|
||||
}],
|
||||
metadata: {},
|
||||
id: '1'
|
||||
};
|
||||
let expectedADSOutput: azdata.nb.IExecuteResult = {
|
||||
id: '1',
|
||||
output_type: 'execute_result',
|
||||
data: {
|
||||
'text/plain': '2',
|
||||
'text/html': '<i>2</i>'
|
||||
},
|
||||
metadata: {},
|
||||
execution_count: 1
|
||||
};
|
||||
|
||||
let actualOutput = VSCodeContentManager.convertToADSCellOutput(cellOutput, 1);
|
||||
assert.deepStrictEqual(actualOutput, expectedADSOutput);
|
||||
});
|
||||
|
||||
test('Convert ADS notebook execute result to VSCode notebook output', async () => {
|
||||
let cellOutput: azdata.nb.IExecuteResult = {
|
||||
id: 'testId',
|
||||
output_type: OutputTypes.ExecuteResult,
|
||||
data: {
|
||||
'text/plain': 'abc',
|
||||
'text/html': '<i>abc</i>'
|
||||
},
|
||||
execution_count: 1
|
||||
};
|
||||
let expectedVSCodeOutput: vscode.NotebookCellOutput = {
|
||||
items: [{
|
||||
mime: 'text/plain',
|
||||
data: VSBuffer.fromString('abc').buffer
|
||||
}, {
|
||||
mime: 'text/html',
|
||||
data: VSBuffer.fromString('<i>abc</i>').buffer
|
||||
}],
|
||||
id: 'testId',
|
||||
metadata: undefined
|
||||
};
|
||||
let actualOutput = VSCodeContentManager.convertToVSCodeCellOutput(cellOutput);
|
||||
assert.deepStrictEqual(actualOutput, expectedVSCodeOutput);
|
||||
});
|
||||
|
||||
test('Convert ADS notebook stream result to VSCode notebook output', async () => {
|
||||
let cellOutput: azdata.nb.IStreamResult = {
|
||||
id: 'testId',
|
||||
output_type: 'stream',
|
||||
name: 'stdout',
|
||||
text: [
|
||||
'abc'
|
||||
]
|
||||
};
|
||||
let expectedVSCodeOutput: vscode.NotebookCellOutput = {
|
||||
items: [{
|
||||
mime: 'text/html',
|
||||
data: VSBuffer.fromString('abc').buffer
|
||||
}],
|
||||
id: 'testId',
|
||||
metadata: undefined
|
||||
};
|
||||
let actualOutput = VSCodeContentManager.convertToVSCodeCellOutput(cellOutput);
|
||||
assert.deepStrictEqual(actualOutput, expectedVSCodeOutput);
|
||||
});
|
||||
|
||||
test('Convert ADS notebook error with trace to VSCode notebook output', async () => {
|
||||
let cellOutput: azdata.nb.IErrorResult = {
|
||||
id: 'testId',
|
||||
output_type: 'error',
|
||||
ename: 'TestException',
|
||||
evalue: 'Expected test error',
|
||||
traceback: ['Trace line 1', 'Trace line 2']
|
||||
};
|
||||
let expectedVSCodeOutput: vscode.NotebookCellOutput = {
|
||||
items: [{
|
||||
mime: 'text/html',
|
||||
data: VSBuffer.fromString('TestException: Expected test error\nTrace line 1\nTrace line 2').buffer
|
||||
}],
|
||||
id: 'testId',
|
||||
metadata: undefined
|
||||
};
|
||||
let actualOutput = VSCodeContentManager.convertToVSCodeCellOutput(cellOutput);
|
||||
assert.deepStrictEqual(actualOutput, expectedVSCodeOutput);
|
||||
});
|
||||
|
||||
test('Convert ADS notebook error without trace to VSCode notebook output', async () => {
|
||||
let cellOutput: azdata.nb.IErrorResult = {
|
||||
id: 'testId',
|
||||
output_type: 'error',
|
||||
ename: 'TestException',
|
||||
evalue: 'Expected test error'
|
||||
};
|
||||
let expectedVSCodeOutput: vscode.NotebookCellOutput = {
|
||||
items: [{
|
||||
mime: 'text/html',
|
||||
data: VSBuffer.fromString('TestException: Expected test error').buffer
|
||||
}],
|
||||
id: 'testId',
|
||||
metadata: undefined
|
||||
};
|
||||
let actualOutput = VSCodeContentManager.convertToVSCodeCellOutput(cellOutput);
|
||||
assert.deepStrictEqual(actualOutput, expectedVSCodeOutput);
|
||||
});
|
||||
|
||||
test('Deserialize VSCode notebook into ADS notebook data', async () => {
|
||||
let output = await contentManager.deserializeNotebook(''); // Argument is ignored since we're returning a mocked result
|
||||
assert.deepStrictEqual(output, expectedDeserializedNotebook);
|
||||
});
|
||||
|
||||
test('Serialize ADS notebook data into VSCode notebook strings', async () => {
|
||||
await contentManager.serializeNotebook(expectedDeserializedNotebook); // Argument is ignored since we're returning a mocked result
|
||||
assert(serializeSpy.calledOnce);
|
||||
assert.deepStrictEqual(serializeSpy.firstCall.args[0], expectedSerializeArg);
|
||||
});
|
||||
});
|
||||
|
||||
suite('Notebook Controller', () => {
|
||||
});
|
||||
Reference in New Issue
Block a user